blob: cd95b02fd0483f3ca5fce8fce37b955aafaf3757 [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);
Jamie Madill37997142015-01-28 10:06:34 -0500262 mInfoSinkStack.pop();
263
Jamie Madill32aab012015-01-27 14:12:26 -0500264 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500265 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500266 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000267
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200268 objSink << mHeader.c_str();
269 objSink << mBody.c_str();
270 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200271
272 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000273}
274
Jamie Madill570e04d2013-06-21 09:15:33 -0400275void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
276{
277 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
278 {
279 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
280
Jamie Madill32aab012015-01-27 14:12:26 -0500281 TInfoSinkBase structInfoSink;
282 mInfoSinkStack.push(&structInfoSink);
283
Jamie Madill570e04d2013-06-21 09:15:33 -0400284 // This will mark the necessary block elements as referenced
285 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500286
287 TString structName(structInfoSink.c_str());
288 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400289
290 mFlaggedStructOriginalNames[flaggedNode] = structName;
291
292 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
293 {
294 structName.erase(pos, 1);
295 }
296
297 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
298 }
299}
300
Jamie Madill4e1fd412014-07-10 17:50:10 -0400301const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
302{
303 return mUniformHLSL->getInterfaceBlockRegisterMap();
304}
305
Jamie Madill9fe25e92014-07-18 10:33:08 -0400306const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
307{
308 return mUniformHLSL->getUniformRegisterMap();
309}
310
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000311int OutputHLSL::vectorSize(const TType &type) const
312{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000313 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000314 int arraySize = type.isArray() ? type.getArraySize() : 1;
315
316 return elementSize * arraySize;
317}
318
Jamie Madill98493dd2013-07-08 14:39:03 -0400319TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400320{
321 TString init;
322
323 TString preIndentString;
324 TString fullIndentString;
325
326 for (int spaces = 0; spaces < (indent * 4); spaces++)
327 {
328 preIndentString += ' ';
329 }
330
331 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
332 {
333 fullIndentString += ' ';
334 }
335
336 init += preIndentString + "{\n";
337
Jamie Madill98493dd2013-07-08 14:39:03 -0400338 const TFieldList &fields = structure.fields();
339 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400340 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400341 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400342 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400343 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400344
Jamie Madill98493dd2013-07-08 14:39:03 -0400345 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400346 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400347 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400348 }
349 else
350 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400351 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400352 }
353 }
354
355 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
356
357 return init;
358}
359
Jamie Madill8c46ab12015-12-07 16:39:19 -0500360void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000362 TString varyings;
363 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400364 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000365
Jamie Madill829f59e2013-11-13 19:40:54 -0500366 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400367 {
368 TIntermTyped *structNode = flaggedStructIt->first;
369 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400370 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400371 const TString &originalName = mFlaggedStructOriginalNames[structNode];
372
Jamie Madill033dae62014-06-18 12:56:28 -0400373 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400374 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400375 flaggedStructs += "\n";
376 }
377
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000378 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
379 {
380 const TType &type = varying->second->getType();
381 const TString &name = varying->second->getSymbol();
382
383 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400384 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
385 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000386 }
387
388 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
389 {
390 const TType &type = attribute->second->getType();
391 const TString &name = attribute->second->getSymbol();
392
Jamie Madill033dae62014-06-18 12:56:28 -0400393 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000394 }
395
Jamie Madill8daaba12014-06-13 10:04:33 -0400396 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400397
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200398 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400399 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
400
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200401 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500402 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200403 out << "\n// Equality functions\n\n";
404 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500405 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200406 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200407 }
408 }
Olli Etuaho12690762015-03-31 12:55:28 +0300409 if (!mArrayAssignmentFunctions.empty())
410 {
411 out << "\n// Assignment functions\n\n";
412 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
413 {
414 out << assignmentFunction.functionDefinition << "\n";
415 }
416 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300417 if (!mArrayConstructIntoFunctions.empty())
418 {
419 out << "\n// Array constructor functions\n\n";
420 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
421 {
422 out << constructIntoFunction.functionDefinition << "\n";
423 }
424 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200425
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500426 if (mUsesDiscardRewriting)
427 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400428 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500429 }
430
Nicolas Capens655fe362014-04-11 13:12:34 -0400431 if (mUsesNestedBreak)
432 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400433 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400434 }
435
Arun Patole44efa0b2015-03-04 17:11:05 +0530436 if (mRequiresIEEEStrictCompiling)
437 {
438 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
439 }
440
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400441 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
442 "#define LOOP [loop]\n"
443 "#define FLATTEN [flatten]\n"
444 "#else\n"
445 "#define LOOP\n"
446 "#define FLATTEN\n"
447 "#endif\n";
448
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200449 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200451 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
452 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000453
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000454 out << "// Varyings\n";
455 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400456 out << "\n";
457
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200458 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000459 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500460 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000461 {
Jamie Madill46131a32013-06-20 11:55:50 -0400462 const TString &variableName = outputVariableIt->first;
463 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400464
Jamie Madill033dae62014-06-18 12:56:28 -0400465 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400466 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000467 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000468 }
Jamie Madill46131a32013-06-20 11:55:50 -0400469 else
470 {
471 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
472
473 out << "static float4 gl_Color[" << numColorValues << "] =\n"
474 "{\n";
475 for (unsigned int i = 0; i < numColorValues; i++)
476 {
477 out << " float4(0, 0, 0, 0)";
478 if (i + 1 != numColorValues)
479 {
480 out << ",";
481 }
482 out << "\n";
483 }
484
485 out << "};\n";
486 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000487
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400488 if (mUsesFragDepth)
489 {
490 out << "static float gl_Depth = 0.0;\n";
491 }
492
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000493 if (mUsesFragCoord)
494 {
495 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
496 }
497
498 if (mUsesPointCoord)
499 {
500 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
501 }
502
503 if (mUsesFrontFacing)
504 {
505 out << "static bool gl_FrontFacing = false;\n";
506 }
507
508 out << "\n";
509
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000510 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000511 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000512 out << "struct gl_DepthRangeParameters\n"
513 "{\n"
514 " float near;\n"
515 " float far;\n"
516 " float diff;\n"
517 "};\n"
518 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000519 }
520
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200521 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000522 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000523 out << "cbuffer DriverConstants : register(b1)\n"
524 "{\n";
525
526 if (mUsesDepthRange)
527 {
528 out << " float3 dx_DepthRange : packoffset(c0);\n";
529 }
530
531 if (mUsesFragCoord)
532 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000533 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000534 }
535
536 if (mUsesFragCoord || mUsesFrontFacing)
537 {
538 out << " float3 dx_DepthFront : packoffset(c2);\n";
539 }
540
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800541 if (mUsesFragCoord)
542 {
543 // dx_ViewScale is only used in the fragment shader to correct
544 // the value for glFragCoord if necessary
545 out << " float2 dx_ViewScale : packoffset(c3);\n";
546 }
547
Olli Etuaho618bebc2016-01-15 16:40:00 +0200548 if (mOutputType == SH_HLSL_4_1_OUTPUT)
549 {
550 mUniformHLSL->samplerMetadataUniforms(out, "c4");
551 }
552
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000553 out << "};\n";
554 }
555 else
556 {
557 if (mUsesDepthRange)
558 {
559 out << "uniform float3 dx_DepthRange : register(c0);";
560 }
561
562 if (mUsesFragCoord)
563 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000564 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000565 }
566
567 if (mUsesFragCoord || mUsesFrontFacing)
568 {
569 out << "uniform float3 dx_DepthFront : register(c2);\n";
570 }
571 }
572
573 out << "\n";
574
575 if (mUsesDepthRange)
576 {
577 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
578 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000579 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000580
Jamie Madillf91ce812014-06-13 10:04:34 -0400581 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000582 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400583 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000584 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400585 out << flaggedStructs;
586 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000587 }
588
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000589 if (usingMRTExtension && mNumRenderTargets > 1)
590 {
591 out << "#define GL_USES_MRT\n";
592 }
593
594 if (mUsesFragColor)
595 {
596 out << "#define GL_USES_FRAG_COLOR\n";
597 }
598
599 if (mUsesFragData)
600 {
601 out << "#define GL_USES_FRAG_DATA\n";
602 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000604 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000606 out << "// Attributes\n";
607 out << attributes;
608 out << "\n"
609 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400610
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000611 if (mUsesPointSize)
612 {
613 out << "static float gl_PointSize = float(1);\n";
614 }
615
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000616 if (mUsesInstanceID)
617 {
618 out << "static int gl_InstanceID;";
619 }
620
Corentin Wallezb076add2016-01-11 16:45:46 -0500621 if (mUsesVertexID)
622 {
623 out << "static int gl_VertexID;";
624 }
625
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000626 out << "\n"
627 "// Varyings\n";
628 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000629 out << "\n";
630
631 if (mUsesDepthRange)
632 {
633 out << "struct gl_DepthRangeParameters\n"
634 "{\n"
635 " float near;\n"
636 " float far;\n"
637 " float diff;\n"
638 "};\n"
639 "\n";
640 }
641
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200642 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000643 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800644 out << "cbuffer DriverConstants : register(b1)\n"
645 "{\n";
646
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000647 if (mUsesDepthRange)
648 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800649 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000650 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800651
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800652 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
653 // shaders. However, we declare it for all shaders (including Feature Level 10+).
654 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
655 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800656 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800657 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800658 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800659
Olli Etuaho618bebc2016-01-15 16:40:00 +0200660 if (mOutputType == SH_HLSL_4_1_OUTPUT)
661 {
662 mUniformHLSL->samplerMetadataUniforms(out, "c4");
663 }
664
Austin Kinross4fd18b12014-12-22 12:32:05 -0800665 out << "};\n"
666 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000667 }
668 else
669 {
670 if (mUsesDepthRange)
671 {
672 out << "uniform float3 dx_DepthRange : register(c0);\n";
673 }
674
Cooper Partine6664f02015-01-09 16:22:24 -0800675 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
676 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000677 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000678 }
679
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000680 if (mUsesDepthRange)
681 {
682 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
683 "\n";
684 }
685
Jamie Madillf91ce812014-06-13 10:04:34 -0400686 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000687 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400688 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000689 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400690 out << flaggedStructs;
691 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000692 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400693 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000694
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400695 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
696 {
697 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400698 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000699 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400700 switch(textureFunction->sampler)
701 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400702 case EbtSampler2D: out << "int2 "; break;
703 case EbtSampler3D: out << "int3 "; break;
704 case EbtSamplerCube: out << "int2 "; break;
705 case EbtSampler2DArray: out << "int3 "; break;
706 case EbtISampler2D: out << "int2 "; break;
707 case EbtISampler3D: out << "int3 "; break;
708 case EbtISamplerCube: out << "int2 "; break;
709 case EbtISampler2DArray: out << "int3 "; break;
710 case EbtUSampler2D: out << "int2 "; break;
711 case EbtUSampler3D: out << "int3 "; break;
712 case EbtUSamplerCube: out << "int2 "; break;
713 case EbtUSampler2DArray: out << "int3 "; break;
714 case EbtSampler2DShadow: out << "int2 "; break;
715 case EbtSamplerCubeShadow: out << "int2 "; break;
716 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400717 default: UNREACHABLE();
718 }
719 }
720 else // Sampling function
721 {
722 switch(textureFunction->sampler)
723 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400724 case EbtSampler2D: out << "float4 "; break;
725 case EbtSampler3D: out << "float4 "; break;
726 case EbtSamplerCube: out << "float4 "; break;
727 case EbtSampler2DArray: out << "float4 "; break;
728 case EbtISampler2D: out << "int4 "; break;
729 case EbtISampler3D: out << "int4 "; break;
730 case EbtISamplerCube: out << "int4 "; break;
731 case EbtISampler2DArray: out << "int4 "; break;
732 case EbtUSampler2D: out << "uint4 "; break;
733 case EbtUSampler3D: out << "uint4 "; break;
734 case EbtUSamplerCube: out << "uint4 "; break;
735 case EbtUSampler2DArray: out << "uint4 "; break;
736 case EbtSampler2DShadow: out << "float "; break;
737 case EbtSamplerCubeShadow: out << "float "; break;
738 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400739 default: UNREACHABLE();
740 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000741 }
742
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400743 // Function name
744 out << textureFunction->name();
745
746 // Argument list
747 int hlslCoords = 4;
748
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200749 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000750 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400751 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000752 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400753 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
754 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
755 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000756 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400757
Nicolas Capens75fb4752013-07-10 15:14:47 -0400758 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000759 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400760 case TextureFunction::IMPLICIT: break;
761 case TextureFunction::BIAS: hlslCoords = 4; break;
762 case TextureFunction::LOD: hlslCoords = 4; break;
763 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400764 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400765 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000766 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400767 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200768 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400769 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200770 hlslCoords = HLSLTextureCoordsCount(textureFunction->sampler);
771 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400772 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200773 out << TextureString(textureFunction->sampler) << " x, "
774 << SamplerString(textureFunction->sampler) << " s";
775 }
776 else
777 {
778 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
779 out << "const uint samplerIndex";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400780 }
781 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400782
Nicolas Capensfc014542014-02-18 14:47:13 -0500783 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400784 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500785 switch(textureFunction->coords)
786 {
787 case 2: out << ", int2 t"; break;
788 case 3: out << ", int3 t"; break;
789 default: UNREACHABLE();
790 }
791 }
792 else // Floating-point coordinates (except textureSize)
793 {
794 switch(textureFunction->coords)
795 {
796 case 1: out << ", int lod"; break; // textureSize()
797 case 2: out << ", float2 t"; break;
798 case 3: out << ", float3 t"; break;
799 case 4: out << ", float4 t"; break;
800 default: UNREACHABLE();
801 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000802 }
803
Nicolas Capensd11d5492014-02-19 17:06:10 -0500804 if (textureFunction->method == TextureFunction::GRAD)
805 {
806 switch(textureFunction->sampler)
807 {
808 case EbtSampler2D:
809 case EbtISampler2D:
810 case EbtUSampler2D:
811 case EbtSampler2DArray:
812 case EbtISampler2DArray:
813 case EbtUSampler2DArray:
814 case EbtSampler2DShadow:
815 case EbtSampler2DArrayShadow:
816 out << ", float2 ddx, float2 ddy";
817 break;
818 case EbtSampler3D:
819 case EbtISampler3D:
820 case EbtUSampler3D:
821 case EbtSamplerCube:
822 case EbtISamplerCube:
823 case EbtUSamplerCube:
824 case EbtSamplerCubeShadow:
825 out << ", float3 ddx, float3 ddy";
826 break;
827 default: UNREACHABLE();
828 }
829 }
830
Nicolas Capens75fb4752013-07-10 15:14:47 -0400831 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000832 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400833 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400834 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400835 case TextureFunction::LOD: out << ", float lod"; break;
836 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400837 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400838 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500839 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500840 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400841 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000842 }
843
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500844 if (textureFunction->offset)
845 {
846 switch(textureFunction->sampler)
847 {
848 case EbtSampler2D: out << ", int2 offset"; break;
849 case EbtSampler3D: out << ", int3 offset"; break;
850 case EbtSampler2DArray: out << ", int2 offset"; break;
851 case EbtISampler2D: out << ", int2 offset"; break;
852 case EbtISampler3D: out << ", int3 offset"; break;
853 case EbtISampler2DArray: out << ", int2 offset"; break;
854 case EbtUSampler2D: out << ", int2 offset"; break;
855 case EbtUSampler3D: out << ", int3 offset"; break;
856 case EbtUSampler2DArray: out << ", int2 offset"; break;
857 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500858 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500859 default: UNREACHABLE();
860 }
861 }
862
Nicolas Capens84cfa122014-04-14 13:48:45 -0400863 if (textureFunction->method == TextureFunction::BIAS ||
864 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500865 {
866 out << ", float bias";
867 }
868
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400869 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400870 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400871
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200872 // In some cases we use a variable to store the texture/sampler objects, but to work around
873 // a D3D11 compiler bug related to discard inside a loop that is conditional on texture
874 // sampling we need to call the function directly on a reference to the array. The bug was
875 // found using dEQP-GLES3.functional.shaders.discard*loop_texture* tests.
876 TString textureReference("x");
877 TString samplerReference("s");
878 if (mOutputType == SH_HLSL_4_1_OUTPUT)
879 {
880 TString suffix = TextureGroupSuffix(textureFunction->sampler);
881 if (TextureGroup(textureFunction->sampler) == HLSL_TEXTURE_2D)
882 {
883 textureReference = TString("textures") + suffix + "[samplerIndex]";
884 samplerReference = TString("samplers") + suffix + "[samplerIndex]";
885 }
886 else
887 {
888 out << " const uint textureIndex = samplerIndex - textureIndexOffset" << suffix
889 << ";\n";
890 textureReference = TString("textures") + suffix + "[textureIndex]";
891 out << " const uint samplerArrayIndex = samplerIndex - samplerIndexOffset"
892 << suffix << ";\n";
893 samplerReference = TString("samplers") + suffix + "[samplerArrayIndex]";
894 }
895 }
896
Nicolas Capens75fb4752013-07-10 15:14:47 -0400897 if (textureFunction->method == TextureFunction::SIZE)
898 {
Olli Etuahob079c7a2016-04-01 12:32:52 +0300899 out << "int baseLevel = samplerMetadata[samplerIndex].baseLevel;\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400900 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
901 {
Olli Etuahobce743a2016-01-15 17:18:28 +0200902 if (IsSamplerArray(textureFunction->sampler) ||
903 (IsIntegerSampler(textureFunction->sampler) &&
904 IsSamplerCube(textureFunction->sampler)))
Nicolas Capens75fb4752013-07-10 15:14:47 -0400905 {
906 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
Olli Etuahobce743a2016-01-15 17:18:28 +0200907 << " " << textureReference << ".GetDimensions(baseLevel + lod, width, "
908 "height, layers, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400909 }
910 else
911 {
912 out << " uint width; uint height; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200913 << " " << textureReference
Olli Etuahobce743a2016-01-15 17:18:28 +0200914 << ".GetDimensions(baseLevel + lod, width, height, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400915 }
916 }
917 else if (IsSampler3D(textureFunction->sampler))
918 {
919 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200920 << " " << textureReference
Olli Etuahobce743a2016-01-15 17:18:28 +0200921 << ".GetDimensions(baseLevel + lod, width, height, depth, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400922 }
923 else UNREACHABLE();
924
925 switch(textureFunction->sampler)
926 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400927 case EbtSampler2D: out << " return int2(width, height);"; break;
928 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
929 case EbtSamplerCube: out << " return int2(width, height);"; break;
930 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
931 case EbtISampler2D: out << " return int2(width, height);"; break;
932 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
933 case EbtISamplerCube: out << " return int2(width, height);"; break;
934 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
935 case EbtUSampler2D: out << " return int2(width, height);"; break;
936 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
937 case EbtUSamplerCube: out << " return int2(width, height);"; break;
938 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
939 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
940 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
941 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400942 default: UNREACHABLE();
943 }
944 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400945 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400946 {
Olli Etuahod4102f02016-01-22 14:54:04 +0200947 TString texCoordX("t.x");
948 TString texCoordY("t.y");
949 TString texCoordZ("t.z");
950 TString proj = "";
951
952 if (textureFunction->proj)
953 {
954 switch (textureFunction->coords)
955 {
956 case 3:
957 proj = " / t.z";
958 break;
959 case 4:
960 proj = " / t.w";
961 break;
962 default:
963 UNREACHABLE();
964 }
965 if (proj != "")
966 {
967 texCoordX = "(" + texCoordX + proj + ")";
968 texCoordY = "(" + texCoordY + proj + ")";
969 texCoordZ = "(" + texCoordZ + proj + ")";
970 }
971 }
972
Nicolas Capens0027fa92014-02-20 14:26:42 -0500973 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
974 {
975 out << " float width; float height; float layers; float levels;\n";
976
977 out << " uint mip = 0;\n";
978
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200979 out << " " << textureReference
980 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens0027fa92014-02-20 14:26:42 -0500981
982 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
983 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
984 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
985 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
986
987 // FACE_POSITIVE_X = 000b
988 // FACE_NEGATIVE_X = 001b
989 // FACE_POSITIVE_Y = 010b
990 // FACE_NEGATIVE_Y = 011b
991 // FACE_POSITIVE_Z = 100b
992 // FACE_NEGATIVE_Z = 101b
993 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
994
995 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
996 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
997 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
998
999 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
1000 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -05001001
1002 // Mip level computation.
Olli Etuahoced87052016-04-04 16:34:27 +03001003 if (textureFunction->method == TextureFunction::IMPLICIT ||
1004 textureFunction->method == TextureFunction::LOD ||
1005 textureFunction->method == TextureFunction::GRAD)
Jamie Madill8d9f35f2015-11-24 16:10:20 -05001006 {
Olli Etuahoced87052016-04-04 16:34:27 +03001007 if (textureFunction->method == TextureFunction::IMPLICIT)
1008 {
1009 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
1010 " float2 dx = ddx(tSized);\n"
1011 " float2 dy = ddy(tSized);\n"
1012 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n";
1013 }
1014 else if (textureFunction->method == TextureFunction::GRAD)
1015 {
1016 // ESSL 3.00.6 spec section 8.8: "For the cube version, the partial
1017 // derivatives of P are assumed to be in the coordinate system used before
1018 // texture coordinates are projected onto the appropriate cube face."
1019 // ddx[0] and ddy[0] are the derivatives of t.x passed into the function
1020 // ddx[1] and ddy[1] are the derivatives of t.y passed into the function
1021 // ddx[2] and ddy[2] are the derivatives of t.z passed into the function
1022 // Determine the derivatives of u, v and m
1023 out << " float dudx = xMajor ? ddx[2] : (yMajor && t.y < 0.0f ? -ddx[0] "
1024 ": ddx[0]);\n"
1025 " float dudy = xMajor ? ddy[2] : (yMajor && t.y < 0.0f ? -ddy[0] "
1026 ": ddy[0]);\n"
1027 " float dvdx = yMajor ? ddx[2] : (negative ? ddx[1] : -ddx[1]);\n"
1028 " float dvdy = yMajor ? ddy[2] : (negative ? ddy[1] : -ddy[1]);\n"
1029 " float dmdx = xMajor ? ddx[0] : (yMajor ? ddx[1] : ddx[2]);\n"
1030 " float dmdy = xMajor ? ddy[0] : (yMajor ? ddy[1] : ddy[2]);\n";
1031 // Now determine the derivatives of the face coordinates, using the
1032 // derivatives calculated above.
1033 // d / dx (u(x) * 0.5 / m(x) + 0.5)
1034 // = 0.5 * (m(x) * u'(x) - u(x) * m'(x)) / m(x)^2
1035 out << " float dfacexdx = 0.5f * (m * dudx - u * dmdx) / (m * m);\n"
1036 " float dfaceydx = 0.5f * (m * dvdx - v * dmdx) / (m * m);\n"
1037 " float dfacexdy = 0.5f * (m * dudy - u * dmdy) / (m * m);\n"
1038 " float dfaceydy = 0.5f * (m * dvdy - v * dmdy) / (m * m);\n"
1039 " float2 sizeVec = float2(width, height);\n"
1040 " float2 faceddx = float2(dfacexdx, dfaceydx) * sizeVec;\n"
1041 " float2 faceddy = float2(dfacexdy, dfaceydy) * sizeVec;\n";
1042 // Optimization: instead of: log2(max(length(faceddx), length(faceddy)))
1043 // we compute: log2(max(length(faceddx)^2, length(faceddy)^2)) / 2
1044 out << " float lengthfaceddx2 = dot(faceddx, faceddx);\n"
1045 " float lengthfaceddy2 = dot(faceddy, faceddy);\n"
1046 " float lod = log2(max(lengthfaceddx2, lengthfaceddy2)) * "
1047 "0.5f;\n";
1048 }
1049 out << " mip = uint(min(max(round(lod), 0), levels - 1));\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001050 << " " << textureReference
1051 << ".GetDimensions(mip, width, height, layers, levels);\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -05001052 }
Olli Etuahod4102f02016-01-22 14:54:04 +02001053
1054 // Convert from normalized floating-point to integer
1055 texCoordX = "int(floor(width * frac(" + texCoordX + ")))";
1056 texCoordY = "int(floor(height * frac(" + texCoordY + ")))";
1057 texCoordZ = "face";
Nicolas Capens0027fa92014-02-20 14:26:42 -05001058 }
1059 else if (IsIntegerSampler(textureFunction->sampler) &&
1060 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001061 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001062 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001063 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001064 if (IsSamplerArray(textureFunction->sampler))
1065 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001066 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001067
Nicolas Capens9edebd62013-08-06 10:59:10 -04001068 if (textureFunction->method == TextureFunction::LOD0)
1069 {
1070 out << " uint mip = 0;\n";
1071 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001072 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1073 {
1074 out << " uint mip = bias;\n";
1075 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001076 else
1077 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001078
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001079 out << " " << textureReference
1080 << ".GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001081 if (textureFunction->method == TextureFunction::IMPLICIT ||
1082 textureFunction->method == TextureFunction::BIAS)
1083 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001084 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001085 " float dx = length(ddx(tSized));\n"
1086 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001087 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001088
1089 if (textureFunction->method == TextureFunction::BIAS)
1090 {
1091 out << " lod += bias;\n";
1092 }
1093 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001094 else if (textureFunction->method == TextureFunction::GRAD)
1095 {
Olli Etuahoced87052016-04-04 16:34:27 +03001096 out << " float2 sizeVec = float2(width, height);\n"
1097 " float2 sizeDdx = ddx * sizeVec;\n"
1098 " float2 sizeDdy = ddy * sizeVec;\n"
1099 " float lod = log2(max(dot(sizeDdx, sizeDdx), "
1100 "dot(sizeDdy, sizeDdy))) * 0.5f;\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001101 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001102
1103 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1104 }
1105
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001106 out << " " << textureReference
1107 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001108 }
1109 else
1110 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001111 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001112
Nicolas Capens9edebd62013-08-06 10:59:10 -04001113 if (textureFunction->method == TextureFunction::LOD0)
1114 {
1115 out << " uint mip = 0;\n";
1116 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001117 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1118 {
1119 out << " uint mip = bias;\n";
1120 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001121 else
1122 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001123 out << " " << textureReference
1124 << ".GetDimensions(0, width, height, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001125
Nicolas Capens9edebd62013-08-06 10:59:10 -04001126 if (textureFunction->method == TextureFunction::IMPLICIT ||
1127 textureFunction->method == TextureFunction::BIAS)
1128 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001129 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001130 " float dx = length(ddx(tSized));\n"
1131 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001132 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001133
1134 if (textureFunction->method == TextureFunction::BIAS)
1135 {
1136 out << " lod += bias;\n";
1137 }
1138 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001139 else if (textureFunction->method == TextureFunction::GRAD)
1140 {
Olli Etuahoced87052016-04-04 16:34:27 +03001141 out << " float2 sizeVec = float2(width, height);\n"
1142 " float2 sizeDdx = ddx * sizeVec;\n"
1143 " float2 sizeDdy = ddy * sizeVec;\n"
1144 " float lod = log2(max(dot(sizeDdx, sizeDdx), "
1145 "dot(sizeDdy, sizeDdy))) * 0.5f;\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001146 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001147
1148 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1149 }
1150
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001151 out << " " << textureReference
1152 << ".GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001153 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001154 }
1155 else if (IsSampler3D(textureFunction->sampler))
1156 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001157 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001158
Nicolas Capens9edebd62013-08-06 10:59:10 -04001159 if (textureFunction->method == TextureFunction::LOD0)
1160 {
1161 out << " uint mip = 0;\n";
1162 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001163 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1164 {
1165 out << " uint mip = bias;\n";
1166 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001167 else
1168 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001169 out << " " << textureReference
1170 << ".GetDimensions(0, width, height, depth, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001171
Nicolas Capens9edebd62013-08-06 10:59:10 -04001172 if (textureFunction->method == TextureFunction::IMPLICIT ||
1173 textureFunction->method == TextureFunction::BIAS)
1174 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001175 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1176 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001177 " float dx = length(ddx(tSized));\n"
1178 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001179 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001180
1181 if (textureFunction->method == TextureFunction::BIAS)
1182 {
1183 out << " lod += bias;\n";
1184 }
1185 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001186 else if (textureFunction->method == TextureFunction::GRAD)
1187 {
Olli Etuahoced87052016-04-04 16:34:27 +03001188 out << " float3 sizeVec = float3(width, height, depth);\n"
1189 " float3 sizeDdx = ddx * sizeVec;\n"
1190 " float3 sizeDdy = ddy * sizeVec;\n"
1191 " float lod = log2(max(dot(sizeDdx, sizeDdx), dot(sizeDdy, "
1192 "sizeDdy))) * 0.5f;\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001193 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001194
1195 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1196 }
1197
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001198 out << " " << textureReference
1199 << ".GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001200 }
1201 else UNREACHABLE();
Olli Etuahod4102f02016-01-22 14:54:04 +02001202
1203 // Convert from normalized floating-point to integer
Olli Etuahob079c7a2016-04-01 12:32:52 +03001204 out << "int wrapS = samplerMetadata[samplerIndex].wrapModes & 0x3;\n";
1205 if (textureFunction->offset)
1206 {
1207 OutputIntTexCoordWrap(out, "wrapS", "width", texCoordX, "offset.x", "tix");
1208 }
1209 else
1210 {
1211 OutputIntTexCoordWrap(out, "wrapS", "width", texCoordX, "0", "tix");
1212 }
1213 texCoordX = "tix";
1214 out << "int wrapT = (samplerMetadata[samplerIndex].wrapModes >> 2) & 0x3;\n";
1215 if (textureFunction->offset)
1216 {
1217 OutputIntTexCoordWrap(out, "wrapT", "height", texCoordY, "offset.y", "tiy");
1218 }
1219 else
1220 {
1221 OutputIntTexCoordWrap(out, "wrapT", "height", texCoordY, "0", "tiy");
1222 }
1223 texCoordY = "tiy";
Olli Etuahod4102f02016-01-22 14:54:04 +02001224
1225 if (IsSamplerArray(textureFunction->sampler))
1226 {
1227 texCoordZ = "int(max(0, min(layers - 1, floor(0.5 + t.z))))";
1228 }
1229 else if (!IsSamplerCube(textureFunction->sampler) &&
1230 !IsSampler2D(textureFunction->sampler))
1231 {
Olli Etuahob079c7a2016-04-01 12:32:52 +03001232 out << "int wrapR = (samplerMetadata[samplerIndex].wrapModes >> 4) & 0x3;\n";
1233 if (textureFunction->offset)
1234 {
1235 OutputIntTexCoordWrap(out, "wrapR", "depth", texCoordZ, "offset.z", "tiz");
1236 }
1237 else
1238 {
1239 OutputIntTexCoordWrap(out, "wrapR", "depth", texCoordZ, "0", "tiz");
1240 }
1241 texCoordZ = "tiz";
Olli Etuahod4102f02016-01-22 14:54:04 +02001242 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001243 }
1244
1245 out << " return ";
1246
1247 // HLSL intrinsic
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001248 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001249 {
1250 switch(textureFunction->sampler)
1251 {
1252 case EbtSampler2D: out << "tex2D"; break;
1253 case EbtSamplerCube: out << "texCUBE"; break;
1254 default: UNREACHABLE();
1255 }
1256
Nicolas Capens75fb4752013-07-10 15:14:47 -04001257 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001258 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001259 case TextureFunction::IMPLICIT:
1260 out << "(" << samplerReference << ", ";
1261 break;
1262 case TextureFunction::BIAS:
1263 out << "bias(" << samplerReference << ", ";
1264 break;
1265 case TextureFunction::LOD:
1266 out << "lod(" << samplerReference << ", ";
1267 break;
1268 case TextureFunction::LOD0:
1269 out << "lod(" << samplerReference << ", ";
1270 break;
1271 case TextureFunction::LOD0BIAS:
1272 out << "lod(" << samplerReference << ", ";
1273 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001274 default: UNREACHABLE();
1275 }
1276 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001277 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001278 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001279 if (textureFunction->method == TextureFunction::GRAD)
1280 {
1281 if (IsIntegerSampler(textureFunction->sampler))
1282 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001283 out << "" << textureReference << ".Load(";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001284 }
1285 else if (IsShadowSampler(textureFunction->sampler))
1286 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001287 out << "" << textureReference << ".SampleCmpLevelZero(" << samplerReference
1288 << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001289 }
1290 else
1291 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001292 out << "" << textureReference << ".SampleGrad(" << samplerReference << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001293 }
1294 }
1295 else if (IsIntegerSampler(textureFunction->sampler) ||
1296 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001297 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001298 out << "" << textureReference << ".Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001299 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001300 else if (IsShadowSampler(textureFunction->sampler))
1301 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001302 switch(textureFunction->method)
1303 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001304 case TextureFunction::IMPLICIT:
1305 out << "" << textureReference << ".SampleCmp(" << samplerReference
1306 << ", ";
1307 break;
1308 case TextureFunction::BIAS:
1309 out << "" << textureReference << ".SampleCmp(" << samplerReference
1310 << ", ";
1311 break;
1312 case TextureFunction::LOD:
1313 out << "" << textureReference << ".SampleCmp(" << samplerReference
1314 << ", ";
1315 break;
1316 case TextureFunction::LOD0:
1317 out << "" << textureReference << ".SampleCmpLevelZero("
1318 << samplerReference << ", ";
1319 break;
1320 case TextureFunction::LOD0BIAS:
1321 out << "" << textureReference << ".SampleCmpLevelZero("
1322 << samplerReference << ", ";
1323 break;
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001324 default: UNREACHABLE();
1325 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001326 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001327 else
1328 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001329 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001330 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001331 case TextureFunction::IMPLICIT:
1332 out << "" << textureReference << ".Sample(" << samplerReference << ", ";
1333 break;
1334 case TextureFunction::BIAS:
1335 out << "" << textureReference << ".SampleBias(" << samplerReference
1336 << ", ";
1337 break;
1338 case TextureFunction::LOD:
1339 out << "" << textureReference << ".SampleLevel(" << samplerReference
1340 << ", ";
1341 break;
1342 case TextureFunction::LOD0:
1343 out << "" << textureReference << ".SampleLevel(" << samplerReference
1344 << ", ";
1345 break;
1346 case TextureFunction::LOD0BIAS:
1347 out << "" << textureReference << ".SampleLevel(" << samplerReference
1348 << ", ";
1349 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001350 default: UNREACHABLE();
1351 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001352 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001353 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001354 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001355
Nicolas Capensfc014542014-02-18 14:47:13 -05001356 if (IsIntegerSampler(textureFunction->sampler) ||
1357 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001358 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001359 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001360 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001361 case 2: out << "int3("; break;
1362 case 3: out << "int4("; break;
1363 default: UNREACHABLE();
1364 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001365 }
1366 else
1367 {
1368 switch(hlslCoords)
1369 {
1370 case 2: out << "float2("; break;
1371 case 3: out << "float3("; break;
1372 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001373 default: UNREACHABLE();
1374 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001375 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001376
Olli Etuahod4102f02016-01-22 14:54:04 +02001377 out << texCoordX << ", " << texCoordY;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001378
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001379 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001380 {
1381 if (hlslCoords >= 3)
1382 {
1383 if (textureFunction->coords < 3)
1384 {
1385 out << ", 0";
1386 }
1387 else
1388 {
Olli Etuahod4102f02016-01-22 14:54:04 +02001389 out << ", t.z" << proj;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001390 }
1391 }
1392
1393 if (hlslCoords == 4)
1394 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001395 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001396 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001397 case TextureFunction::BIAS: out << ", bias"; break;
1398 case TextureFunction::LOD: out << ", lod"; break;
1399 case TextureFunction::LOD0: out << ", 0"; break;
1400 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001401 default: UNREACHABLE();
1402 }
1403 }
1404
1405 out << "));\n";
1406 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001407 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001408 {
1409 if (hlslCoords >= 3)
1410 {
Olli Etuahod4102f02016-01-22 14:54:04 +02001411 ASSERT(!IsIntegerSampler(textureFunction->sampler) ||
1412 !IsSamplerCube(textureFunction->sampler) || texCoordZ == "face");
1413 out << ", " << texCoordZ;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001414 }
1415
Nicolas Capensd11d5492014-02-19 17:06:10 -05001416 if (textureFunction->method == TextureFunction::GRAD)
1417 {
1418 if (IsIntegerSampler(textureFunction->sampler))
1419 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001420 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001421 }
1422 else if (IsShadowSampler(textureFunction->sampler))
1423 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001424 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001425 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001426 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001427 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1428 // The resulting third component of P' in the shadow forms is used as Dref
1429 out << "), t.z" << proj;
1430 }
1431 else
1432 {
1433 switch(textureFunction->coords)
1434 {
1435 case 3: out << "), t.z"; break;
1436 case 4: out << "), t.w"; break;
1437 default: UNREACHABLE();
1438 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001439 }
1440 }
1441 else
1442 {
1443 out << "), ddx, ddy";
1444 }
1445 }
1446 else if (IsIntegerSampler(textureFunction->sampler) ||
1447 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001448 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001449 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001450 }
1451 else if (IsShadowSampler(textureFunction->sampler))
1452 {
1453 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001454 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001455 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001456 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1457 // The resulting third component of P' in the shadow forms is used as Dref
1458 out << "), t.z" << proj;
1459 }
1460 else
1461 {
1462 switch(textureFunction->coords)
1463 {
1464 case 3: out << "), t.z"; break;
1465 case 4: out << "), t.w"; break;
1466 default: UNREACHABLE();
1467 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001468 }
1469 }
1470 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001471 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001472 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001473 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001474 case TextureFunction::IMPLICIT: out << ")"; break;
1475 case TextureFunction::BIAS: out << "), bias"; break;
1476 case TextureFunction::LOD: out << "), lod"; break;
1477 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001478 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001479 default: UNREACHABLE();
1480 }
1481 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001482
Olli Etuahob079c7a2016-04-01 12:32:52 +03001483 if (textureFunction->offset && (!IsIntegerSampler(textureFunction->sampler) ||
1484 textureFunction->method == TextureFunction::FETCH))
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001485 {
1486 out << ", offset";
1487 }
1488
1489 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001490 }
1491 else UNREACHABLE();
1492 }
1493
1494 out << "\n"
1495 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001496 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001497 }
1498
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001499 if (mUsesFragCoord)
1500 {
1501 out << "#define GL_USES_FRAG_COORD\n";
1502 }
1503
1504 if (mUsesPointCoord)
1505 {
1506 out << "#define GL_USES_POINT_COORD\n";
1507 }
1508
1509 if (mUsesFrontFacing)
1510 {
1511 out << "#define GL_USES_FRONT_FACING\n";
1512 }
1513
1514 if (mUsesPointSize)
1515 {
1516 out << "#define GL_USES_POINT_SIZE\n";
1517 }
1518
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001519 if (mUsesFragDepth)
1520 {
1521 out << "#define GL_USES_FRAG_DEPTH\n";
1522 }
1523
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001524 if (mUsesDepthRange)
1525 {
1526 out << "#define GL_USES_DEPTH_RANGE\n";
1527 }
1528
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001529 if (mUsesXor)
1530 {
1531 out << "bool xor(bool p, bool q)\n"
1532 "{\n"
1533 " return (p || q) && !(p && q);\n"
1534 "}\n"
1535 "\n";
1536 }
1537
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001538 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001539}
1540
1541void OutputHLSL::visitSymbol(TIntermSymbol *node)
1542{
Jamie Madill32aab012015-01-27 14:12:26 -05001543 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001544
Jamie Madill570e04d2013-06-21 09:15:33 -04001545 // Handle accessing std140 structs by value
1546 if (mFlaggedStructMappedNames.count(node) > 0)
1547 {
1548 out << mFlaggedStructMappedNames[node];
1549 return;
1550 }
1551
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001552 TString name = node->getSymbol();
1553
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001554 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001555 {
1556 mUsesDepthRange = true;
1557 out << name;
1558 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001559 else
1560 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001561 TQualifier qualifier = node->getQualifier();
1562
1563 if (qualifier == EvqUniform)
1564 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001565 const TType &nodeType = node->getType();
1566 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001567
1568 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001569 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001570 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001571 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001572 else
1573 {
1574 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001575 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001576
Jamie Madill2e295e22015-04-29 10:41:33 -04001577 ensureStructDefined(nodeType);
1578
Olli Etuaho96963162016-03-21 11:54:33 +02001579 const TName &nameWithMetadata = node->getName();
1580 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001581 }
Jamie Madill19571812013-08-12 15:26:34 -07001582 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001583 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001584 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001585 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001586 }
Jamie Madill033dae62014-06-18 12:56:28 -04001587 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001588 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001589 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001590 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001591 }
Jamie Madill19571812013-08-12 15:26:34 -07001592 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001593 {
1594 mReferencedOutputVariables[name] = node;
1595 out << "out_" << name;
1596 }
1597 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001598 {
1599 out << "gl_Color[0]";
1600 mUsesFragColor = true;
1601 }
1602 else if (qualifier == EvqFragData)
1603 {
1604 out << "gl_Color";
1605 mUsesFragData = true;
1606 }
1607 else if (qualifier == EvqFragCoord)
1608 {
1609 mUsesFragCoord = true;
1610 out << name;
1611 }
1612 else if (qualifier == EvqPointCoord)
1613 {
1614 mUsesPointCoord = true;
1615 out << name;
1616 }
1617 else if (qualifier == EvqFrontFacing)
1618 {
1619 mUsesFrontFacing = true;
1620 out << name;
1621 }
1622 else if (qualifier == EvqPointSize)
1623 {
1624 mUsesPointSize = true;
1625 out << name;
1626 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001627 else if (qualifier == EvqInstanceID)
1628 {
1629 mUsesInstanceID = true;
1630 out << name;
1631 }
Corentin Wallezb076add2016-01-11 16:45:46 -05001632 else if (qualifier == EvqVertexID)
1633 {
1634 mUsesVertexID = true;
1635 out << name;
1636 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001637 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001638 {
1639 mUsesFragDepth = true;
1640 out << "gl_Depth";
1641 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001642 else
1643 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001644 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001645 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646 }
1647}
1648
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001649void OutputHLSL::visitRaw(TIntermRaw *node)
1650{
Jamie Madill32aab012015-01-27 14:12:26 -05001651 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001652}
1653
Olli Etuaho7fb49552015-03-18 17:27:44 +02001654void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1655{
1656 if (type.isScalar() && !type.isArray())
1657 {
1658 if (op == EOpEqual)
1659 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001660 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001661 }
1662 else
1663 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001664 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001665 }
1666 }
1667 else
1668 {
1669 if (visit == PreVisit && op == EOpNotEqual)
1670 {
1671 out << "!";
1672 }
1673
1674 if (type.isArray())
1675 {
1676 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001677 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001678 }
1679 else if (type.getBasicType() == EbtStruct)
1680 {
1681 const TStructure &structure = *type.getStruct();
1682 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001683 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001684 }
1685 else
1686 {
1687 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001688 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001689 }
1690 }
1691}
1692
Olli Etuaho96963162016-03-21 11:54:33 +02001693bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
1694{
1695 // Inside InVisit the current node is already in the path.
1696 const unsigned int initialN = visit == InVisit ? 1u : 0u;
1697 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
1698 {
1699 TIntermNode *ancestor = getAncestorNode(n);
1700 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1701 if (ancestorBinary == nullptr)
1702 {
1703 return false;
1704 }
1705 switch (ancestorBinary->getOp())
1706 {
1707 case EOpIndexDirectStruct:
1708 {
1709 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1710 const TIntermConstantUnion *index =
1711 ancestorBinary->getRight()->getAsConstantUnion();
1712 const TField *field = structure->fields()[index->getIConst(0)];
1713 if (IsSampler(field->type()->getBasicType()))
1714 {
1715 return true;
1716 }
1717 break;
1718 }
1719 case EOpIndexDirect:
1720 break;
1721 default:
1722 // Returning a sampler from indirect indexing is not supported.
1723 return false;
1724 }
1725 }
1726 return false;
1727}
1728
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1730{
Jamie Madill32aab012015-01-27 14:12:26 -05001731 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732
Jamie Madill570e04d2013-06-21 09:15:33 -04001733 // Handle accessing std140 structs by value
1734 if (mFlaggedStructMappedNames.count(node) > 0)
1735 {
1736 out << mFlaggedStructMappedNames[node];
1737 return false;
1738 }
1739
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740 switch (node->getOp())
1741 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001742 case EOpAssign:
1743 if (node->getLeft()->isArray())
1744 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001745 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1746 if (rightAgg != nullptr && rightAgg->isConstructor())
1747 {
1748 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1749 out << functionName << "(";
1750 node->getLeft()->traverse(this);
1751 TIntermSequence *seq = rightAgg->getSequence();
1752 for (auto &arrayElement : *seq)
1753 {
1754 out << ", ";
1755 arrayElement->traverse(this);
1756 }
1757 out << ")";
1758 return false;
1759 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001760 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1761 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1762
1763 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001764 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001765 }
1766 else
1767 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001768 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001769 }
1770 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001771 case EOpInitialize:
1772 if (visit == PreVisit)
1773 {
1774 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1775 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1776 // new variable is created before the assignment is evaluated), so we need to convert
1777 // this to "float t = x, x = t;".
1778
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001779 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001780 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001781 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001782
Olli Etuaho3d932d82016-04-12 11:10:30 +03001783 // Global initializers must be constant at this point.
1784 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
1785 (expression->getQualifier() == EvqConst &&
1786 expression->getAsConstantUnion() != nullptr));
1787 if (writeSameSymbolInitializer(out, symbolNode, expression))
Jamie Madill37997142015-01-28 10:06:34 -05001788 {
1789 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001790 return false;
1791 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001792 else if (writeConstantInitialization(out, symbolNode, expression))
1793 {
1794 return false;
1795 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001796 }
1797 else if (visit == InVisit)
1798 {
1799 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001800 }
1801 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001802 case EOpAddAssign:
1803 outputTriplet(out, visit, "(", " += ", ")");
1804 break;
1805 case EOpSubAssign:
1806 outputTriplet(out, visit, "(", " -= ", ")");
1807 break;
1808 case EOpMulAssign:
1809 outputTriplet(out, visit, "(", " *= ", ")");
1810 break;
1811 case EOpVectorTimesScalarAssign:
1812 outputTriplet(out, visit, "(", " *= ", ")");
1813 break;
1814 case EOpMatrixTimesScalarAssign:
1815 outputTriplet(out, visit, "(", " *= ", ")");
1816 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001817 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001818 if (visit == PreVisit)
1819 {
1820 out << "(";
1821 }
1822 else if (visit == InVisit)
1823 {
1824 out << " = mul(";
1825 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001826 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001827 }
1828 else
1829 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001830 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001831 }
1832 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001833 case EOpMatrixTimesMatrixAssign:
1834 if (visit == PreVisit)
1835 {
1836 out << "(";
1837 }
1838 else if (visit == InVisit)
1839 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001840 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001841 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001842 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001843 }
1844 else
1845 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001846 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001847 }
1848 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001849 case EOpDivAssign:
1850 outputTriplet(out, visit, "(", " /= ", ")");
1851 break;
1852 case EOpIModAssign:
1853 outputTriplet(out, visit, "(", " %= ", ")");
1854 break;
1855 case EOpBitShiftLeftAssign:
1856 outputTriplet(out, visit, "(", " <<= ", ")");
1857 break;
1858 case EOpBitShiftRightAssign:
1859 outputTriplet(out, visit, "(", " >>= ", ")");
1860 break;
1861 case EOpBitwiseAndAssign:
1862 outputTriplet(out, visit, "(", " &= ", ")");
1863 break;
1864 case EOpBitwiseXorAssign:
1865 outputTriplet(out, visit, "(", " ^= ", ")");
1866 break;
1867 case EOpBitwiseOrAssign:
1868 outputTriplet(out, visit, "(", " |= ", ")");
1869 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001870 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001871 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001872 const TType& leftType = node->getLeft()->getType();
1873 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001874 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001875 if (visit == PreVisit)
1876 {
1877 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1878 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001879 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001880 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001881 return false;
1882 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001883 }
Olli Etuaho96963162016-03-21 11:54:33 +02001884 else if (ancestorEvaluatesToSamplerInStruct(visit))
1885 {
1886 // All parts of an expression that access a sampler in a struct need to use _ as
1887 // separator to access the sampler variable that has been moved out of the struct.
1888 outputTriplet(out, visit, "", "_", "");
1889 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001890 else
1891 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001892 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001893 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001894 }
1895 break;
1896 case EOpIndexIndirect:
1897 // We do not currently support indirect references to interface blocks
1898 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001899 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001900 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001901 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001902 {
1903 const TStructure* structure = node->getLeft()->getType().getStruct();
1904 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1905 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001906
Olli Etuaho96963162016-03-21 11:54:33 +02001907 // In cases where indexing returns a sampler, we need to access the sampler variable
1908 // that has been moved out of the struct.
1909 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1910 if (visit == PreVisit && indexingReturnsSampler)
1911 {
1912 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1913 // This prefix is only output at the beginning of the indexing expression, which
1914 // may have multiple parts.
1915 out << "angle";
1916 }
1917 if (!indexingReturnsSampler)
1918 {
1919 // All parts of an expression that access a sampler in a struct need to use _ as
1920 // separator to access the sampler variable that has been moved out of the struct.
1921 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1922 }
1923 if (visit == InVisit)
1924 {
1925 if (indexingReturnsSampler)
1926 {
1927 out << "_" + field->name();
1928 }
1929 else
1930 {
1931 out << "." + DecorateField(field->name(), *structure);
1932 }
1933
1934 return false;
1935 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001936 }
1937 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001938 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001939 if (visit == InVisit)
1940 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001941 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1942 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1943 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001944 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001945
1946 return false;
1947 }
1948 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949 case EOpVectorSwizzle:
1950 if (visit == InVisit)
1951 {
1952 out << ".";
1953
1954 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1955
1956 if (swizzle)
1957 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001958 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001960 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001961 {
1962 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1963
1964 if (element)
1965 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001966 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967
1968 switch (i)
1969 {
1970 case 0: out << "x"; break;
1971 case 1: out << "y"; break;
1972 case 2: out << "z"; break;
1973 case 3: out << "w"; break;
1974 default: UNREACHABLE();
1975 }
1976 }
1977 else UNREACHABLE();
1978 }
1979 }
1980 else UNREACHABLE();
1981
1982 return false; // Fully processed
1983 }
1984 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001985 case EOpAdd:
1986 outputTriplet(out, visit, "(", " + ", ")");
1987 break;
1988 case EOpSub:
1989 outputTriplet(out, visit, "(", " - ", ")");
1990 break;
1991 case EOpMul:
1992 outputTriplet(out, visit, "(", " * ", ")");
1993 break;
1994 case EOpDiv:
1995 outputTriplet(out, visit, "(", " / ", ")");
1996 break;
1997 case EOpIMod:
1998 outputTriplet(out, visit, "(", " % ", ")");
1999 break;
2000 case EOpBitShiftLeft:
2001 outputTriplet(out, visit, "(", " << ", ")");
2002 break;
2003 case EOpBitShiftRight:
2004 outputTriplet(out, visit, "(", " >> ", ")");
2005 break;
2006 case EOpBitwiseAnd:
2007 outputTriplet(out, visit, "(", " & ", ")");
2008 break;
2009 case EOpBitwiseXor:
2010 outputTriplet(out, visit, "(", " ^ ", ")");
2011 break;
2012 case EOpBitwiseOr:
2013 outputTriplet(out, visit, "(", " | ", ")");
2014 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002015 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002016 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02002017 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002018 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002019 case EOpLessThan:
2020 outputTriplet(out, visit, "(", " < ", ")");
2021 break;
2022 case EOpGreaterThan:
2023 outputTriplet(out, visit, "(", " > ", ")");
2024 break;
2025 case EOpLessThanEqual:
2026 outputTriplet(out, visit, "(", " <= ", ")");
2027 break;
2028 case EOpGreaterThanEqual:
2029 outputTriplet(out, visit, "(", " >= ", ")");
2030 break;
2031 case EOpVectorTimesScalar:
2032 outputTriplet(out, visit, "(", " * ", ")");
2033 break;
2034 case EOpMatrixTimesScalar:
2035 outputTriplet(out, visit, "(", " * ", ")");
2036 break;
2037 case EOpVectorTimesMatrix:
2038 outputTriplet(out, visit, "mul(", ", transpose(", "))");
2039 break;
2040 case EOpMatrixTimesVector:
2041 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
2042 break;
2043 case EOpMatrixTimesMatrix:
2044 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
2045 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002046 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03002047 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
2048 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002049 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03002050 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002051 case EOpLogicalXor:
2052 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002053 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002054 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002055 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03002056 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
2057 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002058 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03002059 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002060 default: UNREACHABLE();
2061 }
2062
2063 return true;
2064}
2065
2066bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2067{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002068 TInfoSinkBase &out = getInfoSink();
2069
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 switch (node->getOp())
2071 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002072 case EOpNegative:
2073 outputTriplet(out, visit, "(-", "", ")");
2074 break;
2075 case EOpPositive:
2076 outputTriplet(out, visit, "(+", "", ")");
2077 break;
2078 case EOpVectorLogicalNot:
2079 outputTriplet(out, visit, "(!", "", ")");
2080 break;
2081 case EOpLogicalNot:
2082 outputTriplet(out, visit, "(!", "", ")");
2083 break;
2084 case EOpBitwiseNot:
2085 outputTriplet(out, visit, "(~", "", ")");
2086 break;
2087 case EOpPostIncrement:
2088 outputTriplet(out, visit, "(", "", "++)");
2089 break;
2090 case EOpPostDecrement:
2091 outputTriplet(out, visit, "(", "", "--)");
2092 break;
2093 case EOpPreIncrement:
2094 outputTriplet(out, visit, "(++", "", ")");
2095 break;
2096 case EOpPreDecrement:
2097 outputTriplet(out, visit, "(--", "", ")");
2098 break;
2099 case EOpRadians:
2100 outputTriplet(out, visit, "radians(", "", ")");
2101 break;
2102 case EOpDegrees:
2103 outputTriplet(out, visit, "degrees(", "", ")");
2104 break;
2105 case EOpSin:
2106 outputTriplet(out, visit, "sin(", "", ")");
2107 break;
2108 case EOpCos:
2109 outputTriplet(out, visit, "cos(", "", ")");
2110 break;
2111 case EOpTan:
2112 outputTriplet(out, visit, "tan(", "", ")");
2113 break;
2114 case EOpAsin:
2115 outputTriplet(out, visit, "asin(", "", ")");
2116 break;
2117 case EOpAcos:
2118 outputTriplet(out, visit, "acos(", "", ")");
2119 break;
2120 case EOpAtan:
2121 outputTriplet(out, visit, "atan(", "", ")");
2122 break;
2123 case EOpSinh:
2124 outputTriplet(out, visit, "sinh(", "", ")");
2125 break;
2126 case EOpCosh:
2127 outputTriplet(out, visit, "cosh(", "", ")");
2128 break;
2129 case EOpTanh:
2130 outputTriplet(out, visit, "tanh(", "", ")");
2131 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002132 case EOpAsinh:
2133 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002134 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002135 break;
2136 case EOpAcosh:
2137 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002138 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002139 break;
2140 case EOpAtanh:
2141 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002142 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002143 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002144 case EOpExp:
2145 outputTriplet(out, visit, "exp(", "", ")");
2146 break;
2147 case EOpLog:
2148 outputTriplet(out, visit, "log(", "", ")");
2149 break;
2150 case EOpExp2:
2151 outputTriplet(out, visit, "exp2(", "", ")");
2152 break;
2153 case EOpLog2:
2154 outputTriplet(out, visit, "log2(", "", ")");
2155 break;
2156 case EOpSqrt:
2157 outputTriplet(out, visit, "sqrt(", "", ")");
2158 break;
2159 case EOpInverseSqrt:
2160 outputTriplet(out, visit, "rsqrt(", "", ")");
2161 break;
2162 case EOpAbs:
2163 outputTriplet(out, visit, "abs(", "", ")");
2164 break;
2165 case EOpSign:
2166 outputTriplet(out, visit, "sign(", "", ")");
2167 break;
2168 case EOpFloor:
2169 outputTriplet(out, visit, "floor(", "", ")");
2170 break;
2171 case EOpTrunc:
2172 outputTriplet(out, visit, "trunc(", "", ")");
2173 break;
2174 case EOpRound:
2175 outputTriplet(out, visit, "round(", "", ")");
2176 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08002177 case EOpRoundEven:
2178 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002179 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08002180 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002181 case EOpCeil:
2182 outputTriplet(out, visit, "ceil(", "", ")");
2183 break;
2184 case EOpFract:
2185 outputTriplet(out, visit, "frac(", "", ")");
2186 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05302187 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002188 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05302189 mRequiresIEEEStrictCompiling = true;
2190 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002191 case EOpIsInf:
2192 outputTriplet(out, visit, "isinf(", "", ")");
2193 break;
2194 case EOpFloatBitsToInt:
2195 outputTriplet(out, visit, "asint(", "", ")");
2196 break;
2197 case EOpFloatBitsToUint:
2198 outputTriplet(out, visit, "asuint(", "", ")");
2199 break;
2200 case EOpIntBitsToFloat:
2201 outputTriplet(out, visit, "asfloat(", "", ")");
2202 break;
2203 case EOpUintBitsToFloat:
2204 outputTriplet(out, visit, "asfloat(", "", ")");
2205 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02002206 case EOpPackSnorm2x16:
2207 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002208 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002209 break;
2210 case EOpPackUnorm2x16:
2211 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002212 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002213 break;
2214 case EOpPackHalf2x16:
2215 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002216 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002217 break;
2218 case EOpUnpackSnorm2x16:
2219 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002220 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002221 break;
2222 case EOpUnpackUnorm2x16:
2223 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002224 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002225 break;
2226 case EOpUnpackHalf2x16:
2227 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002228 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002229 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002230 case EOpLength:
2231 outputTriplet(out, visit, "length(", "", ")");
2232 break;
2233 case EOpNormalize:
2234 outputTriplet(out, visit, "normalize(", "", ")");
2235 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002236 case EOpDFdx:
2237 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2238 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002239 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002240 }
2241 else
2242 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002243 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002244 }
2245 break;
2246 case EOpDFdy:
2247 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2248 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002249 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002250 }
2251 else
2252 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002253 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002254 }
2255 break;
2256 case EOpFwidth:
2257 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2258 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002259 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002260 }
2261 else
2262 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002263 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002264 }
2265 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002266 case EOpTranspose:
2267 outputTriplet(out, visit, "transpose(", "", ")");
2268 break;
2269 case EOpDeterminant:
2270 outputTriplet(out, visit, "determinant(transpose(", "", "))");
2271 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002272 case EOpInverse:
2273 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002274 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002275 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002276
Jamie Madill8c46ab12015-12-07 16:39:19 -05002277 case EOpAny:
2278 outputTriplet(out, visit, "any(", "", ")");
2279 break;
2280 case EOpAll:
2281 outputTriplet(out, visit, "all(", "", ")");
2282 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283 default: UNREACHABLE();
2284 }
2285
2286 return true;
2287}
2288
Olli Etuaho96963162016-03-21 11:54:33 +02002289TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
2290{
2291 if (node->getAsSymbolNode())
2292 {
2293 return node->getAsSymbolNode()->getSymbol();
2294 }
2295 TIntermBinary *nodeBinary = node->getAsBinaryNode();
2296 switch (nodeBinary->getOp())
2297 {
2298 case EOpIndexDirect:
2299 {
2300 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
2301
2302 TInfoSinkBase prefixSink;
2303 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
2304 return TString(prefixSink.c_str());
2305 }
2306 case EOpIndexDirectStruct:
2307 {
2308 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
2309 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
2310 const TField *field = s->fields()[index];
2311
2312 TInfoSinkBase prefixSink;
2313 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
2314 << field->name();
2315 return TString(prefixSink.c_str());
2316 }
2317 default:
2318 UNREACHABLE();
2319 return TString("");
2320 }
2321}
2322
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2324{
Jamie Madill32aab012015-01-27 14:12:26 -05002325 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327 switch (node->getOp())
2328 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002329 case EOpSequence:
2330 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002331 if (mInsideFunction)
2332 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002333 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002334 out << "{\n";
2335 }
2336
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002337 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002338 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002339 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002340
Olli Etuahoa6f22092015-05-08 18:31:10 +03002341 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002342
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002343 // Don't output ; after case labels, they're terminated by :
2344 // This is needed especially since outputting a ; after a case statement would turn empty
2345 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03002346 // Also no need to output ; after selection (if) statements or sequences. This is done just
2347 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002348 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
2349 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03002350 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002351 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002352 }
2353
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002354 if (mInsideFunction)
2355 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002356 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002357 out << "}\n";
2358 }
2359
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002360 return false;
2361 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362 case EOpDeclaration:
2363 if (visit == PreVisit)
2364 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002365 TIntermSequence *sequence = node->getSequence();
2366 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03002367 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002369 if (variable &&
2370 (variable->getQualifier() == EvqTemporary ||
2371 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002373 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00002374
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002375 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002377 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002379 out << "static ";
2380 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002381
Olli Etuahoa6f22092015-05-08 18:31:10 +03002382 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04002383
Olli Etuahoa6f22092015-05-08 18:31:10 +03002384 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04002385
Olli Etuahoa6f22092015-05-08 18:31:10 +03002386 if (symbol)
2387 {
2388 symbol->traverse(this);
2389 out << ArrayString(symbol->getType());
2390 out << " = " + initializer(symbol->getType());
2391 }
2392 else
2393 {
2394 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002397 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2398 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002399 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002400 }
2401 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402 }
Jamie Madill033dae62014-06-18 12:56:28 -04002403 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002404 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002405 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002406 {
2407 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2408
2409 if (symbol)
2410 {
2411 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2412 mReferencedVaryings[symbol->getSymbol()] = symbol;
2413 }
2414 else
2415 {
2416 (*sit)->traverse(this);
2417 }
2418 }
2419 }
2420
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 return false;
2422 }
2423 else if (visit == InVisit)
2424 {
2425 out << ", ";
2426 }
2427 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04002428 case EOpInvariantDeclaration:
2429 // Do not do any translation
2430 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002431 case EOpPrototype:
2432 if (visit == PreVisit)
2433 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002434 size_t index = mCallDag.findIndex(node);
2435 // Skip the prototype if it is not implemented (and thus not used)
2436 if (index == CallDAG::InvalidIndex)
2437 {
2438 return false;
2439 }
2440
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002441 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002442
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002443 TString name = DecorateFunctionIfNeeded(node->getNameObj());
2444 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
2445 << (mOutputLod0Function ? "Lod0(" : "(");
2446
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002447 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002448 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002449 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002450
2451 if (symbol)
2452 {
2453 out << argumentString(symbol);
2454
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002455 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002456 {
2457 out << ", ";
2458 }
2459 }
2460 else UNREACHABLE();
2461 }
2462
2463 out << ");\n";
2464
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002465 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002466 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2467 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002468 {
2469 mOutputLod0Function = true;
2470 node->traverse(this);
2471 mOutputLod0Function = false;
2472 }
2473
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002474 return false;
2475 }
2476 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002477 case EOpComma:
2478 outputTriplet(out, visit, "(", ", ", ")");
2479 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 case EOpFunction:
2481 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002482 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002483 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002484
Corentin Wallez1239ee92015-03-19 14:38:02 -07002485 size_t index = mCallDag.findIndex(node);
2486 ASSERT(index != CallDAG::InvalidIndex);
2487 mCurrentFunctionMetadata = &mASTMetadataList[index];
2488
Jamie Madill033dae62014-06-18 12:56:28 -04002489 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002490
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002491 TIntermSequence *sequence = node->getSequence();
2492 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
2493
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002494 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002495 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002496 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002498 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002499 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002500 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002501 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002502 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002503
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002504 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002505 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002506 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002507
2508 if (symbol)
2509 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002510 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002511
2512 out << argumentString(symbol);
2513
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002514 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002515 {
2516 out << ", ";
2517 }
2518 }
2519 else UNREACHABLE();
2520 }
2521
Olli Etuaho4785fec2015-05-18 16:09:37 +03002522 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002523
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002524 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002525 {
2526 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002527 TIntermNode *body = (*sequence)[1];
2528 // The function body node will output braces.
2529 ASSERT(IsSequence(body));
2530 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002531 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002533 else
2534 {
2535 out << "{}\n";
2536 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002537
Corentin Wallez1239ee92015-03-19 14:38:02 -07002538 mCurrentFunctionMetadata = nullptr;
2539
2540 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2541 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002542 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002543 ASSERT(name != "main");
2544 mOutputLod0Function = true;
2545 node->traverse(this);
2546 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002547 }
2548
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002549 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550 }
2551 break;
2552 case EOpFunctionCall:
2553 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002554 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002555
Corentin Wallez1239ee92015-03-19 14:38:02 -07002556 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002557 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002559 if (node->isArray())
2560 {
2561 UNIMPLEMENTED();
2562 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002563 size_t index = mCallDag.findIndex(node);
2564 ASSERT(index != CallDAG::InvalidIndex);
2565 lod0 &= mASTMetadataList[index].mNeedsLod0;
2566
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002567 out << DecorateFunctionIfNeeded(node->getNameObj());
2568 out << DisambiguateFunctionName(node->getSequence());
2569 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570 }
2571 else
2572 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002573 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002574 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002575
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002576 TextureFunction textureFunction;
2577 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002578 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002579 textureFunction.method = TextureFunction::IMPLICIT;
2580 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002581 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002582
2583 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002584 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002585 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002586 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002587 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002588 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002589 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002590 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002591 }
Nicolas Capens46485082014-04-15 13:12:50 -04002592 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2593 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002594 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002595 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002596 }
Nicolas Capens46485082014-04-15 13:12:50 -04002597 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002598 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002599 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002600 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002601 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002602 else if (name == "textureSize")
2603 {
2604 textureFunction.method = TextureFunction::SIZE;
2605 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002606 else if (name == "textureOffset")
2607 {
2608 textureFunction.method = TextureFunction::IMPLICIT;
2609 textureFunction.offset = true;
2610 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002611 else if (name == "textureProjOffset")
2612 {
2613 textureFunction.method = TextureFunction::IMPLICIT;
2614 textureFunction.offset = true;
2615 textureFunction.proj = true;
2616 }
2617 else if (name == "textureLodOffset")
2618 {
2619 textureFunction.method = TextureFunction::LOD;
2620 textureFunction.offset = true;
2621 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002622 else if (name == "textureProjLodOffset")
2623 {
2624 textureFunction.method = TextureFunction::LOD;
2625 textureFunction.proj = true;
2626 textureFunction.offset = true;
2627 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002628 else if (name == "texelFetch")
2629 {
2630 textureFunction.method = TextureFunction::FETCH;
2631 }
2632 else if (name == "texelFetchOffset")
2633 {
2634 textureFunction.method = TextureFunction::FETCH;
2635 textureFunction.offset = true;
2636 }
Nicolas Capens46485082014-04-15 13:12:50 -04002637 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002638 {
2639 textureFunction.method = TextureFunction::GRAD;
2640 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002641 else if (name == "textureGradOffset")
2642 {
2643 textureFunction.method = TextureFunction::GRAD;
2644 textureFunction.offset = true;
2645 }
Nicolas Capens46485082014-04-15 13:12:50 -04002646 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002647 {
2648 textureFunction.method = TextureFunction::GRAD;
2649 textureFunction.proj = true;
2650 }
2651 else if (name == "textureProjGradOffset")
2652 {
2653 textureFunction.method = TextureFunction::GRAD;
2654 textureFunction.proj = true;
2655 textureFunction.offset = true;
2656 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002657 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002658
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002659 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002660 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002661 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2662
2663 if (textureFunction.offset)
2664 {
2665 mandatoryArgumentCount++;
2666 }
2667
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002668 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002669
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002670 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002671 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002672 if (bias)
2673 {
2674 textureFunction.method = TextureFunction::LOD0BIAS;
2675 }
2676 else
2677 {
2678 textureFunction.method = TextureFunction::LOD0;
2679 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002680 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002681 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002682 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002683 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002684 }
2685 }
2686
2687 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002688
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002689 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002691
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002692 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002693 {
Olli Etuaho96963162016-03-21 11:54:33 +02002694 TIntermTyped *typedArg = (*arg)->getAsTyped();
2695 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002696 {
2697 out << "texture_";
2698 (*arg)->traverse(this);
2699 out << ", sampler_";
2700 }
2701
2702 (*arg)->traverse(this);
2703
Olli Etuaho96963162016-03-21 11:54:33 +02002704 if (typedArg->getType().isStructureContainingSamplers())
2705 {
2706 const TType &argType = typedArg->getType();
2707 TVector<TIntermSymbol *> samplerSymbols;
2708 TString structName = samplerNamePrefixFromStruct(typedArg);
2709 argType.createSamplerSymbols("angle_" + structName, "",
2710 argType.isArray() ? argType.getArraySize() : 0,
2711 &samplerSymbols, nullptr);
2712 for (const TIntermSymbol *sampler : samplerSymbols)
2713 {
2714 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2715 {
2716 out << ", texture_" << sampler->getSymbol();
2717 out << ", sampler_" << sampler->getSymbol();
2718 }
2719 else
2720 {
2721 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
2722 // of D3D9, it's the sampler variable.
2723 out << ", " + sampler->getSymbol();
2724 }
2725 }
2726 }
2727
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002728 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002729 {
2730 out << ", ";
2731 }
2732 }
2733
2734 out << ")";
2735
2736 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737 }
2738 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002739 case EOpParameters:
2740 outputTriplet(out, visit, "(", ", ", ")\n{\n");
2741 break;
2742 case EOpConstructFloat:
2743 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
2744 break;
2745 case EOpConstructVec2:
2746 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
2747 break;
2748 case EOpConstructVec3:
2749 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
2750 break;
2751 case EOpConstructVec4:
2752 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
2753 break;
2754 case EOpConstructBool:
2755 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
2756 break;
2757 case EOpConstructBVec2:
2758 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
2759 break;
2760 case EOpConstructBVec3:
2761 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
2762 break;
2763 case EOpConstructBVec4:
2764 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
2765 break;
2766 case EOpConstructInt:
2767 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
2768 break;
2769 case EOpConstructIVec2:
2770 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
2771 break;
2772 case EOpConstructIVec3:
2773 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
2774 break;
2775 case EOpConstructIVec4:
2776 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
2777 break;
2778 case EOpConstructUInt:
2779 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
2780 break;
2781 case EOpConstructUVec2:
2782 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
2783 break;
2784 case EOpConstructUVec3:
2785 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
2786 break;
2787 case EOpConstructUVec4:
2788 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
2789 break;
2790 case EOpConstructMat2:
2791 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
2792 break;
2793 case EOpConstructMat2x3:
2794 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
2795 break;
2796 case EOpConstructMat2x4:
2797 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
2798 break;
2799 case EOpConstructMat3x2:
2800 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
2801 break;
2802 case EOpConstructMat3:
2803 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
2804 break;
2805 case EOpConstructMat3x4:
2806 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
2807 break;
2808 case EOpConstructMat4x2:
2809 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
2810 break;
2811 case EOpConstructMat4x3:
2812 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
2813 break;
2814 case EOpConstructMat4:
2815 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
2816 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002817 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002818 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002819 if (node->getType().isArray())
2820 {
2821 UNIMPLEMENTED();
2822 }
Jamie Madill033dae62014-06-18 12:56:28 -04002823 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002824 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002825 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002826 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002827 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002828 case EOpLessThan:
2829 outputTriplet(out, visit, "(", " < ", ")");
2830 break;
2831 case EOpGreaterThan:
2832 outputTriplet(out, visit, "(", " > ", ")");
2833 break;
2834 case EOpLessThanEqual:
2835 outputTriplet(out, visit, "(", " <= ", ")");
2836 break;
2837 case EOpGreaterThanEqual:
2838 outputTriplet(out, visit, "(", " >= ", ")");
2839 break;
2840 case EOpVectorEqual:
2841 outputTriplet(out, visit, "(", " == ", ")");
2842 break;
2843 case EOpVectorNotEqual:
2844 outputTriplet(out, visit, "(", " != ", ")");
2845 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002846 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002847 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002848 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002849 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002850 case EOpModf:
2851 outputTriplet(out, visit, "modf(", ", ", ")");
2852 break;
2853 case EOpPow:
2854 outputTriplet(out, visit, "pow(", ", ", ")");
2855 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002856 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002857 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002858 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002859 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002860 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002861 case EOpMin:
2862 outputTriplet(out, visit, "min(", ", ", ")");
2863 break;
2864 case EOpMax:
2865 outputTriplet(out, visit, "max(", ", ", ")");
2866 break;
2867 case EOpClamp:
2868 outputTriplet(out, visit, "clamp(", ", ", ")");
2869 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302870 case EOpMix:
2871 {
2872 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2873 if (lastParamNode->getType().getBasicType() == EbtBool)
2874 {
2875 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2876 // so use emulated version.
2877 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002878 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05302879 }
2880 else
2881 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002882 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302883 }
2884 }
2885 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002886 case EOpStep:
2887 outputTriplet(out, visit, "step(", ", ", ")");
2888 break;
2889 case EOpSmoothStep:
2890 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2891 break;
2892 case EOpDistance:
2893 outputTriplet(out, visit, "distance(", ", ", ")");
2894 break;
2895 case EOpDot:
2896 outputTriplet(out, visit, "dot(", ", ", ")");
2897 break;
2898 case EOpCross:
2899 outputTriplet(out, visit, "cross(", ", ", ")");
2900 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002901 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002902 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002903 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002904 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002905 case EOpReflect:
2906 outputTriplet(out, visit, "reflect(", ", ", ")");
2907 break;
2908 case EOpRefract:
2909 outputTriplet(out, visit, "refract(", ", ", ")");
2910 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002911 case EOpOuterProduct:
2912 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002913 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02002914 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002915 case EOpMul:
2916 outputTriplet(out, visit, "(", " * ", ")");
2917 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002918 default: UNREACHABLE();
2919 }
2920
2921 return true;
2922}
2923
Jamie Madill8c46ab12015-12-07 16:39:19 -05002924void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002925{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002926 out << "if (";
2927
2928 node->getCondition()->traverse(this);
2929
2930 out << ")\n";
2931
Jamie Madill8c46ab12015-12-07 16:39:19 -05002932 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002933
2934 bool discard = false;
2935
2936 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002937 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002938 // The trueBlock child node will output braces.
2939 ASSERT(IsSequence(node->getTrueBlock()));
2940
Olli Etuahoa6f22092015-05-08 18:31:10 +03002941 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002942
Olli Etuahoa6f22092015-05-08 18:31:10 +03002943 // Detect true discard
2944 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2945 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002946 else
2947 {
2948 // TODO(oetuaho): Check if the semicolon inside is necessary.
2949 // It's there as a result of conservative refactoring of the output.
2950 out << "{;}\n";
2951 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002952
Jamie Madill8c46ab12015-12-07 16:39:19 -05002953 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002954
Olli Etuahoa6f22092015-05-08 18:31:10 +03002955 if (node->getFalseBlock())
2956 {
2957 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002958
Jamie Madill8c46ab12015-12-07 16:39:19 -05002959 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002960
Olli Etuaho4785fec2015-05-18 16:09:37 +03002961 // Either this is "else if" or the falseBlock child node will output braces.
2962 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2963
Olli Etuahoa6f22092015-05-08 18:31:10 +03002964 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002965
Jamie Madill8c46ab12015-12-07 16:39:19 -05002966 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002967
Olli Etuahoa6f22092015-05-08 18:31:10 +03002968 // Detect false discard
2969 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2970 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002971
Olli Etuahoa6f22092015-05-08 18:31:10 +03002972 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002973 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002974 {
2975 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002976 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002977}
2978
2979bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2980{
2981 TInfoSinkBase &out = getInfoSink();
2982
2983 ASSERT(!node->usesTernaryOperator());
Olli Etuaho3d932d82016-04-12 11:10:30 +03002984 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002985
2986 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002987 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002988 {
2989 out << "FLATTEN ";
2990 }
2991
Jamie Madill8c46ab12015-12-07 16:39:19 -05002992 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002993
2994 return false;
2995}
2996
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002997bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002998{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002999 TInfoSinkBase &out = getInfoSink();
3000
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003001 if (node->getStatementList())
3002 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02003003 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05003004 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003005 // The curly braces get written when visiting the statementList aggregate
3006 }
3007 else
3008 {
3009 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05003010 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003011 }
3012 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02003013}
3014
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003015bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02003016{
Jamie Madill8c46ab12015-12-07 16:39:19 -05003017 TInfoSinkBase &out = getInfoSink();
3018
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003019 if (node->hasCondition())
3020 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003021 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003022 return true;
3023 }
3024 else
3025 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02003026 out << "default:\n";
3027 return false;
3028 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02003029}
3030
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003031void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
3032{
Jamie Madill8c46ab12015-12-07 16:39:19 -05003033 TInfoSinkBase &out = getInfoSink();
3034 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003035}
3036
3037bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
3038{
Nicolas Capens655fe362014-04-11 13:12:34 -04003039 mNestedLoopDepth++;
3040
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003041 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07003042 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07003043 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003044
Jamie Madill8c46ab12015-12-07 16:39:19 -05003045 TInfoSinkBase &out = getInfoSink();
3046
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003047 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003048 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003049 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00003050 {
Nicolas Capens655fe362014-04-11 13:12:34 -04003051 mInsideDiscontinuousLoop = wasDiscontinuous;
3052 mNestedLoopDepth--;
3053
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00003054 return false;
3055 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003056 }
3057
Corentin Wallez1239ee92015-03-19 14:38:02 -07003058 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00003059 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003060 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07003061 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003062
Jamie Madill8c46ab12015-12-07 16:39:19 -05003063 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003064 }
3065 else
3066 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07003067 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003068
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003069 if (node->getInit())
3070 {
3071 node->getInit()->traverse(this);
3072 }
3073
3074 out << "; ";
3075
alokp@chromium.org52813552010-11-16 18:36:09 +00003076 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003077 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003078 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003079 }
3080
3081 out << "; ";
3082
alokp@chromium.org52813552010-11-16 18:36:09 +00003083 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003084 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003085 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003086 }
3087
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003088 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003089
Jamie Madill8c46ab12015-12-07 16:39:19 -05003090 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003091 }
3092
3093 if (node->getBody())
3094 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03003095 // The loop body node will output braces.
3096 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03003097 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003098 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03003099 else
3100 {
3101 // TODO(oetuaho): Check if the semicolon inside is necessary.
3102 // It's there as a result of conservative refactoring of the output.
3103 out << "{;}\n";
3104 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003105
Jamie Madill8c46ab12015-12-07 16:39:19 -05003106 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003107
alokp@chromium.org52813552010-11-16 18:36:09 +00003108 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003109 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003110 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003111 out << "while(\n";
3112
alokp@chromium.org52813552010-11-16 18:36:09 +00003113 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003114
daniel@transgaming.com73536982012-03-21 20:45:49 +00003115 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003116 }
3117
daniel@transgaming.com73536982012-03-21 20:45:49 +00003118 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003119
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003120 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04003121 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003123 return false;
3124}
3125
3126bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
3127{
Jamie Madill32aab012015-01-27 14:12:26 -05003128 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003129
3130 switch (node->getFlowOp())
3131 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003132 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05003133 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003134 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003135 case EOpBreak:
3136 if (visit == PreVisit)
3137 {
Nicolas Capens655fe362014-04-11 13:12:34 -04003138 if (mNestedLoopDepth > 1)
3139 {
3140 mUsesNestedBreak = true;
3141 }
3142
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003143 if (mExcessiveLoopIndex)
3144 {
3145 out << "{Break";
3146 mExcessiveLoopIndex->traverse(this);
3147 out << " = true; break;}\n";
3148 }
3149 else
3150 {
3151 out << "break;\n";
3152 }
3153 }
3154 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05003155 case EOpContinue:
3156 outputTriplet(out, visit, "continue;\n", "", "");
3157 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003158 case EOpReturn:
3159 if (visit == PreVisit)
3160 {
3161 if (node->getExpression())
3162 {
3163 out << "return ";
3164 }
3165 else
3166 {
3167 out << "return;\n";
3168 }
3169 }
3170 else if (visit == PostVisit)
3171 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003172 if (node->getExpression())
3173 {
3174 out << ";\n";
3175 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003176 }
3177 break;
3178 default: UNREACHABLE();
3179 }
3180
3181 return true;
3182}
3183
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003184bool OutputHLSL::isSingleStatement(TIntermNode *node)
3185{
3186 TIntermAggregate *aggregate = node->getAsAggregate();
3187
3188 if (aggregate)
3189 {
3190 if (aggregate->getOp() == EOpSequence)
3191 {
3192 return false;
3193 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04003194 else if (aggregate->getOp() == EOpDeclaration)
3195 {
3196 // Declaring multiple comma-separated variables must be considered multiple statements
3197 // because each individual declaration has side effects which are visible in the next.
3198 return false;
3199 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003200 else
3201 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07003202 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003203 {
3204 if (!isSingleStatement(*sit))
3205 {
3206 return false;
3207 }
3208 }
3209
3210 return true;
3211 }
3212 }
3213
3214 return true;
3215}
3216
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003217// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
3218// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05003219bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003220{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003221 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003222
3223 // Parse loops of the form:
3224 // for(int index = initial; index [comparator] limit; index += increment)
3225 TIntermSymbol *index = NULL;
3226 TOperator comparator = EOpNull;
3227 int initial = 0;
3228 int limit = 0;
3229 int increment = 0;
3230
3231 // Parse index name and intial value
3232 if (node->getInit())
3233 {
3234 TIntermAggregate *init = node->getInit()->getAsAggregate();
3235
3236 if (init)
3237 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07003238 TIntermSequence *sequence = init->getSequence();
3239 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003240
3241 if (variable && variable->getQualifier() == EvqTemporary)
3242 {
3243 TIntermBinary *assign = variable->getAsBinaryNode();
3244
3245 if (assign->getOp() == EOpInitialize)
3246 {
3247 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3248 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3249
3250 if (symbol && constant)
3251 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003252 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003253 {
3254 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003255 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003256 }
3257 }
3258 }
3259 }
3260 }
3261 }
3262
3263 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003264 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003265 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003266 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003267
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003268 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3269 {
3270 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3271
3272 if (constant)
3273 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003274 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003275 {
3276 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003277 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003278 }
3279 }
3280 }
3281 }
3282
3283 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003284 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003285 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003286 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3287 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003288
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003289 if (binaryTerminal)
3290 {
3291 TOperator op = binaryTerminal->getOp();
3292 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3293
3294 if (constant)
3295 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003296 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003297 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003298 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003299
3300 switch (op)
3301 {
3302 case EOpAddAssign: increment = value; break;
3303 case EOpSubAssign: increment = -value; break;
3304 default: UNIMPLEMENTED();
3305 }
3306 }
3307 }
3308 }
3309 else if (unaryTerminal)
3310 {
3311 TOperator op = unaryTerminal->getOp();
3312
3313 switch (op)
3314 {
3315 case EOpPostIncrement: increment = 1; break;
3316 case EOpPostDecrement: increment = -1; break;
3317 case EOpPreIncrement: increment = 1; break;
3318 case EOpPreDecrement: increment = -1; break;
3319 default: UNIMPLEMENTED();
3320 }
3321 }
3322 }
3323
3324 if (index != NULL && comparator != EOpNull && increment != 0)
3325 {
3326 if (comparator == EOpLessThanEqual)
3327 {
3328 comparator = EOpLessThan;
3329 limit += 1;
3330 }
3331
3332 if (comparator == EOpLessThan)
3333 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003334 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003335
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003336 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003337 {
3338 return false; // Not an excessive loop
3339 }
3340
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003341 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3342 mExcessiveLoopIndex = index;
3343
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003344 out << "{int ";
3345 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003346 out << ";\n"
3347 "bool Break";
3348 index->traverse(this);
3349 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003350
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003351 bool firstLoopFragment = true;
3352
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003353 while (iterations > 0)
3354 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003355 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003356
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003357 if (!firstLoopFragment)
3358 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003359 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003360 index->traverse(this);
3361 out << ") {\n";
3362 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003363
3364 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3365 {
3366 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3367 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003368
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003369 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07003370 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003371
Corentin Wallez1239ee92015-03-19 14:38:02 -07003372 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003373 index->traverse(this);
3374 out << " = ";
3375 out << initial;
3376
3377 out << "; ";
3378 index->traverse(this);
3379 out << " < ";
3380 out << clampedLimit;
3381
3382 out << "; ";
3383 index->traverse(this);
3384 out << " += ";
3385 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003386 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003387
Jamie Madill8c46ab12015-12-07 16:39:19 -05003388 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003389 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003390
3391 if (node->getBody())
3392 {
3393 node->getBody()->traverse(this);
3394 }
3395
Jamie Madill8c46ab12015-12-07 16:39:19 -05003396 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003397 out << ";}\n";
3398
3399 if (!firstLoopFragment)
3400 {
3401 out << "}\n";
3402 }
3403
3404 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003405
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003406 initial += MAX_LOOP_ITERATIONS * increment;
3407 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003408 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003409
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003410 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003411
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003412 mExcessiveLoopIndex = restoreIndex;
3413
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003414 return true;
3415 }
3416 else UNIMPLEMENTED();
3417 }
3418
3419 return false; // Not handled as an excessive loop
3420}
3421
Jamie Madill8c46ab12015-12-07 16:39:19 -05003422void OutputHLSL::outputTriplet(TInfoSinkBase &out,
3423 Visit visit,
3424 const char *preString,
3425 const char *inString,
3426 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003427{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003428 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003429 {
3430 out << preString;
3431 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003432 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003433 {
3434 out << inString;
3435 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003436 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003437 {
3438 out << postString;
3439 }
3440}
3441
Jamie Madill8c46ab12015-12-07 16:39:19 -05003442void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003443{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003444 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003445 {
Jamie Madill32aab012015-01-27 14:12:26 -05003446 out << "\n";
3447 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003448
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003449 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003450 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003451 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003452 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003453
Jamie Madill32aab012015-01-27 14:12:26 -05003454 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003455 }
3456}
3457
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003458TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3459{
3460 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003461 const TType &type = symbol->getType();
3462 const TName &name = symbol->getName();
3463 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003464
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003465 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003466 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003467 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003468 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003469 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003470 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003471 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003472 }
3473
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003474 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003475 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003476 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3477 {
3478 // Samplers are passed as indices to the sampler array.
3479 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3480 return "const uint " + nameStr + ArrayString(type);
3481 }
3482 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3483 {
3484 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
3485 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
3486 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
3487 ArrayString(type);
3488 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003489 }
3490
Olli Etuaho96963162016-03-21 11:54:33 +02003491 TStringStream argString;
3492 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
3493 << ArrayString(type);
3494
3495 // If the structure parameter contains samplers, they need to be passed into the function as
3496 // separate parameters. HLSL doesn't natively support samplers in structs.
3497 if (type.isStructureContainingSamplers())
3498 {
3499 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3500 TVector<TIntermSymbol *> samplerSymbols;
3501 type.createSamplerSymbols("angle" + nameStr, "", 0, &samplerSymbols, nullptr);
3502 for (const TIntermSymbol *sampler : samplerSymbols)
3503 {
3504 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3505 {
3506 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
3507 }
3508 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3509 {
3510 const TType &samplerType = sampler->getType();
3511 ASSERT((!type.isArray() && !samplerType.isArray()) ||
3512 type.getArraySize() == samplerType.getArraySize());
3513 ASSERT(IsSampler(samplerType.getBasicType()));
3514 argString << ", " << QualifierString(qualifier) << " "
3515 << TextureString(samplerType.getBasicType()) << " texture_"
3516 << sampler->getSymbol() << ArrayString(type) << ", "
3517 << QualifierString(qualifier) << " "
3518 << SamplerString(samplerType.getBasicType()) << " sampler_"
3519 << sampler->getSymbol() << ArrayString(type);
3520 }
3521 else
3522 {
3523 const TType &samplerType = sampler->getType();
3524 ASSERT((!type.isArray() && !samplerType.isArray()) ||
3525 type.getArraySize() == samplerType.getArraySize());
3526 ASSERT(IsSampler(samplerType.getBasicType()));
3527 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
3528 << " " << sampler->getSymbol() << ArrayString(type);
3529 }
3530 }
3531 }
3532
3533 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003534}
3535
3536TString OutputHLSL::initializer(const TType &type)
3537{
3538 TString string;
3539
Jamie Madill94bf7f22013-07-08 13:31:15 -04003540 size_t size = type.getObjectSize();
3541 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003542 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003543 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003544
Jamie Madill94bf7f22013-07-08 13:31:15 -04003545 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003546 {
3547 string += ", ";
3548 }
3549 }
3550
daniel@transgaming.comead23042010-04-29 03:35:36 +00003551 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003552}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003553
Jamie Madill8c46ab12015-12-07 16:39:19 -05003554void OutputHLSL::outputConstructor(TInfoSinkBase &out,
3555 Visit visit,
3556 const TType &type,
3557 const char *name,
3558 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003559{
Olli Etuahof40319e2015-03-10 14:33:00 +02003560 if (type.isArray())
3561 {
3562 UNIMPLEMENTED();
3563 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003564
3565 if (visit == PreVisit)
3566 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003567 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003568
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003569 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003570 }
3571 else if (visit == InVisit)
3572 {
3573 out << ", ";
3574 }
3575 else if (visit == PostVisit)
3576 {
3577 out << ")";
3578 }
3579}
3580
Jamie Madill8c46ab12015-12-07 16:39:19 -05003581const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
3582 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003583 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003584{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003585 const TConstantUnion *constUnionIterated = constUnion;
3586
Jamie Madill98493dd2013-07-08 14:39:03 -04003587 const TStructure* structure = type.getStruct();
3588 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003589 {
Jamie Madill033dae62014-06-18 12:56:28 -04003590 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003591
Jamie Madill98493dd2013-07-08 14:39:03 -04003592 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003593
Jamie Madill98493dd2013-07-08 14:39:03 -04003594 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003595 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003596 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05003597 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003598
Jamie Madill98493dd2013-07-08 14:39:03 -04003599 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003600 {
3601 out << ", ";
3602 }
3603 }
3604
3605 out << ")";
3606 }
3607 else
3608 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003609 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003610 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04003611
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003612 if (writeType)
3613 {
Jamie Madill033dae62014-06-18 12:56:28 -04003614 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003615 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003616 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003617 if (writeType)
3618 {
3619 out << ")";
3620 }
3621 }
3622
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003623 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003624}
3625
Jamie Madill8c46ab12015-12-07 16:39:19 -05003626void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003627{
3628 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05003629 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003630}
3631
Jamie Madill37997142015-01-28 10:06:34 -05003632bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3633{
3634 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3635 expression->traverse(&searchSymbol);
3636
3637 if (searchSymbol.foundMatch())
3638 {
3639 // Type already printed
3640 out << "t" + str(mUniqueIndex) + " = ";
3641 expression->traverse(this);
3642 out << ", ";
3643 symbolNode->traverse(this);
3644 out << " = t" + str(mUniqueIndex);
3645
3646 mUniqueIndex++;
3647 return true;
3648 }
3649
3650 return false;
3651}
3652
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003653bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3654{
3655 // We support writing constant unions and constructors that only take constant unions as
3656 // parameters as HLSL literals.
3657 if (expression->getAsConstantUnion())
3658 {
3659 return true;
3660 }
3661 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3662 !expression->getAsAggregate()->isConstructor())
3663 {
3664 return false;
3665 }
3666 TIntermAggregate *constructor = expression->getAsAggregate();
3667 for (TIntermNode *&node : *constructor->getSequence())
3668 {
3669 if (!node->getAsConstantUnion())
3670 return false;
3671 }
3672 return true;
3673}
3674
3675bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3676 TIntermSymbol *symbolNode,
3677 TIntermTyped *expression)
3678{
3679 if (canWriteAsHLSLLiteral(expression))
3680 {
3681 symbolNode->traverse(this);
3682 if (expression->getType().isArray())
3683 {
3684 out << "[" << expression->getType().getArraySize() << "]";
3685 }
3686 out << " = {";
3687 if (expression->getAsConstantUnion())
3688 {
3689 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3690 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3691 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3692 }
3693 else
3694 {
3695 TIntermAggregate *constructor = expression->getAsAggregate();
3696 ASSERT(constructor != nullptr);
3697 for (TIntermNode *&node : *constructor->getSequence())
3698 {
3699 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3700 ASSERT(nodeConst);
3701 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3702 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3703 if (node != constructor->getSequence()->back())
3704 {
3705 out << ", ";
3706 }
3707 }
3708 }
3709 out << "}";
3710 return true;
3711 }
3712 return false;
3713}
3714
Jamie Madill55e79e02015-02-09 15:35:00 -05003715TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3716{
3717 const TFieldList &fields = structure.fields();
3718
3719 for (const auto &eqFunction : mStructEqualityFunctions)
3720 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003721 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003722 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003723 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003724 }
3725 }
3726
3727 const TString &structNameString = StructNameString(structure);
3728
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003729 StructEqualityFunction *function = new StructEqualityFunction();
3730 function->structure = &structure;
3731 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003732
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003733 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003734
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003735 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3736 << "{\n"
3737 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003738
3739 for (size_t i = 0; i < fields.size(); i++)
3740 {
3741 const TField *field = fields[i];
3742 const TType *fieldType = field->type();
3743
3744 const TString &fieldNameA = "a." + Decorate(field->name());
3745 const TString &fieldNameB = "b." + Decorate(field->name());
3746
3747 if (i > 0)
3748 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003749 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003750 }
3751
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003752 fnOut << "(";
3753 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3754 fnOut << fieldNameA;
3755 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3756 fnOut << fieldNameB;
3757 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3758 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003759 }
3760
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003761 fnOut << ";\n" << "}\n";
3762
3763 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003764
3765 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003766 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003767
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003768 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003769}
3770
Olli Etuaho7fb49552015-03-18 17:27:44 +02003771TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3772{
3773 for (const auto &eqFunction : mArrayEqualityFunctions)
3774 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003775 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003776 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003777 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003778 }
3779 }
3780
3781 const TString &typeName = TypeString(type);
3782
Olli Etuaho12690762015-03-31 12:55:28 +03003783 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003784 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003785
3786 TInfoSinkBase fnNameOut;
3787 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003788 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003789
3790 TType nonArrayType = type;
3791 nonArrayType.clearArrayness();
3792
3793 TInfoSinkBase fnOut;
3794
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003795 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003796 << typeName << " a[" << type.getArraySize() << "], "
3797 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003798 << "{\n"
3799 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3800 " {\n"
3801 " if (";
3802
3803 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3804 fnOut << "a[i]";
3805 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3806 fnOut << "b[i]";
3807 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3808
3809 fnOut << ") { return false; }\n"
3810 " }\n"
3811 " return true;\n"
3812 "}\n";
3813
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003814 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003815
3816 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003817 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003818
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003819 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003820}
3821
Olli Etuaho12690762015-03-31 12:55:28 +03003822TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3823{
3824 for (const auto &assignFunction : mArrayAssignmentFunctions)
3825 {
3826 if (assignFunction.type == type)
3827 {
3828 return assignFunction.functionName;
3829 }
3830 }
3831
3832 const TString &typeName = TypeString(type);
3833
3834 ArrayHelperFunction function;
3835 function.type = type;
3836
3837 TInfoSinkBase fnNameOut;
3838 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3839 function.functionName = fnNameOut.c_str();
3840
3841 TInfoSinkBase fnOut;
3842
3843 fnOut << "void " << function.functionName << "(out "
3844 << typeName << " a[" << type.getArraySize() << "], "
3845 << typeName << " b[" << type.getArraySize() << "])\n"
3846 << "{\n"
3847 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3848 " {\n"
3849 " a[i] = b[i];\n"
3850 " }\n"
3851 "}\n";
3852
3853 function.functionDefinition = fnOut.c_str();
3854
3855 mArrayAssignmentFunctions.push_back(function);
3856
3857 return function.functionName;
3858}
3859
Olli Etuaho9638c352015-04-01 14:34:52 +03003860TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3861{
3862 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3863 {
3864 if (constructIntoFunction.type == type)
3865 {
3866 return constructIntoFunction.functionName;
3867 }
3868 }
3869
3870 const TString &typeName = TypeString(type);
3871
3872 ArrayHelperFunction function;
3873 function.type = type;
3874
3875 TInfoSinkBase fnNameOut;
3876 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3877 function.functionName = fnNameOut.c_str();
3878
3879 TInfoSinkBase fnOut;
3880
3881 fnOut << "void " << function.functionName << "(out "
3882 << typeName << " a[" << type.getArraySize() << "]";
3883 for (int i = 0; i < type.getArraySize(); ++i)
3884 {
3885 fnOut << ", " << typeName << " b" << i;
3886 }
3887 fnOut << ")\n"
3888 "{\n";
3889
3890 for (int i = 0; i < type.getArraySize(); ++i)
3891 {
3892 fnOut << " a[" << i << "] = b" << i << ";\n";
3893 }
3894 fnOut << "}\n";
3895
3896 function.functionDefinition = fnOut.c_str();
3897
3898 mArrayConstructIntoFunctions.push_back(function);
3899
3900 return function.functionName;
3901}
3902
Jamie Madill2e295e22015-04-29 10:41:33 -04003903void OutputHLSL::ensureStructDefined(const TType &type)
3904{
3905 TStructure *structure = type.getStruct();
3906
3907 if (structure)
3908 {
3909 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3910 }
3911}
3912
3913
Olli Etuaho9638c352015-04-01 14:34:52 +03003914
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003915}