blob: 225f154c790e58592432ae7b0d8a88fa1e476e69 [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 Etuahob079c7a2016-04-01 12:32:52 +030077void OutputIntTexCoordWrap(TInfoSinkBase &out,
78 const char *wrapMode,
79 const char *size,
80 const TString &texCoord,
81 const TString &texCoordOffset,
82 const char *texCoordOutName)
83{
84 // GLES 3.0.4 table 3.22 specifies how the wrap modes work. We don't use the formulas verbatim
85 // but rather use equivalent formulas that map better to HLSL.
86 out << "int " << texCoordOutName << ";\n";
87 out << "float " << texCoordOutName << "Offset = " << texCoord << " + float(" << texCoordOffset
88 << ") / " << size << ";\n";
89
90 // CLAMP_TO_EDGE
91 out << "if (" << wrapMode << " == 1)\n";
92 out << "{\n";
93 out << " " << texCoordOutName << " = clamp(int(floor(" << size << " * " << texCoordOutName
94 << "Offset)), 0, int(" << size << ") - 1);\n";
95 out << "}\n";
96
97 // MIRRORED_REPEAT
98 out << "else if (" << wrapMode << " == 3)\n";
99 out << "{\n";
100 out << " float coordWrapped = 1.0 - abs(frac(abs(" << texCoordOutName
101 << "Offset) * 0.5) * 2.0 - 1.0);\n";
102 out << " " << texCoordOutName << " = int(floor(" << size << " * coordWrapped));\n";
103 out << "}\n";
104
105 // REPEAT
106 out << "else\n";
107 out << "{\n";
108 out << " " << texCoordOutName << " = int(floor(" << size << " * frac(" << texCoordOutName
109 << "Offset)));\n";
110 out << "}\n";
111}
112
Olli Etuaho4785fec2015-05-18 16:09:37 +0300113} // namespace
114
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000115namespace sh
116{
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000117
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400118TString OutputHLSL::TextureFunction::name() const
119{
120 TString name = "gl_texture";
121
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200122 // We need to include full the sampler type in the function name to make the signature unique
123 // on D3D11, where samplers are passed to texture functions as indices.
124 name += TextureTypeSuffix(this->sampler);
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400125
126 if (proj)
127 {
128 name += "Proj";
129 }
130
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500131 if (offset)
132 {
133 name += "Offset";
134 }
135
Nicolas Capens75fb4752013-07-10 15:14:47 -0400136 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400137 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500138 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400139 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500140 case LOD: name += "Lod"; break;
141 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400142 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500143 case SIZE: name += "Size"; break;
144 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500145 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400146 default: UNREACHABLE();
147 }
148
149 return name + "(";
150}
151
152bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
153{
154 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400155 if (sampler > rhs.sampler) return false;
156
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400157 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400158 if (coords > rhs.coords) return false;
159
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400160 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400161 if (proj && !rhs.proj) return false;
162
163 if (!offset && rhs.offset) return true;
164 if (offset && !rhs.offset) return false;
165
Nicolas Capens75fb4752013-07-10 15:14:47 -0400166 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400167 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400168
169 return false;
170}
171
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200172OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
173 const TExtensionBehavior &extensionBehavior,
174 const char *sourcePath, ShShaderOutput outputType,
175 int numRenderTargets, const std::vector<Uniform> &uniforms,
176 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400177 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200178 mShaderType(shaderType),
179 mShaderVersion(shaderVersion),
180 mExtensionBehavior(extensionBehavior),
181 mSourcePath(sourcePath),
182 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700183 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000184 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700185 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000187 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000188
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000189 mUsesFragColor = false;
190 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000191 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000192 mUsesFragCoord = false;
193 mUsesPointCoord = false;
194 mUsesFrontFacing = false;
195 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000196 mUsesInstanceID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500197 mUsesVertexID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400198 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000199 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500200 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400201 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530202 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000203
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000204 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000205
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000206 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000207 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400208 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000209
210 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000211
Jamie Madill8daaba12014-06-13 10:04:33 -0400212 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200213 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400214
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200215 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000216 {
Arun Patole63419392015-03-13 11:51:07 +0530217 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
218 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
219 // In both cases total 3 uniform registers need to be reserved.
220 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000221 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000222
Geoff Lang00140f42016-02-03 18:47:33 +0000223 // Reserve registers for the default uniform block and driver constants
224 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225}
226
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000227OutputHLSL::~OutputHLSL()
228{
Jamie Madill8daaba12014-06-13 10:04:33 -0400229 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400230 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200231 for (auto &eqFunction : mStructEqualityFunctions)
232 {
233 SafeDelete(eqFunction);
234 }
235 for (auto &eqFunction : mArrayEqualityFunctions)
236 {
237 SafeDelete(eqFunction);
238 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000239}
240
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200241void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000242{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200243 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400244 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000245
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200246 BuiltInFunctionEmulator builtInFunctionEmulator;
247 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200248 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500249
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700250 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700251 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
252 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300253 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700254 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700255
Jamie Madill37997142015-01-28 10:06:34 -0500256 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500257 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200258 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500259 mInfoSinkStack.pop();
260
Jamie Madill37997142015-01-28 10:06:34 -0500261 mInfoSinkStack.push(&mFooter);
262 if (!mDeferredGlobalInitializers.empty())
263 {
264 writeDeferredGlobalInitializers(mFooter);
265 }
266 mInfoSinkStack.pop();
267
Jamie Madill32aab012015-01-27 14:12:26 -0500268 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500269 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500270 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000271
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200272 objSink << mHeader.c_str();
273 objSink << mBody.c_str();
274 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200275
276 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000277}
278
Jamie Madill570e04d2013-06-21 09:15:33 -0400279void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
280{
281 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
282 {
283 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
284
Jamie Madill32aab012015-01-27 14:12:26 -0500285 TInfoSinkBase structInfoSink;
286 mInfoSinkStack.push(&structInfoSink);
287
Jamie Madill570e04d2013-06-21 09:15:33 -0400288 // This will mark the necessary block elements as referenced
289 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500290
291 TString structName(structInfoSink.c_str());
292 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400293
294 mFlaggedStructOriginalNames[flaggedNode] = structName;
295
296 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
297 {
298 structName.erase(pos, 1);
299 }
300
301 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
302 }
303}
304
Jamie Madill4e1fd412014-07-10 17:50:10 -0400305const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
306{
307 return mUniformHLSL->getInterfaceBlockRegisterMap();
308}
309
Jamie Madill9fe25e92014-07-18 10:33:08 -0400310const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
311{
312 return mUniformHLSL->getUniformRegisterMap();
313}
314
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000315int OutputHLSL::vectorSize(const TType &type) const
316{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000317 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000318 int arraySize = type.isArray() ? type.getArraySize() : 1;
319
320 return elementSize * arraySize;
321}
322
Jamie Madill98493dd2013-07-08 14:39:03 -0400323TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400324{
325 TString init;
326
327 TString preIndentString;
328 TString fullIndentString;
329
330 for (int spaces = 0; spaces < (indent * 4); spaces++)
331 {
332 preIndentString += ' ';
333 }
334
335 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
336 {
337 fullIndentString += ' ';
338 }
339
340 init += preIndentString + "{\n";
341
Jamie Madill98493dd2013-07-08 14:39:03 -0400342 const TFieldList &fields = structure.fields();
343 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400344 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400345 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400346 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400347 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400348
Jamie Madill98493dd2013-07-08 14:39:03 -0400349 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400350 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400351 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400352 }
353 else
354 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400355 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400356 }
357 }
358
359 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
360
361 return init;
362}
363
Jamie Madill8c46ab12015-12-07 16:39:19 -0500364void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000366 TString varyings;
367 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400368 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000369
Jamie Madill829f59e2013-11-13 19:40:54 -0500370 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400371 {
372 TIntermTyped *structNode = flaggedStructIt->first;
373 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400374 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400375 const TString &originalName = mFlaggedStructOriginalNames[structNode];
376
Jamie Madill033dae62014-06-18 12:56:28 -0400377 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400378 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400379 flaggedStructs += "\n";
380 }
381
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000382 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
383 {
384 const TType &type = varying->second->getType();
385 const TString &name = varying->second->getSymbol();
386
387 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400388 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
389 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000390 }
391
392 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
393 {
394 const TType &type = attribute->second->getType();
395 const TString &name = attribute->second->getSymbol();
396
Jamie Madill033dae62014-06-18 12:56:28 -0400397 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000398 }
399
Jamie Madill8daaba12014-06-13 10:04:33 -0400400 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400401
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200402 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400403 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
404
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200405 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500406 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200407 out << "\n// Equality functions\n\n";
408 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500409 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200410 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200411 }
412 }
Olli Etuaho12690762015-03-31 12:55:28 +0300413 if (!mArrayAssignmentFunctions.empty())
414 {
415 out << "\n// Assignment functions\n\n";
416 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
417 {
418 out << assignmentFunction.functionDefinition << "\n";
419 }
420 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300421 if (!mArrayConstructIntoFunctions.empty())
422 {
423 out << "\n// Array constructor functions\n\n";
424 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
425 {
426 out << constructIntoFunction.functionDefinition << "\n";
427 }
428 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200429
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500430 if (mUsesDiscardRewriting)
431 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400432 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500433 }
434
Nicolas Capens655fe362014-04-11 13:12:34 -0400435 if (mUsesNestedBreak)
436 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400437 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400438 }
439
Arun Patole44efa0b2015-03-04 17:11:05 +0530440 if (mRequiresIEEEStrictCompiling)
441 {
442 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
443 }
444
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400445 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
446 "#define LOOP [loop]\n"
447 "#define FLATTEN [flatten]\n"
448 "#else\n"
449 "#define LOOP\n"
450 "#define FLATTEN\n"
451 "#endif\n";
452
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200453 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000454 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200455 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
456 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000457
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000458 out << "// Varyings\n";
459 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400460 out << "\n";
461
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200462 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000463 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500464 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000465 {
Jamie Madill46131a32013-06-20 11:55:50 -0400466 const TString &variableName = outputVariableIt->first;
467 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400468
Jamie Madill033dae62014-06-18 12:56:28 -0400469 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400470 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000471 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000472 }
Jamie Madill46131a32013-06-20 11:55:50 -0400473 else
474 {
475 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
476
477 out << "static float4 gl_Color[" << numColorValues << "] =\n"
478 "{\n";
479 for (unsigned int i = 0; i < numColorValues; i++)
480 {
481 out << " float4(0, 0, 0, 0)";
482 if (i + 1 != numColorValues)
483 {
484 out << ",";
485 }
486 out << "\n";
487 }
488
489 out << "};\n";
490 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000491
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400492 if (mUsesFragDepth)
493 {
494 out << "static float gl_Depth = 0.0;\n";
495 }
496
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000497 if (mUsesFragCoord)
498 {
499 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
500 }
501
502 if (mUsesPointCoord)
503 {
504 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
505 }
506
507 if (mUsesFrontFacing)
508 {
509 out << "static bool gl_FrontFacing = false;\n";
510 }
511
512 out << "\n";
513
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000514 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000515 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000516 out << "struct gl_DepthRangeParameters\n"
517 "{\n"
518 " float near;\n"
519 " float far;\n"
520 " float diff;\n"
521 "};\n"
522 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000523 }
524
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200525 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000526 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000527 out << "cbuffer DriverConstants : register(b1)\n"
528 "{\n";
529
530 if (mUsesDepthRange)
531 {
532 out << " float3 dx_DepthRange : packoffset(c0);\n";
533 }
534
535 if (mUsesFragCoord)
536 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000537 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000538 }
539
540 if (mUsesFragCoord || mUsesFrontFacing)
541 {
542 out << " float3 dx_DepthFront : packoffset(c2);\n";
543 }
544
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800545 if (mUsesFragCoord)
546 {
547 // dx_ViewScale is only used in the fragment shader to correct
548 // the value for glFragCoord if necessary
549 out << " float2 dx_ViewScale : packoffset(c3);\n";
550 }
551
Olli Etuaho618bebc2016-01-15 16:40:00 +0200552 if (mOutputType == SH_HLSL_4_1_OUTPUT)
553 {
554 mUniformHLSL->samplerMetadataUniforms(out, "c4");
555 }
556
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000557 out << "};\n";
558 }
559 else
560 {
561 if (mUsesDepthRange)
562 {
563 out << "uniform float3 dx_DepthRange : register(c0);";
564 }
565
566 if (mUsesFragCoord)
567 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000568 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000569 }
570
571 if (mUsesFragCoord || mUsesFrontFacing)
572 {
573 out << "uniform float3 dx_DepthFront : register(c2);\n";
574 }
575 }
576
577 out << "\n";
578
579 if (mUsesDepthRange)
580 {
581 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
582 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000583 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000584
Jamie Madillf91ce812014-06-13 10:04:34 -0400585 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000586 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400587 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000588 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400589 out << flaggedStructs;
590 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000591 }
592
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000593 if (usingMRTExtension && mNumRenderTargets > 1)
594 {
595 out << "#define GL_USES_MRT\n";
596 }
597
598 if (mUsesFragColor)
599 {
600 out << "#define GL_USES_FRAG_COLOR\n";
601 }
602
603 if (mUsesFragData)
604 {
605 out << "#define GL_USES_FRAG_DATA\n";
606 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000608 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000610 out << "// Attributes\n";
611 out << attributes;
612 out << "\n"
613 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400614
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000615 if (mUsesPointSize)
616 {
617 out << "static float gl_PointSize = float(1);\n";
618 }
619
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000620 if (mUsesInstanceID)
621 {
622 out << "static int gl_InstanceID;";
623 }
624
Corentin Wallezb076add2016-01-11 16:45:46 -0500625 if (mUsesVertexID)
626 {
627 out << "static int gl_VertexID;";
628 }
629
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000630 out << "\n"
631 "// Varyings\n";
632 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000633 out << "\n";
634
635 if (mUsesDepthRange)
636 {
637 out << "struct gl_DepthRangeParameters\n"
638 "{\n"
639 " float near;\n"
640 " float far;\n"
641 " float diff;\n"
642 "};\n"
643 "\n";
644 }
645
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200646 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000647 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800648 out << "cbuffer DriverConstants : register(b1)\n"
649 "{\n";
650
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000651 if (mUsesDepthRange)
652 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800653 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000654 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800655
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800656 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
657 // shaders. However, we declare it for all shaders (including Feature Level 10+).
658 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
659 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800660 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800661 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800662 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800663
Olli Etuaho618bebc2016-01-15 16:40:00 +0200664 if (mOutputType == SH_HLSL_4_1_OUTPUT)
665 {
666 mUniformHLSL->samplerMetadataUniforms(out, "c4");
667 }
668
Austin Kinross4fd18b12014-12-22 12:32:05 -0800669 out << "};\n"
670 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000671 }
672 else
673 {
674 if (mUsesDepthRange)
675 {
676 out << "uniform float3 dx_DepthRange : register(c0);\n";
677 }
678
Cooper Partine6664f02015-01-09 16:22:24 -0800679 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
680 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000681 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000682 }
683
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000684 if (mUsesDepthRange)
685 {
686 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
687 "\n";
688 }
689
Jamie Madillf91ce812014-06-13 10:04:34 -0400690 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000691 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400692 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000693 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400694 out << flaggedStructs;
695 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000696 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400697 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000698
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400699 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
700 {
701 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400702 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000703 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400704 switch(textureFunction->sampler)
705 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400706 case EbtSampler2D: out << "int2 "; break;
707 case EbtSampler3D: out << "int3 "; break;
708 case EbtSamplerCube: out << "int2 "; break;
709 case EbtSampler2DArray: out << "int3 "; break;
710 case EbtISampler2D: out << "int2 "; break;
711 case EbtISampler3D: out << "int3 "; break;
712 case EbtISamplerCube: out << "int2 "; break;
713 case EbtISampler2DArray: out << "int3 "; break;
714 case EbtUSampler2D: out << "int2 "; break;
715 case EbtUSampler3D: out << "int3 "; break;
716 case EbtUSamplerCube: out << "int2 "; break;
717 case EbtUSampler2DArray: out << "int3 "; break;
718 case EbtSampler2DShadow: out << "int2 "; break;
719 case EbtSamplerCubeShadow: out << "int2 "; break;
720 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400721 default: UNREACHABLE();
722 }
723 }
724 else // Sampling function
725 {
726 switch(textureFunction->sampler)
727 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400728 case EbtSampler2D: out << "float4 "; break;
729 case EbtSampler3D: out << "float4 "; break;
730 case EbtSamplerCube: out << "float4 "; break;
731 case EbtSampler2DArray: out << "float4 "; break;
732 case EbtISampler2D: out << "int4 "; break;
733 case EbtISampler3D: out << "int4 "; break;
734 case EbtISamplerCube: out << "int4 "; break;
735 case EbtISampler2DArray: out << "int4 "; break;
736 case EbtUSampler2D: out << "uint4 "; break;
737 case EbtUSampler3D: out << "uint4 "; break;
738 case EbtUSamplerCube: out << "uint4 "; break;
739 case EbtUSampler2DArray: out << "uint4 "; break;
740 case EbtSampler2DShadow: out << "float "; break;
741 case EbtSamplerCubeShadow: out << "float "; break;
742 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400743 default: UNREACHABLE();
744 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000745 }
746
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400747 // Function name
748 out << textureFunction->name();
749
750 // Argument list
751 int hlslCoords = 4;
752
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200753 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000754 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400755 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000756 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400757 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
758 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
759 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000760 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400761
Nicolas Capens75fb4752013-07-10 15:14:47 -0400762 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000763 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400764 case TextureFunction::IMPLICIT: break;
765 case TextureFunction::BIAS: hlslCoords = 4; break;
766 case TextureFunction::LOD: hlslCoords = 4; break;
767 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400768 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400769 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000770 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400771 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200772 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400773 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200774 hlslCoords = HLSLTextureCoordsCount(textureFunction->sampler);
775 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400776 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200777 out << TextureString(textureFunction->sampler) << " x, "
778 << SamplerString(textureFunction->sampler) << " s";
779 }
780 else
781 {
782 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
783 out << "const uint samplerIndex";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400784 }
785 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400786
Nicolas Capensfc014542014-02-18 14:47:13 -0500787 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400788 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500789 switch(textureFunction->coords)
790 {
791 case 2: out << ", int2 t"; break;
792 case 3: out << ", int3 t"; break;
793 default: UNREACHABLE();
794 }
795 }
796 else // Floating-point coordinates (except textureSize)
797 {
798 switch(textureFunction->coords)
799 {
800 case 1: out << ", int lod"; break; // textureSize()
801 case 2: out << ", float2 t"; break;
802 case 3: out << ", float3 t"; break;
803 case 4: out << ", float4 t"; break;
804 default: UNREACHABLE();
805 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000806 }
807
Nicolas Capensd11d5492014-02-19 17:06:10 -0500808 if (textureFunction->method == TextureFunction::GRAD)
809 {
810 switch(textureFunction->sampler)
811 {
812 case EbtSampler2D:
813 case EbtISampler2D:
814 case EbtUSampler2D:
815 case EbtSampler2DArray:
816 case EbtISampler2DArray:
817 case EbtUSampler2DArray:
818 case EbtSampler2DShadow:
819 case EbtSampler2DArrayShadow:
820 out << ", float2 ddx, float2 ddy";
821 break;
822 case EbtSampler3D:
823 case EbtISampler3D:
824 case EbtUSampler3D:
825 case EbtSamplerCube:
826 case EbtISamplerCube:
827 case EbtUSamplerCube:
828 case EbtSamplerCubeShadow:
829 out << ", float3 ddx, float3 ddy";
830 break;
831 default: UNREACHABLE();
832 }
833 }
834
Nicolas Capens75fb4752013-07-10 15:14:47 -0400835 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000836 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400837 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400838 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400839 case TextureFunction::LOD: out << ", float lod"; break;
840 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400841 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400842 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500843 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500844 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400845 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000846 }
847
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500848 if (textureFunction->offset)
849 {
850 switch(textureFunction->sampler)
851 {
852 case EbtSampler2D: out << ", int2 offset"; break;
853 case EbtSampler3D: out << ", int3 offset"; break;
854 case EbtSampler2DArray: out << ", int2 offset"; break;
855 case EbtISampler2D: out << ", int2 offset"; break;
856 case EbtISampler3D: out << ", int3 offset"; break;
857 case EbtISampler2DArray: out << ", int2 offset"; break;
858 case EbtUSampler2D: out << ", int2 offset"; break;
859 case EbtUSampler3D: out << ", int3 offset"; break;
860 case EbtUSampler2DArray: out << ", int2 offset"; break;
861 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500862 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500863 default: UNREACHABLE();
864 }
865 }
866
Nicolas Capens84cfa122014-04-14 13:48:45 -0400867 if (textureFunction->method == TextureFunction::BIAS ||
868 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500869 {
870 out << ", float bias";
871 }
872
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400873 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400874 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400875
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200876 // In some cases we use a variable to store the texture/sampler objects, but to work around
877 // a D3D11 compiler bug related to discard inside a loop that is conditional on texture
878 // sampling we need to call the function directly on a reference to the array. The bug was
879 // found using dEQP-GLES3.functional.shaders.discard*loop_texture* tests.
880 TString textureReference("x");
881 TString samplerReference("s");
882 if (mOutputType == SH_HLSL_4_1_OUTPUT)
883 {
884 TString suffix = TextureGroupSuffix(textureFunction->sampler);
885 if (TextureGroup(textureFunction->sampler) == HLSL_TEXTURE_2D)
886 {
887 textureReference = TString("textures") + suffix + "[samplerIndex]";
888 samplerReference = TString("samplers") + suffix + "[samplerIndex]";
889 }
890 else
891 {
892 out << " const uint textureIndex = samplerIndex - textureIndexOffset" << suffix
893 << ";\n";
894 textureReference = TString("textures") + suffix + "[textureIndex]";
895 out << " const uint samplerArrayIndex = samplerIndex - samplerIndexOffset"
896 << suffix << ";\n";
897 samplerReference = TString("samplers") + suffix + "[samplerArrayIndex]";
898 }
899 }
900
Nicolas Capens75fb4752013-07-10 15:14:47 -0400901 if (textureFunction->method == TextureFunction::SIZE)
902 {
Olli Etuahob079c7a2016-04-01 12:32:52 +0300903 out << "int baseLevel = samplerMetadata[samplerIndex].baseLevel;\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400904 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
905 {
Olli Etuahobce743a2016-01-15 17:18:28 +0200906 if (IsSamplerArray(textureFunction->sampler) ||
907 (IsIntegerSampler(textureFunction->sampler) &&
908 IsSamplerCube(textureFunction->sampler)))
Nicolas Capens75fb4752013-07-10 15:14:47 -0400909 {
910 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
Olli Etuahobce743a2016-01-15 17:18:28 +0200911 << " " << textureReference << ".GetDimensions(baseLevel + lod, width, "
912 "height, layers, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400913 }
914 else
915 {
916 out << " uint width; uint height; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200917 << " " << textureReference
Olli Etuahobce743a2016-01-15 17:18:28 +0200918 << ".GetDimensions(baseLevel + lod, width, height, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400919 }
920 }
921 else if (IsSampler3D(textureFunction->sampler))
922 {
923 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200924 << " " << textureReference
Olli Etuahobce743a2016-01-15 17:18:28 +0200925 << ".GetDimensions(baseLevel + lod, width, height, depth, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400926 }
927 else UNREACHABLE();
928
929 switch(textureFunction->sampler)
930 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400931 case EbtSampler2D: out << " return int2(width, height);"; break;
932 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
933 case EbtSamplerCube: out << " return int2(width, height);"; break;
934 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
935 case EbtISampler2D: out << " return int2(width, height);"; break;
936 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
937 case EbtISamplerCube: out << " return int2(width, height);"; break;
938 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
939 case EbtUSampler2D: out << " return int2(width, height);"; break;
940 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
941 case EbtUSamplerCube: out << " return int2(width, height);"; break;
942 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
943 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
944 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
945 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400946 default: UNREACHABLE();
947 }
948 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400949 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400950 {
Olli Etuahod4102f02016-01-22 14:54:04 +0200951 TString texCoordX("t.x");
952 TString texCoordY("t.y");
953 TString texCoordZ("t.z");
954 TString proj = "";
955
956 if (textureFunction->proj)
957 {
958 switch (textureFunction->coords)
959 {
960 case 3:
961 proj = " / t.z";
962 break;
963 case 4:
964 proj = " / t.w";
965 break;
966 default:
967 UNREACHABLE();
968 }
969 if (proj != "")
970 {
971 texCoordX = "(" + texCoordX + proj + ")";
972 texCoordY = "(" + texCoordY + proj + ")";
973 texCoordZ = "(" + texCoordZ + proj + ")";
974 }
975 }
976
Nicolas Capens0027fa92014-02-20 14:26:42 -0500977 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
978 {
979 out << " float width; float height; float layers; float levels;\n";
980
981 out << " uint mip = 0;\n";
982
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200983 out << " " << textureReference
984 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens0027fa92014-02-20 14:26:42 -0500985
986 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
987 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
988 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
989 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
990
991 // FACE_POSITIVE_X = 000b
992 // FACE_NEGATIVE_X = 001b
993 // FACE_POSITIVE_Y = 010b
994 // FACE_NEGATIVE_Y = 011b
995 // FACE_POSITIVE_Z = 100b
996 // FACE_NEGATIVE_Z = 101b
997 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
998
999 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
1000 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
1001 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
1002
1003 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
1004 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -05001005
1006 // Mip level computation.
Olli Etuahoced87052016-04-04 16:34:27 +03001007 if (textureFunction->method == TextureFunction::IMPLICIT ||
1008 textureFunction->method == TextureFunction::LOD ||
1009 textureFunction->method == TextureFunction::GRAD)
Jamie Madill8d9f35f2015-11-24 16:10:20 -05001010 {
Olli Etuahoced87052016-04-04 16:34:27 +03001011 if (textureFunction->method == TextureFunction::IMPLICIT)
1012 {
1013 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
1014 " float2 dx = ddx(tSized);\n"
1015 " float2 dy = ddy(tSized);\n"
1016 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n";
1017 }
1018 else if (textureFunction->method == TextureFunction::GRAD)
1019 {
1020 // ESSL 3.00.6 spec section 8.8: "For the cube version, the partial
1021 // derivatives of P are assumed to be in the coordinate system used before
1022 // texture coordinates are projected onto the appropriate cube face."
1023 // ddx[0] and ddy[0] are the derivatives of t.x passed into the function
1024 // ddx[1] and ddy[1] are the derivatives of t.y passed into the function
1025 // ddx[2] and ddy[2] are the derivatives of t.z passed into the function
1026 // Determine the derivatives of u, v and m
1027 out << " float dudx = xMajor ? ddx[2] : (yMajor && t.y < 0.0f ? -ddx[0] "
1028 ": ddx[0]);\n"
1029 " float dudy = xMajor ? ddy[2] : (yMajor && t.y < 0.0f ? -ddy[0] "
1030 ": ddy[0]);\n"
1031 " float dvdx = yMajor ? ddx[2] : (negative ? ddx[1] : -ddx[1]);\n"
1032 " float dvdy = yMajor ? ddy[2] : (negative ? ddy[1] : -ddy[1]);\n"
1033 " float dmdx = xMajor ? ddx[0] : (yMajor ? ddx[1] : ddx[2]);\n"
1034 " float dmdy = xMajor ? ddy[0] : (yMajor ? ddy[1] : ddy[2]);\n";
1035 // Now determine the derivatives of the face coordinates, using the
1036 // derivatives calculated above.
1037 // d / dx (u(x) * 0.5 / m(x) + 0.5)
1038 // = 0.5 * (m(x) * u'(x) - u(x) * m'(x)) / m(x)^2
1039 out << " float dfacexdx = 0.5f * (m * dudx - u * dmdx) / (m * m);\n"
1040 " float dfaceydx = 0.5f * (m * dvdx - v * dmdx) / (m * m);\n"
1041 " float dfacexdy = 0.5f * (m * dudy - u * dmdy) / (m * m);\n"
1042 " float dfaceydy = 0.5f * (m * dvdy - v * dmdy) / (m * m);\n"
1043 " float2 sizeVec = float2(width, height);\n"
1044 " float2 faceddx = float2(dfacexdx, dfaceydx) * sizeVec;\n"
1045 " float2 faceddy = float2(dfacexdy, dfaceydy) * sizeVec;\n";
1046 // Optimization: instead of: log2(max(length(faceddx), length(faceddy)))
1047 // we compute: log2(max(length(faceddx)^2, length(faceddy)^2)) / 2
1048 out << " float lengthfaceddx2 = dot(faceddx, faceddx);\n"
1049 " float lengthfaceddy2 = dot(faceddy, faceddy);\n"
1050 " float lod = log2(max(lengthfaceddx2, lengthfaceddy2)) * "
1051 "0.5f;\n";
1052 }
1053 out << " mip = uint(min(max(round(lod), 0), levels - 1));\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001054 << " " << textureReference
1055 << ".GetDimensions(mip, width, height, layers, levels);\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -05001056 }
Olli Etuahod4102f02016-01-22 14:54:04 +02001057
1058 // Convert from normalized floating-point to integer
1059 texCoordX = "int(floor(width * frac(" + texCoordX + ")))";
1060 texCoordY = "int(floor(height * frac(" + texCoordY + ")))";
1061 texCoordZ = "face";
Nicolas Capens0027fa92014-02-20 14:26:42 -05001062 }
1063 else if (IsIntegerSampler(textureFunction->sampler) &&
1064 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001065 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001066 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001067 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001068 if (IsSamplerArray(textureFunction->sampler))
1069 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001070 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001071
Nicolas Capens9edebd62013-08-06 10:59:10 -04001072 if (textureFunction->method == TextureFunction::LOD0)
1073 {
1074 out << " uint mip = 0;\n";
1075 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001076 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1077 {
1078 out << " uint mip = bias;\n";
1079 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001080 else
1081 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001082
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001083 out << " " << textureReference
1084 << ".GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001085 if (textureFunction->method == TextureFunction::IMPLICIT ||
1086 textureFunction->method == TextureFunction::BIAS)
1087 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001088 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001089 " float dx = length(ddx(tSized));\n"
1090 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001091 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001092
1093 if (textureFunction->method == TextureFunction::BIAS)
1094 {
1095 out << " lod += bias;\n";
1096 }
1097 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001098 else if (textureFunction->method == TextureFunction::GRAD)
1099 {
Olli Etuahoced87052016-04-04 16:34:27 +03001100 out << " float2 sizeVec = float2(width, height);\n"
1101 " float2 sizeDdx = ddx * sizeVec;\n"
1102 " float2 sizeDdy = ddy * sizeVec;\n"
1103 " float lod = log2(max(dot(sizeDdx, sizeDdx), "
1104 "dot(sizeDdy, sizeDdy))) * 0.5f;\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001105 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001106
1107 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1108 }
1109
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001110 out << " " << textureReference
1111 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001112 }
1113 else
1114 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001115 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001116
Nicolas Capens9edebd62013-08-06 10:59:10 -04001117 if (textureFunction->method == TextureFunction::LOD0)
1118 {
1119 out << " uint mip = 0;\n";
1120 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001121 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1122 {
1123 out << " uint mip = bias;\n";
1124 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001125 else
1126 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001127 out << " " << textureReference
1128 << ".GetDimensions(0, width, height, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001129
Nicolas Capens9edebd62013-08-06 10:59:10 -04001130 if (textureFunction->method == TextureFunction::IMPLICIT ||
1131 textureFunction->method == TextureFunction::BIAS)
1132 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001133 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001134 " float dx = length(ddx(tSized));\n"
1135 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001136 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001137
1138 if (textureFunction->method == TextureFunction::BIAS)
1139 {
1140 out << " lod += bias;\n";
1141 }
1142 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001143 else if (textureFunction->method == TextureFunction::GRAD)
1144 {
Olli Etuahoced87052016-04-04 16:34:27 +03001145 out << " float2 sizeVec = float2(width, height);\n"
1146 " float2 sizeDdx = ddx * sizeVec;\n"
1147 " float2 sizeDdy = ddy * sizeVec;\n"
1148 " float lod = log2(max(dot(sizeDdx, sizeDdx), "
1149 "dot(sizeDdy, sizeDdy))) * 0.5f;\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001150 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001151
1152 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1153 }
1154
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001155 out << " " << textureReference
1156 << ".GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001157 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001158 }
1159 else if (IsSampler3D(textureFunction->sampler))
1160 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001161 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001162
Nicolas Capens9edebd62013-08-06 10:59:10 -04001163 if (textureFunction->method == TextureFunction::LOD0)
1164 {
1165 out << " uint mip = 0;\n";
1166 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001167 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1168 {
1169 out << " uint mip = bias;\n";
1170 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001171 else
1172 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001173 out << " " << textureReference
1174 << ".GetDimensions(0, width, height, depth, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001175
Nicolas Capens9edebd62013-08-06 10:59:10 -04001176 if (textureFunction->method == TextureFunction::IMPLICIT ||
1177 textureFunction->method == TextureFunction::BIAS)
1178 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001179 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1180 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001181 " float dx = length(ddx(tSized));\n"
1182 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001183 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001184
1185 if (textureFunction->method == TextureFunction::BIAS)
1186 {
1187 out << " lod += bias;\n";
1188 }
1189 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001190 else if (textureFunction->method == TextureFunction::GRAD)
1191 {
Olli Etuahoced87052016-04-04 16:34:27 +03001192 out << " float3 sizeVec = float3(width, height, depth);\n"
1193 " float3 sizeDdx = ddx * sizeVec;\n"
1194 " float3 sizeDdy = ddy * sizeVec;\n"
1195 " float lod = log2(max(dot(sizeDdx, sizeDdx), dot(sizeDdy, "
1196 "sizeDdy))) * 0.5f;\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001197 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001198
1199 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1200 }
1201
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001202 out << " " << textureReference
1203 << ".GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001204 }
1205 else UNREACHABLE();
Olli Etuahod4102f02016-01-22 14:54:04 +02001206
1207 // Convert from normalized floating-point to integer
Olli Etuahob079c7a2016-04-01 12:32:52 +03001208 out << "int wrapS = samplerMetadata[samplerIndex].wrapModes & 0x3;\n";
1209 if (textureFunction->offset)
1210 {
1211 OutputIntTexCoordWrap(out, "wrapS", "width", texCoordX, "offset.x", "tix");
1212 }
1213 else
1214 {
1215 OutputIntTexCoordWrap(out, "wrapS", "width", texCoordX, "0", "tix");
1216 }
1217 texCoordX = "tix";
1218 out << "int wrapT = (samplerMetadata[samplerIndex].wrapModes >> 2) & 0x3;\n";
1219 if (textureFunction->offset)
1220 {
1221 OutputIntTexCoordWrap(out, "wrapT", "height", texCoordY, "offset.y", "tiy");
1222 }
1223 else
1224 {
1225 OutputIntTexCoordWrap(out, "wrapT", "height", texCoordY, "0", "tiy");
1226 }
1227 texCoordY = "tiy";
Olli Etuahod4102f02016-01-22 14:54:04 +02001228
1229 if (IsSamplerArray(textureFunction->sampler))
1230 {
1231 texCoordZ = "int(max(0, min(layers - 1, floor(0.5 + t.z))))";
1232 }
1233 else if (!IsSamplerCube(textureFunction->sampler) &&
1234 !IsSampler2D(textureFunction->sampler))
1235 {
Olli Etuahob079c7a2016-04-01 12:32:52 +03001236 out << "int wrapR = (samplerMetadata[samplerIndex].wrapModes >> 4) & 0x3;\n";
1237 if (textureFunction->offset)
1238 {
1239 OutputIntTexCoordWrap(out, "wrapR", "depth", texCoordZ, "offset.z", "tiz");
1240 }
1241 else
1242 {
1243 OutputIntTexCoordWrap(out, "wrapR", "depth", texCoordZ, "0", "tiz");
1244 }
1245 texCoordZ = "tiz";
Olli Etuahod4102f02016-01-22 14:54:04 +02001246 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001247 }
1248
1249 out << " return ";
1250
1251 // HLSL intrinsic
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001252 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001253 {
1254 switch(textureFunction->sampler)
1255 {
1256 case EbtSampler2D: out << "tex2D"; break;
1257 case EbtSamplerCube: out << "texCUBE"; break;
1258 default: UNREACHABLE();
1259 }
1260
Nicolas Capens75fb4752013-07-10 15:14:47 -04001261 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001262 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001263 case TextureFunction::IMPLICIT:
1264 out << "(" << samplerReference << ", ";
1265 break;
1266 case TextureFunction::BIAS:
1267 out << "bias(" << samplerReference << ", ";
1268 break;
1269 case TextureFunction::LOD:
1270 out << "lod(" << samplerReference << ", ";
1271 break;
1272 case TextureFunction::LOD0:
1273 out << "lod(" << samplerReference << ", ";
1274 break;
1275 case TextureFunction::LOD0BIAS:
1276 out << "lod(" << samplerReference << ", ";
1277 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001278 default: UNREACHABLE();
1279 }
1280 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001281 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001282 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001283 if (textureFunction->method == TextureFunction::GRAD)
1284 {
1285 if (IsIntegerSampler(textureFunction->sampler))
1286 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001287 out << "" << textureReference << ".Load(";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001288 }
1289 else if (IsShadowSampler(textureFunction->sampler))
1290 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001291 out << "" << textureReference << ".SampleCmpLevelZero(" << samplerReference
1292 << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001293 }
1294 else
1295 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001296 out << "" << textureReference << ".SampleGrad(" << samplerReference << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001297 }
1298 }
1299 else if (IsIntegerSampler(textureFunction->sampler) ||
1300 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001301 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001302 out << "" << textureReference << ".Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001303 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001304 else if (IsShadowSampler(textureFunction->sampler))
1305 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001306 switch(textureFunction->method)
1307 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001308 case TextureFunction::IMPLICIT:
1309 out << "" << textureReference << ".SampleCmp(" << samplerReference
1310 << ", ";
1311 break;
1312 case TextureFunction::BIAS:
1313 out << "" << textureReference << ".SampleCmp(" << samplerReference
1314 << ", ";
1315 break;
1316 case TextureFunction::LOD:
1317 out << "" << textureReference << ".SampleCmp(" << samplerReference
1318 << ", ";
1319 break;
1320 case TextureFunction::LOD0:
1321 out << "" << textureReference << ".SampleCmpLevelZero("
1322 << samplerReference << ", ";
1323 break;
1324 case TextureFunction::LOD0BIAS:
1325 out << "" << textureReference << ".SampleCmpLevelZero("
1326 << samplerReference << ", ";
1327 break;
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001328 default: UNREACHABLE();
1329 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001330 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001331 else
1332 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001333 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001334 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001335 case TextureFunction::IMPLICIT:
1336 out << "" << textureReference << ".Sample(" << samplerReference << ", ";
1337 break;
1338 case TextureFunction::BIAS:
1339 out << "" << textureReference << ".SampleBias(" << samplerReference
1340 << ", ";
1341 break;
1342 case TextureFunction::LOD:
1343 out << "" << textureReference << ".SampleLevel(" << samplerReference
1344 << ", ";
1345 break;
1346 case TextureFunction::LOD0:
1347 out << "" << textureReference << ".SampleLevel(" << samplerReference
1348 << ", ";
1349 break;
1350 case TextureFunction::LOD0BIAS:
1351 out << "" << textureReference << ".SampleLevel(" << samplerReference
1352 << ", ";
1353 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001354 default: UNREACHABLE();
1355 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001356 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001357 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001358 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001359
Nicolas Capensfc014542014-02-18 14:47:13 -05001360 if (IsIntegerSampler(textureFunction->sampler) ||
1361 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001362 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001363 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001364 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001365 case 2: out << "int3("; break;
1366 case 3: out << "int4("; break;
1367 default: UNREACHABLE();
1368 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001369 }
1370 else
1371 {
1372 switch(hlslCoords)
1373 {
1374 case 2: out << "float2("; break;
1375 case 3: out << "float3("; break;
1376 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001377 default: UNREACHABLE();
1378 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001379 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001380
Olli Etuahod4102f02016-01-22 14:54:04 +02001381 out << texCoordX << ", " << texCoordY;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001382
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001383 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001384 {
1385 if (hlslCoords >= 3)
1386 {
1387 if (textureFunction->coords < 3)
1388 {
1389 out << ", 0";
1390 }
1391 else
1392 {
Olli Etuahod4102f02016-01-22 14:54:04 +02001393 out << ", t.z" << proj;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001394 }
1395 }
1396
1397 if (hlslCoords == 4)
1398 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001399 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001400 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001401 case TextureFunction::BIAS: out << ", bias"; break;
1402 case TextureFunction::LOD: out << ", lod"; break;
1403 case TextureFunction::LOD0: out << ", 0"; break;
1404 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001405 default: UNREACHABLE();
1406 }
1407 }
1408
1409 out << "));\n";
1410 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001411 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001412 {
1413 if (hlslCoords >= 3)
1414 {
Olli Etuahod4102f02016-01-22 14:54:04 +02001415 ASSERT(!IsIntegerSampler(textureFunction->sampler) ||
1416 !IsSamplerCube(textureFunction->sampler) || texCoordZ == "face");
1417 out << ", " << texCoordZ;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001418 }
1419
Nicolas Capensd11d5492014-02-19 17:06:10 -05001420 if (textureFunction->method == TextureFunction::GRAD)
1421 {
1422 if (IsIntegerSampler(textureFunction->sampler))
1423 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001424 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001425 }
1426 else if (IsShadowSampler(textureFunction->sampler))
1427 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001428 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001429 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001430 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001431 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1432 // The resulting third component of P' in the shadow forms is used as Dref
1433 out << "), t.z" << proj;
1434 }
1435 else
1436 {
1437 switch(textureFunction->coords)
1438 {
1439 case 3: out << "), t.z"; break;
1440 case 4: out << "), t.w"; break;
1441 default: UNREACHABLE();
1442 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001443 }
1444 }
1445 else
1446 {
1447 out << "), ddx, ddy";
1448 }
1449 }
1450 else if (IsIntegerSampler(textureFunction->sampler) ||
1451 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001452 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001453 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001454 }
1455 else if (IsShadowSampler(textureFunction->sampler))
1456 {
1457 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001458 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001459 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001460 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1461 // The resulting third component of P' in the shadow forms is used as Dref
1462 out << "), t.z" << proj;
1463 }
1464 else
1465 {
1466 switch(textureFunction->coords)
1467 {
1468 case 3: out << "), t.z"; break;
1469 case 4: out << "), t.w"; break;
1470 default: UNREACHABLE();
1471 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001472 }
1473 }
1474 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001475 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001476 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001477 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001478 case TextureFunction::IMPLICIT: out << ")"; break;
1479 case TextureFunction::BIAS: out << "), bias"; break;
1480 case TextureFunction::LOD: out << "), lod"; break;
1481 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001482 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001483 default: UNREACHABLE();
1484 }
1485 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001486
Olli Etuahob079c7a2016-04-01 12:32:52 +03001487 if (textureFunction->offset && (!IsIntegerSampler(textureFunction->sampler) ||
1488 textureFunction->method == TextureFunction::FETCH))
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001489 {
1490 out << ", offset";
1491 }
1492
1493 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001494 }
1495 else UNREACHABLE();
1496 }
1497
1498 out << "\n"
1499 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001500 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001501 }
1502
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001503 if (mUsesFragCoord)
1504 {
1505 out << "#define GL_USES_FRAG_COORD\n";
1506 }
1507
1508 if (mUsesPointCoord)
1509 {
1510 out << "#define GL_USES_POINT_COORD\n";
1511 }
1512
1513 if (mUsesFrontFacing)
1514 {
1515 out << "#define GL_USES_FRONT_FACING\n";
1516 }
1517
1518 if (mUsesPointSize)
1519 {
1520 out << "#define GL_USES_POINT_SIZE\n";
1521 }
1522
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001523 if (mUsesFragDepth)
1524 {
1525 out << "#define GL_USES_FRAG_DEPTH\n";
1526 }
1527
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001528 if (mUsesDepthRange)
1529 {
1530 out << "#define GL_USES_DEPTH_RANGE\n";
1531 }
1532
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001533 if (mUsesXor)
1534 {
1535 out << "bool xor(bool p, bool q)\n"
1536 "{\n"
1537 " return (p || q) && !(p && q);\n"
1538 "}\n"
1539 "\n";
1540 }
1541
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001542 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001543}
1544
1545void OutputHLSL::visitSymbol(TIntermSymbol *node)
1546{
Jamie Madill32aab012015-01-27 14:12:26 -05001547 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001548
Jamie Madill570e04d2013-06-21 09:15:33 -04001549 // Handle accessing std140 structs by value
1550 if (mFlaggedStructMappedNames.count(node) > 0)
1551 {
1552 out << mFlaggedStructMappedNames[node];
1553 return;
1554 }
1555
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001556 TString name = node->getSymbol();
1557
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001558 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001559 {
1560 mUsesDepthRange = true;
1561 out << name;
1562 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001563 else
1564 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001565 TQualifier qualifier = node->getQualifier();
1566
1567 if (qualifier == EvqUniform)
1568 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001569 const TType &nodeType = node->getType();
1570 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001571
1572 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001573 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001574 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001575 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001576 else
1577 {
1578 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001579 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001580
Jamie Madill2e295e22015-04-29 10:41:33 -04001581 ensureStructDefined(nodeType);
1582
Olli Etuaho96963162016-03-21 11:54:33 +02001583 const TName &nameWithMetadata = node->getName();
1584 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001585 }
Jamie Madill19571812013-08-12 15:26:34 -07001586 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001587 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001588 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001589 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001590 }
Jamie Madill033dae62014-06-18 12:56:28 -04001591 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001592 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001593 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001594 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001595 }
Jamie Madill19571812013-08-12 15:26:34 -07001596 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001597 {
1598 mReferencedOutputVariables[name] = node;
1599 out << "out_" << name;
1600 }
1601 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001602 {
1603 out << "gl_Color[0]";
1604 mUsesFragColor = true;
1605 }
1606 else if (qualifier == EvqFragData)
1607 {
1608 out << "gl_Color";
1609 mUsesFragData = true;
1610 }
1611 else if (qualifier == EvqFragCoord)
1612 {
1613 mUsesFragCoord = true;
1614 out << name;
1615 }
1616 else if (qualifier == EvqPointCoord)
1617 {
1618 mUsesPointCoord = true;
1619 out << name;
1620 }
1621 else if (qualifier == EvqFrontFacing)
1622 {
1623 mUsesFrontFacing = true;
1624 out << name;
1625 }
1626 else if (qualifier == EvqPointSize)
1627 {
1628 mUsesPointSize = true;
1629 out << name;
1630 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001631 else if (qualifier == EvqInstanceID)
1632 {
1633 mUsesInstanceID = true;
1634 out << name;
1635 }
Corentin Wallezb076add2016-01-11 16:45:46 -05001636 else if (qualifier == EvqVertexID)
1637 {
1638 mUsesVertexID = true;
1639 out << name;
1640 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001641 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001642 {
1643 mUsesFragDepth = true;
1644 out << "gl_Depth";
1645 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001646 else
1647 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001648 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001649 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001650 }
1651}
1652
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001653void OutputHLSL::visitRaw(TIntermRaw *node)
1654{
Jamie Madill32aab012015-01-27 14:12:26 -05001655 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001656}
1657
Olli Etuaho7fb49552015-03-18 17:27:44 +02001658void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1659{
1660 if (type.isScalar() && !type.isArray())
1661 {
1662 if (op == EOpEqual)
1663 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001664 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001665 }
1666 else
1667 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001668 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001669 }
1670 }
1671 else
1672 {
1673 if (visit == PreVisit && op == EOpNotEqual)
1674 {
1675 out << "!";
1676 }
1677
1678 if (type.isArray())
1679 {
1680 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001681 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001682 }
1683 else if (type.getBasicType() == EbtStruct)
1684 {
1685 const TStructure &structure = *type.getStruct();
1686 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001687 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001688 }
1689 else
1690 {
1691 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001692 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001693 }
1694 }
1695}
1696
Olli Etuaho96963162016-03-21 11:54:33 +02001697bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
1698{
1699 // Inside InVisit the current node is already in the path.
1700 const unsigned int initialN = visit == InVisit ? 1u : 0u;
1701 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
1702 {
1703 TIntermNode *ancestor = getAncestorNode(n);
1704 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1705 if (ancestorBinary == nullptr)
1706 {
1707 return false;
1708 }
1709 switch (ancestorBinary->getOp())
1710 {
1711 case EOpIndexDirectStruct:
1712 {
1713 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1714 const TIntermConstantUnion *index =
1715 ancestorBinary->getRight()->getAsConstantUnion();
1716 const TField *field = structure->fields()[index->getIConst(0)];
1717 if (IsSampler(field->type()->getBasicType()))
1718 {
1719 return true;
1720 }
1721 break;
1722 }
1723 case EOpIndexDirect:
1724 break;
1725 default:
1726 // Returning a sampler from indirect indexing is not supported.
1727 return false;
1728 }
1729 }
1730 return false;
1731}
1732
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1734{
Jamie Madill32aab012015-01-27 14:12:26 -05001735 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736
Jamie Madill570e04d2013-06-21 09:15:33 -04001737 // Handle accessing std140 structs by value
1738 if (mFlaggedStructMappedNames.count(node) > 0)
1739 {
1740 out << mFlaggedStructMappedNames[node];
1741 return false;
1742 }
1743
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744 switch (node->getOp())
1745 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001746 case EOpAssign:
1747 if (node->getLeft()->isArray())
1748 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001749 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1750 if (rightAgg != nullptr && rightAgg->isConstructor())
1751 {
1752 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1753 out << functionName << "(";
1754 node->getLeft()->traverse(this);
1755 TIntermSequence *seq = rightAgg->getSequence();
1756 for (auto &arrayElement : *seq)
1757 {
1758 out << ", ";
1759 arrayElement->traverse(this);
1760 }
1761 out << ")";
1762 return false;
1763 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001764 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1765 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1766
1767 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001768 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001769 }
1770 else
1771 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001772 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001773 }
1774 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001775 case EOpInitialize:
1776 if (visit == PreVisit)
1777 {
1778 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1779 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1780 // new variable is created before the assignment is evaluated), so we need to convert
1781 // this to "float t = x, x = t;".
1782
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001783 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001784 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001785 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001786
Jamie Madill37997142015-01-28 10:06:34 -05001787 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1788 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001789 {
Jamie Madill37997142015-01-28 10:06:34 -05001790 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001791 // after we initialize uniforms.
1792 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1793 deferredInit->setLeft(node->getLeft());
1794 deferredInit->setRight(node->getRight());
1795 deferredInit->setType(node->getType());
1796 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001797 const TString &initString = initializer(node->getType());
1798 node->setRight(new TIntermRaw(node->getType(), initString));
1799 }
1800 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1801 {
1802 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001803 return false;
1804 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001805 else if (writeConstantInitialization(out, symbolNode, expression))
1806 {
1807 return false;
1808 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001809 }
1810 else if (visit == InVisit)
1811 {
1812 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001813 }
1814 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001815 case EOpAddAssign:
1816 outputTriplet(out, visit, "(", " += ", ")");
1817 break;
1818 case EOpSubAssign:
1819 outputTriplet(out, visit, "(", " -= ", ")");
1820 break;
1821 case EOpMulAssign:
1822 outputTriplet(out, visit, "(", " *= ", ")");
1823 break;
1824 case EOpVectorTimesScalarAssign:
1825 outputTriplet(out, visit, "(", " *= ", ")");
1826 break;
1827 case EOpMatrixTimesScalarAssign:
1828 outputTriplet(out, visit, "(", " *= ", ")");
1829 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001830 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001831 if (visit == PreVisit)
1832 {
1833 out << "(";
1834 }
1835 else if (visit == InVisit)
1836 {
1837 out << " = mul(";
1838 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001839 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001840 }
1841 else
1842 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001843 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001844 }
1845 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001846 case EOpMatrixTimesMatrixAssign:
1847 if (visit == PreVisit)
1848 {
1849 out << "(";
1850 }
1851 else if (visit == InVisit)
1852 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001853 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001854 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001855 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001856 }
1857 else
1858 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001859 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001860 }
1861 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001862 case EOpDivAssign:
1863 outputTriplet(out, visit, "(", " /= ", ")");
1864 break;
1865 case EOpIModAssign:
1866 outputTriplet(out, visit, "(", " %= ", ")");
1867 break;
1868 case EOpBitShiftLeftAssign:
1869 outputTriplet(out, visit, "(", " <<= ", ")");
1870 break;
1871 case EOpBitShiftRightAssign:
1872 outputTriplet(out, visit, "(", " >>= ", ")");
1873 break;
1874 case EOpBitwiseAndAssign:
1875 outputTriplet(out, visit, "(", " &= ", ")");
1876 break;
1877 case EOpBitwiseXorAssign:
1878 outputTriplet(out, visit, "(", " ^= ", ")");
1879 break;
1880 case EOpBitwiseOrAssign:
1881 outputTriplet(out, visit, "(", " |= ", ")");
1882 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001883 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001884 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001885 const TType& leftType = node->getLeft()->getType();
1886 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001887 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001888 if (visit == PreVisit)
1889 {
1890 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1891 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001892 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001893 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001894 return false;
1895 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001896 }
Olli Etuaho96963162016-03-21 11:54:33 +02001897 else if (ancestorEvaluatesToSamplerInStruct(visit))
1898 {
1899 // All parts of an expression that access a sampler in a struct need to use _ as
1900 // separator to access the sampler variable that has been moved out of the struct.
1901 outputTriplet(out, visit, "", "_", "");
1902 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001903 else
1904 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001905 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001906 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001907 }
1908 break;
1909 case EOpIndexIndirect:
1910 // We do not currently support indirect references to interface blocks
1911 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001912 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001913 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001914 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001915 {
1916 const TStructure* structure = node->getLeft()->getType().getStruct();
1917 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1918 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001919
Olli Etuaho96963162016-03-21 11:54:33 +02001920 // In cases where indexing returns a sampler, we need to access the sampler variable
1921 // that has been moved out of the struct.
1922 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1923 if (visit == PreVisit && indexingReturnsSampler)
1924 {
1925 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1926 // This prefix is only output at the beginning of the indexing expression, which
1927 // may have multiple parts.
1928 out << "angle";
1929 }
1930 if (!indexingReturnsSampler)
1931 {
1932 // All parts of an expression that access a sampler in a struct need to use _ as
1933 // separator to access the sampler variable that has been moved out of the struct.
1934 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1935 }
1936 if (visit == InVisit)
1937 {
1938 if (indexingReturnsSampler)
1939 {
1940 out << "_" + field->name();
1941 }
1942 else
1943 {
1944 out << "." + DecorateField(field->name(), *structure);
1945 }
1946
1947 return false;
1948 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001949 }
1950 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001951 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001952 if (visit == InVisit)
1953 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001954 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1955 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1956 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001957 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001958
1959 return false;
1960 }
1961 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962 case EOpVectorSwizzle:
1963 if (visit == InVisit)
1964 {
1965 out << ".";
1966
1967 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1968
1969 if (swizzle)
1970 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001971 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001972
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001973 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001974 {
1975 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1976
1977 if (element)
1978 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001979 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980
1981 switch (i)
1982 {
1983 case 0: out << "x"; break;
1984 case 1: out << "y"; break;
1985 case 2: out << "z"; break;
1986 case 3: out << "w"; break;
1987 default: UNREACHABLE();
1988 }
1989 }
1990 else UNREACHABLE();
1991 }
1992 }
1993 else UNREACHABLE();
1994
1995 return false; // Fully processed
1996 }
1997 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001998 case EOpAdd:
1999 outputTriplet(out, visit, "(", " + ", ")");
2000 break;
2001 case EOpSub:
2002 outputTriplet(out, visit, "(", " - ", ")");
2003 break;
2004 case EOpMul:
2005 outputTriplet(out, visit, "(", " * ", ")");
2006 break;
2007 case EOpDiv:
2008 outputTriplet(out, visit, "(", " / ", ")");
2009 break;
2010 case EOpIMod:
2011 outputTriplet(out, visit, "(", " % ", ")");
2012 break;
2013 case EOpBitShiftLeft:
2014 outputTriplet(out, visit, "(", " << ", ")");
2015 break;
2016 case EOpBitShiftRight:
2017 outputTriplet(out, visit, "(", " >> ", ")");
2018 break;
2019 case EOpBitwiseAnd:
2020 outputTriplet(out, visit, "(", " & ", ")");
2021 break;
2022 case EOpBitwiseXor:
2023 outputTriplet(out, visit, "(", " ^ ", ")");
2024 break;
2025 case EOpBitwiseOr:
2026 outputTriplet(out, visit, "(", " | ", ")");
2027 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002028 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002029 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02002030 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002031 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002032 case EOpLessThan:
2033 outputTriplet(out, visit, "(", " < ", ")");
2034 break;
2035 case EOpGreaterThan:
2036 outputTriplet(out, visit, "(", " > ", ")");
2037 break;
2038 case EOpLessThanEqual:
2039 outputTriplet(out, visit, "(", " <= ", ")");
2040 break;
2041 case EOpGreaterThanEqual:
2042 outputTriplet(out, visit, "(", " >= ", ")");
2043 break;
2044 case EOpVectorTimesScalar:
2045 outputTriplet(out, visit, "(", " * ", ")");
2046 break;
2047 case EOpMatrixTimesScalar:
2048 outputTriplet(out, visit, "(", " * ", ")");
2049 break;
2050 case EOpVectorTimesMatrix:
2051 outputTriplet(out, visit, "mul(", ", transpose(", "))");
2052 break;
2053 case EOpMatrixTimesVector:
2054 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
2055 break;
2056 case EOpMatrixTimesMatrix:
2057 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
2058 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002059 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03002060 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
2061 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002062 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03002063 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002064 case EOpLogicalXor:
2065 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002066 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002067 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002068 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03002069 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
2070 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002071 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03002072 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073 default: UNREACHABLE();
2074 }
2075
2076 return true;
2077}
2078
2079bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2080{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002081 TInfoSinkBase &out = getInfoSink();
2082
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 switch (node->getOp())
2084 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002085 case EOpNegative:
2086 outputTriplet(out, visit, "(-", "", ")");
2087 break;
2088 case EOpPositive:
2089 outputTriplet(out, visit, "(+", "", ")");
2090 break;
2091 case EOpVectorLogicalNot:
2092 outputTriplet(out, visit, "(!", "", ")");
2093 break;
2094 case EOpLogicalNot:
2095 outputTriplet(out, visit, "(!", "", ")");
2096 break;
2097 case EOpBitwiseNot:
2098 outputTriplet(out, visit, "(~", "", ")");
2099 break;
2100 case EOpPostIncrement:
2101 outputTriplet(out, visit, "(", "", "++)");
2102 break;
2103 case EOpPostDecrement:
2104 outputTriplet(out, visit, "(", "", "--)");
2105 break;
2106 case EOpPreIncrement:
2107 outputTriplet(out, visit, "(++", "", ")");
2108 break;
2109 case EOpPreDecrement:
2110 outputTriplet(out, visit, "(--", "", ")");
2111 break;
2112 case EOpRadians:
2113 outputTriplet(out, visit, "radians(", "", ")");
2114 break;
2115 case EOpDegrees:
2116 outputTriplet(out, visit, "degrees(", "", ")");
2117 break;
2118 case EOpSin:
2119 outputTriplet(out, visit, "sin(", "", ")");
2120 break;
2121 case EOpCos:
2122 outputTriplet(out, visit, "cos(", "", ")");
2123 break;
2124 case EOpTan:
2125 outputTriplet(out, visit, "tan(", "", ")");
2126 break;
2127 case EOpAsin:
2128 outputTriplet(out, visit, "asin(", "", ")");
2129 break;
2130 case EOpAcos:
2131 outputTriplet(out, visit, "acos(", "", ")");
2132 break;
2133 case EOpAtan:
2134 outputTriplet(out, visit, "atan(", "", ")");
2135 break;
2136 case EOpSinh:
2137 outputTriplet(out, visit, "sinh(", "", ")");
2138 break;
2139 case EOpCosh:
2140 outputTriplet(out, visit, "cosh(", "", ")");
2141 break;
2142 case EOpTanh:
2143 outputTriplet(out, visit, "tanh(", "", ")");
2144 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002145 case EOpAsinh:
2146 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002147 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002148 break;
2149 case EOpAcosh:
2150 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002151 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002152 break;
2153 case EOpAtanh:
2154 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002155 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002156 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002157 case EOpExp:
2158 outputTriplet(out, visit, "exp(", "", ")");
2159 break;
2160 case EOpLog:
2161 outputTriplet(out, visit, "log(", "", ")");
2162 break;
2163 case EOpExp2:
2164 outputTriplet(out, visit, "exp2(", "", ")");
2165 break;
2166 case EOpLog2:
2167 outputTriplet(out, visit, "log2(", "", ")");
2168 break;
2169 case EOpSqrt:
2170 outputTriplet(out, visit, "sqrt(", "", ")");
2171 break;
2172 case EOpInverseSqrt:
2173 outputTriplet(out, visit, "rsqrt(", "", ")");
2174 break;
2175 case EOpAbs:
2176 outputTriplet(out, visit, "abs(", "", ")");
2177 break;
2178 case EOpSign:
2179 outputTriplet(out, visit, "sign(", "", ")");
2180 break;
2181 case EOpFloor:
2182 outputTriplet(out, visit, "floor(", "", ")");
2183 break;
2184 case EOpTrunc:
2185 outputTriplet(out, visit, "trunc(", "", ")");
2186 break;
2187 case EOpRound:
2188 outputTriplet(out, visit, "round(", "", ")");
2189 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08002190 case EOpRoundEven:
2191 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002192 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08002193 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002194 case EOpCeil:
2195 outputTriplet(out, visit, "ceil(", "", ")");
2196 break;
2197 case EOpFract:
2198 outputTriplet(out, visit, "frac(", "", ")");
2199 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05302200 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002201 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05302202 mRequiresIEEEStrictCompiling = true;
2203 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002204 case EOpIsInf:
2205 outputTriplet(out, visit, "isinf(", "", ")");
2206 break;
2207 case EOpFloatBitsToInt:
2208 outputTriplet(out, visit, "asint(", "", ")");
2209 break;
2210 case EOpFloatBitsToUint:
2211 outputTriplet(out, visit, "asuint(", "", ")");
2212 break;
2213 case EOpIntBitsToFloat:
2214 outputTriplet(out, visit, "asfloat(", "", ")");
2215 break;
2216 case EOpUintBitsToFloat:
2217 outputTriplet(out, visit, "asfloat(", "", ")");
2218 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02002219 case EOpPackSnorm2x16:
2220 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002221 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002222 break;
2223 case EOpPackUnorm2x16:
2224 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002225 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002226 break;
2227 case EOpPackHalf2x16:
2228 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002229 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002230 break;
2231 case EOpUnpackSnorm2x16:
2232 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002233 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002234 break;
2235 case EOpUnpackUnorm2x16:
2236 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002237 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002238 break;
2239 case EOpUnpackHalf2x16:
2240 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002241 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002242 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002243 case EOpLength:
2244 outputTriplet(out, visit, "length(", "", ")");
2245 break;
2246 case EOpNormalize:
2247 outputTriplet(out, visit, "normalize(", "", ")");
2248 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002249 case EOpDFdx:
2250 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2251 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002252 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002253 }
2254 else
2255 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002256 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002257 }
2258 break;
2259 case EOpDFdy:
2260 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2261 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002262 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002263 }
2264 else
2265 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002266 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002267 }
2268 break;
2269 case EOpFwidth:
2270 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2271 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002272 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002273 }
2274 else
2275 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002276 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002277 }
2278 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002279 case EOpTranspose:
2280 outputTriplet(out, visit, "transpose(", "", ")");
2281 break;
2282 case EOpDeterminant:
2283 outputTriplet(out, visit, "determinant(transpose(", "", "))");
2284 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002285 case EOpInverse:
2286 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002287 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002288 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002289
Jamie Madill8c46ab12015-12-07 16:39:19 -05002290 case EOpAny:
2291 outputTriplet(out, visit, "any(", "", ")");
2292 break;
2293 case EOpAll:
2294 outputTriplet(out, visit, "all(", "", ")");
2295 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 default: UNREACHABLE();
2297 }
2298
2299 return true;
2300}
2301
Olli Etuaho96963162016-03-21 11:54:33 +02002302TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
2303{
2304 if (node->getAsSymbolNode())
2305 {
2306 return node->getAsSymbolNode()->getSymbol();
2307 }
2308 TIntermBinary *nodeBinary = node->getAsBinaryNode();
2309 switch (nodeBinary->getOp())
2310 {
2311 case EOpIndexDirect:
2312 {
2313 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
2314
2315 TInfoSinkBase prefixSink;
2316 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
2317 return TString(prefixSink.c_str());
2318 }
2319 case EOpIndexDirectStruct:
2320 {
2321 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
2322 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
2323 const TField *field = s->fields()[index];
2324
2325 TInfoSinkBase prefixSink;
2326 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
2327 << field->name();
2328 return TString(prefixSink.c_str());
2329 }
2330 default:
2331 UNREACHABLE();
2332 return TString("");
2333 }
2334}
2335
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2337{
Jamie Madill32aab012015-01-27 14:12:26 -05002338 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002340 switch (node->getOp())
2341 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002342 case EOpSequence:
2343 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002344 if (mInsideFunction)
2345 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002346 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002347 out << "{\n";
2348 }
2349
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002350 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002351 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002352 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002353
Olli Etuahoa6f22092015-05-08 18:31:10 +03002354 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002355
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002356 // Don't output ; after case labels, they're terminated by :
2357 // This is needed especially since outputting a ; after a case statement would turn empty
2358 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03002359 // Also no need to output ; after selection (if) statements or sequences. This is done just
2360 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002361 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
2362 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03002363 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002364 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002365 }
2366
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002367 if (mInsideFunction)
2368 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002369 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002370 out << "}\n";
2371 }
2372
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002373 return false;
2374 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 case EOpDeclaration:
2376 if (visit == PreVisit)
2377 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002378 TIntermSequence *sequence = node->getSequence();
2379 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03002380 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002382 if (variable &&
2383 (variable->getQualifier() == EvqTemporary ||
2384 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002386 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00002387
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002388 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002390 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002392 out << "static ";
2393 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002394
Olli Etuahoa6f22092015-05-08 18:31:10 +03002395 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04002396
Olli Etuahoa6f22092015-05-08 18:31:10 +03002397 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04002398
Olli Etuahoa6f22092015-05-08 18:31:10 +03002399 if (symbol)
2400 {
2401 symbol->traverse(this);
2402 out << ArrayString(symbol->getType());
2403 out << " = " + initializer(symbol->getType());
2404 }
2405 else
2406 {
2407 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002410 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2411 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002412 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002413 }
2414 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415 }
Jamie Madill033dae62014-06-18 12:56:28 -04002416 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002417 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002418 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002419 {
2420 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2421
2422 if (symbol)
2423 {
2424 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2425 mReferencedVaryings[symbol->getSymbol()] = symbol;
2426 }
2427 else
2428 {
2429 (*sit)->traverse(this);
2430 }
2431 }
2432 }
2433
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 return false;
2435 }
2436 else if (visit == InVisit)
2437 {
2438 out << ", ";
2439 }
2440 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04002441 case EOpInvariantDeclaration:
2442 // Do not do any translation
2443 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002444 case EOpPrototype:
2445 if (visit == PreVisit)
2446 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002447 size_t index = mCallDag.findIndex(node);
2448 // Skip the prototype if it is not implemented (and thus not used)
2449 if (index == CallDAG::InvalidIndex)
2450 {
2451 return false;
2452 }
2453
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002454 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002455
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002456 TString name = DecorateFunctionIfNeeded(node->getNameObj());
2457 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
2458 << (mOutputLod0Function ? "Lod0(" : "(");
2459
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002460 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002461 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002462 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002463
2464 if (symbol)
2465 {
2466 out << argumentString(symbol);
2467
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002468 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002469 {
2470 out << ", ";
2471 }
2472 }
2473 else UNREACHABLE();
2474 }
2475
2476 out << ");\n";
2477
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002478 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002479 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2480 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002481 {
2482 mOutputLod0Function = true;
2483 node->traverse(this);
2484 mOutputLod0Function = false;
2485 }
2486
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002487 return false;
2488 }
2489 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002490 case EOpComma:
2491 outputTriplet(out, visit, "(", ", ", ")");
2492 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493 case EOpFunction:
2494 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002495 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002496 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497
Corentin Wallez1239ee92015-03-19 14:38:02 -07002498 size_t index = mCallDag.findIndex(node);
2499 ASSERT(index != CallDAG::InvalidIndex);
2500 mCurrentFunctionMetadata = &mASTMetadataList[index];
2501
Jamie Madill033dae62014-06-18 12:56:28 -04002502 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002503
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002504 TIntermSequence *sequence = node->getSequence();
2505 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
2506
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002507 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002509 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002510 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002511 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002513 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002514 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002515 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002516
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002517 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002518 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002519 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002520
2521 if (symbol)
2522 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002523 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002524
2525 out << argumentString(symbol);
2526
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002527 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002528 {
2529 out << ", ";
2530 }
2531 }
2532 else UNREACHABLE();
2533 }
2534
Olli Etuaho4785fec2015-05-18 16:09:37 +03002535 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002536
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002537 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002538 {
2539 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002540 TIntermNode *body = (*sequence)[1];
2541 // The function body node will output braces.
2542 ASSERT(IsSequence(body));
2543 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002544 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002546 else
2547 {
2548 out << "{}\n";
2549 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002550
Corentin Wallez1239ee92015-03-19 14:38:02 -07002551 mCurrentFunctionMetadata = nullptr;
2552
2553 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2554 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002555 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002556 ASSERT(name != "main");
2557 mOutputLod0Function = true;
2558 node->traverse(this);
2559 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002560 }
2561
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002562 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002563 }
2564 break;
2565 case EOpFunctionCall:
2566 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002567 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002568
Corentin Wallez1239ee92015-03-19 14:38:02 -07002569 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002570 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002571 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002572 if (node->isArray())
2573 {
2574 UNIMPLEMENTED();
2575 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002576 size_t index = mCallDag.findIndex(node);
2577 ASSERT(index != CallDAG::InvalidIndex);
2578 lod0 &= mASTMetadataList[index].mNeedsLod0;
2579
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002580 out << DecorateFunctionIfNeeded(node->getNameObj());
2581 out << DisambiguateFunctionName(node->getSequence());
2582 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002583 }
2584 else
2585 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002586 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002587 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002588
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002589 TextureFunction textureFunction;
2590 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002591 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002592 textureFunction.method = TextureFunction::IMPLICIT;
2593 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002594 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002595
2596 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002597 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002598 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002599 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002600 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002601 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002602 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002603 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002604 }
Nicolas Capens46485082014-04-15 13:12:50 -04002605 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2606 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002607 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002608 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002609 }
Nicolas Capens46485082014-04-15 13:12:50 -04002610 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002611 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002612 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002613 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002614 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002615 else if (name == "textureSize")
2616 {
2617 textureFunction.method = TextureFunction::SIZE;
2618 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002619 else if (name == "textureOffset")
2620 {
2621 textureFunction.method = TextureFunction::IMPLICIT;
2622 textureFunction.offset = true;
2623 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002624 else if (name == "textureProjOffset")
2625 {
2626 textureFunction.method = TextureFunction::IMPLICIT;
2627 textureFunction.offset = true;
2628 textureFunction.proj = true;
2629 }
2630 else if (name == "textureLodOffset")
2631 {
2632 textureFunction.method = TextureFunction::LOD;
2633 textureFunction.offset = true;
2634 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002635 else if (name == "textureProjLodOffset")
2636 {
2637 textureFunction.method = TextureFunction::LOD;
2638 textureFunction.proj = true;
2639 textureFunction.offset = true;
2640 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002641 else if (name == "texelFetch")
2642 {
2643 textureFunction.method = TextureFunction::FETCH;
2644 }
2645 else if (name == "texelFetchOffset")
2646 {
2647 textureFunction.method = TextureFunction::FETCH;
2648 textureFunction.offset = true;
2649 }
Nicolas Capens46485082014-04-15 13:12:50 -04002650 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002651 {
2652 textureFunction.method = TextureFunction::GRAD;
2653 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002654 else if (name == "textureGradOffset")
2655 {
2656 textureFunction.method = TextureFunction::GRAD;
2657 textureFunction.offset = true;
2658 }
Nicolas Capens46485082014-04-15 13:12:50 -04002659 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002660 {
2661 textureFunction.method = TextureFunction::GRAD;
2662 textureFunction.proj = true;
2663 }
2664 else if (name == "textureProjGradOffset")
2665 {
2666 textureFunction.method = TextureFunction::GRAD;
2667 textureFunction.proj = true;
2668 textureFunction.offset = true;
2669 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002670 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002671
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002672 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002673 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002674 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2675
2676 if (textureFunction.offset)
2677 {
2678 mandatoryArgumentCount++;
2679 }
2680
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002681 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002682
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002683 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002684 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002685 if (bias)
2686 {
2687 textureFunction.method = TextureFunction::LOD0BIAS;
2688 }
2689 else
2690 {
2691 textureFunction.method = TextureFunction::LOD0;
2692 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002693 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002694 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002695 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002696 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002697 }
2698 }
2699
2700 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002701
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002702 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002704
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002705 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002706 {
Olli Etuaho96963162016-03-21 11:54:33 +02002707 TIntermTyped *typedArg = (*arg)->getAsTyped();
2708 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002709 {
2710 out << "texture_";
2711 (*arg)->traverse(this);
2712 out << ", sampler_";
2713 }
2714
2715 (*arg)->traverse(this);
2716
Olli Etuaho96963162016-03-21 11:54:33 +02002717 if (typedArg->getType().isStructureContainingSamplers())
2718 {
2719 const TType &argType = typedArg->getType();
2720 TVector<TIntermSymbol *> samplerSymbols;
2721 TString structName = samplerNamePrefixFromStruct(typedArg);
2722 argType.createSamplerSymbols("angle_" + structName, "",
2723 argType.isArray() ? argType.getArraySize() : 0,
2724 &samplerSymbols, nullptr);
2725 for (const TIntermSymbol *sampler : samplerSymbols)
2726 {
2727 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2728 {
2729 out << ", texture_" << sampler->getSymbol();
2730 out << ", sampler_" << sampler->getSymbol();
2731 }
2732 else
2733 {
2734 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
2735 // of D3D9, it's the sampler variable.
2736 out << ", " + sampler->getSymbol();
2737 }
2738 }
2739 }
2740
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002741 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002742 {
2743 out << ", ";
2744 }
2745 }
2746
2747 out << ")";
2748
2749 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002750 }
2751 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002752 case EOpParameters:
2753 outputTriplet(out, visit, "(", ", ", ")\n{\n");
2754 break;
2755 case EOpConstructFloat:
2756 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
2757 break;
2758 case EOpConstructVec2:
2759 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
2760 break;
2761 case EOpConstructVec3:
2762 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
2763 break;
2764 case EOpConstructVec4:
2765 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
2766 break;
2767 case EOpConstructBool:
2768 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
2769 break;
2770 case EOpConstructBVec2:
2771 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
2772 break;
2773 case EOpConstructBVec3:
2774 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
2775 break;
2776 case EOpConstructBVec4:
2777 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
2778 break;
2779 case EOpConstructInt:
2780 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
2781 break;
2782 case EOpConstructIVec2:
2783 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
2784 break;
2785 case EOpConstructIVec3:
2786 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
2787 break;
2788 case EOpConstructIVec4:
2789 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
2790 break;
2791 case EOpConstructUInt:
2792 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
2793 break;
2794 case EOpConstructUVec2:
2795 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
2796 break;
2797 case EOpConstructUVec3:
2798 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
2799 break;
2800 case EOpConstructUVec4:
2801 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
2802 break;
2803 case EOpConstructMat2:
2804 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
2805 break;
2806 case EOpConstructMat2x3:
2807 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
2808 break;
2809 case EOpConstructMat2x4:
2810 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
2811 break;
2812 case EOpConstructMat3x2:
2813 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
2814 break;
2815 case EOpConstructMat3:
2816 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
2817 break;
2818 case EOpConstructMat3x4:
2819 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
2820 break;
2821 case EOpConstructMat4x2:
2822 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
2823 break;
2824 case EOpConstructMat4x3:
2825 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
2826 break;
2827 case EOpConstructMat4:
2828 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
2829 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002830 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002831 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002832 if (node->getType().isArray())
2833 {
2834 UNIMPLEMENTED();
2835 }
Jamie Madill033dae62014-06-18 12:56:28 -04002836 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002837 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002838 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002839 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002840 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002841 case EOpLessThan:
2842 outputTriplet(out, visit, "(", " < ", ")");
2843 break;
2844 case EOpGreaterThan:
2845 outputTriplet(out, visit, "(", " > ", ")");
2846 break;
2847 case EOpLessThanEqual:
2848 outputTriplet(out, visit, "(", " <= ", ")");
2849 break;
2850 case EOpGreaterThanEqual:
2851 outputTriplet(out, visit, "(", " >= ", ")");
2852 break;
2853 case EOpVectorEqual:
2854 outputTriplet(out, visit, "(", " == ", ")");
2855 break;
2856 case EOpVectorNotEqual:
2857 outputTriplet(out, visit, "(", " != ", ")");
2858 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002859 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002860 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002861 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002862 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002863 case EOpModf:
2864 outputTriplet(out, visit, "modf(", ", ", ")");
2865 break;
2866 case EOpPow:
2867 outputTriplet(out, visit, "pow(", ", ", ")");
2868 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002870 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002871 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002872 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002874 case EOpMin:
2875 outputTriplet(out, visit, "min(", ", ", ")");
2876 break;
2877 case EOpMax:
2878 outputTriplet(out, visit, "max(", ", ", ")");
2879 break;
2880 case EOpClamp:
2881 outputTriplet(out, visit, "clamp(", ", ", ")");
2882 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302883 case EOpMix:
2884 {
2885 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2886 if (lastParamNode->getType().getBasicType() == EbtBool)
2887 {
2888 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2889 // so use emulated version.
2890 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002891 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05302892 }
2893 else
2894 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002895 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302896 }
2897 }
2898 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002899 case EOpStep:
2900 outputTriplet(out, visit, "step(", ", ", ")");
2901 break;
2902 case EOpSmoothStep:
2903 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2904 break;
2905 case EOpDistance:
2906 outputTriplet(out, visit, "distance(", ", ", ")");
2907 break;
2908 case EOpDot:
2909 outputTriplet(out, visit, "dot(", ", ", ")");
2910 break;
2911 case EOpCross:
2912 outputTriplet(out, visit, "cross(", ", ", ")");
2913 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002914 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002915 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002916 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002917 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002918 case EOpReflect:
2919 outputTriplet(out, visit, "reflect(", ", ", ")");
2920 break;
2921 case EOpRefract:
2922 outputTriplet(out, visit, "refract(", ", ", ")");
2923 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002924 case EOpOuterProduct:
2925 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002926 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02002927 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002928 case EOpMul:
2929 outputTriplet(out, visit, "(", " * ", ")");
2930 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002931 default: UNREACHABLE();
2932 }
2933
2934 return true;
2935}
2936
Jamie Madill8c46ab12015-12-07 16:39:19 -05002937void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002938{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002939 out << "if (";
2940
2941 node->getCondition()->traverse(this);
2942
2943 out << ")\n";
2944
Jamie Madill8c46ab12015-12-07 16:39:19 -05002945 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002946
2947 bool discard = false;
2948
2949 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002950 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002951 // The trueBlock child node will output braces.
2952 ASSERT(IsSequence(node->getTrueBlock()));
2953
Olli Etuahoa6f22092015-05-08 18:31:10 +03002954 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002955
Olli Etuahoa6f22092015-05-08 18:31:10 +03002956 // Detect true discard
2957 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2958 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002959 else
2960 {
2961 // TODO(oetuaho): Check if the semicolon inside is necessary.
2962 // It's there as a result of conservative refactoring of the output.
2963 out << "{;}\n";
2964 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002965
Jamie Madill8c46ab12015-12-07 16:39:19 -05002966 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002967
Olli Etuahoa6f22092015-05-08 18:31:10 +03002968 if (node->getFalseBlock())
2969 {
2970 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002971
Jamie Madill8c46ab12015-12-07 16:39:19 -05002972 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002973
Olli Etuaho4785fec2015-05-18 16:09:37 +03002974 // Either this is "else if" or the falseBlock child node will output braces.
2975 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2976
Olli Etuahoa6f22092015-05-08 18:31:10 +03002977 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002978
Jamie Madill8c46ab12015-12-07 16:39:19 -05002979 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002980
Olli Etuahoa6f22092015-05-08 18:31:10 +03002981 // Detect false discard
2982 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2983 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002984
Olli Etuahoa6f22092015-05-08 18:31:10 +03002985 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002986 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002987 {
2988 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002989 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002990}
2991
2992bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2993{
2994 TInfoSinkBase &out = getInfoSink();
2995
2996 ASSERT(!node->usesTernaryOperator());
2997
2998 if (!mInsideFunction)
2999 {
3000 // This is part of unfolded global initialization.
3001 mDeferredGlobalInitializers.push_back(node);
3002 return false;
3003 }
3004
3005 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07003006 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03003007 {
3008 out << "FLATTEN ";
3009 }
3010
Jamie Madill8c46ab12015-12-07 16:39:19 -05003011 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003012
3013 return false;
3014}
3015
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003016bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02003017{
Jamie Madill8c46ab12015-12-07 16:39:19 -05003018 TInfoSinkBase &out = getInfoSink();
3019
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003020 if (node->getStatementList())
3021 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02003022 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05003023 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003024 // The curly braces get written when visiting the statementList aggregate
3025 }
3026 else
3027 {
3028 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05003029 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003030 }
3031 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02003032}
3033
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003034bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02003035{
Jamie Madill8c46ab12015-12-07 16:39:19 -05003036 TInfoSinkBase &out = getInfoSink();
3037
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003038 if (node->hasCondition())
3039 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003040 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003041 return true;
3042 }
3043 else
3044 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003045 out << "default:\n";
3046 return false;
3047 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02003048}
3049
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003050void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
3051{
Jamie Madill8c46ab12015-12-07 16:39:19 -05003052 TInfoSinkBase &out = getInfoSink();
3053 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003054}
3055
3056bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
3057{
Nicolas Capens655fe362014-04-11 13:12:34 -04003058 mNestedLoopDepth++;
3059
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003060 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07003061 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07003062 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003063
Jamie Madill8c46ab12015-12-07 16:39:19 -05003064 TInfoSinkBase &out = getInfoSink();
3065
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003066 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003067 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003068 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00003069 {
Nicolas Capens655fe362014-04-11 13:12:34 -04003070 mInsideDiscontinuousLoop = wasDiscontinuous;
3071 mNestedLoopDepth--;
3072
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00003073 return false;
3074 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003075 }
3076
Corentin Wallez1239ee92015-03-19 14:38:02 -07003077 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00003078 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003079 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07003080 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003081
Jamie Madill8c46ab12015-12-07 16:39:19 -05003082 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003083 }
3084 else
3085 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07003086 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003087
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003088 if (node->getInit())
3089 {
3090 node->getInit()->traverse(this);
3091 }
3092
3093 out << "; ";
3094
alokp@chromium.org52813552010-11-16 18:36:09 +00003095 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003096 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003097 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003098 }
3099
3100 out << "; ";
3101
alokp@chromium.org52813552010-11-16 18:36:09 +00003102 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003103 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003104 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003105 }
3106
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003107 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003108
Jamie Madill8c46ab12015-12-07 16:39:19 -05003109 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003110 }
3111
3112 if (node->getBody())
3113 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03003114 // The loop body node will output braces.
3115 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03003116 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003117 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03003118 else
3119 {
3120 // TODO(oetuaho): Check if the semicolon inside is necessary.
3121 // It's there as a result of conservative refactoring of the output.
3122 out << "{;}\n";
3123 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003124
Jamie Madill8c46ab12015-12-07 16:39:19 -05003125 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003126
alokp@chromium.org52813552010-11-16 18:36:09 +00003127 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003128 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003129 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003130 out << "while(\n";
3131
alokp@chromium.org52813552010-11-16 18:36:09 +00003132 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003133
daniel@transgaming.com73536982012-03-21 20:45:49 +00003134 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003135 }
3136
daniel@transgaming.com73536982012-03-21 20:45:49 +00003137 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003138
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003139 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04003140 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003141
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003142 return false;
3143}
3144
3145bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
3146{
Jamie Madill32aab012015-01-27 14:12:26 -05003147 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003148
3149 switch (node->getFlowOp())
3150 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003151 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05003152 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003153 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003154 case EOpBreak:
3155 if (visit == PreVisit)
3156 {
Nicolas Capens655fe362014-04-11 13:12:34 -04003157 if (mNestedLoopDepth > 1)
3158 {
3159 mUsesNestedBreak = true;
3160 }
3161
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003162 if (mExcessiveLoopIndex)
3163 {
3164 out << "{Break";
3165 mExcessiveLoopIndex->traverse(this);
3166 out << " = true; break;}\n";
3167 }
3168 else
3169 {
3170 out << "break;\n";
3171 }
3172 }
3173 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05003174 case EOpContinue:
3175 outputTriplet(out, visit, "continue;\n", "", "");
3176 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003177 case EOpReturn:
3178 if (visit == PreVisit)
3179 {
3180 if (node->getExpression())
3181 {
3182 out << "return ";
3183 }
3184 else
3185 {
3186 out << "return;\n";
3187 }
3188 }
3189 else if (visit == PostVisit)
3190 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003191 if (node->getExpression())
3192 {
3193 out << ";\n";
3194 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003195 }
3196 break;
3197 default: UNREACHABLE();
3198 }
3199
3200 return true;
3201}
3202
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003203bool OutputHLSL::isSingleStatement(TIntermNode *node)
3204{
3205 TIntermAggregate *aggregate = node->getAsAggregate();
3206
3207 if (aggregate)
3208 {
3209 if (aggregate->getOp() == EOpSequence)
3210 {
3211 return false;
3212 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04003213 else if (aggregate->getOp() == EOpDeclaration)
3214 {
3215 // Declaring multiple comma-separated variables must be considered multiple statements
3216 // because each individual declaration has side effects which are visible in the next.
3217 return false;
3218 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003219 else
3220 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07003221 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003222 {
3223 if (!isSingleStatement(*sit))
3224 {
3225 return false;
3226 }
3227 }
3228
3229 return true;
3230 }
3231 }
3232
3233 return true;
3234}
3235
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003236// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
3237// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05003238bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003239{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003240 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003241
3242 // Parse loops of the form:
3243 // for(int index = initial; index [comparator] limit; index += increment)
3244 TIntermSymbol *index = NULL;
3245 TOperator comparator = EOpNull;
3246 int initial = 0;
3247 int limit = 0;
3248 int increment = 0;
3249
3250 // Parse index name and intial value
3251 if (node->getInit())
3252 {
3253 TIntermAggregate *init = node->getInit()->getAsAggregate();
3254
3255 if (init)
3256 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07003257 TIntermSequence *sequence = init->getSequence();
3258 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003259
3260 if (variable && variable->getQualifier() == EvqTemporary)
3261 {
3262 TIntermBinary *assign = variable->getAsBinaryNode();
3263
3264 if (assign->getOp() == EOpInitialize)
3265 {
3266 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3267 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3268
3269 if (symbol && constant)
3270 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003271 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003272 {
3273 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003274 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003275 }
3276 }
3277 }
3278 }
3279 }
3280 }
3281
3282 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003283 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003284 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003285 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003286
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003287 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3288 {
3289 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3290
3291 if (constant)
3292 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003293 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003294 {
3295 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003296 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003297 }
3298 }
3299 }
3300 }
3301
3302 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003303 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003304 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003305 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3306 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003307
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003308 if (binaryTerminal)
3309 {
3310 TOperator op = binaryTerminal->getOp();
3311 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3312
3313 if (constant)
3314 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003315 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003316 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003317 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003318
3319 switch (op)
3320 {
3321 case EOpAddAssign: increment = value; break;
3322 case EOpSubAssign: increment = -value; break;
3323 default: UNIMPLEMENTED();
3324 }
3325 }
3326 }
3327 }
3328 else if (unaryTerminal)
3329 {
3330 TOperator op = unaryTerminal->getOp();
3331
3332 switch (op)
3333 {
3334 case EOpPostIncrement: increment = 1; break;
3335 case EOpPostDecrement: increment = -1; break;
3336 case EOpPreIncrement: increment = 1; break;
3337 case EOpPreDecrement: increment = -1; break;
3338 default: UNIMPLEMENTED();
3339 }
3340 }
3341 }
3342
3343 if (index != NULL && comparator != EOpNull && increment != 0)
3344 {
3345 if (comparator == EOpLessThanEqual)
3346 {
3347 comparator = EOpLessThan;
3348 limit += 1;
3349 }
3350
3351 if (comparator == EOpLessThan)
3352 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003353 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003354
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003355 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003356 {
3357 return false; // Not an excessive loop
3358 }
3359
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003360 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3361 mExcessiveLoopIndex = index;
3362
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003363 out << "{int ";
3364 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003365 out << ";\n"
3366 "bool Break";
3367 index->traverse(this);
3368 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003369
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003370 bool firstLoopFragment = true;
3371
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003372 while (iterations > 0)
3373 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003374 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003375
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003376 if (!firstLoopFragment)
3377 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003378 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003379 index->traverse(this);
3380 out << ") {\n";
3381 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003382
3383 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3384 {
3385 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3386 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003387
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003388 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07003389 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003390
Corentin Wallez1239ee92015-03-19 14:38:02 -07003391 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003392 index->traverse(this);
3393 out << " = ";
3394 out << initial;
3395
3396 out << "; ";
3397 index->traverse(this);
3398 out << " < ";
3399 out << clampedLimit;
3400
3401 out << "; ";
3402 index->traverse(this);
3403 out << " += ";
3404 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003405 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003406
Jamie Madill8c46ab12015-12-07 16:39:19 -05003407 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003408 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003409
3410 if (node->getBody())
3411 {
3412 node->getBody()->traverse(this);
3413 }
3414
Jamie Madill8c46ab12015-12-07 16:39:19 -05003415 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003416 out << ";}\n";
3417
3418 if (!firstLoopFragment)
3419 {
3420 out << "}\n";
3421 }
3422
3423 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003424
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003425 initial += MAX_LOOP_ITERATIONS * increment;
3426 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003427 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003428
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003429 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003430
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003431 mExcessiveLoopIndex = restoreIndex;
3432
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003433 return true;
3434 }
3435 else UNIMPLEMENTED();
3436 }
3437
3438 return false; // Not handled as an excessive loop
3439}
3440
Jamie Madill8c46ab12015-12-07 16:39:19 -05003441void OutputHLSL::outputTriplet(TInfoSinkBase &out,
3442 Visit visit,
3443 const char *preString,
3444 const char *inString,
3445 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003446{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003447 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003448 {
3449 out << preString;
3450 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003451 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003452 {
3453 out << inString;
3454 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003455 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003456 {
3457 out << postString;
3458 }
3459}
3460
Jamie Madill8c46ab12015-12-07 16:39:19 -05003461void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003462{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003463 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003464 {
Jamie Madill32aab012015-01-27 14:12:26 -05003465 out << "\n";
3466 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003467
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003468 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003469 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003470 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003471 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003472
Jamie Madill32aab012015-01-27 14:12:26 -05003473 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003474 }
3475}
3476
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003477TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3478{
3479 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003480 const TType &type = symbol->getType();
3481 const TName &name = symbol->getName();
3482 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003483
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003484 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003485 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003486 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003487 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003488 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003489 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003490 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003491 }
3492
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003493 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003494 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003495 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3496 {
3497 // Samplers are passed as indices to the sampler array.
3498 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3499 return "const uint " + nameStr + ArrayString(type);
3500 }
3501 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3502 {
3503 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
3504 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
3505 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
3506 ArrayString(type);
3507 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003508 }
3509
Olli Etuaho96963162016-03-21 11:54:33 +02003510 TStringStream argString;
3511 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
3512 << ArrayString(type);
3513
3514 // If the structure parameter contains samplers, they need to be passed into the function as
3515 // separate parameters. HLSL doesn't natively support samplers in structs.
3516 if (type.isStructureContainingSamplers())
3517 {
3518 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3519 TVector<TIntermSymbol *> samplerSymbols;
3520 type.createSamplerSymbols("angle" + nameStr, "", 0, &samplerSymbols, nullptr);
3521 for (const TIntermSymbol *sampler : samplerSymbols)
3522 {
3523 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3524 {
3525 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
3526 }
3527 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3528 {
3529 const TType &samplerType = sampler->getType();
3530 ASSERT((!type.isArray() && !samplerType.isArray()) ||
3531 type.getArraySize() == samplerType.getArraySize());
3532 ASSERT(IsSampler(samplerType.getBasicType()));
3533 argString << ", " << QualifierString(qualifier) << " "
3534 << TextureString(samplerType.getBasicType()) << " texture_"
3535 << sampler->getSymbol() << ArrayString(type) << ", "
3536 << QualifierString(qualifier) << " "
3537 << SamplerString(samplerType.getBasicType()) << " sampler_"
3538 << sampler->getSymbol() << ArrayString(type);
3539 }
3540 else
3541 {
3542 const TType &samplerType = sampler->getType();
3543 ASSERT((!type.isArray() && !samplerType.isArray()) ||
3544 type.getArraySize() == samplerType.getArraySize());
3545 ASSERT(IsSampler(samplerType.getBasicType()));
3546 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
3547 << " " << sampler->getSymbol() << ArrayString(type);
3548 }
3549 }
3550 }
3551
3552 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003553}
3554
3555TString OutputHLSL::initializer(const TType &type)
3556{
3557 TString string;
3558
Jamie Madill94bf7f22013-07-08 13:31:15 -04003559 size_t size = type.getObjectSize();
3560 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003561 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003562 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003563
Jamie Madill94bf7f22013-07-08 13:31:15 -04003564 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003565 {
3566 string += ", ";
3567 }
3568 }
3569
daniel@transgaming.comead23042010-04-29 03:35:36 +00003570 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003571}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003572
Jamie Madill8c46ab12015-12-07 16:39:19 -05003573void OutputHLSL::outputConstructor(TInfoSinkBase &out,
3574 Visit visit,
3575 const TType &type,
3576 const char *name,
3577 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003578{
Olli Etuahof40319e2015-03-10 14:33:00 +02003579 if (type.isArray())
3580 {
3581 UNIMPLEMENTED();
3582 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003583
3584 if (visit == PreVisit)
3585 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003586 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003587
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003588 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003589 }
3590 else if (visit == InVisit)
3591 {
3592 out << ", ";
3593 }
3594 else if (visit == PostVisit)
3595 {
3596 out << ")";
3597 }
3598}
3599
Jamie Madill8c46ab12015-12-07 16:39:19 -05003600const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
3601 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003602 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003603{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003604 const TConstantUnion *constUnionIterated = constUnion;
3605
Jamie Madill98493dd2013-07-08 14:39:03 -04003606 const TStructure* structure = type.getStruct();
3607 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003608 {
Jamie Madill033dae62014-06-18 12:56:28 -04003609 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003610
Jamie Madill98493dd2013-07-08 14:39:03 -04003611 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003612
Jamie Madill98493dd2013-07-08 14:39:03 -04003613 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003614 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003615 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05003616 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003617
Jamie Madill98493dd2013-07-08 14:39:03 -04003618 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003619 {
3620 out << ", ";
3621 }
3622 }
3623
3624 out << ")";
3625 }
3626 else
3627 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003628 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003629 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04003630
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003631 if (writeType)
3632 {
Jamie Madill033dae62014-06-18 12:56:28 -04003633 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003634 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003635 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003636 if (writeType)
3637 {
3638 out << ")";
3639 }
3640 }
3641
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003642 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003643}
3644
Jamie Madill8c46ab12015-12-07 16:39:19 -05003645void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003646{
3647 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05003648 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003649}
3650
Jamie Madill37997142015-01-28 10:06:34 -05003651bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3652{
3653 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3654 expression->traverse(&searchSymbol);
3655
3656 if (searchSymbol.foundMatch())
3657 {
3658 // Type already printed
3659 out << "t" + str(mUniqueIndex) + " = ";
3660 expression->traverse(this);
3661 out << ", ";
3662 symbolNode->traverse(this);
3663 out << " = t" + str(mUniqueIndex);
3664
3665 mUniqueIndex++;
3666 return true;
3667 }
3668
3669 return false;
3670}
3671
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003672bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3673{
3674 // We support writing constant unions and constructors that only take constant unions as
3675 // parameters as HLSL literals.
3676 if (expression->getAsConstantUnion())
3677 {
3678 return true;
3679 }
3680 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3681 !expression->getAsAggregate()->isConstructor())
3682 {
3683 return false;
3684 }
3685 TIntermAggregate *constructor = expression->getAsAggregate();
3686 for (TIntermNode *&node : *constructor->getSequence())
3687 {
3688 if (!node->getAsConstantUnion())
3689 return false;
3690 }
3691 return true;
3692}
3693
3694bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3695 TIntermSymbol *symbolNode,
3696 TIntermTyped *expression)
3697{
3698 if (canWriteAsHLSLLiteral(expression))
3699 {
3700 symbolNode->traverse(this);
3701 if (expression->getType().isArray())
3702 {
3703 out << "[" << expression->getType().getArraySize() << "]";
3704 }
3705 out << " = {";
3706 if (expression->getAsConstantUnion())
3707 {
3708 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3709 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3710 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3711 }
3712 else
3713 {
3714 TIntermAggregate *constructor = expression->getAsAggregate();
3715 ASSERT(constructor != nullptr);
3716 for (TIntermNode *&node : *constructor->getSequence())
3717 {
3718 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3719 ASSERT(nodeConst);
3720 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3721 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3722 if (node != constructor->getSequence()->back())
3723 {
3724 out << ", ";
3725 }
3726 }
3727 }
3728 out << "}";
3729 return true;
3730 }
3731 return false;
3732}
3733
Jamie Madill37997142015-01-28 10:06:34 -05003734void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3735{
3736 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3737 << "\n"
3738 << "void initializeDeferredGlobals()\n"
3739 << "{\n";
3740
3741 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3742 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003743 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3744 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3745 if (binary != nullptr)
3746 {
3747 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3748 TIntermTyped *expression = binary->getRight();
3749 ASSERT(symbol);
3750 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003751
Olli Etuahod81ed842015-05-12 12:46:35 +03003752 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003753
Olli Etuahod81ed842015-05-12 12:46:35 +03003754 if (!writeSameSymbolInitializer(out, symbol, expression))
3755 {
3756 ASSERT(mInfoSinkStack.top() == &out);
3757 expression->traverse(this);
3758 }
3759 out << ";\n";
3760 }
3761 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003762 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003763 writeSelection(out, selection);
Jamie Madill37997142015-01-28 10:06:34 -05003764 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003765 else
3766 {
3767 UNREACHABLE();
3768 }
Jamie Madill37997142015-01-28 10:06:34 -05003769 }
3770
3771 out << "}\n"
3772 << "\n";
3773}
3774
Jamie Madill55e79e02015-02-09 15:35:00 -05003775TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3776{
3777 const TFieldList &fields = structure.fields();
3778
3779 for (const auto &eqFunction : mStructEqualityFunctions)
3780 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003781 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003782 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003783 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003784 }
3785 }
3786
3787 const TString &structNameString = StructNameString(structure);
3788
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003789 StructEqualityFunction *function = new StructEqualityFunction();
3790 function->structure = &structure;
3791 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003792
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003793 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003794
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003795 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3796 << "{\n"
3797 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003798
3799 for (size_t i = 0; i < fields.size(); i++)
3800 {
3801 const TField *field = fields[i];
3802 const TType *fieldType = field->type();
3803
3804 const TString &fieldNameA = "a." + Decorate(field->name());
3805 const TString &fieldNameB = "b." + Decorate(field->name());
3806
3807 if (i > 0)
3808 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003809 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003810 }
3811
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003812 fnOut << "(";
3813 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3814 fnOut << fieldNameA;
3815 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3816 fnOut << fieldNameB;
3817 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3818 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003819 }
3820
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003821 fnOut << ";\n" << "}\n";
3822
3823 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003824
3825 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003826 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003827
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003828 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003829}
3830
Olli Etuaho7fb49552015-03-18 17:27:44 +02003831TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3832{
3833 for (const auto &eqFunction : mArrayEqualityFunctions)
3834 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003835 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003836 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003837 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003838 }
3839 }
3840
3841 const TString &typeName = TypeString(type);
3842
Olli Etuaho12690762015-03-31 12:55:28 +03003843 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003844 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003845
3846 TInfoSinkBase fnNameOut;
3847 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003848 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003849
3850 TType nonArrayType = type;
3851 nonArrayType.clearArrayness();
3852
3853 TInfoSinkBase fnOut;
3854
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003855 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003856 << typeName << " a[" << type.getArraySize() << "], "
3857 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003858 << "{\n"
3859 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3860 " {\n"
3861 " if (";
3862
3863 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3864 fnOut << "a[i]";
3865 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3866 fnOut << "b[i]";
3867 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3868
3869 fnOut << ") { return false; }\n"
3870 " }\n"
3871 " return true;\n"
3872 "}\n";
3873
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003874 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003875
3876 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003877 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003878
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003879 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003880}
3881
Olli Etuaho12690762015-03-31 12:55:28 +03003882TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3883{
3884 for (const auto &assignFunction : mArrayAssignmentFunctions)
3885 {
3886 if (assignFunction.type == type)
3887 {
3888 return assignFunction.functionName;
3889 }
3890 }
3891
3892 const TString &typeName = TypeString(type);
3893
3894 ArrayHelperFunction function;
3895 function.type = type;
3896
3897 TInfoSinkBase fnNameOut;
3898 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3899 function.functionName = fnNameOut.c_str();
3900
3901 TInfoSinkBase fnOut;
3902
3903 fnOut << "void " << function.functionName << "(out "
3904 << typeName << " a[" << type.getArraySize() << "], "
3905 << typeName << " b[" << type.getArraySize() << "])\n"
3906 << "{\n"
3907 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3908 " {\n"
3909 " a[i] = b[i];\n"
3910 " }\n"
3911 "}\n";
3912
3913 function.functionDefinition = fnOut.c_str();
3914
3915 mArrayAssignmentFunctions.push_back(function);
3916
3917 return function.functionName;
3918}
3919
Olli Etuaho9638c352015-04-01 14:34:52 +03003920TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3921{
3922 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3923 {
3924 if (constructIntoFunction.type == type)
3925 {
3926 return constructIntoFunction.functionName;
3927 }
3928 }
3929
3930 const TString &typeName = TypeString(type);
3931
3932 ArrayHelperFunction function;
3933 function.type = type;
3934
3935 TInfoSinkBase fnNameOut;
3936 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3937 function.functionName = fnNameOut.c_str();
3938
3939 TInfoSinkBase fnOut;
3940
3941 fnOut << "void " << function.functionName << "(out "
3942 << typeName << " a[" << type.getArraySize() << "]";
3943 for (int i = 0; i < type.getArraySize(); ++i)
3944 {
3945 fnOut << ", " << typeName << " b" << i;
3946 }
3947 fnOut << ")\n"
3948 "{\n";
3949
3950 for (int i = 0; i < type.getArraySize(); ++i)
3951 {
3952 fnOut << " a[" << i << "] = b" << i << ";\n";
3953 }
3954 fnOut << "}\n";
3955
3956 function.functionDefinition = fnOut.c_str();
3957
3958 mArrayConstructIntoFunctions.push_back(function);
3959
3960 return function.functionName;
3961}
3962
Jamie Madill2e295e22015-04-29 10:41:33 -04003963void OutputHLSL::ensureStructDefined(const TType &type)
3964{
3965 TStructure *structure = type.getStruct();
3966
3967 if (structure)
3968 {
3969 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3970 }
3971}
3972
3973
Olli Etuaho9638c352015-04-01 14:34:52 +03003974
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003975}