blob: 8b914b1dfe502f36f410a43d14cc8674b8faa604 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030014#include "common/debug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020016#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/FlagStd140Structs.h"
19#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/SearchSymbol.h"
23#include "compiler/translator/StructureHLSL.h"
24#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050025#include "compiler/translator/UniformHLSL.h"
26#include "compiler/translator/UtilsHLSL.h"
27#include "compiler/translator/blocklayout.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050028#include "compiler/translator/util.h"
29
Olli Etuaho4785fec2015-05-18 16:09:37 +030030namespace
31{
32
33bool IsSequence(TIntermNode *node)
34{
35 return node->getAsAggregate() != nullptr && node->getAsAggregate()->getOp() == EOpSequence;
36}
37
Olli Etuaho18b9deb2015-11-05 12:14:50 +020038void WriteSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
39{
40 ASSERT(constUnion != nullptr);
41 switch (constUnion->getType())
42 {
43 case EbtFloat:
44 out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst()));
45 break;
46 case EbtInt:
47 out << constUnion->getIConst();
48 break;
49 case EbtUInt:
50 out << constUnion->getUConst();
51 break;
52 case EbtBool:
53 out << constUnion->getBConst();
54 break;
55 default:
56 UNREACHABLE();
57 }
58}
59
60const TConstantUnion *WriteConstantUnionArray(TInfoSinkBase &out,
61 const TConstantUnion *const constUnion,
62 const size_t size)
63{
64 const TConstantUnion *constUnionIterated = constUnion;
65 for (size_t i = 0; i < size; i++, constUnionIterated++)
66 {
67 WriteSingleConstant(out, constUnionIterated);
68
69 if (i != size - 1)
70 {
71 out << ", ";
72 }
73 }
74 return constUnionIterated;
75}
76
Olli Etuaho4785fec2015-05-18 16:09:37 +030077} // namespace
78
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079namespace sh
80{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000081
Nicolas Capense0ba27a2013-06-24 16:10:52 -040082TString OutputHLSL::TextureFunction::name() const
83{
84 TString name = "gl_texture";
85
Olli Etuaho9b4e8622015-12-22 15:53:22 +020086 // We need to include full the sampler type in the function name to make the signature unique
87 // on D3D11, where samplers are passed to texture functions as indices.
88 name += TextureTypeSuffix(this->sampler);
Nicolas Capense0ba27a2013-06-24 16:10:52 -040089
90 if (proj)
91 {
92 name += "Proj";
93 }
94
Nicolas Capensb1f45b72013-12-19 17:37:19 -050095 if (offset)
96 {
97 name += "Offset";
98 }
99
Nicolas Capens75fb4752013-07-10 15:14:47 -0400100 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400101 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500102 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400103 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500104 case LOD: name += "Lod"; break;
105 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400106 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500107 case SIZE: name += "Size"; break;
108 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500109 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400110 default: UNREACHABLE();
111 }
112
113 return name + "(";
114}
115
116bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
117{
118 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400119 if (sampler > rhs.sampler) return false;
120
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400121 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400122 if (coords > rhs.coords) return false;
123
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400124 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400125 if (proj && !rhs.proj) return false;
126
127 if (!offset && rhs.offset) return true;
128 if (offset && !rhs.offset) return false;
129
Nicolas Capens75fb4752013-07-10 15:14:47 -0400130 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400131 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400132
133 return false;
134}
135
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200136OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
137 const TExtensionBehavior &extensionBehavior,
138 const char *sourcePath, ShShaderOutput outputType,
139 int numRenderTargets, const std::vector<Uniform> &uniforms,
140 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400141 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200142 mShaderType(shaderType),
143 mShaderVersion(shaderVersion),
144 mExtensionBehavior(extensionBehavior),
145 mSourcePath(sourcePath),
146 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700147 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000148 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700149 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000151 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000152
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000153 mUsesFragColor = false;
154 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000155 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000156 mUsesFragCoord = false;
157 mUsesPointCoord = false;
158 mUsesFrontFacing = false;
159 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000160 mUsesInstanceID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500161 mUsesVertexID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400162 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000163 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500164 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400165 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530166 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000167
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000168 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000169
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000170 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000171 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400172 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000173
174 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000175
Jamie Madill8daaba12014-06-13 10:04:33 -0400176 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200177 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400178
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200179 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000180 {
Arun Patole63419392015-03-13 11:51:07 +0530181 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
182 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
183 // In both cases total 3 uniform registers need to be reserved.
184 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000185 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000186
Geoff Lang00140f42016-02-03 18:47:33 +0000187 // Reserve registers for the default uniform block and driver constants
188 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189}
190
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000191OutputHLSL::~OutputHLSL()
192{
Jamie Madill8daaba12014-06-13 10:04:33 -0400193 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400194 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200195 for (auto &eqFunction : mStructEqualityFunctions)
196 {
197 SafeDelete(eqFunction);
198 }
199 for (auto &eqFunction : mArrayEqualityFunctions)
200 {
201 SafeDelete(eqFunction);
202 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000203}
204
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200205void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000206{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200207 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400208 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000209
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200210 BuiltInFunctionEmulator builtInFunctionEmulator;
211 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200212 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500213
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700214 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700215 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
216 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300217 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700218 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700219
Jamie Madill37997142015-01-28 10:06:34 -0500220 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500221 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200222 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500223 mInfoSinkStack.pop();
224
Jamie Madill37997142015-01-28 10:06:34 -0500225 mInfoSinkStack.push(&mFooter);
226 if (!mDeferredGlobalInitializers.empty())
227 {
228 writeDeferredGlobalInitializers(mFooter);
229 }
230 mInfoSinkStack.pop();
231
Jamie Madill32aab012015-01-27 14:12:26 -0500232 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500233 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500234 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000235
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200236 objSink << mHeader.c_str();
237 objSink << mBody.c_str();
238 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200239
240 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000241}
242
Jamie Madill570e04d2013-06-21 09:15:33 -0400243void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
244{
245 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
246 {
247 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
248
Jamie Madill32aab012015-01-27 14:12:26 -0500249 TInfoSinkBase structInfoSink;
250 mInfoSinkStack.push(&structInfoSink);
251
Jamie Madill570e04d2013-06-21 09:15:33 -0400252 // This will mark the necessary block elements as referenced
253 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500254
255 TString structName(structInfoSink.c_str());
256 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400257
258 mFlaggedStructOriginalNames[flaggedNode] = structName;
259
260 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
261 {
262 structName.erase(pos, 1);
263 }
264
265 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
266 }
267}
268
Jamie Madill4e1fd412014-07-10 17:50:10 -0400269const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
270{
271 return mUniformHLSL->getInterfaceBlockRegisterMap();
272}
273
Jamie Madill9fe25e92014-07-18 10:33:08 -0400274const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
275{
276 return mUniformHLSL->getUniformRegisterMap();
277}
278
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000279int OutputHLSL::vectorSize(const TType &type) const
280{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000281 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000282 int arraySize = type.isArray() ? type.getArraySize() : 1;
283
284 return elementSize * arraySize;
285}
286
Jamie Madill98493dd2013-07-08 14:39:03 -0400287TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400288{
289 TString init;
290
291 TString preIndentString;
292 TString fullIndentString;
293
294 for (int spaces = 0; spaces < (indent * 4); spaces++)
295 {
296 preIndentString += ' ';
297 }
298
299 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
300 {
301 fullIndentString += ' ';
302 }
303
304 init += preIndentString + "{\n";
305
Jamie Madill98493dd2013-07-08 14:39:03 -0400306 const TFieldList &fields = structure.fields();
307 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400308 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400309 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400310 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400311 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400312
Jamie Madill98493dd2013-07-08 14:39:03 -0400313 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400314 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400315 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400316 }
317 else
318 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400319 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400320 }
321 }
322
323 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
324
325 return init;
326}
327
Jamie Madill8c46ab12015-12-07 16:39:19 -0500328void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000330 TString varyings;
331 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400332 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000333
Jamie Madill829f59e2013-11-13 19:40:54 -0500334 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400335 {
336 TIntermTyped *structNode = flaggedStructIt->first;
337 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400338 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400339 const TString &originalName = mFlaggedStructOriginalNames[structNode];
340
Jamie Madill033dae62014-06-18 12:56:28 -0400341 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400342 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400343 flaggedStructs += "\n";
344 }
345
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000346 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
347 {
348 const TType &type = varying->second->getType();
349 const TString &name = varying->second->getSymbol();
350
351 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400352 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
353 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000354 }
355
356 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
357 {
358 const TType &type = attribute->second->getType();
359 const TString &name = attribute->second->getSymbol();
360
Jamie Madill033dae62014-06-18 12:56:28 -0400361 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000362 }
363
Jamie Madill8daaba12014-06-13 10:04:33 -0400364 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400365
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200366 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400367 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
368
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200369 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500370 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200371 out << "\n// Equality functions\n\n";
372 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500373 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200374 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200375 }
376 }
Olli Etuaho12690762015-03-31 12:55:28 +0300377 if (!mArrayAssignmentFunctions.empty())
378 {
379 out << "\n// Assignment functions\n\n";
380 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
381 {
382 out << assignmentFunction.functionDefinition << "\n";
383 }
384 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300385 if (!mArrayConstructIntoFunctions.empty())
386 {
387 out << "\n// Array constructor functions\n\n";
388 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
389 {
390 out << constructIntoFunction.functionDefinition << "\n";
391 }
392 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200393
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500394 if (mUsesDiscardRewriting)
395 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400396 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500397 }
398
Nicolas Capens655fe362014-04-11 13:12:34 -0400399 if (mUsesNestedBreak)
400 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400401 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400402 }
403
Arun Patole44efa0b2015-03-04 17:11:05 +0530404 if (mRequiresIEEEStrictCompiling)
405 {
406 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
407 }
408
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400409 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
410 "#define LOOP [loop]\n"
411 "#define FLATTEN [flatten]\n"
412 "#else\n"
413 "#define LOOP\n"
414 "#define FLATTEN\n"
415 "#endif\n";
416
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200417 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200419 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
420 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000421
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000422 out << "// Varyings\n";
423 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400424 out << "\n";
425
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200426 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000427 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500428 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000429 {
Jamie Madill46131a32013-06-20 11:55:50 -0400430 const TString &variableName = outputVariableIt->first;
431 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400432
Jamie Madill033dae62014-06-18 12:56:28 -0400433 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400434 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000435 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000436 }
Jamie Madill46131a32013-06-20 11:55:50 -0400437 else
438 {
439 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
440
441 out << "static float4 gl_Color[" << numColorValues << "] =\n"
442 "{\n";
443 for (unsigned int i = 0; i < numColorValues; i++)
444 {
445 out << " float4(0, 0, 0, 0)";
446 if (i + 1 != numColorValues)
447 {
448 out << ",";
449 }
450 out << "\n";
451 }
452
453 out << "};\n";
454 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000455
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400456 if (mUsesFragDepth)
457 {
458 out << "static float gl_Depth = 0.0;\n";
459 }
460
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000461 if (mUsesFragCoord)
462 {
463 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
464 }
465
466 if (mUsesPointCoord)
467 {
468 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
469 }
470
471 if (mUsesFrontFacing)
472 {
473 out << "static bool gl_FrontFacing = false;\n";
474 }
475
476 out << "\n";
477
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000478 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000479 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000480 out << "struct gl_DepthRangeParameters\n"
481 "{\n"
482 " float near;\n"
483 " float far;\n"
484 " float diff;\n"
485 "};\n"
486 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000487 }
488
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200489 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000490 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000491 out << "cbuffer DriverConstants : register(b1)\n"
492 "{\n";
493
494 if (mUsesDepthRange)
495 {
496 out << " float3 dx_DepthRange : packoffset(c0);\n";
497 }
498
499 if (mUsesFragCoord)
500 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000501 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000502 }
503
504 if (mUsesFragCoord || mUsesFrontFacing)
505 {
506 out << " float3 dx_DepthFront : packoffset(c2);\n";
507 }
508
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800509 if (mUsesFragCoord)
510 {
511 // dx_ViewScale is only used in the fragment shader to correct
512 // the value for glFragCoord if necessary
513 out << " float2 dx_ViewScale : packoffset(c3);\n";
514 }
515
Olli Etuaho618bebc2016-01-15 16:40:00 +0200516 if (mOutputType == SH_HLSL_4_1_OUTPUT)
517 {
518 mUniformHLSL->samplerMetadataUniforms(out, "c4");
519 }
520
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000521 out << "};\n";
522 }
523 else
524 {
525 if (mUsesDepthRange)
526 {
527 out << "uniform float3 dx_DepthRange : register(c0);";
528 }
529
530 if (mUsesFragCoord)
531 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000532 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000533 }
534
535 if (mUsesFragCoord || mUsesFrontFacing)
536 {
537 out << "uniform float3 dx_DepthFront : register(c2);\n";
538 }
539 }
540
541 out << "\n";
542
543 if (mUsesDepthRange)
544 {
545 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
546 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000547 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000548
Jamie Madillf91ce812014-06-13 10:04:34 -0400549 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000550 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400551 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000552 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400553 out << flaggedStructs;
554 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000555 }
556
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000557 if (usingMRTExtension && mNumRenderTargets > 1)
558 {
559 out << "#define GL_USES_MRT\n";
560 }
561
562 if (mUsesFragColor)
563 {
564 out << "#define GL_USES_FRAG_COLOR\n";
565 }
566
567 if (mUsesFragData)
568 {
569 out << "#define GL_USES_FRAG_DATA\n";
570 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000572 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000573 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000574 out << "// Attributes\n";
575 out << attributes;
576 out << "\n"
577 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400578
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000579 if (mUsesPointSize)
580 {
581 out << "static float gl_PointSize = float(1);\n";
582 }
583
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000584 if (mUsesInstanceID)
585 {
586 out << "static int gl_InstanceID;";
587 }
588
Corentin Wallezb076add2016-01-11 16:45:46 -0500589 if (mUsesVertexID)
590 {
591 out << "static int gl_VertexID;";
592 }
593
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000594 out << "\n"
595 "// Varyings\n";
596 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000597 out << "\n";
598
599 if (mUsesDepthRange)
600 {
601 out << "struct gl_DepthRangeParameters\n"
602 "{\n"
603 " float near;\n"
604 " float far;\n"
605 " float diff;\n"
606 "};\n"
607 "\n";
608 }
609
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200610 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000611 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800612 out << "cbuffer DriverConstants : register(b1)\n"
613 "{\n";
614
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000615 if (mUsesDepthRange)
616 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800617 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000618 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800619
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800620 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
621 // shaders. However, we declare it for all shaders (including Feature Level 10+).
622 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
623 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800624 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800625 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800626 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800627
Olli Etuaho618bebc2016-01-15 16:40:00 +0200628 if (mOutputType == SH_HLSL_4_1_OUTPUT)
629 {
630 mUniformHLSL->samplerMetadataUniforms(out, "c4");
631 }
632
Austin Kinross4fd18b12014-12-22 12:32:05 -0800633 out << "};\n"
634 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000635 }
636 else
637 {
638 if (mUsesDepthRange)
639 {
640 out << "uniform float3 dx_DepthRange : register(c0);\n";
641 }
642
Cooper Partine6664f02015-01-09 16:22:24 -0800643 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
644 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000645 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000646 }
647
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000648 if (mUsesDepthRange)
649 {
650 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
651 "\n";
652 }
653
Jamie Madillf91ce812014-06-13 10:04:34 -0400654 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000655 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400656 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000657 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400658 out << flaggedStructs;
659 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000660 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400661 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000662
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400663 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
664 {
665 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400666 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000667 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400668 switch(textureFunction->sampler)
669 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400670 case EbtSampler2D: out << "int2 "; break;
671 case EbtSampler3D: out << "int3 "; break;
672 case EbtSamplerCube: out << "int2 "; break;
673 case EbtSampler2DArray: out << "int3 "; break;
674 case EbtISampler2D: out << "int2 "; break;
675 case EbtISampler3D: out << "int3 "; break;
676 case EbtISamplerCube: out << "int2 "; break;
677 case EbtISampler2DArray: out << "int3 "; break;
678 case EbtUSampler2D: out << "int2 "; break;
679 case EbtUSampler3D: out << "int3 "; break;
680 case EbtUSamplerCube: out << "int2 "; break;
681 case EbtUSampler2DArray: out << "int3 "; break;
682 case EbtSampler2DShadow: out << "int2 "; break;
683 case EbtSamplerCubeShadow: out << "int2 "; break;
684 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400685 default: UNREACHABLE();
686 }
687 }
688 else // Sampling function
689 {
690 switch(textureFunction->sampler)
691 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400692 case EbtSampler2D: out << "float4 "; break;
693 case EbtSampler3D: out << "float4 "; break;
694 case EbtSamplerCube: out << "float4 "; break;
695 case EbtSampler2DArray: out << "float4 "; break;
696 case EbtISampler2D: out << "int4 "; break;
697 case EbtISampler3D: out << "int4 "; break;
698 case EbtISamplerCube: out << "int4 "; break;
699 case EbtISampler2DArray: out << "int4 "; break;
700 case EbtUSampler2D: out << "uint4 "; break;
701 case EbtUSampler3D: out << "uint4 "; break;
702 case EbtUSamplerCube: out << "uint4 "; break;
703 case EbtUSampler2DArray: out << "uint4 "; break;
704 case EbtSampler2DShadow: out << "float "; break;
705 case EbtSamplerCubeShadow: out << "float "; break;
706 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400707 default: UNREACHABLE();
708 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000709 }
710
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400711 // Function name
712 out << textureFunction->name();
713
714 // Argument list
715 int hlslCoords = 4;
716
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200717 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000718 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400719 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000720 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400721 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
722 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
723 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000724 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400725
Nicolas Capens75fb4752013-07-10 15:14:47 -0400726 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000727 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400728 case TextureFunction::IMPLICIT: break;
729 case TextureFunction::BIAS: hlslCoords = 4; break;
730 case TextureFunction::LOD: hlslCoords = 4; break;
731 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400732 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400733 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000734 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400735 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200736 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400737 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200738 hlslCoords = HLSLTextureCoordsCount(textureFunction->sampler);
739 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400740 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200741 out << TextureString(textureFunction->sampler) << " x, "
742 << SamplerString(textureFunction->sampler) << " s";
743 }
744 else
745 {
746 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
747 out << "const uint samplerIndex";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400748 }
749 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400750
Nicolas Capensfc014542014-02-18 14:47:13 -0500751 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400752 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500753 switch(textureFunction->coords)
754 {
755 case 2: out << ", int2 t"; break;
756 case 3: out << ", int3 t"; break;
757 default: UNREACHABLE();
758 }
759 }
760 else // Floating-point coordinates (except textureSize)
761 {
762 switch(textureFunction->coords)
763 {
764 case 1: out << ", int lod"; break; // textureSize()
765 case 2: out << ", float2 t"; break;
766 case 3: out << ", float3 t"; break;
767 case 4: out << ", float4 t"; break;
768 default: UNREACHABLE();
769 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000770 }
771
Nicolas Capensd11d5492014-02-19 17:06:10 -0500772 if (textureFunction->method == TextureFunction::GRAD)
773 {
774 switch(textureFunction->sampler)
775 {
776 case EbtSampler2D:
777 case EbtISampler2D:
778 case EbtUSampler2D:
779 case EbtSampler2DArray:
780 case EbtISampler2DArray:
781 case EbtUSampler2DArray:
782 case EbtSampler2DShadow:
783 case EbtSampler2DArrayShadow:
784 out << ", float2 ddx, float2 ddy";
785 break;
786 case EbtSampler3D:
787 case EbtISampler3D:
788 case EbtUSampler3D:
789 case EbtSamplerCube:
790 case EbtISamplerCube:
791 case EbtUSamplerCube:
792 case EbtSamplerCubeShadow:
793 out << ", float3 ddx, float3 ddy";
794 break;
795 default: UNREACHABLE();
796 }
797 }
798
Nicolas Capens75fb4752013-07-10 15:14:47 -0400799 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000800 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400801 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400802 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400803 case TextureFunction::LOD: out << ", float lod"; break;
804 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400805 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400806 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500807 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500808 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400809 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000810 }
811
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500812 if (textureFunction->offset)
813 {
814 switch(textureFunction->sampler)
815 {
816 case EbtSampler2D: out << ", int2 offset"; break;
817 case EbtSampler3D: out << ", int3 offset"; break;
818 case EbtSampler2DArray: out << ", int2 offset"; break;
819 case EbtISampler2D: out << ", int2 offset"; break;
820 case EbtISampler3D: out << ", int3 offset"; break;
821 case EbtISampler2DArray: out << ", int2 offset"; break;
822 case EbtUSampler2D: out << ", int2 offset"; break;
823 case EbtUSampler3D: out << ", int3 offset"; break;
824 case EbtUSampler2DArray: out << ", int2 offset"; break;
825 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500826 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500827 default: UNREACHABLE();
828 }
829 }
830
Nicolas Capens84cfa122014-04-14 13:48:45 -0400831 if (textureFunction->method == TextureFunction::BIAS ||
832 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500833 {
834 out << ", float bias";
835 }
836
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400837 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400838 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400839
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200840 // In some cases we use a variable to store the texture/sampler objects, but to work around
841 // a D3D11 compiler bug related to discard inside a loop that is conditional on texture
842 // sampling we need to call the function directly on a reference to the array. The bug was
843 // found using dEQP-GLES3.functional.shaders.discard*loop_texture* tests.
844 TString textureReference("x");
845 TString samplerReference("s");
846 if (mOutputType == SH_HLSL_4_1_OUTPUT)
847 {
848 TString suffix = TextureGroupSuffix(textureFunction->sampler);
849 if (TextureGroup(textureFunction->sampler) == HLSL_TEXTURE_2D)
850 {
851 textureReference = TString("textures") + suffix + "[samplerIndex]";
852 samplerReference = TString("samplers") + suffix + "[samplerIndex]";
853 }
854 else
855 {
856 out << " const uint textureIndex = samplerIndex - textureIndexOffset" << suffix
857 << ";\n";
858 textureReference = TString("textures") + suffix + "[textureIndex]";
859 out << " const uint samplerArrayIndex = samplerIndex - samplerIndexOffset"
860 << suffix << ";\n";
861 samplerReference = TString("samplers") + suffix + "[samplerArrayIndex]";
862 }
863 }
864
Nicolas Capens75fb4752013-07-10 15:14:47 -0400865 if (textureFunction->method == TextureFunction::SIZE)
866 {
Olli Etuahof8bf5832016-02-11 16:21:49 +0200867 out << "int baseLevel = samplerMetadata[samplerIndex].x;\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400868 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
869 {
Olli Etuahobce743a2016-01-15 17:18:28 +0200870 if (IsSamplerArray(textureFunction->sampler) ||
871 (IsIntegerSampler(textureFunction->sampler) &&
872 IsSamplerCube(textureFunction->sampler)))
Nicolas Capens75fb4752013-07-10 15:14:47 -0400873 {
874 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
Olli Etuahobce743a2016-01-15 17:18:28 +0200875 << " " << textureReference << ".GetDimensions(baseLevel + lod, width, "
876 "height, layers, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400877 }
878 else
879 {
880 out << " uint width; uint height; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200881 << " " << textureReference
Olli Etuahobce743a2016-01-15 17:18:28 +0200882 << ".GetDimensions(baseLevel + lod, width, height, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400883 }
884 }
885 else if (IsSampler3D(textureFunction->sampler))
886 {
887 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200888 << " " << textureReference
Olli Etuahobce743a2016-01-15 17:18:28 +0200889 << ".GetDimensions(baseLevel + lod, width, height, depth, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400890 }
891 else UNREACHABLE();
892
893 switch(textureFunction->sampler)
894 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400895 case EbtSampler2D: out << " return int2(width, height);"; break;
896 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
897 case EbtSamplerCube: out << " return int2(width, height);"; break;
898 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
899 case EbtISampler2D: out << " return int2(width, height);"; break;
900 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
901 case EbtISamplerCube: out << " return int2(width, height);"; break;
902 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
903 case EbtUSampler2D: out << " return int2(width, height);"; break;
904 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
905 case EbtUSamplerCube: out << " return int2(width, height);"; break;
906 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
907 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
908 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
909 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400910 default: UNREACHABLE();
911 }
912 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400913 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400914 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500915 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
916 {
917 out << " float width; float height; float layers; float levels;\n";
918
919 out << " uint mip = 0;\n";
920
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200921 out << " " << textureReference
922 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens0027fa92014-02-20 14:26:42 -0500923
924 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
925 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
926 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
927 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
928
929 // FACE_POSITIVE_X = 000b
930 // FACE_NEGATIVE_X = 001b
931 // FACE_POSITIVE_Y = 010b
932 // FACE_NEGATIVE_Y = 011b
933 // FACE_POSITIVE_Z = 100b
934 // FACE_NEGATIVE_Z = 101b
935 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
936
937 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
938 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
939 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
940
941 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
942 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500943
944 // Mip level computation.
945 if (textureFunction->method == TextureFunction::IMPLICIT)
946 {
947 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
948 " float2 dx = ddx(tSized);\n"
949 " float2 dy = ddy(tSized);\n"
950 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n"
951 " mip = uint(min(max(round(lod), 0), levels - 1));\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200952 << " " << textureReference
953 << ".GetDimensions(mip, width, height, layers, levels);\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500954 }
Nicolas Capens0027fa92014-02-20 14:26:42 -0500955 }
956 else if (IsIntegerSampler(textureFunction->sampler) &&
957 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400958 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400959 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400960 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400961 if (IsSamplerArray(textureFunction->sampler))
962 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400963 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400964
Nicolas Capens9edebd62013-08-06 10:59:10 -0400965 if (textureFunction->method == TextureFunction::LOD0)
966 {
967 out << " uint mip = 0;\n";
968 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400969 else if (textureFunction->method == TextureFunction::LOD0BIAS)
970 {
971 out << " uint mip = bias;\n";
972 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400973 else
974 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200975
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200976 out << " " << textureReference
977 << ".GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400978 if (textureFunction->method == TextureFunction::IMPLICIT ||
979 textureFunction->method == TextureFunction::BIAS)
980 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200981 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400982 " float dx = length(ddx(tSized));\n"
983 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500984 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400985
986 if (textureFunction->method == TextureFunction::BIAS)
987 {
988 out << " lod += bias;\n";
989 }
990 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500991 else if (textureFunction->method == TextureFunction::GRAD)
992 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200993 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500994 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400995
996 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
997 }
998
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200999 out << " " << textureReference
1000 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001001 }
1002 else
1003 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001004 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001005
Nicolas Capens9edebd62013-08-06 10:59:10 -04001006 if (textureFunction->method == TextureFunction::LOD0)
1007 {
1008 out << " uint mip = 0;\n";
1009 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001010 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1011 {
1012 out << " uint mip = bias;\n";
1013 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001014 else
1015 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001016 out << " " << textureReference
1017 << ".GetDimensions(0, width, height, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001018
Nicolas Capens9edebd62013-08-06 10:59:10 -04001019 if (textureFunction->method == TextureFunction::IMPLICIT ||
1020 textureFunction->method == TextureFunction::BIAS)
1021 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001022 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001023 " float dx = length(ddx(tSized));\n"
1024 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001025 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001026
1027 if (textureFunction->method == TextureFunction::BIAS)
1028 {
1029 out << " lod += bias;\n";
1030 }
1031 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001032 else if (textureFunction->method == TextureFunction::GRAD)
1033 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001034 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001035 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001036
1037 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1038 }
1039
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001040 out << " " << textureReference
1041 << ".GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001042 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001043 }
1044 else if (IsSampler3D(textureFunction->sampler))
1045 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001046 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001047
Nicolas Capens9edebd62013-08-06 10:59:10 -04001048 if (textureFunction->method == TextureFunction::LOD0)
1049 {
1050 out << " uint mip = 0;\n";
1051 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001052 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1053 {
1054 out << " uint mip = bias;\n";
1055 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001056 else
1057 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001058 out << " " << textureReference
1059 << ".GetDimensions(0, width, height, depth, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001060
Nicolas Capens9edebd62013-08-06 10:59:10 -04001061 if (textureFunction->method == TextureFunction::IMPLICIT ||
1062 textureFunction->method == TextureFunction::BIAS)
1063 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001064 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1065 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001066 " float dx = length(ddx(tSized));\n"
1067 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001068 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001069
1070 if (textureFunction->method == TextureFunction::BIAS)
1071 {
1072 out << " lod += bias;\n";
1073 }
1074 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001075 else if (textureFunction->method == TextureFunction::GRAD)
1076 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001077 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001078 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001079
1080 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1081 }
1082
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001083 out << " " << textureReference
1084 << ".GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001085 }
1086 else UNREACHABLE();
1087 }
1088
1089 out << " return ";
1090
1091 // HLSL intrinsic
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001092 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001093 {
1094 switch(textureFunction->sampler)
1095 {
1096 case EbtSampler2D: out << "tex2D"; break;
1097 case EbtSamplerCube: out << "texCUBE"; break;
1098 default: UNREACHABLE();
1099 }
1100
Nicolas Capens75fb4752013-07-10 15:14:47 -04001101 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001102 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001103 case TextureFunction::IMPLICIT:
1104 out << "(" << samplerReference << ", ";
1105 break;
1106 case TextureFunction::BIAS:
1107 out << "bias(" << samplerReference << ", ";
1108 break;
1109 case TextureFunction::LOD:
1110 out << "lod(" << samplerReference << ", ";
1111 break;
1112 case TextureFunction::LOD0:
1113 out << "lod(" << samplerReference << ", ";
1114 break;
1115 case TextureFunction::LOD0BIAS:
1116 out << "lod(" << samplerReference << ", ";
1117 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001118 default: UNREACHABLE();
1119 }
1120 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001121 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001122 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001123 if (textureFunction->method == TextureFunction::GRAD)
1124 {
1125 if (IsIntegerSampler(textureFunction->sampler))
1126 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001127 out << "" << textureReference << ".Load(";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001128 }
1129 else if (IsShadowSampler(textureFunction->sampler))
1130 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001131 out << "" << textureReference << ".SampleCmpLevelZero(" << samplerReference
1132 << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001133 }
1134 else
1135 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001136 out << "" << textureReference << ".SampleGrad(" << samplerReference << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001137 }
1138 }
1139 else if (IsIntegerSampler(textureFunction->sampler) ||
1140 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001141 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001142 out << "" << textureReference << ".Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001143 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001144 else if (IsShadowSampler(textureFunction->sampler))
1145 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001146 switch(textureFunction->method)
1147 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001148 case TextureFunction::IMPLICIT:
1149 out << "" << textureReference << ".SampleCmp(" << samplerReference
1150 << ", ";
1151 break;
1152 case TextureFunction::BIAS:
1153 out << "" << textureReference << ".SampleCmp(" << samplerReference
1154 << ", ";
1155 break;
1156 case TextureFunction::LOD:
1157 out << "" << textureReference << ".SampleCmp(" << samplerReference
1158 << ", ";
1159 break;
1160 case TextureFunction::LOD0:
1161 out << "" << textureReference << ".SampleCmpLevelZero("
1162 << samplerReference << ", ";
1163 break;
1164 case TextureFunction::LOD0BIAS:
1165 out << "" << textureReference << ".SampleCmpLevelZero("
1166 << samplerReference << ", ";
1167 break;
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001168 default: UNREACHABLE();
1169 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001170 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001171 else
1172 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001173 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001174 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001175 case TextureFunction::IMPLICIT:
1176 out << "" << textureReference << ".Sample(" << samplerReference << ", ";
1177 break;
1178 case TextureFunction::BIAS:
1179 out << "" << textureReference << ".SampleBias(" << samplerReference
1180 << ", ";
1181 break;
1182 case TextureFunction::LOD:
1183 out << "" << textureReference << ".SampleLevel(" << samplerReference
1184 << ", ";
1185 break;
1186 case TextureFunction::LOD0:
1187 out << "" << textureReference << ".SampleLevel(" << samplerReference
1188 << ", ";
1189 break;
1190 case TextureFunction::LOD0BIAS:
1191 out << "" << textureReference << ".SampleLevel(" << samplerReference
1192 << ", ";
1193 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001194 default: UNREACHABLE();
1195 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001196 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001197 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001198 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001199
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001200 // Integer sampling requires integer addresses
1201 TString addressx = "";
1202 TString addressy = "";
1203 TString addressz = "";
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001204 TString close = "";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001205
Nicolas Capensfc014542014-02-18 14:47:13 -05001206 if (IsIntegerSampler(textureFunction->sampler) ||
1207 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001208 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001209 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001210 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001211 case 2: out << "int3("; break;
1212 case 3: out << "int4("; break;
1213 default: UNREACHABLE();
1214 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001215
Nicolas Capensfc014542014-02-18 14:47:13 -05001216 // Convert from normalized floating-point to integer
1217 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001218 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001219 addressx = "int(floor(width * frac((";
1220 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001221
Nicolas Capensfc014542014-02-18 14:47:13 -05001222 if (IsSamplerArray(textureFunction->sampler))
1223 {
1224 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1225 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001226 else if (IsSamplerCube(textureFunction->sampler))
Nicolas Capens0027fa92014-02-20 14:26:42 -05001227 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001228 addressz = "((((";
Nicolas Capens0027fa92014-02-20 14:26:42 -05001229 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001230 else
1231 {
1232 addressz = "int(floor(depth * frac((";
1233 }
1234
1235 close = "))))";
Nicolas Capensfc014542014-02-18 14:47:13 -05001236 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001237 }
1238 else
1239 {
1240 switch(hlslCoords)
1241 {
1242 case 2: out << "float2("; break;
1243 case 3: out << "float3("; break;
1244 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001245 default: UNREACHABLE();
1246 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001247 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001248
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001249 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001250
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001251 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001252 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001253 switch(textureFunction->coords)
1254 {
1255 case 3: proj = " / t.z"; break;
1256 case 4: proj = " / t.w"; break;
1257 default: UNREACHABLE();
1258 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001259 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001260
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001261 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001262
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001263 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001264 {
1265 if (hlslCoords >= 3)
1266 {
1267 if (textureFunction->coords < 3)
1268 {
1269 out << ", 0";
1270 }
1271 else
1272 {
1273 out << ", t.z" + proj;
1274 }
1275 }
1276
1277 if (hlslCoords == 4)
1278 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001279 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001280 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001281 case TextureFunction::BIAS: out << ", bias"; break;
1282 case TextureFunction::LOD: out << ", lod"; break;
1283 case TextureFunction::LOD0: out << ", 0"; break;
1284 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001285 default: UNREACHABLE();
1286 }
1287 }
1288
1289 out << "));\n";
1290 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001291 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001292 {
1293 if (hlslCoords >= 3)
1294 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001295 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1296 {
1297 out << ", face";
1298 }
1299 else
1300 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001301 out << ", " + addressz + ("t.z" + proj) + close;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001302 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001303 }
1304
Nicolas Capensd11d5492014-02-19 17:06:10 -05001305 if (textureFunction->method == TextureFunction::GRAD)
1306 {
1307 if (IsIntegerSampler(textureFunction->sampler))
1308 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001309 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001310 }
1311 else if (IsShadowSampler(textureFunction->sampler))
1312 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001313 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001314 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001315 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001316 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1317 // The resulting third component of P' in the shadow forms is used as Dref
1318 out << "), t.z" << proj;
1319 }
1320 else
1321 {
1322 switch(textureFunction->coords)
1323 {
1324 case 3: out << "), t.z"; break;
1325 case 4: out << "), t.w"; break;
1326 default: UNREACHABLE();
1327 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001328 }
1329 }
1330 else
1331 {
1332 out << "), ddx, ddy";
1333 }
1334 }
1335 else if (IsIntegerSampler(textureFunction->sampler) ||
1336 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001337 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001338 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001339 }
1340 else if (IsShadowSampler(textureFunction->sampler))
1341 {
1342 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001343 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001344 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001345 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1346 // The resulting third component of P' in the shadow forms is used as Dref
1347 out << "), t.z" << proj;
1348 }
1349 else
1350 {
1351 switch(textureFunction->coords)
1352 {
1353 case 3: out << "), t.z"; break;
1354 case 4: out << "), t.w"; break;
1355 default: UNREACHABLE();
1356 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001357 }
1358 }
1359 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001360 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001361 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001362 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001363 case TextureFunction::IMPLICIT: out << ")"; break;
1364 case TextureFunction::BIAS: out << "), bias"; break;
1365 case TextureFunction::LOD: out << "), lod"; break;
1366 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001367 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001368 default: UNREACHABLE();
1369 }
1370 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001371
1372 if (textureFunction->offset)
1373 {
1374 out << ", offset";
1375 }
1376
1377 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001378 }
1379 else UNREACHABLE();
1380 }
1381
1382 out << "\n"
1383 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001384 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001385 }
1386
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001387 if (mUsesFragCoord)
1388 {
1389 out << "#define GL_USES_FRAG_COORD\n";
1390 }
1391
1392 if (mUsesPointCoord)
1393 {
1394 out << "#define GL_USES_POINT_COORD\n";
1395 }
1396
1397 if (mUsesFrontFacing)
1398 {
1399 out << "#define GL_USES_FRONT_FACING\n";
1400 }
1401
1402 if (mUsesPointSize)
1403 {
1404 out << "#define GL_USES_POINT_SIZE\n";
1405 }
1406
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001407 if (mUsesFragDepth)
1408 {
1409 out << "#define GL_USES_FRAG_DEPTH\n";
1410 }
1411
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001412 if (mUsesDepthRange)
1413 {
1414 out << "#define GL_USES_DEPTH_RANGE\n";
1415 }
1416
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001417 if (mUsesXor)
1418 {
1419 out << "bool xor(bool p, bool q)\n"
1420 "{\n"
1421 " return (p || q) && !(p && q);\n"
1422 "}\n"
1423 "\n";
1424 }
1425
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001426 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001427}
1428
1429void OutputHLSL::visitSymbol(TIntermSymbol *node)
1430{
Jamie Madill32aab012015-01-27 14:12:26 -05001431 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001432
Jamie Madill570e04d2013-06-21 09:15:33 -04001433 // Handle accessing std140 structs by value
1434 if (mFlaggedStructMappedNames.count(node) > 0)
1435 {
1436 out << mFlaggedStructMappedNames[node];
1437 return;
1438 }
1439
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001440 TString name = node->getSymbol();
1441
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001442 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001443 {
1444 mUsesDepthRange = true;
1445 out << name;
1446 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001447 else
1448 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001449 TQualifier qualifier = node->getQualifier();
1450
1451 if (qualifier == EvqUniform)
1452 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001453 const TType &nodeType = node->getType();
1454 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001455
1456 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001457 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001458 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001459 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001460 else
1461 {
1462 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001463 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001464
Jamie Madill2e295e22015-04-29 10:41:33 -04001465 ensureStructDefined(nodeType);
1466
Olli Etuaho96963162016-03-21 11:54:33 +02001467 const TName &nameWithMetadata = node->getName();
1468 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001469 }
Jamie Madill19571812013-08-12 15:26:34 -07001470 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001471 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001472 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001473 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001474 }
Jamie Madill033dae62014-06-18 12:56:28 -04001475 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001476 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001477 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001478 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001479 }
Jamie Madill19571812013-08-12 15:26:34 -07001480 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001481 {
1482 mReferencedOutputVariables[name] = node;
1483 out << "out_" << name;
1484 }
1485 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001486 {
1487 out << "gl_Color[0]";
1488 mUsesFragColor = true;
1489 }
1490 else if (qualifier == EvqFragData)
1491 {
1492 out << "gl_Color";
1493 mUsesFragData = true;
1494 }
1495 else if (qualifier == EvqFragCoord)
1496 {
1497 mUsesFragCoord = true;
1498 out << name;
1499 }
1500 else if (qualifier == EvqPointCoord)
1501 {
1502 mUsesPointCoord = true;
1503 out << name;
1504 }
1505 else if (qualifier == EvqFrontFacing)
1506 {
1507 mUsesFrontFacing = true;
1508 out << name;
1509 }
1510 else if (qualifier == EvqPointSize)
1511 {
1512 mUsesPointSize = true;
1513 out << name;
1514 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001515 else if (qualifier == EvqInstanceID)
1516 {
1517 mUsesInstanceID = true;
1518 out << name;
1519 }
Corentin Wallezb076add2016-01-11 16:45:46 -05001520 else if (qualifier == EvqVertexID)
1521 {
1522 mUsesVertexID = true;
1523 out << name;
1524 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001525 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001526 {
1527 mUsesFragDepth = true;
1528 out << "gl_Depth";
1529 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001530 else
1531 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001532 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001533 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001534 }
1535}
1536
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001537void OutputHLSL::visitRaw(TIntermRaw *node)
1538{
Jamie Madill32aab012015-01-27 14:12:26 -05001539 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001540}
1541
Olli Etuaho7fb49552015-03-18 17:27:44 +02001542void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1543{
1544 if (type.isScalar() && !type.isArray())
1545 {
1546 if (op == EOpEqual)
1547 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001548 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001549 }
1550 else
1551 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001552 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001553 }
1554 }
1555 else
1556 {
1557 if (visit == PreVisit && op == EOpNotEqual)
1558 {
1559 out << "!";
1560 }
1561
1562 if (type.isArray())
1563 {
1564 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001565 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001566 }
1567 else if (type.getBasicType() == EbtStruct)
1568 {
1569 const TStructure &structure = *type.getStruct();
1570 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001571 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001572 }
1573 else
1574 {
1575 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001576 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001577 }
1578 }
1579}
1580
Olli Etuaho96963162016-03-21 11:54:33 +02001581bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
1582{
1583 // Inside InVisit the current node is already in the path.
1584 const unsigned int initialN = visit == InVisit ? 1u : 0u;
1585 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
1586 {
1587 TIntermNode *ancestor = getAncestorNode(n);
1588 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1589 if (ancestorBinary == nullptr)
1590 {
1591 return false;
1592 }
1593 switch (ancestorBinary->getOp())
1594 {
1595 case EOpIndexDirectStruct:
1596 {
1597 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1598 const TIntermConstantUnion *index =
1599 ancestorBinary->getRight()->getAsConstantUnion();
1600 const TField *field = structure->fields()[index->getIConst(0)];
1601 if (IsSampler(field->type()->getBasicType()))
1602 {
1603 return true;
1604 }
1605 break;
1606 }
1607 case EOpIndexDirect:
1608 break;
1609 default:
1610 // Returning a sampler from indirect indexing is not supported.
1611 return false;
1612 }
1613 }
1614 return false;
1615}
1616
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001617bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1618{
Jamie Madill32aab012015-01-27 14:12:26 -05001619 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001620
Jamie Madill570e04d2013-06-21 09:15:33 -04001621 // Handle accessing std140 structs by value
1622 if (mFlaggedStructMappedNames.count(node) > 0)
1623 {
1624 out << mFlaggedStructMappedNames[node];
1625 return false;
1626 }
1627
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001628 switch (node->getOp())
1629 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001630 case EOpAssign:
1631 if (node->getLeft()->isArray())
1632 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001633 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1634 if (rightAgg != nullptr && rightAgg->isConstructor())
1635 {
1636 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1637 out << functionName << "(";
1638 node->getLeft()->traverse(this);
1639 TIntermSequence *seq = rightAgg->getSequence();
1640 for (auto &arrayElement : *seq)
1641 {
1642 out << ", ";
1643 arrayElement->traverse(this);
1644 }
1645 out << ")";
1646 return false;
1647 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001648 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1649 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1650
1651 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001652 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001653 }
1654 else
1655 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001656 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001657 }
1658 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001659 case EOpInitialize:
1660 if (visit == PreVisit)
1661 {
1662 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1663 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1664 // new variable is created before the assignment is evaluated), so we need to convert
1665 // this to "float t = x, x = t;".
1666
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001667 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001668 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001669 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001670
Jamie Madill37997142015-01-28 10:06:34 -05001671 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1672 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001673 {
Jamie Madill37997142015-01-28 10:06:34 -05001674 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001675 // after we initialize uniforms.
1676 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1677 deferredInit->setLeft(node->getLeft());
1678 deferredInit->setRight(node->getRight());
1679 deferredInit->setType(node->getType());
1680 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001681 const TString &initString = initializer(node->getType());
1682 node->setRight(new TIntermRaw(node->getType(), initString));
1683 }
1684 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1685 {
1686 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001687 return false;
1688 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001689 else if (writeConstantInitialization(out, symbolNode, expression))
1690 {
1691 return false;
1692 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001693 }
1694 else if (visit == InVisit)
1695 {
1696 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001697 }
1698 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001699 case EOpAddAssign:
1700 outputTriplet(out, visit, "(", " += ", ")");
1701 break;
1702 case EOpSubAssign:
1703 outputTriplet(out, visit, "(", " -= ", ")");
1704 break;
1705 case EOpMulAssign:
1706 outputTriplet(out, visit, "(", " *= ", ")");
1707 break;
1708 case EOpVectorTimesScalarAssign:
1709 outputTriplet(out, visit, "(", " *= ", ")");
1710 break;
1711 case EOpMatrixTimesScalarAssign:
1712 outputTriplet(out, visit, "(", " *= ", ")");
1713 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001714 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001715 if (visit == PreVisit)
1716 {
1717 out << "(";
1718 }
1719 else if (visit == InVisit)
1720 {
1721 out << " = mul(";
1722 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001723 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001724 }
1725 else
1726 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001727 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001728 }
1729 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001730 case EOpMatrixTimesMatrixAssign:
1731 if (visit == PreVisit)
1732 {
1733 out << "(";
1734 }
1735 else if (visit == InVisit)
1736 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001737 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001738 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001739 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001740 }
1741 else
1742 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001743 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001744 }
1745 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001746 case EOpDivAssign:
1747 outputTriplet(out, visit, "(", " /= ", ")");
1748 break;
1749 case EOpIModAssign:
1750 outputTriplet(out, visit, "(", " %= ", ")");
1751 break;
1752 case EOpBitShiftLeftAssign:
1753 outputTriplet(out, visit, "(", " <<= ", ")");
1754 break;
1755 case EOpBitShiftRightAssign:
1756 outputTriplet(out, visit, "(", " >>= ", ")");
1757 break;
1758 case EOpBitwiseAndAssign:
1759 outputTriplet(out, visit, "(", " &= ", ")");
1760 break;
1761 case EOpBitwiseXorAssign:
1762 outputTriplet(out, visit, "(", " ^= ", ")");
1763 break;
1764 case EOpBitwiseOrAssign:
1765 outputTriplet(out, visit, "(", " |= ", ")");
1766 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001767 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001768 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001769 const TType& leftType = node->getLeft()->getType();
1770 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001771 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001772 if (visit == PreVisit)
1773 {
1774 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1775 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001776 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001777 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001778 return false;
1779 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001780 }
Olli Etuaho96963162016-03-21 11:54:33 +02001781 else if (ancestorEvaluatesToSamplerInStruct(visit))
1782 {
1783 // All parts of an expression that access a sampler in a struct need to use _ as
1784 // separator to access the sampler variable that has been moved out of the struct.
1785 outputTriplet(out, visit, "", "_", "");
1786 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001787 else
1788 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001789 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001790 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001791 }
1792 break;
1793 case EOpIndexIndirect:
1794 // We do not currently support indirect references to interface blocks
1795 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001796 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001797 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001798 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001799 {
1800 const TStructure* structure = node->getLeft()->getType().getStruct();
1801 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1802 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001803
Olli Etuaho96963162016-03-21 11:54:33 +02001804 // In cases where indexing returns a sampler, we need to access the sampler variable
1805 // that has been moved out of the struct.
1806 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1807 if (visit == PreVisit && indexingReturnsSampler)
1808 {
1809 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1810 // This prefix is only output at the beginning of the indexing expression, which
1811 // may have multiple parts.
1812 out << "angle";
1813 }
1814 if (!indexingReturnsSampler)
1815 {
1816 // All parts of an expression that access a sampler in a struct need to use _ as
1817 // separator to access the sampler variable that has been moved out of the struct.
1818 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1819 }
1820 if (visit == InVisit)
1821 {
1822 if (indexingReturnsSampler)
1823 {
1824 out << "_" + field->name();
1825 }
1826 else
1827 {
1828 out << "." + DecorateField(field->name(), *structure);
1829 }
1830
1831 return false;
1832 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001833 }
1834 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001835 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001836 if (visit == InVisit)
1837 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001838 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1839 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1840 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001841 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001842
1843 return false;
1844 }
1845 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846 case EOpVectorSwizzle:
1847 if (visit == InVisit)
1848 {
1849 out << ".";
1850
1851 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1852
1853 if (swizzle)
1854 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001855 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001857 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001858 {
1859 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1860
1861 if (element)
1862 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001863 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864
1865 switch (i)
1866 {
1867 case 0: out << "x"; break;
1868 case 1: out << "y"; break;
1869 case 2: out << "z"; break;
1870 case 3: out << "w"; break;
1871 default: UNREACHABLE();
1872 }
1873 }
1874 else UNREACHABLE();
1875 }
1876 }
1877 else UNREACHABLE();
1878
1879 return false; // Fully processed
1880 }
1881 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001882 case EOpAdd:
1883 outputTriplet(out, visit, "(", " + ", ")");
1884 break;
1885 case EOpSub:
1886 outputTriplet(out, visit, "(", " - ", ")");
1887 break;
1888 case EOpMul:
1889 outputTriplet(out, visit, "(", " * ", ")");
1890 break;
1891 case EOpDiv:
1892 outputTriplet(out, visit, "(", " / ", ")");
1893 break;
1894 case EOpIMod:
1895 outputTriplet(out, visit, "(", " % ", ")");
1896 break;
1897 case EOpBitShiftLeft:
1898 outputTriplet(out, visit, "(", " << ", ")");
1899 break;
1900 case EOpBitShiftRight:
1901 outputTriplet(out, visit, "(", " >> ", ")");
1902 break;
1903 case EOpBitwiseAnd:
1904 outputTriplet(out, visit, "(", " & ", ")");
1905 break;
1906 case EOpBitwiseXor:
1907 outputTriplet(out, visit, "(", " ^ ", ")");
1908 break;
1909 case EOpBitwiseOr:
1910 outputTriplet(out, visit, "(", " | ", ")");
1911 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001912 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001913 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001914 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001915 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001916 case EOpLessThan:
1917 outputTriplet(out, visit, "(", " < ", ")");
1918 break;
1919 case EOpGreaterThan:
1920 outputTriplet(out, visit, "(", " > ", ")");
1921 break;
1922 case EOpLessThanEqual:
1923 outputTriplet(out, visit, "(", " <= ", ")");
1924 break;
1925 case EOpGreaterThanEqual:
1926 outputTriplet(out, visit, "(", " >= ", ")");
1927 break;
1928 case EOpVectorTimesScalar:
1929 outputTriplet(out, visit, "(", " * ", ")");
1930 break;
1931 case EOpMatrixTimesScalar:
1932 outputTriplet(out, visit, "(", " * ", ")");
1933 break;
1934 case EOpVectorTimesMatrix:
1935 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1936 break;
1937 case EOpMatrixTimesVector:
1938 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1939 break;
1940 case EOpMatrixTimesMatrix:
1941 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1942 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001943 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001944 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1945 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001946 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001947 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001948 case EOpLogicalXor:
1949 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001950 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001951 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001952 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001953 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1954 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001955 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001956 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957 default: UNREACHABLE();
1958 }
1959
1960 return true;
1961}
1962
1963bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1964{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001965 TInfoSinkBase &out = getInfoSink();
1966
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967 switch (node->getOp())
1968 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001969 case EOpNegative:
1970 outputTriplet(out, visit, "(-", "", ")");
1971 break;
1972 case EOpPositive:
1973 outputTriplet(out, visit, "(+", "", ")");
1974 break;
1975 case EOpVectorLogicalNot:
1976 outputTriplet(out, visit, "(!", "", ")");
1977 break;
1978 case EOpLogicalNot:
1979 outputTriplet(out, visit, "(!", "", ")");
1980 break;
1981 case EOpBitwiseNot:
1982 outputTriplet(out, visit, "(~", "", ")");
1983 break;
1984 case EOpPostIncrement:
1985 outputTriplet(out, visit, "(", "", "++)");
1986 break;
1987 case EOpPostDecrement:
1988 outputTriplet(out, visit, "(", "", "--)");
1989 break;
1990 case EOpPreIncrement:
1991 outputTriplet(out, visit, "(++", "", ")");
1992 break;
1993 case EOpPreDecrement:
1994 outputTriplet(out, visit, "(--", "", ")");
1995 break;
1996 case EOpRadians:
1997 outputTriplet(out, visit, "radians(", "", ")");
1998 break;
1999 case EOpDegrees:
2000 outputTriplet(out, visit, "degrees(", "", ")");
2001 break;
2002 case EOpSin:
2003 outputTriplet(out, visit, "sin(", "", ")");
2004 break;
2005 case EOpCos:
2006 outputTriplet(out, visit, "cos(", "", ")");
2007 break;
2008 case EOpTan:
2009 outputTriplet(out, visit, "tan(", "", ")");
2010 break;
2011 case EOpAsin:
2012 outputTriplet(out, visit, "asin(", "", ")");
2013 break;
2014 case EOpAcos:
2015 outputTriplet(out, visit, "acos(", "", ")");
2016 break;
2017 case EOpAtan:
2018 outputTriplet(out, visit, "atan(", "", ")");
2019 break;
2020 case EOpSinh:
2021 outputTriplet(out, visit, "sinh(", "", ")");
2022 break;
2023 case EOpCosh:
2024 outputTriplet(out, visit, "cosh(", "", ")");
2025 break;
2026 case EOpTanh:
2027 outputTriplet(out, visit, "tanh(", "", ")");
2028 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002029 case EOpAsinh:
2030 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002031 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002032 break;
2033 case EOpAcosh:
2034 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002035 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002036 break;
2037 case EOpAtanh:
2038 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002039 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002040 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002041 case EOpExp:
2042 outputTriplet(out, visit, "exp(", "", ")");
2043 break;
2044 case EOpLog:
2045 outputTriplet(out, visit, "log(", "", ")");
2046 break;
2047 case EOpExp2:
2048 outputTriplet(out, visit, "exp2(", "", ")");
2049 break;
2050 case EOpLog2:
2051 outputTriplet(out, visit, "log2(", "", ")");
2052 break;
2053 case EOpSqrt:
2054 outputTriplet(out, visit, "sqrt(", "", ")");
2055 break;
2056 case EOpInverseSqrt:
2057 outputTriplet(out, visit, "rsqrt(", "", ")");
2058 break;
2059 case EOpAbs:
2060 outputTriplet(out, visit, "abs(", "", ")");
2061 break;
2062 case EOpSign:
2063 outputTriplet(out, visit, "sign(", "", ")");
2064 break;
2065 case EOpFloor:
2066 outputTriplet(out, visit, "floor(", "", ")");
2067 break;
2068 case EOpTrunc:
2069 outputTriplet(out, visit, "trunc(", "", ")");
2070 break;
2071 case EOpRound:
2072 outputTriplet(out, visit, "round(", "", ")");
2073 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08002074 case EOpRoundEven:
2075 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002076 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08002077 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002078 case EOpCeil:
2079 outputTriplet(out, visit, "ceil(", "", ")");
2080 break;
2081 case EOpFract:
2082 outputTriplet(out, visit, "frac(", "", ")");
2083 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05302084 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002085 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05302086 mRequiresIEEEStrictCompiling = true;
2087 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002088 case EOpIsInf:
2089 outputTriplet(out, visit, "isinf(", "", ")");
2090 break;
2091 case EOpFloatBitsToInt:
2092 outputTriplet(out, visit, "asint(", "", ")");
2093 break;
2094 case EOpFloatBitsToUint:
2095 outputTriplet(out, visit, "asuint(", "", ")");
2096 break;
2097 case EOpIntBitsToFloat:
2098 outputTriplet(out, visit, "asfloat(", "", ")");
2099 break;
2100 case EOpUintBitsToFloat:
2101 outputTriplet(out, visit, "asfloat(", "", ")");
2102 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02002103 case EOpPackSnorm2x16:
2104 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002105 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002106 break;
2107 case EOpPackUnorm2x16:
2108 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002109 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002110 break;
2111 case EOpPackHalf2x16:
2112 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002113 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002114 break;
2115 case EOpUnpackSnorm2x16:
2116 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002117 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002118 break;
2119 case EOpUnpackUnorm2x16:
2120 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002121 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002122 break;
2123 case EOpUnpackHalf2x16:
2124 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002125 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002126 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002127 case EOpLength:
2128 outputTriplet(out, visit, "length(", "", ")");
2129 break;
2130 case EOpNormalize:
2131 outputTriplet(out, visit, "normalize(", "", ")");
2132 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002133 case EOpDFdx:
2134 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2135 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002136 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002137 }
2138 else
2139 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002140 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002141 }
2142 break;
2143 case EOpDFdy:
2144 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2145 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002146 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002147 }
2148 else
2149 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002150 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002151 }
2152 break;
2153 case EOpFwidth:
2154 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2155 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002156 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002157 }
2158 else
2159 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002160 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002161 }
2162 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002163 case EOpTranspose:
2164 outputTriplet(out, visit, "transpose(", "", ")");
2165 break;
2166 case EOpDeterminant:
2167 outputTriplet(out, visit, "determinant(transpose(", "", "))");
2168 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002169 case EOpInverse:
2170 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002171 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002172 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002173
Jamie Madill8c46ab12015-12-07 16:39:19 -05002174 case EOpAny:
2175 outputTriplet(out, visit, "any(", "", ")");
2176 break;
2177 case EOpAll:
2178 outputTriplet(out, visit, "all(", "", ")");
2179 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180 default: UNREACHABLE();
2181 }
2182
2183 return true;
2184}
2185
Olli Etuaho96963162016-03-21 11:54:33 +02002186TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
2187{
2188 if (node->getAsSymbolNode())
2189 {
2190 return node->getAsSymbolNode()->getSymbol();
2191 }
2192 TIntermBinary *nodeBinary = node->getAsBinaryNode();
2193 switch (nodeBinary->getOp())
2194 {
2195 case EOpIndexDirect:
2196 {
2197 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
2198
2199 TInfoSinkBase prefixSink;
2200 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
2201 return TString(prefixSink.c_str());
2202 }
2203 case EOpIndexDirectStruct:
2204 {
2205 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
2206 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
2207 const TField *field = s->fields()[index];
2208
2209 TInfoSinkBase prefixSink;
2210 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
2211 << field->name();
2212 return TString(prefixSink.c_str());
2213 }
2214 default:
2215 UNREACHABLE();
2216 return TString("");
2217 }
2218}
2219
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002220bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2221{
Jamie Madill32aab012015-01-27 14:12:26 -05002222 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002223
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002224 switch (node->getOp())
2225 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002226 case EOpSequence:
2227 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002228 if (mInsideFunction)
2229 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002230 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002231 out << "{\n";
2232 }
2233
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002234 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002235 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002236 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002237
Olli Etuahoa6f22092015-05-08 18:31:10 +03002238 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002239
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002240 // Don't output ; after case labels, they're terminated by :
2241 // This is needed especially since outputting a ; after a case statement would turn empty
2242 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03002243 // Also no need to output ; after selection (if) statements or sequences. This is done just
2244 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002245 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
2246 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03002247 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002248 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002249 }
2250
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002251 if (mInsideFunction)
2252 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002253 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002254 out << "}\n";
2255 }
2256
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002257 return false;
2258 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 case EOpDeclaration:
2260 if (visit == PreVisit)
2261 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002262 TIntermSequence *sequence = node->getSequence();
2263 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03002264 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002266 if (variable &&
2267 (variable->getQualifier() == EvqTemporary ||
2268 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002270 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00002271
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002272 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002273 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002274 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002275 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002276 out << "static ";
2277 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002278
Olli Etuahoa6f22092015-05-08 18:31:10 +03002279 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04002280
Olli Etuahoa6f22092015-05-08 18:31:10 +03002281 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04002282
Olli Etuahoa6f22092015-05-08 18:31:10 +03002283 if (symbol)
2284 {
2285 symbol->traverse(this);
2286 out << ArrayString(symbol->getType());
2287 out << " = " + initializer(symbol->getType());
2288 }
2289 else
2290 {
2291 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002294 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2295 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002296 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002297 }
2298 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 }
Jamie Madill033dae62014-06-18 12:56:28 -04002300 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002301 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002302 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002303 {
2304 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2305
2306 if (symbol)
2307 {
2308 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2309 mReferencedVaryings[symbol->getSymbol()] = symbol;
2310 }
2311 else
2312 {
2313 (*sit)->traverse(this);
2314 }
2315 }
2316 }
2317
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318 return false;
2319 }
2320 else if (visit == InVisit)
2321 {
2322 out << ", ";
2323 }
2324 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04002325 case EOpInvariantDeclaration:
2326 // Do not do any translation
2327 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002328 case EOpPrototype:
2329 if (visit == PreVisit)
2330 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002331 size_t index = mCallDag.findIndex(node);
2332 // Skip the prototype if it is not implemented (and thus not used)
2333 if (index == CallDAG::InvalidIndex)
2334 {
2335 return false;
2336 }
2337
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002338 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002339
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002340 TString name = DecorateFunctionIfNeeded(node->getNameObj());
2341 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
2342 << (mOutputLod0Function ? "Lod0(" : "(");
2343
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002344 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002345 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002346 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002347
2348 if (symbol)
2349 {
2350 out << argumentString(symbol);
2351
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002352 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002353 {
2354 out << ", ";
2355 }
2356 }
2357 else UNREACHABLE();
2358 }
2359
2360 out << ");\n";
2361
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002362 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002363 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2364 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002365 {
2366 mOutputLod0Function = true;
2367 node->traverse(this);
2368 mOutputLod0Function = false;
2369 }
2370
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002371 return false;
2372 }
2373 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002374 case EOpComma:
2375 outputTriplet(out, visit, "(", ", ", ")");
2376 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002377 case EOpFunction:
2378 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002379 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002380 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381
Corentin Wallez1239ee92015-03-19 14:38:02 -07002382 size_t index = mCallDag.findIndex(node);
2383 ASSERT(index != CallDAG::InvalidIndex);
2384 mCurrentFunctionMetadata = &mASTMetadataList[index];
2385
Jamie Madill033dae62014-06-18 12:56:28 -04002386 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002387
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002388 TIntermSequence *sequence = node->getSequence();
2389 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
2390
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002391 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002393 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002395 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002397 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002398 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002399 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002400
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002401 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002402 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002403 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002404
2405 if (symbol)
2406 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002407 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002408
2409 out << argumentString(symbol);
2410
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002411 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002412 {
2413 out << ", ";
2414 }
2415 }
2416 else UNREACHABLE();
2417 }
2418
Olli Etuaho4785fec2015-05-18 16:09:37 +03002419 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002420
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002421 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002422 {
2423 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002424 TIntermNode *body = (*sequence)[1];
2425 // The function body node will output braces.
2426 ASSERT(IsSequence(body));
2427 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002428 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002430 else
2431 {
2432 out << "{}\n";
2433 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002434
Corentin Wallez1239ee92015-03-19 14:38:02 -07002435 mCurrentFunctionMetadata = nullptr;
2436
2437 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2438 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002439 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002440 ASSERT(name != "main");
2441 mOutputLod0Function = true;
2442 node->traverse(this);
2443 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002444 }
2445
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002446 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447 }
2448 break;
2449 case EOpFunctionCall:
2450 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002451 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002452
Corentin Wallez1239ee92015-03-19 14:38:02 -07002453 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002454 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002455 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002456 if (node->isArray())
2457 {
2458 UNIMPLEMENTED();
2459 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002460 size_t index = mCallDag.findIndex(node);
2461 ASSERT(index != CallDAG::InvalidIndex);
2462 lod0 &= mASTMetadataList[index].mNeedsLod0;
2463
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002464 out << DecorateFunctionIfNeeded(node->getNameObj());
2465 out << DisambiguateFunctionName(node->getSequence());
2466 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 }
2468 else
2469 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002470 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002471 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002472
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002473 TextureFunction textureFunction;
2474 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002475 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002476 textureFunction.method = TextureFunction::IMPLICIT;
2477 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002478 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002479
2480 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002481 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002482 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002483 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002484 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002485 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002486 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002487 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002488 }
Nicolas Capens46485082014-04-15 13:12:50 -04002489 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2490 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002491 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002492 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002493 }
Nicolas Capens46485082014-04-15 13:12:50 -04002494 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002495 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002496 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002497 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002498 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002499 else if (name == "textureSize")
2500 {
2501 textureFunction.method = TextureFunction::SIZE;
2502 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002503 else if (name == "textureOffset")
2504 {
2505 textureFunction.method = TextureFunction::IMPLICIT;
2506 textureFunction.offset = true;
2507 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002508 else if (name == "textureProjOffset")
2509 {
2510 textureFunction.method = TextureFunction::IMPLICIT;
2511 textureFunction.offset = true;
2512 textureFunction.proj = true;
2513 }
2514 else if (name == "textureLodOffset")
2515 {
2516 textureFunction.method = TextureFunction::LOD;
2517 textureFunction.offset = true;
2518 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002519 else if (name == "textureProjLodOffset")
2520 {
2521 textureFunction.method = TextureFunction::LOD;
2522 textureFunction.proj = true;
2523 textureFunction.offset = true;
2524 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002525 else if (name == "texelFetch")
2526 {
2527 textureFunction.method = TextureFunction::FETCH;
2528 }
2529 else if (name == "texelFetchOffset")
2530 {
2531 textureFunction.method = TextureFunction::FETCH;
2532 textureFunction.offset = true;
2533 }
Nicolas Capens46485082014-04-15 13:12:50 -04002534 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002535 {
2536 textureFunction.method = TextureFunction::GRAD;
2537 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002538 else if (name == "textureGradOffset")
2539 {
2540 textureFunction.method = TextureFunction::GRAD;
2541 textureFunction.offset = true;
2542 }
Nicolas Capens46485082014-04-15 13:12:50 -04002543 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002544 {
2545 textureFunction.method = TextureFunction::GRAD;
2546 textureFunction.proj = true;
2547 }
2548 else if (name == "textureProjGradOffset")
2549 {
2550 textureFunction.method = TextureFunction::GRAD;
2551 textureFunction.proj = true;
2552 textureFunction.offset = true;
2553 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002554 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002555
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002556 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002557 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002558 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2559
2560 if (textureFunction.offset)
2561 {
2562 mandatoryArgumentCount++;
2563 }
2564
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002565 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002566
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002567 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002568 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002569 if (bias)
2570 {
2571 textureFunction.method = TextureFunction::LOD0BIAS;
2572 }
2573 else
2574 {
2575 textureFunction.method = TextureFunction::LOD0;
2576 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002577 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002578 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002579 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002580 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002581 }
2582 }
2583
2584 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002585
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002586 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002587 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002588
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002589 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002590 {
Olli Etuaho96963162016-03-21 11:54:33 +02002591 TIntermTyped *typedArg = (*arg)->getAsTyped();
2592 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002593 {
2594 out << "texture_";
2595 (*arg)->traverse(this);
2596 out << ", sampler_";
2597 }
2598
2599 (*arg)->traverse(this);
2600
Olli Etuaho96963162016-03-21 11:54:33 +02002601 if (typedArg->getType().isStructureContainingSamplers())
2602 {
2603 const TType &argType = typedArg->getType();
2604 TVector<TIntermSymbol *> samplerSymbols;
2605 TString structName = samplerNamePrefixFromStruct(typedArg);
2606 argType.createSamplerSymbols("angle_" + structName, "",
2607 argType.isArray() ? argType.getArraySize() : 0,
2608 &samplerSymbols, nullptr);
2609 for (const TIntermSymbol *sampler : samplerSymbols)
2610 {
2611 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2612 {
2613 out << ", texture_" << sampler->getSymbol();
2614 out << ", sampler_" << sampler->getSymbol();
2615 }
2616 else
2617 {
2618 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
2619 // of D3D9, it's the sampler variable.
2620 out << ", " + sampler->getSymbol();
2621 }
2622 }
2623 }
2624
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002625 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002626 {
2627 out << ", ";
2628 }
2629 }
2630
2631 out << ")";
2632
2633 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634 }
2635 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002636 case EOpParameters:
2637 outputTriplet(out, visit, "(", ", ", ")\n{\n");
2638 break;
2639 case EOpConstructFloat:
2640 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
2641 break;
2642 case EOpConstructVec2:
2643 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
2644 break;
2645 case EOpConstructVec3:
2646 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
2647 break;
2648 case EOpConstructVec4:
2649 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
2650 break;
2651 case EOpConstructBool:
2652 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
2653 break;
2654 case EOpConstructBVec2:
2655 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
2656 break;
2657 case EOpConstructBVec3:
2658 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
2659 break;
2660 case EOpConstructBVec4:
2661 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
2662 break;
2663 case EOpConstructInt:
2664 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
2665 break;
2666 case EOpConstructIVec2:
2667 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
2668 break;
2669 case EOpConstructIVec3:
2670 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
2671 break;
2672 case EOpConstructIVec4:
2673 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
2674 break;
2675 case EOpConstructUInt:
2676 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
2677 break;
2678 case EOpConstructUVec2:
2679 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
2680 break;
2681 case EOpConstructUVec3:
2682 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
2683 break;
2684 case EOpConstructUVec4:
2685 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
2686 break;
2687 case EOpConstructMat2:
2688 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
2689 break;
2690 case EOpConstructMat2x3:
2691 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
2692 break;
2693 case EOpConstructMat2x4:
2694 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
2695 break;
2696 case EOpConstructMat3x2:
2697 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
2698 break;
2699 case EOpConstructMat3:
2700 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
2701 break;
2702 case EOpConstructMat3x4:
2703 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
2704 break;
2705 case EOpConstructMat4x2:
2706 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
2707 break;
2708 case EOpConstructMat4x3:
2709 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
2710 break;
2711 case EOpConstructMat4:
2712 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
2713 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002714 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002715 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002716 if (node->getType().isArray())
2717 {
2718 UNIMPLEMENTED();
2719 }
Jamie Madill033dae62014-06-18 12:56:28 -04002720 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002721 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002722 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002723 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002724 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002725 case EOpLessThan:
2726 outputTriplet(out, visit, "(", " < ", ")");
2727 break;
2728 case EOpGreaterThan:
2729 outputTriplet(out, visit, "(", " > ", ")");
2730 break;
2731 case EOpLessThanEqual:
2732 outputTriplet(out, visit, "(", " <= ", ")");
2733 break;
2734 case EOpGreaterThanEqual:
2735 outputTriplet(out, visit, "(", " >= ", ")");
2736 break;
2737 case EOpVectorEqual:
2738 outputTriplet(out, visit, "(", " == ", ")");
2739 break;
2740 case EOpVectorNotEqual:
2741 outputTriplet(out, visit, "(", " != ", ")");
2742 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002743 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002744 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002745 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002746 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002747 case EOpModf:
2748 outputTriplet(out, visit, "modf(", ", ", ")");
2749 break;
2750 case EOpPow:
2751 outputTriplet(out, visit, "pow(", ", ", ")");
2752 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002753 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002754 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002755 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002756 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002757 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002758 case EOpMin:
2759 outputTriplet(out, visit, "min(", ", ", ")");
2760 break;
2761 case EOpMax:
2762 outputTriplet(out, visit, "max(", ", ", ")");
2763 break;
2764 case EOpClamp:
2765 outputTriplet(out, visit, "clamp(", ", ", ")");
2766 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302767 case EOpMix:
2768 {
2769 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2770 if (lastParamNode->getType().getBasicType() == EbtBool)
2771 {
2772 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2773 // so use emulated version.
2774 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002775 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05302776 }
2777 else
2778 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002779 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302780 }
2781 }
2782 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002783 case EOpStep:
2784 outputTriplet(out, visit, "step(", ", ", ")");
2785 break;
2786 case EOpSmoothStep:
2787 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2788 break;
2789 case EOpDistance:
2790 outputTriplet(out, visit, "distance(", ", ", ")");
2791 break;
2792 case EOpDot:
2793 outputTriplet(out, visit, "dot(", ", ", ")");
2794 break;
2795 case EOpCross:
2796 outputTriplet(out, visit, "cross(", ", ", ")");
2797 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002798 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002799 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002800 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002801 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002802 case EOpReflect:
2803 outputTriplet(out, visit, "reflect(", ", ", ")");
2804 break;
2805 case EOpRefract:
2806 outputTriplet(out, visit, "refract(", ", ", ")");
2807 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002808 case EOpOuterProduct:
2809 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002810 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02002811 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002812 case EOpMul:
2813 outputTriplet(out, visit, "(", " * ", ")");
2814 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 default: UNREACHABLE();
2816 }
2817
2818 return true;
2819}
2820
Jamie Madill8c46ab12015-12-07 16:39:19 -05002821void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002822{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002823 out << "if (";
2824
2825 node->getCondition()->traverse(this);
2826
2827 out << ")\n";
2828
Jamie Madill8c46ab12015-12-07 16:39:19 -05002829 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002830
2831 bool discard = false;
2832
2833 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002834 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002835 // The trueBlock child node will output braces.
2836 ASSERT(IsSequence(node->getTrueBlock()));
2837
Olli Etuahoa6f22092015-05-08 18:31:10 +03002838 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002839
Olli Etuahoa6f22092015-05-08 18:31:10 +03002840 // Detect true discard
2841 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2842 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002843 else
2844 {
2845 // TODO(oetuaho): Check if the semicolon inside is necessary.
2846 // It's there as a result of conservative refactoring of the output.
2847 out << "{;}\n";
2848 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002849
Jamie Madill8c46ab12015-12-07 16:39:19 -05002850 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002851
Olli Etuahoa6f22092015-05-08 18:31:10 +03002852 if (node->getFalseBlock())
2853 {
2854 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002855
Jamie Madill8c46ab12015-12-07 16:39:19 -05002856 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002857
Olli Etuaho4785fec2015-05-18 16:09:37 +03002858 // Either this is "else if" or the falseBlock child node will output braces.
2859 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2860
Olli Etuahoa6f22092015-05-08 18:31:10 +03002861 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002862
Jamie Madill8c46ab12015-12-07 16:39:19 -05002863 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002864
Olli Etuahoa6f22092015-05-08 18:31:10 +03002865 // Detect false discard
2866 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2867 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002868
Olli Etuahoa6f22092015-05-08 18:31:10 +03002869 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002870 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002871 {
2872 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002873 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002874}
2875
2876bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2877{
2878 TInfoSinkBase &out = getInfoSink();
2879
2880 ASSERT(!node->usesTernaryOperator());
2881
2882 if (!mInsideFunction)
2883 {
2884 // This is part of unfolded global initialization.
2885 mDeferredGlobalInitializers.push_back(node);
2886 return false;
2887 }
2888
2889 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002890 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002891 {
2892 out << "FLATTEN ";
2893 }
2894
Jamie Madill8c46ab12015-12-07 16:39:19 -05002895 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002896
2897 return false;
2898}
2899
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002900bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002901{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002902 TInfoSinkBase &out = getInfoSink();
2903
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002904 if (node->getStatementList())
2905 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002906 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002907 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002908 // The curly braces get written when visiting the statementList aggregate
2909 }
2910 else
2911 {
2912 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002913 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002914 }
2915 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002916}
2917
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002918bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002919{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002920 TInfoSinkBase &out = getInfoSink();
2921
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002922 if (node->hasCondition())
2923 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002924 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002925 return true;
2926 }
2927 else
2928 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002929 out << "default:\n";
2930 return false;
2931 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002932}
2933
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002934void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2935{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002936 TInfoSinkBase &out = getInfoSink();
2937 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002938}
2939
2940bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2941{
Nicolas Capens655fe362014-04-11 13:12:34 -04002942 mNestedLoopDepth++;
2943
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002944 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002945 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002946 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002947
Jamie Madill8c46ab12015-12-07 16:39:19 -05002948 TInfoSinkBase &out = getInfoSink();
2949
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002950 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002951 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002952 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002953 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002954 mInsideDiscontinuousLoop = wasDiscontinuous;
2955 mNestedLoopDepth--;
2956
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002957 return false;
2958 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002959 }
2960
Corentin Wallez1239ee92015-03-19 14:38:02 -07002961 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002962 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002963 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002964 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002965
Jamie Madill8c46ab12015-12-07 16:39:19 -05002966 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002967 }
2968 else
2969 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002970 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002971
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002972 if (node->getInit())
2973 {
2974 node->getInit()->traverse(this);
2975 }
2976
2977 out << "; ";
2978
alokp@chromium.org52813552010-11-16 18:36:09 +00002979 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002980 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002981 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002982 }
2983
2984 out << "; ";
2985
alokp@chromium.org52813552010-11-16 18:36:09 +00002986 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002987 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002988 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002989 }
2990
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002991 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002992
Jamie Madill8c46ab12015-12-07 16:39:19 -05002993 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002994 }
2995
2996 if (node->getBody())
2997 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002998 // The loop body node will output braces.
2999 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03003000 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003001 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03003002 else
3003 {
3004 // TODO(oetuaho): Check if the semicolon inside is necessary.
3005 // It's there as a result of conservative refactoring of the output.
3006 out << "{;}\n";
3007 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003008
Jamie Madill8c46ab12015-12-07 16:39:19 -05003009 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003010
alokp@chromium.org52813552010-11-16 18:36:09 +00003011 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003012 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003013 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003014 out << "while(\n";
3015
alokp@chromium.org52813552010-11-16 18:36:09 +00003016 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003017
daniel@transgaming.com73536982012-03-21 20:45:49 +00003018 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003019 }
3020
daniel@transgaming.com73536982012-03-21 20:45:49 +00003021 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003022
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003023 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04003024 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003025
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003026 return false;
3027}
3028
3029bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
3030{
Jamie Madill32aab012015-01-27 14:12:26 -05003031 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003032
3033 switch (node->getFlowOp())
3034 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003035 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05003036 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003037 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003038 case EOpBreak:
3039 if (visit == PreVisit)
3040 {
Nicolas Capens655fe362014-04-11 13:12:34 -04003041 if (mNestedLoopDepth > 1)
3042 {
3043 mUsesNestedBreak = true;
3044 }
3045
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003046 if (mExcessiveLoopIndex)
3047 {
3048 out << "{Break";
3049 mExcessiveLoopIndex->traverse(this);
3050 out << " = true; break;}\n";
3051 }
3052 else
3053 {
3054 out << "break;\n";
3055 }
3056 }
3057 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05003058 case EOpContinue:
3059 outputTriplet(out, visit, "continue;\n", "", "");
3060 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003061 case EOpReturn:
3062 if (visit == PreVisit)
3063 {
3064 if (node->getExpression())
3065 {
3066 out << "return ";
3067 }
3068 else
3069 {
3070 out << "return;\n";
3071 }
3072 }
3073 else if (visit == PostVisit)
3074 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003075 if (node->getExpression())
3076 {
3077 out << ";\n";
3078 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003079 }
3080 break;
3081 default: UNREACHABLE();
3082 }
3083
3084 return true;
3085}
3086
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003087bool OutputHLSL::isSingleStatement(TIntermNode *node)
3088{
3089 TIntermAggregate *aggregate = node->getAsAggregate();
3090
3091 if (aggregate)
3092 {
3093 if (aggregate->getOp() == EOpSequence)
3094 {
3095 return false;
3096 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04003097 else if (aggregate->getOp() == EOpDeclaration)
3098 {
3099 // Declaring multiple comma-separated variables must be considered multiple statements
3100 // because each individual declaration has side effects which are visible in the next.
3101 return false;
3102 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003103 else
3104 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07003105 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003106 {
3107 if (!isSingleStatement(*sit))
3108 {
3109 return false;
3110 }
3111 }
3112
3113 return true;
3114 }
3115 }
3116
3117 return true;
3118}
3119
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003120// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
3121// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05003122bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003123{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003124 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003125
3126 // Parse loops of the form:
3127 // for(int index = initial; index [comparator] limit; index += increment)
3128 TIntermSymbol *index = NULL;
3129 TOperator comparator = EOpNull;
3130 int initial = 0;
3131 int limit = 0;
3132 int increment = 0;
3133
3134 // Parse index name and intial value
3135 if (node->getInit())
3136 {
3137 TIntermAggregate *init = node->getInit()->getAsAggregate();
3138
3139 if (init)
3140 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07003141 TIntermSequence *sequence = init->getSequence();
3142 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003143
3144 if (variable && variable->getQualifier() == EvqTemporary)
3145 {
3146 TIntermBinary *assign = variable->getAsBinaryNode();
3147
3148 if (assign->getOp() == EOpInitialize)
3149 {
3150 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3151 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3152
3153 if (symbol && constant)
3154 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003155 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003156 {
3157 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003158 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003159 }
3160 }
3161 }
3162 }
3163 }
3164 }
3165
3166 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003167 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003168 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003169 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003170
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003171 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3172 {
3173 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3174
3175 if (constant)
3176 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003177 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003178 {
3179 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003180 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003181 }
3182 }
3183 }
3184 }
3185
3186 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003187 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003188 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003189 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3190 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003191
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003192 if (binaryTerminal)
3193 {
3194 TOperator op = binaryTerminal->getOp();
3195 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3196
3197 if (constant)
3198 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003199 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003200 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003201 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003202
3203 switch (op)
3204 {
3205 case EOpAddAssign: increment = value; break;
3206 case EOpSubAssign: increment = -value; break;
3207 default: UNIMPLEMENTED();
3208 }
3209 }
3210 }
3211 }
3212 else if (unaryTerminal)
3213 {
3214 TOperator op = unaryTerminal->getOp();
3215
3216 switch (op)
3217 {
3218 case EOpPostIncrement: increment = 1; break;
3219 case EOpPostDecrement: increment = -1; break;
3220 case EOpPreIncrement: increment = 1; break;
3221 case EOpPreDecrement: increment = -1; break;
3222 default: UNIMPLEMENTED();
3223 }
3224 }
3225 }
3226
3227 if (index != NULL && comparator != EOpNull && increment != 0)
3228 {
3229 if (comparator == EOpLessThanEqual)
3230 {
3231 comparator = EOpLessThan;
3232 limit += 1;
3233 }
3234
3235 if (comparator == EOpLessThan)
3236 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003237 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003238
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003239 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003240 {
3241 return false; // Not an excessive loop
3242 }
3243
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003244 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3245 mExcessiveLoopIndex = index;
3246
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003247 out << "{int ";
3248 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003249 out << ";\n"
3250 "bool Break";
3251 index->traverse(this);
3252 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003253
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003254 bool firstLoopFragment = true;
3255
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003256 while (iterations > 0)
3257 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003258 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003259
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003260 if (!firstLoopFragment)
3261 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003262 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003263 index->traverse(this);
3264 out << ") {\n";
3265 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003266
3267 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3268 {
3269 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3270 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003271
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003272 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07003273 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003274
Corentin Wallez1239ee92015-03-19 14:38:02 -07003275 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003276 index->traverse(this);
3277 out << " = ";
3278 out << initial;
3279
3280 out << "; ";
3281 index->traverse(this);
3282 out << " < ";
3283 out << clampedLimit;
3284
3285 out << "; ";
3286 index->traverse(this);
3287 out << " += ";
3288 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003289 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003290
Jamie Madill8c46ab12015-12-07 16:39:19 -05003291 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003292 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003293
3294 if (node->getBody())
3295 {
3296 node->getBody()->traverse(this);
3297 }
3298
Jamie Madill8c46ab12015-12-07 16:39:19 -05003299 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003300 out << ";}\n";
3301
3302 if (!firstLoopFragment)
3303 {
3304 out << "}\n";
3305 }
3306
3307 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003308
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003309 initial += MAX_LOOP_ITERATIONS * increment;
3310 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003311 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003312
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003313 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003314
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003315 mExcessiveLoopIndex = restoreIndex;
3316
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003317 return true;
3318 }
3319 else UNIMPLEMENTED();
3320 }
3321
3322 return false; // Not handled as an excessive loop
3323}
3324
Jamie Madill8c46ab12015-12-07 16:39:19 -05003325void OutputHLSL::outputTriplet(TInfoSinkBase &out,
3326 Visit visit,
3327 const char *preString,
3328 const char *inString,
3329 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003330{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003331 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003332 {
3333 out << preString;
3334 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003335 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003336 {
3337 out << inString;
3338 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003339 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003340 {
3341 out << postString;
3342 }
3343}
3344
Jamie Madill8c46ab12015-12-07 16:39:19 -05003345void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003346{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003347 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003348 {
Jamie Madill32aab012015-01-27 14:12:26 -05003349 out << "\n";
3350 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003351
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003352 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003353 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003354 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003355 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003356
Jamie Madill32aab012015-01-27 14:12:26 -05003357 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003358 }
3359}
3360
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003361TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3362{
3363 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003364 const TType &type = symbol->getType();
3365 const TName &name = symbol->getName();
3366 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003367
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003368 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003369 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003370 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003371 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003372 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003373 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003374 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003375 }
3376
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003377 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003378 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003379 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3380 {
3381 // Samplers are passed as indices to the sampler array.
3382 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3383 return "const uint " + nameStr + ArrayString(type);
3384 }
3385 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3386 {
3387 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
3388 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
3389 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
3390 ArrayString(type);
3391 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003392 }
3393
Olli Etuaho96963162016-03-21 11:54:33 +02003394 TStringStream argString;
3395 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
3396 << ArrayString(type);
3397
3398 // If the structure parameter contains samplers, they need to be passed into the function as
3399 // separate parameters. HLSL doesn't natively support samplers in structs.
3400 if (type.isStructureContainingSamplers())
3401 {
3402 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3403 TVector<TIntermSymbol *> samplerSymbols;
3404 type.createSamplerSymbols("angle" + nameStr, "", 0, &samplerSymbols, nullptr);
3405 for (const TIntermSymbol *sampler : samplerSymbols)
3406 {
3407 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3408 {
3409 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
3410 }
3411 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3412 {
3413 const TType &samplerType = sampler->getType();
3414 ASSERT((!type.isArray() && !samplerType.isArray()) ||
3415 type.getArraySize() == samplerType.getArraySize());
3416 ASSERT(IsSampler(samplerType.getBasicType()));
3417 argString << ", " << QualifierString(qualifier) << " "
3418 << TextureString(samplerType.getBasicType()) << " texture_"
3419 << sampler->getSymbol() << ArrayString(type) << ", "
3420 << QualifierString(qualifier) << " "
3421 << SamplerString(samplerType.getBasicType()) << " sampler_"
3422 << sampler->getSymbol() << ArrayString(type);
3423 }
3424 else
3425 {
3426 const TType &samplerType = sampler->getType();
3427 ASSERT((!type.isArray() && !samplerType.isArray()) ||
3428 type.getArraySize() == samplerType.getArraySize());
3429 ASSERT(IsSampler(samplerType.getBasicType()));
3430 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
3431 << " " << sampler->getSymbol() << ArrayString(type);
3432 }
3433 }
3434 }
3435
3436 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003437}
3438
3439TString OutputHLSL::initializer(const TType &type)
3440{
3441 TString string;
3442
Jamie Madill94bf7f22013-07-08 13:31:15 -04003443 size_t size = type.getObjectSize();
3444 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003445 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003446 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003447
Jamie Madill94bf7f22013-07-08 13:31:15 -04003448 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003449 {
3450 string += ", ";
3451 }
3452 }
3453
daniel@transgaming.comead23042010-04-29 03:35:36 +00003454 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003455}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003456
Jamie Madill8c46ab12015-12-07 16:39:19 -05003457void OutputHLSL::outputConstructor(TInfoSinkBase &out,
3458 Visit visit,
3459 const TType &type,
3460 const char *name,
3461 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003462{
Olli Etuahof40319e2015-03-10 14:33:00 +02003463 if (type.isArray())
3464 {
3465 UNIMPLEMENTED();
3466 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003467
3468 if (visit == PreVisit)
3469 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003470 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003471
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003472 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003473 }
3474 else if (visit == InVisit)
3475 {
3476 out << ", ";
3477 }
3478 else if (visit == PostVisit)
3479 {
3480 out << ")";
3481 }
3482}
3483
Jamie Madill8c46ab12015-12-07 16:39:19 -05003484const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
3485 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003486 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003487{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003488 const TConstantUnion *constUnionIterated = constUnion;
3489
Jamie Madill98493dd2013-07-08 14:39:03 -04003490 const TStructure* structure = type.getStruct();
3491 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003492 {
Jamie Madill033dae62014-06-18 12:56:28 -04003493 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003494
Jamie Madill98493dd2013-07-08 14:39:03 -04003495 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003496
Jamie Madill98493dd2013-07-08 14:39:03 -04003497 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003498 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003499 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05003500 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003501
Jamie Madill98493dd2013-07-08 14:39:03 -04003502 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003503 {
3504 out << ", ";
3505 }
3506 }
3507
3508 out << ")";
3509 }
3510 else
3511 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003512 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003513 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04003514
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003515 if (writeType)
3516 {
Jamie Madill033dae62014-06-18 12:56:28 -04003517 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003518 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003519 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003520 if (writeType)
3521 {
3522 out << ")";
3523 }
3524 }
3525
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003526 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003527}
3528
Jamie Madill8c46ab12015-12-07 16:39:19 -05003529void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003530{
3531 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05003532 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003533}
3534
Jamie Madill37997142015-01-28 10:06:34 -05003535bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3536{
3537 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3538 expression->traverse(&searchSymbol);
3539
3540 if (searchSymbol.foundMatch())
3541 {
3542 // Type already printed
3543 out << "t" + str(mUniqueIndex) + " = ";
3544 expression->traverse(this);
3545 out << ", ";
3546 symbolNode->traverse(this);
3547 out << " = t" + str(mUniqueIndex);
3548
3549 mUniqueIndex++;
3550 return true;
3551 }
3552
3553 return false;
3554}
3555
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003556bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3557{
3558 // We support writing constant unions and constructors that only take constant unions as
3559 // parameters as HLSL literals.
3560 if (expression->getAsConstantUnion())
3561 {
3562 return true;
3563 }
3564 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3565 !expression->getAsAggregate()->isConstructor())
3566 {
3567 return false;
3568 }
3569 TIntermAggregate *constructor = expression->getAsAggregate();
3570 for (TIntermNode *&node : *constructor->getSequence())
3571 {
3572 if (!node->getAsConstantUnion())
3573 return false;
3574 }
3575 return true;
3576}
3577
3578bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3579 TIntermSymbol *symbolNode,
3580 TIntermTyped *expression)
3581{
3582 if (canWriteAsHLSLLiteral(expression))
3583 {
3584 symbolNode->traverse(this);
3585 if (expression->getType().isArray())
3586 {
3587 out << "[" << expression->getType().getArraySize() << "]";
3588 }
3589 out << " = {";
3590 if (expression->getAsConstantUnion())
3591 {
3592 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3593 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3594 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3595 }
3596 else
3597 {
3598 TIntermAggregate *constructor = expression->getAsAggregate();
3599 ASSERT(constructor != nullptr);
3600 for (TIntermNode *&node : *constructor->getSequence())
3601 {
3602 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3603 ASSERT(nodeConst);
3604 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3605 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3606 if (node != constructor->getSequence()->back())
3607 {
3608 out << ", ";
3609 }
3610 }
3611 }
3612 out << "}";
3613 return true;
3614 }
3615 return false;
3616}
3617
Jamie Madill37997142015-01-28 10:06:34 -05003618void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3619{
3620 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3621 << "\n"
3622 << "void initializeDeferredGlobals()\n"
3623 << "{\n";
3624
3625 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3626 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003627 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3628 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3629 if (binary != nullptr)
3630 {
3631 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3632 TIntermTyped *expression = binary->getRight();
3633 ASSERT(symbol);
3634 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003635
Olli Etuahod81ed842015-05-12 12:46:35 +03003636 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003637
Olli Etuahod81ed842015-05-12 12:46:35 +03003638 if (!writeSameSymbolInitializer(out, symbol, expression))
3639 {
3640 ASSERT(mInfoSinkStack.top() == &out);
3641 expression->traverse(this);
3642 }
3643 out << ";\n";
3644 }
3645 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003646 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003647 writeSelection(out, selection);
Jamie Madill37997142015-01-28 10:06:34 -05003648 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003649 else
3650 {
3651 UNREACHABLE();
3652 }
Jamie Madill37997142015-01-28 10:06:34 -05003653 }
3654
3655 out << "}\n"
3656 << "\n";
3657}
3658
Jamie Madill55e79e02015-02-09 15:35:00 -05003659TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3660{
3661 const TFieldList &fields = structure.fields();
3662
3663 for (const auto &eqFunction : mStructEqualityFunctions)
3664 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003665 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003666 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003667 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003668 }
3669 }
3670
3671 const TString &structNameString = StructNameString(structure);
3672
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003673 StructEqualityFunction *function = new StructEqualityFunction();
3674 function->structure = &structure;
3675 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003676
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003677 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003678
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003679 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3680 << "{\n"
3681 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003682
3683 for (size_t i = 0; i < fields.size(); i++)
3684 {
3685 const TField *field = fields[i];
3686 const TType *fieldType = field->type();
3687
3688 const TString &fieldNameA = "a." + Decorate(field->name());
3689 const TString &fieldNameB = "b." + Decorate(field->name());
3690
3691 if (i > 0)
3692 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003693 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003694 }
3695
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003696 fnOut << "(";
3697 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3698 fnOut << fieldNameA;
3699 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3700 fnOut << fieldNameB;
3701 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3702 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003703 }
3704
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003705 fnOut << ";\n" << "}\n";
3706
3707 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003708
3709 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003710 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003711
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003712 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003713}
3714
Olli Etuaho7fb49552015-03-18 17:27:44 +02003715TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3716{
3717 for (const auto &eqFunction : mArrayEqualityFunctions)
3718 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003719 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003720 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003721 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003722 }
3723 }
3724
3725 const TString &typeName = TypeString(type);
3726
Olli Etuaho12690762015-03-31 12:55:28 +03003727 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003728 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003729
3730 TInfoSinkBase fnNameOut;
3731 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003732 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003733
3734 TType nonArrayType = type;
3735 nonArrayType.clearArrayness();
3736
3737 TInfoSinkBase fnOut;
3738
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003739 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003740 << typeName << " a[" << type.getArraySize() << "], "
3741 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003742 << "{\n"
3743 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3744 " {\n"
3745 " if (";
3746
3747 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3748 fnOut << "a[i]";
3749 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3750 fnOut << "b[i]";
3751 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3752
3753 fnOut << ") { return false; }\n"
3754 " }\n"
3755 " return true;\n"
3756 "}\n";
3757
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003758 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003759
3760 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003761 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003762
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003763 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003764}
3765
Olli Etuaho12690762015-03-31 12:55:28 +03003766TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3767{
3768 for (const auto &assignFunction : mArrayAssignmentFunctions)
3769 {
3770 if (assignFunction.type == type)
3771 {
3772 return assignFunction.functionName;
3773 }
3774 }
3775
3776 const TString &typeName = TypeString(type);
3777
3778 ArrayHelperFunction function;
3779 function.type = type;
3780
3781 TInfoSinkBase fnNameOut;
3782 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3783 function.functionName = fnNameOut.c_str();
3784
3785 TInfoSinkBase fnOut;
3786
3787 fnOut << "void " << function.functionName << "(out "
3788 << typeName << " a[" << type.getArraySize() << "], "
3789 << typeName << " b[" << type.getArraySize() << "])\n"
3790 << "{\n"
3791 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3792 " {\n"
3793 " a[i] = b[i];\n"
3794 " }\n"
3795 "}\n";
3796
3797 function.functionDefinition = fnOut.c_str();
3798
3799 mArrayAssignmentFunctions.push_back(function);
3800
3801 return function.functionName;
3802}
3803
Olli Etuaho9638c352015-04-01 14:34:52 +03003804TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3805{
3806 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3807 {
3808 if (constructIntoFunction.type == type)
3809 {
3810 return constructIntoFunction.functionName;
3811 }
3812 }
3813
3814 const TString &typeName = TypeString(type);
3815
3816 ArrayHelperFunction function;
3817 function.type = type;
3818
3819 TInfoSinkBase fnNameOut;
3820 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3821 function.functionName = fnNameOut.c_str();
3822
3823 TInfoSinkBase fnOut;
3824
3825 fnOut << "void " << function.functionName << "(out "
3826 << typeName << " a[" << type.getArraySize() << "]";
3827 for (int i = 0; i < type.getArraySize(); ++i)
3828 {
3829 fnOut << ", " << typeName << " b" << i;
3830 }
3831 fnOut << ")\n"
3832 "{\n";
3833
3834 for (int i = 0; i < type.getArraySize(); ++i)
3835 {
3836 fnOut << " a[" << i << "] = b" << i << ";\n";
3837 }
3838 fnOut << "}\n";
3839
3840 function.functionDefinition = fnOut.c_str();
3841
3842 mArrayConstructIntoFunctions.push_back(function);
3843
3844 return function.functionName;
3845}
3846
Jamie Madill2e295e22015-04-29 10:41:33 -04003847void OutputHLSL::ensureStructDefined(const TType &type)
3848{
3849 TStructure *structure = type.getStruct();
3850
3851 if (structure)
3852 {
3853 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3854 }
3855}
3856
3857
Olli Etuaho9638c352015-04-01 14:34:52 +03003858
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003859}