blob: a5c9d2a973782100d8db894374b37796573e7c45 [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 {
Olli Etuahod4102f02016-01-22 14:54:04 +0200915 TString texCoordX("t.x");
916 TString texCoordY("t.y");
917 TString texCoordZ("t.z");
918 TString proj = "";
919
920 if (textureFunction->proj)
921 {
922 switch (textureFunction->coords)
923 {
924 case 3:
925 proj = " / t.z";
926 break;
927 case 4:
928 proj = " / t.w";
929 break;
930 default:
931 UNREACHABLE();
932 }
933 if (proj != "")
934 {
935 texCoordX = "(" + texCoordX + proj + ")";
936 texCoordY = "(" + texCoordY + proj + ")";
937 texCoordZ = "(" + texCoordZ + proj + ")";
938 }
939 }
940
Nicolas Capens0027fa92014-02-20 14:26:42 -0500941 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
942 {
943 out << " float width; float height; float layers; float levels;\n";
944
945 out << " uint mip = 0;\n";
946
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200947 out << " " << textureReference
948 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens0027fa92014-02-20 14:26:42 -0500949
950 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
951 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
952 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
953 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
954
955 // FACE_POSITIVE_X = 000b
956 // FACE_NEGATIVE_X = 001b
957 // FACE_POSITIVE_Y = 010b
958 // FACE_NEGATIVE_Y = 011b
959 // FACE_POSITIVE_Z = 100b
960 // FACE_NEGATIVE_Z = 101b
961 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
962
963 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
964 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
965 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
966
967 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
968 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500969
970 // Mip level computation.
Olli Etuahoced87052016-04-04 16:34:27 +0300971 if (textureFunction->method == TextureFunction::IMPLICIT ||
972 textureFunction->method == TextureFunction::LOD ||
973 textureFunction->method == TextureFunction::GRAD)
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500974 {
Olli Etuahoced87052016-04-04 16:34:27 +0300975 if (textureFunction->method == TextureFunction::IMPLICIT)
976 {
977 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
978 " float2 dx = ddx(tSized);\n"
979 " float2 dy = ddy(tSized);\n"
980 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n";
981 }
982 else if (textureFunction->method == TextureFunction::GRAD)
983 {
984 // ESSL 3.00.6 spec section 8.8: "For the cube version, the partial
985 // derivatives of P are assumed to be in the coordinate system used before
986 // texture coordinates are projected onto the appropriate cube face."
987 // ddx[0] and ddy[0] are the derivatives of t.x passed into the function
988 // ddx[1] and ddy[1] are the derivatives of t.y passed into the function
989 // ddx[2] and ddy[2] are the derivatives of t.z passed into the function
990 // Determine the derivatives of u, v and m
991 out << " float dudx = xMajor ? ddx[2] : (yMajor && t.y < 0.0f ? -ddx[0] "
992 ": ddx[0]);\n"
993 " float dudy = xMajor ? ddy[2] : (yMajor && t.y < 0.0f ? -ddy[0] "
994 ": ddy[0]);\n"
995 " float dvdx = yMajor ? ddx[2] : (negative ? ddx[1] : -ddx[1]);\n"
996 " float dvdy = yMajor ? ddy[2] : (negative ? ddy[1] : -ddy[1]);\n"
997 " float dmdx = xMajor ? ddx[0] : (yMajor ? ddx[1] : ddx[2]);\n"
998 " float dmdy = xMajor ? ddy[0] : (yMajor ? ddy[1] : ddy[2]);\n";
999 // Now determine the derivatives of the face coordinates, using the
1000 // derivatives calculated above.
1001 // d / dx (u(x) * 0.5 / m(x) + 0.5)
1002 // = 0.5 * (m(x) * u'(x) - u(x) * m'(x)) / m(x)^2
1003 out << " float dfacexdx = 0.5f * (m * dudx - u * dmdx) / (m * m);\n"
1004 " float dfaceydx = 0.5f * (m * dvdx - v * dmdx) / (m * m);\n"
1005 " float dfacexdy = 0.5f * (m * dudy - u * dmdy) / (m * m);\n"
1006 " float dfaceydy = 0.5f * (m * dvdy - v * dmdy) / (m * m);\n"
1007 " float2 sizeVec = float2(width, height);\n"
1008 " float2 faceddx = float2(dfacexdx, dfaceydx) * sizeVec;\n"
1009 " float2 faceddy = float2(dfacexdy, dfaceydy) * sizeVec;\n";
1010 // Optimization: instead of: log2(max(length(faceddx), length(faceddy)))
1011 // we compute: log2(max(length(faceddx)^2, length(faceddy)^2)) / 2
1012 out << " float lengthfaceddx2 = dot(faceddx, faceddx);\n"
1013 " float lengthfaceddy2 = dot(faceddy, faceddy);\n"
1014 " float lod = log2(max(lengthfaceddx2, lengthfaceddy2)) * "
1015 "0.5f;\n";
1016 }
1017 out << " mip = uint(min(max(round(lod), 0), levels - 1));\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001018 << " " << textureReference
1019 << ".GetDimensions(mip, width, height, layers, levels);\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -05001020 }
Olli Etuahod4102f02016-01-22 14:54:04 +02001021
1022 // Convert from normalized floating-point to integer
1023 texCoordX = "int(floor(width * frac(" + texCoordX + ")))";
1024 texCoordY = "int(floor(height * frac(" + texCoordY + ")))";
1025 texCoordZ = "face";
Nicolas Capens0027fa92014-02-20 14:26:42 -05001026 }
1027 else if (IsIntegerSampler(textureFunction->sampler) &&
1028 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001029 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001030 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001031 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001032 if (IsSamplerArray(textureFunction->sampler))
1033 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001034 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001035
Nicolas Capens9edebd62013-08-06 10:59:10 -04001036 if (textureFunction->method == TextureFunction::LOD0)
1037 {
1038 out << " uint mip = 0;\n";
1039 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001040 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1041 {
1042 out << " uint mip = bias;\n";
1043 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001044 else
1045 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001046
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001047 out << " " << textureReference
1048 << ".GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001049 if (textureFunction->method == TextureFunction::IMPLICIT ||
1050 textureFunction->method == TextureFunction::BIAS)
1051 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001052 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001053 " float dx = length(ddx(tSized));\n"
1054 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001055 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001056
1057 if (textureFunction->method == TextureFunction::BIAS)
1058 {
1059 out << " lod += bias;\n";
1060 }
1061 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001062 else if (textureFunction->method == TextureFunction::GRAD)
1063 {
Olli Etuahoced87052016-04-04 16:34:27 +03001064 out << " float2 sizeVec = float2(width, height);\n"
1065 " float2 sizeDdx = ddx * sizeVec;\n"
1066 " float2 sizeDdy = ddy * sizeVec;\n"
1067 " float lod = log2(max(dot(sizeDdx, sizeDdx), "
1068 "dot(sizeDdy, sizeDdy))) * 0.5f;\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001069 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001070
1071 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1072 }
1073
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001074 out << " " << textureReference
1075 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001076 }
1077 else
1078 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001079 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001080
Nicolas Capens9edebd62013-08-06 10:59:10 -04001081 if (textureFunction->method == TextureFunction::LOD0)
1082 {
1083 out << " uint mip = 0;\n";
1084 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001085 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1086 {
1087 out << " uint mip = bias;\n";
1088 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001089 else
1090 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001091 out << " " << textureReference
1092 << ".GetDimensions(0, width, height, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001093
Nicolas Capens9edebd62013-08-06 10:59:10 -04001094 if (textureFunction->method == TextureFunction::IMPLICIT ||
1095 textureFunction->method == TextureFunction::BIAS)
1096 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001097 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001098 " float dx = length(ddx(tSized));\n"
1099 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001100 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001101
1102 if (textureFunction->method == TextureFunction::BIAS)
1103 {
1104 out << " lod += bias;\n";
1105 }
1106 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001107 else if (textureFunction->method == TextureFunction::GRAD)
1108 {
Olli Etuahoced87052016-04-04 16:34:27 +03001109 out << " float2 sizeVec = float2(width, height);\n"
1110 " float2 sizeDdx = ddx * sizeVec;\n"
1111 " float2 sizeDdy = ddy * sizeVec;\n"
1112 " float lod = log2(max(dot(sizeDdx, sizeDdx), "
1113 "dot(sizeDdy, sizeDdy))) * 0.5f;\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001114 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001115
1116 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1117 }
1118
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001119 out << " " << textureReference
1120 << ".GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001121 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001122 }
1123 else if (IsSampler3D(textureFunction->sampler))
1124 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001125 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001126
Nicolas Capens9edebd62013-08-06 10:59:10 -04001127 if (textureFunction->method == TextureFunction::LOD0)
1128 {
1129 out << " uint mip = 0;\n";
1130 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001131 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1132 {
1133 out << " uint mip = bias;\n";
1134 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001135 else
1136 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001137 out << " " << textureReference
1138 << ".GetDimensions(0, width, height, depth, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001139
Nicolas Capens9edebd62013-08-06 10:59:10 -04001140 if (textureFunction->method == TextureFunction::IMPLICIT ||
1141 textureFunction->method == TextureFunction::BIAS)
1142 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001143 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1144 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001145 " float dx = length(ddx(tSized));\n"
1146 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001147 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001148
1149 if (textureFunction->method == TextureFunction::BIAS)
1150 {
1151 out << " lod += bias;\n";
1152 }
1153 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001154 else if (textureFunction->method == TextureFunction::GRAD)
1155 {
Olli Etuahoced87052016-04-04 16:34:27 +03001156 out << " float3 sizeVec = float3(width, height, depth);\n"
1157 " float3 sizeDdx = ddx * sizeVec;\n"
1158 " float3 sizeDdy = ddy * sizeVec;\n"
1159 " float lod = log2(max(dot(sizeDdx, sizeDdx), dot(sizeDdy, "
1160 "sizeDdy))) * 0.5f;\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001161 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001162
1163 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1164 }
1165
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001166 out << " " << textureReference
1167 << ".GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001168 }
1169 else UNREACHABLE();
Olli Etuahod4102f02016-01-22 14:54:04 +02001170
1171 // Convert from normalized floating-point to integer
1172 texCoordX = "int(floor(width * frac(" + texCoordX + ")))";
1173 texCoordY = "int(floor(height * frac(" + texCoordY + ")))";
1174
1175 if (IsSamplerArray(textureFunction->sampler))
1176 {
1177 texCoordZ = "int(max(0, min(layers - 1, floor(0.5 + t.z))))";
1178 }
1179 else if (!IsSamplerCube(textureFunction->sampler) &&
1180 !IsSampler2D(textureFunction->sampler))
1181 {
1182 texCoordZ = "int(floor(depth * frac(" + texCoordZ + ")))";
1183 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001184 }
1185
1186 out << " return ";
1187
1188 // HLSL intrinsic
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001189 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001190 {
1191 switch(textureFunction->sampler)
1192 {
1193 case EbtSampler2D: out << "tex2D"; break;
1194 case EbtSamplerCube: out << "texCUBE"; break;
1195 default: UNREACHABLE();
1196 }
1197
Nicolas Capens75fb4752013-07-10 15:14:47 -04001198 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001199 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001200 case TextureFunction::IMPLICIT:
1201 out << "(" << samplerReference << ", ";
1202 break;
1203 case TextureFunction::BIAS:
1204 out << "bias(" << samplerReference << ", ";
1205 break;
1206 case TextureFunction::LOD:
1207 out << "lod(" << samplerReference << ", ";
1208 break;
1209 case TextureFunction::LOD0:
1210 out << "lod(" << samplerReference << ", ";
1211 break;
1212 case TextureFunction::LOD0BIAS:
1213 out << "lod(" << samplerReference << ", ";
1214 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001215 default: UNREACHABLE();
1216 }
1217 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001218 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001219 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001220 if (textureFunction->method == TextureFunction::GRAD)
1221 {
1222 if (IsIntegerSampler(textureFunction->sampler))
1223 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001224 out << "" << textureReference << ".Load(";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001225 }
1226 else if (IsShadowSampler(textureFunction->sampler))
1227 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001228 out << "" << textureReference << ".SampleCmpLevelZero(" << samplerReference
1229 << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001230 }
1231 else
1232 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001233 out << "" << textureReference << ".SampleGrad(" << samplerReference << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001234 }
1235 }
1236 else if (IsIntegerSampler(textureFunction->sampler) ||
1237 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001238 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001239 out << "" << textureReference << ".Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001240 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001241 else if (IsShadowSampler(textureFunction->sampler))
1242 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001243 switch(textureFunction->method)
1244 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001245 case TextureFunction::IMPLICIT:
1246 out << "" << textureReference << ".SampleCmp(" << samplerReference
1247 << ", ";
1248 break;
1249 case TextureFunction::BIAS:
1250 out << "" << textureReference << ".SampleCmp(" << samplerReference
1251 << ", ";
1252 break;
1253 case TextureFunction::LOD:
1254 out << "" << textureReference << ".SampleCmp(" << samplerReference
1255 << ", ";
1256 break;
1257 case TextureFunction::LOD0:
1258 out << "" << textureReference << ".SampleCmpLevelZero("
1259 << samplerReference << ", ";
1260 break;
1261 case TextureFunction::LOD0BIAS:
1262 out << "" << textureReference << ".SampleCmpLevelZero("
1263 << samplerReference << ", ";
1264 break;
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001265 default: UNREACHABLE();
1266 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001267 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001268 else
1269 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001270 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001271 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001272 case TextureFunction::IMPLICIT:
1273 out << "" << textureReference << ".Sample(" << samplerReference << ", ";
1274 break;
1275 case TextureFunction::BIAS:
1276 out << "" << textureReference << ".SampleBias(" << samplerReference
1277 << ", ";
1278 break;
1279 case TextureFunction::LOD:
1280 out << "" << textureReference << ".SampleLevel(" << samplerReference
1281 << ", ";
1282 break;
1283 case TextureFunction::LOD0:
1284 out << "" << textureReference << ".SampleLevel(" << samplerReference
1285 << ", ";
1286 break;
1287 case TextureFunction::LOD0BIAS:
1288 out << "" << textureReference << ".SampleLevel(" << samplerReference
1289 << ", ";
1290 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001291 default: UNREACHABLE();
1292 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001293 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001294 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001295 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001296
Nicolas Capensfc014542014-02-18 14:47:13 -05001297 if (IsIntegerSampler(textureFunction->sampler) ||
1298 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001299 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001300 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001301 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001302 case 2: out << "int3("; break;
1303 case 3: out << "int4("; break;
1304 default: UNREACHABLE();
1305 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001306 }
1307 else
1308 {
1309 switch(hlslCoords)
1310 {
1311 case 2: out << "float2("; break;
1312 case 3: out << "float3("; break;
1313 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001314 default: UNREACHABLE();
1315 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001316 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001317
Olli Etuahod4102f02016-01-22 14:54:04 +02001318 out << texCoordX << ", " << texCoordY;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001319
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001320 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001321 {
1322 if (hlslCoords >= 3)
1323 {
1324 if (textureFunction->coords < 3)
1325 {
1326 out << ", 0";
1327 }
1328 else
1329 {
Olli Etuahod4102f02016-01-22 14:54:04 +02001330 out << ", t.z" << proj;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001331 }
1332 }
1333
1334 if (hlslCoords == 4)
1335 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001336 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001337 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001338 case TextureFunction::BIAS: out << ", bias"; break;
1339 case TextureFunction::LOD: out << ", lod"; break;
1340 case TextureFunction::LOD0: out << ", 0"; break;
1341 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001342 default: UNREACHABLE();
1343 }
1344 }
1345
1346 out << "));\n";
1347 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001348 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001349 {
1350 if (hlslCoords >= 3)
1351 {
Olli Etuahod4102f02016-01-22 14:54:04 +02001352 ASSERT(!IsIntegerSampler(textureFunction->sampler) ||
1353 !IsSamplerCube(textureFunction->sampler) || texCoordZ == "face");
1354 out << ", " << texCoordZ;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001355 }
1356
Nicolas Capensd11d5492014-02-19 17:06:10 -05001357 if (textureFunction->method == TextureFunction::GRAD)
1358 {
1359 if (IsIntegerSampler(textureFunction->sampler))
1360 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001361 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001362 }
1363 else if (IsShadowSampler(textureFunction->sampler))
1364 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001365 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001366 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001367 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001368 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1369 // The resulting third component of P' in the shadow forms is used as Dref
1370 out << "), t.z" << proj;
1371 }
1372 else
1373 {
1374 switch(textureFunction->coords)
1375 {
1376 case 3: out << "), t.z"; break;
1377 case 4: out << "), t.w"; break;
1378 default: UNREACHABLE();
1379 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001380 }
1381 }
1382 else
1383 {
1384 out << "), ddx, ddy";
1385 }
1386 }
1387 else if (IsIntegerSampler(textureFunction->sampler) ||
1388 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001389 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001390 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001391 }
1392 else if (IsShadowSampler(textureFunction->sampler))
1393 {
1394 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001395 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001396 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001397 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1398 // The resulting third component of P' in the shadow forms is used as Dref
1399 out << "), t.z" << proj;
1400 }
1401 else
1402 {
1403 switch(textureFunction->coords)
1404 {
1405 case 3: out << "), t.z"; break;
1406 case 4: out << "), t.w"; break;
1407 default: UNREACHABLE();
1408 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001409 }
1410 }
1411 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001412 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001413 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001414 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001415 case TextureFunction::IMPLICIT: out << ")"; break;
1416 case TextureFunction::BIAS: out << "), bias"; break;
1417 case TextureFunction::LOD: out << "), lod"; break;
1418 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001419 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001420 default: UNREACHABLE();
1421 }
1422 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001423
1424 if (textureFunction->offset)
1425 {
1426 out << ", offset";
1427 }
1428
1429 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001430 }
1431 else UNREACHABLE();
1432 }
1433
1434 out << "\n"
1435 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001436 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001437 }
1438
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001439 if (mUsesFragCoord)
1440 {
1441 out << "#define GL_USES_FRAG_COORD\n";
1442 }
1443
1444 if (mUsesPointCoord)
1445 {
1446 out << "#define GL_USES_POINT_COORD\n";
1447 }
1448
1449 if (mUsesFrontFacing)
1450 {
1451 out << "#define GL_USES_FRONT_FACING\n";
1452 }
1453
1454 if (mUsesPointSize)
1455 {
1456 out << "#define GL_USES_POINT_SIZE\n";
1457 }
1458
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001459 if (mUsesFragDepth)
1460 {
1461 out << "#define GL_USES_FRAG_DEPTH\n";
1462 }
1463
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001464 if (mUsesDepthRange)
1465 {
1466 out << "#define GL_USES_DEPTH_RANGE\n";
1467 }
1468
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001469 if (mUsesXor)
1470 {
1471 out << "bool xor(bool p, bool q)\n"
1472 "{\n"
1473 " return (p || q) && !(p && q);\n"
1474 "}\n"
1475 "\n";
1476 }
1477
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001478 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001479}
1480
1481void OutputHLSL::visitSymbol(TIntermSymbol *node)
1482{
Jamie Madill32aab012015-01-27 14:12:26 -05001483 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001484
Jamie Madill570e04d2013-06-21 09:15:33 -04001485 // Handle accessing std140 structs by value
1486 if (mFlaggedStructMappedNames.count(node) > 0)
1487 {
1488 out << mFlaggedStructMappedNames[node];
1489 return;
1490 }
1491
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001492 TString name = node->getSymbol();
1493
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001494 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001495 {
1496 mUsesDepthRange = true;
1497 out << name;
1498 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001499 else
1500 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001501 TQualifier qualifier = node->getQualifier();
1502
1503 if (qualifier == EvqUniform)
1504 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001505 const TType &nodeType = node->getType();
1506 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001507
1508 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001509 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001510 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001511 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001512 else
1513 {
1514 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001515 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001516
Jamie Madill2e295e22015-04-29 10:41:33 -04001517 ensureStructDefined(nodeType);
1518
Olli Etuaho96963162016-03-21 11:54:33 +02001519 const TName &nameWithMetadata = node->getName();
1520 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001521 }
Jamie Madill19571812013-08-12 15:26:34 -07001522 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001523 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001524 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001525 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001526 }
Jamie Madill033dae62014-06-18 12:56:28 -04001527 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001528 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001529 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001530 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001531 }
Jamie Madill19571812013-08-12 15:26:34 -07001532 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001533 {
1534 mReferencedOutputVariables[name] = node;
1535 out << "out_" << name;
1536 }
1537 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001538 {
1539 out << "gl_Color[0]";
1540 mUsesFragColor = true;
1541 }
1542 else if (qualifier == EvqFragData)
1543 {
1544 out << "gl_Color";
1545 mUsesFragData = true;
1546 }
1547 else if (qualifier == EvqFragCoord)
1548 {
1549 mUsesFragCoord = true;
1550 out << name;
1551 }
1552 else if (qualifier == EvqPointCoord)
1553 {
1554 mUsesPointCoord = true;
1555 out << name;
1556 }
1557 else if (qualifier == EvqFrontFacing)
1558 {
1559 mUsesFrontFacing = true;
1560 out << name;
1561 }
1562 else if (qualifier == EvqPointSize)
1563 {
1564 mUsesPointSize = true;
1565 out << name;
1566 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001567 else if (qualifier == EvqInstanceID)
1568 {
1569 mUsesInstanceID = true;
1570 out << name;
1571 }
Corentin Wallezb076add2016-01-11 16:45:46 -05001572 else if (qualifier == EvqVertexID)
1573 {
1574 mUsesVertexID = true;
1575 out << name;
1576 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001577 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001578 {
1579 mUsesFragDepth = true;
1580 out << "gl_Depth";
1581 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001582 else
1583 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001584 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001585 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001586 }
1587}
1588
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001589void OutputHLSL::visitRaw(TIntermRaw *node)
1590{
Jamie Madill32aab012015-01-27 14:12:26 -05001591 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001592}
1593
Olli Etuaho7fb49552015-03-18 17:27:44 +02001594void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1595{
1596 if (type.isScalar() && !type.isArray())
1597 {
1598 if (op == EOpEqual)
1599 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001600 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001601 }
1602 else
1603 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001604 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001605 }
1606 }
1607 else
1608 {
1609 if (visit == PreVisit && op == EOpNotEqual)
1610 {
1611 out << "!";
1612 }
1613
1614 if (type.isArray())
1615 {
1616 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001617 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001618 }
1619 else if (type.getBasicType() == EbtStruct)
1620 {
1621 const TStructure &structure = *type.getStruct();
1622 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001623 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001624 }
1625 else
1626 {
1627 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001628 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001629 }
1630 }
1631}
1632
Olli Etuaho96963162016-03-21 11:54:33 +02001633bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
1634{
1635 // Inside InVisit the current node is already in the path.
1636 const unsigned int initialN = visit == InVisit ? 1u : 0u;
1637 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
1638 {
1639 TIntermNode *ancestor = getAncestorNode(n);
1640 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1641 if (ancestorBinary == nullptr)
1642 {
1643 return false;
1644 }
1645 switch (ancestorBinary->getOp())
1646 {
1647 case EOpIndexDirectStruct:
1648 {
1649 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1650 const TIntermConstantUnion *index =
1651 ancestorBinary->getRight()->getAsConstantUnion();
1652 const TField *field = structure->fields()[index->getIConst(0)];
1653 if (IsSampler(field->type()->getBasicType()))
1654 {
1655 return true;
1656 }
1657 break;
1658 }
1659 case EOpIndexDirect:
1660 break;
1661 default:
1662 // Returning a sampler from indirect indexing is not supported.
1663 return false;
1664 }
1665 }
1666 return false;
1667}
1668
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1670{
Jamie Madill32aab012015-01-27 14:12:26 -05001671 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672
Jamie Madill570e04d2013-06-21 09:15:33 -04001673 // Handle accessing std140 structs by value
1674 if (mFlaggedStructMappedNames.count(node) > 0)
1675 {
1676 out << mFlaggedStructMappedNames[node];
1677 return false;
1678 }
1679
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001680 switch (node->getOp())
1681 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001682 case EOpAssign:
1683 if (node->getLeft()->isArray())
1684 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001685 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1686 if (rightAgg != nullptr && rightAgg->isConstructor())
1687 {
1688 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1689 out << functionName << "(";
1690 node->getLeft()->traverse(this);
1691 TIntermSequence *seq = rightAgg->getSequence();
1692 for (auto &arrayElement : *seq)
1693 {
1694 out << ", ";
1695 arrayElement->traverse(this);
1696 }
1697 out << ")";
1698 return false;
1699 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001700 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1701 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1702
1703 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001704 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001705 }
1706 else
1707 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001708 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001709 }
1710 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001711 case EOpInitialize:
1712 if (visit == PreVisit)
1713 {
1714 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1715 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1716 // new variable is created before the assignment is evaluated), so we need to convert
1717 // this to "float t = x, x = t;".
1718
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001719 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001720 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001721 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001722
Jamie Madill37997142015-01-28 10:06:34 -05001723 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1724 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001725 {
Jamie Madill37997142015-01-28 10:06:34 -05001726 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001727 // after we initialize uniforms.
1728 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1729 deferredInit->setLeft(node->getLeft());
1730 deferredInit->setRight(node->getRight());
1731 deferredInit->setType(node->getType());
1732 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001733 const TString &initString = initializer(node->getType());
1734 node->setRight(new TIntermRaw(node->getType(), initString));
1735 }
1736 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1737 {
1738 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001739 return false;
1740 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001741 else if (writeConstantInitialization(out, symbolNode, expression))
1742 {
1743 return false;
1744 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001745 }
1746 else if (visit == InVisit)
1747 {
1748 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001749 }
1750 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001751 case EOpAddAssign:
1752 outputTriplet(out, visit, "(", " += ", ")");
1753 break;
1754 case EOpSubAssign:
1755 outputTriplet(out, visit, "(", " -= ", ")");
1756 break;
1757 case EOpMulAssign:
1758 outputTriplet(out, visit, "(", " *= ", ")");
1759 break;
1760 case EOpVectorTimesScalarAssign:
1761 outputTriplet(out, visit, "(", " *= ", ")");
1762 break;
1763 case EOpMatrixTimesScalarAssign:
1764 outputTriplet(out, visit, "(", " *= ", ")");
1765 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001766 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001767 if (visit == PreVisit)
1768 {
1769 out << "(";
1770 }
1771 else if (visit == InVisit)
1772 {
1773 out << " = mul(";
1774 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001775 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001776 }
1777 else
1778 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001779 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001780 }
1781 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001782 case EOpMatrixTimesMatrixAssign:
1783 if (visit == PreVisit)
1784 {
1785 out << "(";
1786 }
1787 else if (visit == InVisit)
1788 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001789 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001790 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001791 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001792 }
1793 else
1794 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001795 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001796 }
1797 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001798 case EOpDivAssign:
1799 outputTriplet(out, visit, "(", " /= ", ")");
1800 break;
1801 case EOpIModAssign:
1802 outputTriplet(out, visit, "(", " %= ", ")");
1803 break;
1804 case EOpBitShiftLeftAssign:
1805 outputTriplet(out, visit, "(", " <<= ", ")");
1806 break;
1807 case EOpBitShiftRightAssign:
1808 outputTriplet(out, visit, "(", " >>= ", ")");
1809 break;
1810 case EOpBitwiseAndAssign:
1811 outputTriplet(out, visit, "(", " &= ", ")");
1812 break;
1813 case EOpBitwiseXorAssign:
1814 outputTriplet(out, visit, "(", " ^= ", ")");
1815 break;
1816 case EOpBitwiseOrAssign:
1817 outputTriplet(out, visit, "(", " |= ", ")");
1818 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001819 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001820 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001821 const TType& leftType = node->getLeft()->getType();
1822 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001823 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001824 if (visit == PreVisit)
1825 {
1826 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1827 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001828 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001829 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001830 return false;
1831 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001832 }
Olli Etuaho96963162016-03-21 11:54:33 +02001833 else if (ancestorEvaluatesToSamplerInStruct(visit))
1834 {
1835 // All parts of an expression that access a sampler in a struct need to use _ as
1836 // separator to access the sampler variable that has been moved out of the struct.
1837 outputTriplet(out, visit, "", "_", "");
1838 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001839 else
1840 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001841 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001842 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001843 }
1844 break;
1845 case EOpIndexIndirect:
1846 // We do not currently support indirect references to interface blocks
1847 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001848 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001849 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001850 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001851 {
1852 const TStructure* structure = node->getLeft()->getType().getStruct();
1853 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1854 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001855
Olli Etuaho96963162016-03-21 11:54:33 +02001856 // In cases where indexing returns a sampler, we need to access the sampler variable
1857 // that has been moved out of the struct.
1858 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1859 if (visit == PreVisit && indexingReturnsSampler)
1860 {
1861 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1862 // This prefix is only output at the beginning of the indexing expression, which
1863 // may have multiple parts.
1864 out << "angle";
1865 }
1866 if (!indexingReturnsSampler)
1867 {
1868 // All parts of an expression that access a sampler in a struct need to use _ as
1869 // separator to access the sampler variable that has been moved out of the struct.
1870 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1871 }
1872 if (visit == InVisit)
1873 {
1874 if (indexingReturnsSampler)
1875 {
1876 out << "_" + field->name();
1877 }
1878 else
1879 {
1880 out << "." + DecorateField(field->name(), *structure);
1881 }
1882
1883 return false;
1884 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001885 }
1886 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001887 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001888 if (visit == InVisit)
1889 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001890 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1891 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1892 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001893 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001894
1895 return false;
1896 }
1897 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 case EOpVectorSwizzle:
1899 if (visit == InVisit)
1900 {
1901 out << ".";
1902
1903 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1904
1905 if (swizzle)
1906 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001907 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001909 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001910 {
1911 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1912
1913 if (element)
1914 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001915 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001916
1917 switch (i)
1918 {
1919 case 0: out << "x"; break;
1920 case 1: out << "y"; break;
1921 case 2: out << "z"; break;
1922 case 3: out << "w"; break;
1923 default: UNREACHABLE();
1924 }
1925 }
1926 else UNREACHABLE();
1927 }
1928 }
1929 else UNREACHABLE();
1930
1931 return false; // Fully processed
1932 }
1933 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001934 case EOpAdd:
1935 outputTriplet(out, visit, "(", " + ", ")");
1936 break;
1937 case EOpSub:
1938 outputTriplet(out, visit, "(", " - ", ")");
1939 break;
1940 case EOpMul:
1941 outputTriplet(out, visit, "(", " * ", ")");
1942 break;
1943 case EOpDiv:
1944 outputTriplet(out, visit, "(", " / ", ")");
1945 break;
1946 case EOpIMod:
1947 outputTriplet(out, visit, "(", " % ", ")");
1948 break;
1949 case EOpBitShiftLeft:
1950 outputTriplet(out, visit, "(", " << ", ")");
1951 break;
1952 case EOpBitShiftRight:
1953 outputTriplet(out, visit, "(", " >> ", ")");
1954 break;
1955 case EOpBitwiseAnd:
1956 outputTriplet(out, visit, "(", " & ", ")");
1957 break;
1958 case EOpBitwiseXor:
1959 outputTriplet(out, visit, "(", " ^ ", ")");
1960 break;
1961 case EOpBitwiseOr:
1962 outputTriplet(out, visit, "(", " | ", ")");
1963 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001964 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001965 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001966 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001967 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001968 case EOpLessThan:
1969 outputTriplet(out, visit, "(", " < ", ")");
1970 break;
1971 case EOpGreaterThan:
1972 outputTriplet(out, visit, "(", " > ", ")");
1973 break;
1974 case EOpLessThanEqual:
1975 outputTriplet(out, visit, "(", " <= ", ")");
1976 break;
1977 case EOpGreaterThanEqual:
1978 outputTriplet(out, visit, "(", " >= ", ")");
1979 break;
1980 case EOpVectorTimesScalar:
1981 outputTriplet(out, visit, "(", " * ", ")");
1982 break;
1983 case EOpMatrixTimesScalar:
1984 outputTriplet(out, visit, "(", " * ", ")");
1985 break;
1986 case EOpVectorTimesMatrix:
1987 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1988 break;
1989 case EOpMatrixTimesVector:
1990 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1991 break;
1992 case EOpMatrixTimesMatrix:
1993 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1994 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001995 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001996 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1997 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001998 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001999 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002000 case EOpLogicalXor:
2001 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002002 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002003 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002004 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03002005 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
2006 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002007 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03002008 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009 default: UNREACHABLE();
2010 }
2011
2012 return true;
2013}
2014
2015bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2016{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002017 TInfoSinkBase &out = getInfoSink();
2018
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019 switch (node->getOp())
2020 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002021 case EOpNegative:
2022 outputTriplet(out, visit, "(-", "", ")");
2023 break;
2024 case EOpPositive:
2025 outputTriplet(out, visit, "(+", "", ")");
2026 break;
2027 case EOpVectorLogicalNot:
2028 outputTriplet(out, visit, "(!", "", ")");
2029 break;
2030 case EOpLogicalNot:
2031 outputTriplet(out, visit, "(!", "", ")");
2032 break;
2033 case EOpBitwiseNot:
2034 outputTriplet(out, visit, "(~", "", ")");
2035 break;
2036 case EOpPostIncrement:
2037 outputTriplet(out, visit, "(", "", "++)");
2038 break;
2039 case EOpPostDecrement:
2040 outputTriplet(out, visit, "(", "", "--)");
2041 break;
2042 case EOpPreIncrement:
2043 outputTriplet(out, visit, "(++", "", ")");
2044 break;
2045 case EOpPreDecrement:
2046 outputTriplet(out, visit, "(--", "", ")");
2047 break;
2048 case EOpRadians:
2049 outputTriplet(out, visit, "radians(", "", ")");
2050 break;
2051 case EOpDegrees:
2052 outputTriplet(out, visit, "degrees(", "", ")");
2053 break;
2054 case EOpSin:
2055 outputTriplet(out, visit, "sin(", "", ")");
2056 break;
2057 case EOpCos:
2058 outputTriplet(out, visit, "cos(", "", ")");
2059 break;
2060 case EOpTan:
2061 outputTriplet(out, visit, "tan(", "", ")");
2062 break;
2063 case EOpAsin:
2064 outputTriplet(out, visit, "asin(", "", ")");
2065 break;
2066 case EOpAcos:
2067 outputTriplet(out, visit, "acos(", "", ")");
2068 break;
2069 case EOpAtan:
2070 outputTriplet(out, visit, "atan(", "", ")");
2071 break;
2072 case EOpSinh:
2073 outputTriplet(out, visit, "sinh(", "", ")");
2074 break;
2075 case EOpCosh:
2076 outputTriplet(out, visit, "cosh(", "", ")");
2077 break;
2078 case EOpTanh:
2079 outputTriplet(out, visit, "tanh(", "", ")");
2080 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002081 case EOpAsinh:
2082 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002083 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002084 break;
2085 case EOpAcosh:
2086 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002087 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002088 break;
2089 case EOpAtanh:
2090 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002091 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002092 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002093 case EOpExp:
2094 outputTriplet(out, visit, "exp(", "", ")");
2095 break;
2096 case EOpLog:
2097 outputTriplet(out, visit, "log(", "", ")");
2098 break;
2099 case EOpExp2:
2100 outputTriplet(out, visit, "exp2(", "", ")");
2101 break;
2102 case EOpLog2:
2103 outputTriplet(out, visit, "log2(", "", ")");
2104 break;
2105 case EOpSqrt:
2106 outputTriplet(out, visit, "sqrt(", "", ")");
2107 break;
2108 case EOpInverseSqrt:
2109 outputTriplet(out, visit, "rsqrt(", "", ")");
2110 break;
2111 case EOpAbs:
2112 outputTriplet(out, visit, "abs(", "", ")");
2113 break;
2114 case EOpSign:
2115 outputTriplet(out, visit, "sign(", "", ")");
2116 break;
2117 case EOpFloor:
2118 outputTriplet(out, visit, "floor(", "", ")");
2119 break;
2120 case EOpTrunc:
2121 outputTriplet(out, visit, "trunc(", "", ")");
2122 break;
2123 case EOpRound:
2124 outputTriplet(out, visit, "round(", "", ")");
2125 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08002126 case EOpRoundEven:
2127 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002128 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08002129 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002130 case EOpCeil:
2131 outputTriplet(out, visit, "ceil(", "", ")");
2132 break;
2133 case EOpFract:
2134 outputTriplet(out, visit, "frac(", "", ")");
2135 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05302136 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002137 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05302138 mRequiresIEEEStrictCompiling = true;
2139 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002140 case EOpIsInf:
2141 outputTriplet(out, visit, "isinf(", "", ")");
2142 break;
2143 case EOpFloatBitsToInt:
2144 outputTriplet(out, visit, "asint(", "", ")");
2145 break;
2146 case EOpFloatBitsToUint:
2147 outputTriplet(out, visit, "asuint(", "", ")");
2148 break;
2149 case EOpIntBitsToFloat:
2150 outputTriplet(out, visit, "asfloat(", "", ")");
2151 break;
2152 case EOpUintBitsToFloat:
2153 outputTriplet(out, visit, "asfloat(", "", ")");
2154 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02002155 case EOpPackSnorm2x16:
2156 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002157 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002158 break;
2159 case EOpPackUnorm2x16:
2160 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002161 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002162 break;
2163 case EOpPackHalf2x16:
2164 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002165 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002166 break;
2167 case EOpUnpackSnorm2x16:
2168 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002169 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002170 break;
2171 case EOpUnpackUnorm2x16:
2172 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002173 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002174 break;
2175 case EOpUnpackHalf2x16:
2176 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002177 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002178 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002179 case EOpLength:
2180 outputTriplet(out, visit, "length(", "", ")");
2181 break;
2182 case EOpNormalize:
2183 outputTriplet(out, visit, "normalize(", "", ")");
2184 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002185 case EOpDFdx:
2186 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2187 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002188 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002189 }
2190 else
2191 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002192 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002193 }
2194 break;
2195 case EOpDFdy:
2196 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2197 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002198 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002199 }
2200 else
2201 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002202 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002203 }
2204 break;
2205 case EOpFwidth:
2206 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2207 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002208 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002209 }
2210 else
2211 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002212 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002213 }
2214 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002215 case EOpTranspose:
2216 outputTriplet(out, visit, "transpose(", "", ")");
2217 break;
2218 case EOpDeterminant:
2219 outputTriplet(out, visit, "determinant(transpose(", "", "))");
2220 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002221 case EOpInverse:
2222 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002223 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002224 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002225
Jamie Madill8c46ab12015-12-07 16:39:19 -05002226 case EOpAny:
2227 outputTriplet(out, visit, "any(", "", ")");
2228 break;
2229 case EOpAll:
2230 outputTriplet(out, visit, "all(", "", ")");
2231 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002232 default: UNREACHABLE();
2233 }
2234
2235 return true;
2236}
2237
Olli Etuaho96963162016-03-21 11:54:33 +02002238TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
2239{
2240 if (node->getAsSymbolNode())
2241 {
2242 return node->getAsSymbolNode()->getSymbol();
2243 }
2244 TIntermBinary *nodeBinary = node->getAsBinaryNode();
2245 switch (nodeBinary->getOp())
2246 {
2247 case EOpIndexDirect:
2248 {
2249 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
2250
2251 TInfoSinkBase prefixSink;
2252 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
2253 return TString(prefixSink.c_str());
2254 }
2255 case EOpIndexDirectStruct:
2256 {
2257 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
2258 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
2259 const TField *field = s->fields()[index];
2260
2261 TInfoSinkBase prefixSink;
2262 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
2263 << field->name();
2264 return TString(prefixSink.c_str());
2265 }
2266 default:
2267 UNREACHABLE();
2268 return TString("");
2269 }
2270}
2271
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2273{
Jamie Madill32aab012015-01-27 14:12:26 -05002274 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002275
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 switch (node->getOp())
2277 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002278 case EOpSequence:
2279 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002280 if (mInsideFunction)
2281 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002282 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002283 out << "{\n";
2284 }
2285
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002286 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002287 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002288 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002289
Olli Etuahoa6f22092015-05-08 18:31:10 +03002290 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002291
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002292 // Don't output ; after case labels, they're terminated by :
2293 // This is needed especially since outputting a ; after a case statement would turn empty
2294 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03002295 // Also no need to output ; after selection (if) statements or sequences. This is done just
2296 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002297 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
2298 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03002299 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002300 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002301 }
2302
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002303 if (mInsideFunction)
2304 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002305 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002306 out << "}\n";
2307 }
2308
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002309 return false;
2310 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 case EOpDeclaration:
2312 if (visit == PreVisit)
2313 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002314 TIntermSequence *sequence = node->getSequence();
2315 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03002316 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002318 if (variable &&
2319 (variable->getQualifier() == EvqTemporary ||
2320 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002322 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00002323
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002324 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002326 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002328 out << "static ";
2329 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002330
Olli Etuahoa6f22092015-05-08 18:31:10 +03002331 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04002332
Olli Etuahoa6f22092015-05-08 18:31:10 +03002333 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04002334
Olli Etuahoa6f22092015-05-08 18:31:10 +03002335 if (symbol)
2336 {
2337 symbol->traverse(this);
2338 out << ArrayString(symbol->getType());
2339 out << " = " + initializer(symbol->getType());
2340 }
2341 else
2342 {
2343 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002346 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2347 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002348 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002349 }
2350 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351 }
Jamie Madill033dae62014-06-18 12:56:28 -04002352 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002353 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002354 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002355 {
2356 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2357
2358 if (symbol)
2359 {
2360 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2361 mReferencedVaryings[symbol->getSymbol()] = symbol;
2362 }
2363 else
2364 {
2365 (*sit)->traverse(this);
2366 }
2367 }
2368 }
2369
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 return false;
2371 }
2372 else if (visit == InVisit)
2373 {
2374 out << ", ";
2375 }
2376 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04002377 case EOpInvariantDeclaration:
2378 // Do not do any translation
2379 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002380 case EOpPrototype:
2381 if (visit == PreVisit)
2382 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002383 size_t index = mCallDag.findIndex(node);
2384 // Skip the prototype if it is not implemented (and thus not used)
2385 if (index == CallDAG::InvalidIndex)
2386 {
2387 return false;
2388 }
2389
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002390 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002391
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002392 TString name = DecorateFunctionIfNeeded(node->getNameObj());
2393 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
2394 << (mOutputLod0Function ? "Lod0(" : "(");
2395
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002396 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002397 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002398 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002399
2400 if (symbol)
2401 {
2402 out << argumentString(symbol);
2403
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002404 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002405 {
2406 out << ", ";
2407 }
2408 }
2409 else UNREACHABLE();
2410 }
2411
2412 out << ");\n";
2413
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002414 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002415 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2416 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002417 {
2418 mOutputLod0Function = true;
2419 node->traverse(this);
2420 mOutputLod0Function = false;
2421 }
2422
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002423 return false;
2424 }
2425 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002426 case EOpComma:
2427 outputTriplet(out, visit, "(", ", ", ")");
2428 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 case EOpFunction:
2430 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002431 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002432 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433
Corentin Wallez1239ee92015-03-19 14:38:02 -07002434 size_t index = mCallDag.findIndex(node);
2435 ASSERT(index != CallDAG::InvalidIndex);
2436 mCurrentFunctionMetadata = &mASTMetadataList[index];
2437
Jamie Madill033dae62014-06-18 12:56:28 -04002438 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002439
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002440 TIntermSequence *sequence = node->getSequence();
2441 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
2442
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002443 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002445 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002447 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002449 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002450 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002451 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002452
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002453 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002454 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002455 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002456
2457 if (symbol)
2458 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002459 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002460
2461 out << argumentString(symbol);
2462
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002463 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002464 {
2465 out << ", ";
2466 }
2467 }
2468 else UNREACHABLE();
2469 }
2470
Olli Etuaho4785fec2015-05-18 16:09:37 +03002471 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002472
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002473 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002474 {
2475 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002476 TIntermNode *body = (*sequence)[1];
2477 // The function body node will output braces.
2478 ASSERT(IsSequence(body));
2479 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002480 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002482 else
2483 {
2484 out << "{}\n";
2485 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002486
Corentin Wallez1239ee92015-03-19 14:38:02 -07002487 mCurrentFunctionMetadata = nullptr;
2488
2489 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2490 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002491 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002492 ASSERT(name != "main");
2493 mOutputLod0Function = true;
2494 node->traverse(this);
2495 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002496 }
2497
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002498 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002499 }
2500 break;
2501 case EOpFunctionCall:
2502 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002503 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002504
Corentin Wallez1239ee92015-03-19 14:38:02 -07002505 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002506 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002508 if (node->isArray())
2509 {
2510 UNIMPLEMENTED();
2511 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002512 size_t index = mCallDag.findIndex(node);
2513 ASSERT(index != CallDAG::InvalidIndex);
2514 lod0 &= mASTMetadataList[index].mNeedsLod0;
2515
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002516 out << DecorateFunctionIfNeeded(node->getNameObj());
2517 out << DisambiguateFunctionName(node->getSequence());
2518 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002519 }
2520 else
2521 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002522 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002523 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002524
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002525 TextureFunction textureFunction;
2526 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002527 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002528 textureFunction.method = TextureFunction::IMPLICIT;
2529 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002530 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002531
2532 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002533 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002534 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002535 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002536 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002537 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002538 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002539 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002540 }
Nicolas Capens46485082014-04-15 13:12:50 -04002541 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2542 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002543 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002544 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002545 }
Nicolas Capens46485082014-04-15 13:12:50 -04002546 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002547 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002548 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002549 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002550 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002551 else if (name == "textureSize")
2552 {
2553 textureFunction.method = TextureFunction::SIZE;
2554 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002555 else if (name == "textureOffset")
2556 {
2557 textureFunction.method = TextureFunction::IMPLICIT;
2558 textureFunction.offset = true;
2559 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002560 else if (name == "textureProjOffset")
2561 {
2562 textureFunction.method = TextureFunction::IMPLICIT;
2563 textureFunction.offset = true;
2564 textureFunction.proj = true;
2565 }
2566 else if (name == "textureLodOffset")
2567 {
2568 textureFunction.method = TextureFunction::LOD;
2569 textureFunction.offset = true;
2570 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002571 else if (name == "textureProjLodOffset")
2572 {
2573 textureFunction.method = TextureFunction::LOD;
2574 textureFunction.proj = true;
2575 textureFunction.offset = true;
2576 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002577 else if (name == "texelFetch")
2578 {
2579 textureFunction.method = TextureFunction::FETCH;
2580 }
2581 else if (name == "texelFetchOffset")
2582 {
2583 textureFunction.method = TextureFunction::FETCH;
2584 textureFunction.offset = true;
2585 }
Nicolas Capens46485082014-04-15 13:12:50 -04002586 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002587 {
2588 textureFunction.method = TextureFunction::GRAD;
2589 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002590 else if (name == "textureGradOffset")
2591 {
2592 textureFunction.method = TextureFunction::GRAD;
2593 textureFunction.offset = true;
2594 }
Nicolas Capens46485082014-04-15 13:12:50 -04002595 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002596 {
2597 textureFunction.method = TextureFunction::GRAD;
2598 textureFunction.proj = true;
2599 }
2600 else if (name == "textureProjGradOffset")
2601 {
2602 textureFunction.method = TextureFunction::GRAD;
2603 textureFunction.proj = true;
2604 textureFunction.offset = true;
2605 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002606 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002607
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002608 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002609 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002610 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2611
2612 if (textureFunction.offset)
2613 {
2614 mandatoryArgumentCount++;
2615 }
2616
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002617 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002618
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002619 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002620 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002621 if (bias)
2622 {
2623 textureFunction.method = TextureFunction::LOD0BIAS;
2624 }
2625 else
2626 {
2627 textureFunction.method = TextureFunction::LOD0;
2628 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002629 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002630 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002631 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002632 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002633 }
2634 }
2635
2636 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002637
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002638 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002639 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002640
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002641 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002642 {
Olli Etuaho96963162016-03-21 11:54:33 +02002643 TIntermTyped *typedArg = (*arg)->getAsTyped();
2644 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002645 {
2646 out << "texture_";
2647 (*arg)->traverse(this);
2648 out << ", sampler_";
2649 }
2650
2651 (*arg)->traverse(this);
2652
Olli Etuaho96963162016-03-21 11:54:33 +02002653 if (typedArg->getType().isStructureContainingSamplers())
2654 {
2655 const TType &argType = typedArg->getType();
2656 TVector<TIntermSymbol *> samplerSymbols;
2657 TString structName = samplerNamePrefixFromStruct(typedArg);
2658 argType.createSamplerSymbols("angle_" + structName, "",
2659 argType.isArray() ? argType.getArraySize() : 0,
2660 &samplerSymbols, nullptr);
2661 for (const TIntermSymbol *sampler : samplerSymbols)
2662 {
2663 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2664 {
2665 out << ", texture_" << sampler->getSymbol();
2666 out << ", sampler_" << sampler->getSymbol();
2667 }
2668 else
2669 {
2670 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
2671 // of D3D9, it's the sampler variable.
2672 out << ", " + sampler->getSymbol();
2673 }
2674 }
2675 }
2676
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002677 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002678 {
2679 out << ", ";
2680 }
2681 }
2682
2683 out << ")";
2684
2685 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686 }
2687 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002688 case EOpParameters:
2689 outputTriplet(out, visit, "(", ", ", ")\n{\n");
2690 break;
2691 case EOpConstructFloat:
2692 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
2693 break;
2694 case EOpConstructVec2:
2695 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
2696 break;
2697 case EOpConstructVec3:
2698 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
2699 break;
2700 case EOpConstructVec4:
2701 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
2702 break;
2703 case EOpConstructBool:
2704 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
2705 break;
2706 case EOpConstructBVec2:
2707 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
2708 break;
2709 case EOpConstructBVec3:
2710 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
2711 break;
2712 case EOpConstructBVec4:
2713 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
2714 break;
2715 case EOpConstructInt:
2716 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
2717 break;
2718 case EOpConstructIVec2:
2719 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
2720 break;
2721 case EOpConstructIVec3:
2722 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
2723 break;
2724 case EOpConstructIVec4:
2725 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
2726 break;
2727 case EOpConstructUInt:
2728 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
2729 break;
2730 case EOpConstructUVec2:
2731 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
2732 break;
2733 case EOpConstructUVec3:
2734 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
2735 break;
2736 case EOpConstructUVec4:
2737 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
2738 break;
2739 case EOpConstructMat2:
2740 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
2741 break;
2742 case EOpConstructMat2x3:
2743 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
2744 break;
2745 case EOpConstructMat2x4:
2746 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
2747 break;
2748 case EOpConstructMat3x2:
2749 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
2750 break;
2751 case EOpConstructMat3:
2752 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
2753 break;
2754 case EOpConstructMat3x4:
2755 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
2756 break;
2757 case EOpConstructMat4x2:
2758 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
2759 break;
2760 case EOpConstructMat4x3:
2761 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
2762 break;
2763 case EOpConstructMat4:
2764 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
2765 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002766 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002767 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002768 if (node->getType().isArray())
2769 {
2770 UNIMPLEMENTED();
2771 }
Jamie Madill033dae62014-06-18 12:56:28 -04002772 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002773 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002774 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002775 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002776 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002777 case EOpLessThan:
2778 outputTriplet(out, visit, "(", " < ", ")");
2779 break;
2780 case EOpGreaterThan:
2781 outputTriplet(out, visit, "(", " > ", ")");
2782 break;
2783 case EOpLessThanEqual:
2784 outputTriplet(out, visit, "(", " <= ", ")");
2785 break;
2786 case EOpGreaterThanEqual:
2787 outputTriplet(out, visit, "(", " >= ", ")");
2788 break;
2789 case EOpVectorEqual:
2790 outputTriplet(out, visit, "(", " == ", ")");
2791 break;
2792 case EOpVectorNotEqual:
2793 outputTriplet(out, visit, "(", " != ", ")");
2794 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002795 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002796 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002797 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002798 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002799 case EOpModf:
2800 outputTriplet(out, visit, "modf(", ", ", ")");
2801 break;
2802 case EOpPow:
2803 outputTriplet(out, visit, "pow(", ", ", ")");
2804 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002805 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002806 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002807 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002808 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002809 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002810 case EOpMin:
2811 outputTriplet(out, visit, "min(", ", ", ")");
2812 break;
2813 case EOpMax:
2814 outputTriplet(out, visit, "max(", ", ", ")");
2815 break;
2816 case EOpClamp:
2817 outputTriplet(out, visit, "clamp(", ", ", ")");
2818 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302819 case EOpMix:
2820 {
2821 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2822 if (lastParamNode->getType().getBasicType() == EbtBool)
2823 {
2824 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2825 // so use emulated version.
2826 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002827 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05302828 }
2829 else
2830 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002831 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302832 }
2833 }
2834 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002835 case EOpStep:
2836 outputTriplet(out, visit, "step(", ", ", ")");
2837 break;
2838 case EOpSmoothStep:
2839 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2840 break;
2841 case EOpDistance:
2842 outputTriplet(out, visit, "distance(", ", ", ")");
2843 break;
2844 case EOpDot:
2845 outputTriplet(out, visit, "dot(", ", ", ")");
2846 break;
2847 case EOpCross:
2848 outputTriplet(out, visit, "cross(", ", ", ")");
2849 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002850 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002851 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002852 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002853 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002854 case EOpReflect:
2855 outputTriplet(out, visit, "reflect(", ", ", ")");
2856 break;
2857 case EOpRefract:
2858 outputTriplet(out, visit, "refract(", ", ", ")");
2859 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002860 case EOpOuterProduct:
2861 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002862 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02002863 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002864 case EOpMul:
2865 outputTriplet(out, visit, "(", " * ", ")");
2866 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867 default: UNREACHABLE();
2868 }
2869
2870 return true;
2871}
2872
Jamie Madill8c46ab12015-12-07 16:39:19 -05002873void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002875 out << "if (";
2876
2877 node->getCondition()->traverse(this);
2878
2879 out << ")\n";
2880
Jamie Madill8c46ab12015-12-07 16:39:19 -05002881 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002882
2883 bool discard = false;
2884
2885 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002886 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002887 // The trueBlock child node will output braces.
2888 ASSERT(IsSequence(node->getTrueBlock()));
2889
Olli Etuahoa6f22092015-05-08 18:31:10 +03002890 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002891
Olli Etuahoa6f22092015-05-08 18:31:10 +03002892 // Detect true discard
2893 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2894 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002895 else
2896 {
2897 // TODO(oetuaho): Check if the semicolon inside is necessary.
2898 // It's there as a result of conservative refactoring of the output.
2899 out << "{;}\n";
2900 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002901
Jamie Madill8c46ab12015-12-07 16:39:19 -05002902 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002903
Olli Etuahoa6f22092015-05-08 18:31:10 +03002904 if (node->getFalseBlock())
2905 {
2906 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002907
Jamie Madill8c46ab12015-12-07 16:39:19 -05002908 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002909
Olli Etuaho4785fec2015-05-18 16:09:37 +03002910 // Either this is "else if" or the falseBlock child node will output braces.
2911 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2912
Olli Etuahoa6f22092015-05-08 18:31:10 +03002913 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002914
Jamie Madill8c46ab12015-12-07 16:39:19 -05002915 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002916
Olli Etuahoa6f22092015-05-08 18:31:10 +03002917 // Detect false discard
2918 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2919 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002920
Olli Etuahoa6f22092015-05-08 18:31:10 +03002921 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002922 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002923 {
2924 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002925 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002926}
2927
2928bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2929{
2930 TInfoSinkBase &out = getInfoSink();
2931
2932 ASSERT(!node->usesTernaryOperator());
2933
2934 if (!mInsideFunction)
2935 {
2936 // This is part of unfolded global initialization.
2937 mDeferredGlobalInitializers.push_back(node);
2938 return false;
2939 }
2940
2941 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002942 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002943 {
2944 out << "FLATTEN ";
2945 }
2946
Jamie Madill8c46ab12015-12-07 16:39:19 -05002947 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002948
2949 return false;
2950}
2951
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002952bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002953{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002954 TInfoSinkBase &out = getInfoSink();
2955
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002956 if (node->getStatementList())
2957 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002958 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002959 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002960 // The curly braces get written when visiting the statementList aggregate
2961 }
2962 else
2963 {
2964 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002965 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002966 }
2967 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002968}
2969
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002970bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002971{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002972 TInfoSinkBase &out = getInfoSink();
2973
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002974 if (node->hasCondition())
2975 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002976 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002977 return true;
2978 }
2979 else
2980 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002981 out << "default:\n";
2982 return false;
2983 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002984}
2985
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002986void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2987{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002988 TInfoSinkBase &out = getInfoSink();
2989 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002990}
2991
2992bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2993{
Nicolas Capens655fe362014-04-11 13:12:34 -04002994 mNestedLoopDepth++;
2995
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002996 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002997 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002998 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002999
Jamie Madill8c46ab12015-12-07 16:39:19 -05003000 TInfoSinkBase &out = getInfoSink();
3001
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003002 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003003 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003004 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00003005 {
Nicolas Capens655fe362014-04-11 13:12:34 -04003006 mInsideDiscontinuousLoop = wasDiscontinuous;
3007 mNestedLoopDepth--;
3008
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00003009 return false;
3010 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003011 }
3012
Corentin Wallez1239ee92015-03-19 14:38:02 -07003013 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00003014 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003015 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07003016 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003017
Jamie Madill8c46ab12015-12-07 16:39:19 -05003018 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003019 }
3020 else
3021 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07003022 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003023
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003024 if (node->getInit())
3025 {
3026 node->getInit()->traverse(this);
3027 }
3028
3029 out << "; ";
3030
alokp@chromium.org52813552010-11-16 18:36:09 +00003031 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003032 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003033 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003034 }
3035
3036 out << "; ";
3037
alokp@chromium.org52813552010-11-16 18:36:09 +00003038 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003039 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003040 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003041 }
3042
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003043 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003044
Jamie Madill8c46ab12015-12-07 16:39:19 -05003045 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003046 }
3047
3048 if (node->getBody())
3049 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03003050 // The loop body node will output braces.
3051 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03003052 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003053 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03003054 else
3055 {
3056 // TODO(oetuaho): Check if the semicolon inside is necessary.
3057 // It's there as a result of conservative refactoring of the output.
3058 out << "{;}\n";
3059 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003060
Jamie Madill8c46ab12015-12-07 16:39:19 -05003061 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003062
alokp@chromium.org52813552010-11-16 18:36:09 +00003063 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003064 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003065 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003066 out << "while(\n";
3067
alokp@chromium.org52813552010-11-16 18:36:09 +00003068 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003069
daniel@transgaming.com73536982012-03-21 20:45:49 +00003070 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003071 }
3072
daniel@transgaming.com73536982012-03-21 20:45:49 +00003073 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003074
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003075 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04003076 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00003077
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003078 return false;
3079}
3080
3081bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
3082{
Jamie Madill32aab012015-01-27 14:12:26 -05003083 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003084
3085 switch (node->getFlowOp())
3086 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003087 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05003088 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003089 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003090 case EOpBreak:
3091 if (visit == PreVisit)
3092 {
Nicolas Capens655fe362014-04-11 13:12:34 -04003093 if (mNestedLoopDepth > 1)
3094 {
3095 mUsesNestedBreak = true;
3096 }
3097
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003098 if (mExcessiveLoopIndex)
3099 {
3100 out << "{Break";
3101 mExcessiveLoopIndex->traverse(this);
3102 out << " = true; break;}\n";
3103 }
3104 else
3105 {
3106 out << "break;\n";
3107 }
3108 }
3109 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05003110 case EOpContinue:
3111 outputTriplet(out, visit, "continue;\n", "", "");
3112 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003113 case EOpReturn:
3114 if (visit == PreVisit)
3115 {
3116 if (node->getExpression())
3117 {
3118 out << "return ";
3119 }
3120 else
3121 {
3122 out << "return;\n";
3123 }
3124 }
3125 else if (visit == PostVisit)
3126 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003127 if (node->getExpression())
3128 {
3129 out << ";\n";
3130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003131 }
3132 break;
3133 default: UNREACHABLE();
3134 }
3135
3136 return true;
3137}
3138
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003139bool OutputHLSL::isSingleStatement(TIntermNode *node)
3140{
3141 TIntermAggregate *aggregate = node->getAsAggregate();
3142
3143 if (aggregate)
3144 {
3145 if (aggregate->getOp() == EOpSequence)
3146 {
3147 return false;
3148 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04003149 else if (aggregate->getOp() == EOpDeclaration)
3150 {
3151 // Declaring multiple comma-separated variables must be considered multiple statements
3152 // because each individual declaration has side effects which are visible in the next.
3153 return false;
3154 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003155 else
3156 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07003157 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00003158 {
3159 if (!isSingleStatement(*sit))
3160 {
3161 return false;
3162 }
3163 }
3164
3165 return true;
3166 }
3167 }
3168
3169 return true;
3170}
3171
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003172// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
3173// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05003174bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003175{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003176 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003177
3178 // Parse loops of the form:
3179 // for(int index = initial; index [comparator] limit; index += increment)
3180 TIntermSymbol *index = NULL;
3181 TOperator comparator = EOpNull;
3182 int initial = 0;
3183 int limit = 0;
3184 int increment = 0;
3185
3186 // Parse index name and intial value
3187 if (node->getInit())
3188 {
3189 TIntermAggregate *init = node->getInit()->getAsAggregate();
3190
3191 if (init)
3192 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07003193 TIntermSequence *sequence = init->getSequence();
3194 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003195
3196 if (variable && variable->getQualifier() == EvqTemporary)
3197 {
3198 TIntermBinary *assign = variable->getAsBinaryNode();
3199
3200 if (assign->getOp() == EOpInitialize)
3201 {
3202 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3203 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3204
3205 if (symbol && constant)
3206 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003207 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003208 {
3209 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003210 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003211 }
3212 }
3213 }
3214 }
3215 }
3216 }
3217
3218 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003219 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003220 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003221 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003222
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003223 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3224 {
3225 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3226
3227 if (constant)
3228 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003229 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003230 {
3231 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003232 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003233 }
3234 }
3235 }
3236 }
3237
3238 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003239 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003240 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003241 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3242 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003243
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003244 if (binaryTerminal)
3245 {
3246 TOperator op = binaryTerminal->getOp();
3247 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3248
3249 if (constant)
3250 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003251 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003252 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003253 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003254
3255 switch (op)
3256 {
3257 case EOpAddAssign: increment = value; break;
3258 case EOpSubAssign: increment = -value; break;
3259 default: UNIMPLEMENTED();
3260 }
3261 }
3262 }
3263 }
3264 else if (unaryTerminal)
3265 {
3266 TOperator op = unaryTerminal->getOp();
3267
3268 switch (op)
3269 {
3270 case EOpPostIncrement: increment = 1; break;
3271 case EOpPostDecrement: increment = -1; break;
3272 case EOpPreIncrement: increment = 1; break;
3273 case EOpPreDecrement: increment = -1; break;
3274 default: UNIMPLEMENTED();
3275 }
3276 }
3277 }
3278
3279 if (index != NULL && comparator != EOpNull && increment != 0)
3280 {
3281 if (comparator == EOpLessThanEqual)
3282 {
3283 comparator = EOpLessThan;
3284 limit += 1;
3285 }
3286
3287 if (comparator == EOpLessThan)
3288 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003289 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003290
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003291 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003292 {
3293 return false; // Not an excessive loop
3294 }
3295
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003296 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3297 mExcessiveLoopIndex = index;
3298
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003299 out << "{int ";
3300 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003301 out << ";\n"
3302 "bool Break";
3303 index->traverse(this);
3304 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003305
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003306 bool firstLoopFragment = true;
3307
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003308 while (iterations > 0)
3309 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003310 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003311
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003312 if (!firstLoopFragment)
3313 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003314 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003315 index->traverse(this);
3316 out << ") {\n";
3317 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003318
3319 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3320 {
3321 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3322 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003323
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003324 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07003325 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003326
Corentin Wallez1239ee92015-03-19 14:38:02 -07003327 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003328 index->traverse(this);
3329 out << " = ";
3330 out << initial;
3331
3332 out << "; ";
3333 index->traverse(this);
3334 out << " < ";
3335 out << clampedLimit;
3336
3337 out << "; ";
3338 index->traverse(this);
3339 out << " += ";
3340 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003341 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003342
Jamie Madill8c46ab12015-12-07 16:39:19 -05003343 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003344 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003345
3346 if (node->getBody())
3347 {
3348 node->getBody()->traverse(this);
3349 }
3350
Jamie Madill8c46ab12015-12-07 16:39:19 -05003351 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003352 out << ";}\n";
3353
3354 if (!firstLoopFragment)
3355 {
3356 out << "}\n";
3357 }
3358
3359 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003360
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003361 initial += MAX_LOOP_ITERATIONS * increment;
3362 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003363 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003364
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003365 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003366
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003367 mExcessiveLoopIndex = restoreIndex;
3368
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003369 return true;
3370 }
3371 else UNIMPLEMENTED();
3372 }
3373
3374 return false; // Not handled as an excessive loop
3375}
3376
Jamie Madill8c46ab12015-12-07 16:39:19 -05003377void OutputHLSL::outputTriplet(TInfoSinkBase &out,
3378 Visit visit,
3379 const char *preString,
3380 const char *inString,
3381 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003382{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003383 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003384 {
3385 out << preString;
3386 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003387 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003388 {
3389 out << inString;
3390 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003391 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003392 {
3393 out << postString;
3394 }
3395}
3396
Jamie Madill8c46ab12015-12-07 16:39:19 -05003397void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003398{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003399 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003400 {
Jamie Madill32aab012015-01-27 14:12:26 -05003401 out << "\n";
3402 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003403
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003404 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003405 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003406 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003407 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003408
Jamie Madill32aab012015-01-27 14:12:26 -05003409 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003410 }
3411}
3412
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003413TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3414{
3415 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003416 const TType &type = symbol->getType();
3417 const TName &name = symbol->getName();
3418 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003419
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003420 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003421 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003422 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003423 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003424 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003425 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003426 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003427 }
3428
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003429 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003430 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003431 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3432 {
3433 // Samplers are passed as indices to the sampler array.
3434 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3435 return "const uint " + nameStr + ArrayString(type);
3436 }
3437 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3438 {
3439 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
3440 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
3441 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
3442 ArrayString(type);
3443 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003444 }
3445
Olli Etuaho96963162016-03-21 11:54:33 +02003446 TStringStream argString;
3447 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
3448 << ArrayString(type);
3449
3450 // If the structure parameter contains samplers, they need to be passed into the function as
3451 // separate parameters. HLSL doesn't natively support samplers in structs.
3452 if (type.isStructureContainingSamplers())
3453 {
3454 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3455 TVector<TIntermSymbol *> samplerSymbols;
3456 type.createSamplerSymbols("angle" + nameStr, "", 0, &samplerSymbols, nullptr);
3457 for (const TIntermSymbol *sampler : samplerSymbols)
3458 {
3459 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3460 {
3461 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
3462 }
3463 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3464 {
3465 const TType &samplerType = sampler->getType();
3466 ASSERT((!type.isArray() && !samplerType.isArray()) ||
3467 type.getArraySize() == samplerType.getArraySize());
3468 ASSERT(IsSampler(samplerType.getBasicType()));
3469 argString << ", " << QualifierString(qualifier) << " "
3470 << TextureString(samplerType.getBasicType()) << " texture_"
3471 << sampler->getSymbol() << ArrayString(type) << ", "
3472 << QualifierString(qualifier) << " "
3473 << SamplerString(samplerType.getBasicType()) << " sampler_"
3474 << sampler->getSymbol() << ArrayString(type);
3475 }
3476 else
3477 {
3478 const TType &samplerType = sampler->getType();
3479 ASSERT((!type.isArray() && !samplerType.isArray()) ||
3480 type.getArraySize() == samplerType.getArraySize());
3481 ASSERT(IsSampler(samplerType.getBasicType()));
3482 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
3483 << " " << sampler->getSymbol() << ArrayString(type);
3484 }
3485 }
3486 }
3487
3488 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003489}
3490
3491TString OutputHLSL::initializer(const TType &type)
3492{
3493 TString string;
3494
Jamie Madill94bf7f22013-07-08 13:31:15 -04003495 size_t size = type.getObjectSize();
3496 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003497 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003498 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003499
Jamie Madill94bf7f22013-07-08 13:31:15 -04003500 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003501 {
3502 string += ", ";
3503 }
3504 }
3505
daniel@transgaming.comead23042010-04-29 03:35:36 +00003506 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003507}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003508
Jamie Madill8c46ab12015-12-07 16:39:19 -05003509void OutputHLSL::outputConstructor(TInfoSinkBase &out,
3510 Visit visit,
3511 const TType &type,
3512 const char *name,
3513 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003514{
Olli Etuahof40319e2015-03-10 14:33:00 +02003515 if (type.isArray())
3516 {
3517 UNIMPLEMENTED();
3518 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003519
3520 if (visit == PreVisit)
3521 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003522 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003523
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003524 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003525 }
3526 else if (visit == InVisit)
3527 {
3528 out << ", ";
3529 }
3530 else if (visit == PostVisit)
3531 {
3532 out << ")";
3533 }
3534}
3535
Jamie Madill8c46ab12015-12-07 16:39:19 -05003536const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
3537 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003538 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003539{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003540 const TConstantUnion *constUnionIterated = constUnion;
3541
Jamie Madill98493dd2013-07-08 14:39:03 -04003542 const TStructure* structure = type.getStruct();
3543 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003544 {
Jamie Madill033dae62014-06-18 12:56:28 -04003545 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003546
Jamie Madill98493dd2013-07-08 14:39:03 -04003547 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003548
Jamie Madill98493dd2013-07-08 14:39:03 -04003549 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003550 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003551 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05003552 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003553
Jamie Madill98493dd2013-07-08 14:39:03 -04003554 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003555 {
3556 out << ", ";
3557 }
3558 }
3559
3560 out << ")";
3561 }
3562 else
3563 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003564 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003565 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04003566
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003567 if (writeType)
3568 {
Jamie Madill033dae62014-06-18 12:56:28 -04003569 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003570 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003571 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003572 if (writeType)
3573 {
3574 out << ")";
3575 }
3576 }
3577
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003578 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003579}
3580
Jamie Madill8c46ab12015-12-07 16:39:19 -05003581void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003582{
3583 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05003584 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003585}
3586
Jamie Madill37997142015-01-28 10:06:34 -05003587bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3588{
3589 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3590 expression->traverse(&searchSymbol);
3591
3592 if (searchSymbol.foundMatch())
3593 {
3594 // Type already printed
3595 out << "t" + str(mUniqueIndex) + " = ";
3596 expression->traverse(this);
3597 out << ", ";
3598 symbolNode->traverse(this);
3599 out << " = t" + str(mUniqueIndex);
3600
3601 mUniqueIndex++;
3602 return true;
3603 }
3604
3605 return false;
3606}
3607
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003608bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3609{
3610 // We support writing constant unions and constructors that only take constant unions as
3611 // parameters as HLSL literals.
3612 if (expression->getAsConstantUnion())
3613 {
3614 return true;
3615 }
3616 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3617 !expression->getAsAggregate()->isConstructor())
3618 {
3619 return false;
3620 }
3621 TIntermAggregate *constructor = expression->getAsAggregate();
3622 for (TIntermNode *&node : *constructor->getSequence())
3623 {
3624 if (!node->getAsConstantUnion())
3625 return false;
3626 }
3627 return true;
3628}
3629
3630bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3631 TIntermSymbol *symbolNode,
3632 TIntermTyped *expression)
3633{
3634 if (canWriteAsHLSLLiteral(expression))
3635 {
3636 symbolNode->traverse(this);
3637 if (expression->getType().isArray())
3638 {
3639 out << "[" << expression->getType().getArraySize() << "]";
3640 }
3641 out << " = {";
3642 if (expression->getAsConstantUnion())
3643 {
3644 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3645 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3646 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3647 }
3648 else
3649 {
3650 TIntermAggregate *constructor = expression->getAsAggregate();
3651 ASSERT(constructor != nullptr);
3652 for (TIntermNode *&node : *constructor->getSequence())
3653 {
3654 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3655 ASSERT(nodeConst);
3656 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3657 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3658 if (node != constructor->getSequence()->back())
3659 {
3660 out << ", ";
3661 }
3662 }
3663 }
3664 out << "}";
3665 return true;
3666 }
3667 return false;
3668}
3669
Jamie Madill37997142015-01-28 10:06:34 -05003670void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3671{
3672 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3673 << "\n"
3674 << "void initializeDeferredGlobals()\n"
3675 << "{\n";
3676
3677 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3678 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003679 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3680 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3681 if (binary != nullptr)
3682 {
3683 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3684 TIntermTyped *expression = binary->getRight();
3685 ASSERT(symbol);
3686 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003687
Olli Etuahod81ed842015-05-12 12:46:35 +03003688 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003689
Olli Etuahod81ed842015-05-12 12:46:35 +03003690 if (!writeSameSymbolInitializer(out, symbol, expression))
3691 {
3692 ASSERT(mInfoSinkStack.top() == &out);
3693 expression->traverse(this);
3694 }
3695 out << ";\n";
3696 }
3697 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003698 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003699 writeSelection(out, selection);
Jamie Madill37997142015-01-28 10:06:34 -05003700 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003701 else
3702 {
3703 UNREACHABLE();
3704 }
Jamie Madill37997142015-01-28 10:06:34 -05003705 }
3706
3707 out << "}\n"
3708 << "\n";
3709}
3710
Jamie Madill55e79e02015-02-09 15:35:00 -05003711TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3712{
3713 const TFieldList &fields = structure.fields();
3714
3715 for (const auto &eqFunction : mStructEqualityFunctions)
3716 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003717 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003718 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003719 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003720 }
3721 }
3722
3723 const TString &structNameString = StructNameString(structure);
3724
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003725 StructEqualityFunction *function = new StructEqualityFunction();
3726 function->structure = &structure;
3727 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003728
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003729 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003730
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003731 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3732 << "{\n"
3733 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003734
3735 for (size_t i = 0; i < fields.size(); i++)
3736 {
3737 const TField *field = fields[i];
3738 const TType *fieldType = field->type();
3739
3740 const TString &fieldNameA = "a." + Decorate(field->name());
3741 const TString &fieldNameB = "b." + Decorate(field->name());
3742
3743 if (i > 0)
3744 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003745 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003746 }
3747
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003748 fnOut << "(";
3749 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3750 fnOut << fieldNameA;
3751 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3752 fnOut << fieldNameB;
3753 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3754 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003755 }
3756
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003757 fnOut << ";\n" << "}\n";
3758
3759 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003760
3761 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003762 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003763
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003764 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003765}
3766
Olli Etuaho7fb49552015-03-18 17:27:44 +02003767TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3768{
3769 for (const auto &eqFunction : mArrayEqualityFunctions)
3770 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003771 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003772 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003773 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003774 }
3775 }
3776
3777 const TString &typeName = TypeString(type);
3778
Olli Etuaho12690762015-03-31 12:55:28 +03003779 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003780 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003781
3782 TInfoSinkBase fnNameOut;
3783 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003784 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003785
3786 TType nonArrayType = type;
3787 nonArrayType.clearArrayness();
3788
3789 TInfoSinkBase fnOut;
3790
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003791 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003792 << typeName << " a[" << type.getArraySize() << "], "
3793 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003794 << "{\n"
3795 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3796 " {\n"
3797 " if (";
3798
3799 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3800 fnOut << "a[i]";
3801 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3802 fnOut << "b[i]";
3803 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3804
3805 fnOut << ") { return false; }\n"
3806 " }\n"
3807 " return true;\n"
3808 "}\n";
3809
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003810 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003811
3812 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003813 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003814
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003815 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003816}
3817
Olli Etuaho12690762015-03-31 12:55:28 +03003818TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3819{
3820 for (const auto &assignFunction : mArrayAssignmentFunctions)
3821 {
3822 if (assignFunction.type == type)
3823 {
3824 return assignFunction.functionName;
3825 }
3826 }
3827
3828 const TString &typeName = TypeString(type);
3829
3830 ArrayHelperFunction function;
3831 function.type = type;
3832
3833 TInfoSinkBase fnNameOut;
3834 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3835 function.functionName = fnNameOut.c_str();
3836
3837 TInfoSinkBase fnOut;
3838
3839 fnOut << "void " << function.functionName << "(out "
3840 << typeName << " a[" << type.getArraySize() << "], "
3841 << typeName << " b[" << type.getArraySize() << "])\n"
3842 << "{\n"
3843 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3844 " {\n"
3845 " a[i] = b[i];\n"
3846 " }\n"
3847 "}\n";
3848
3849 function.functionDefinition = fnOut.c_str();
3850
3851 mArrayAssignmentFunctions.push_back(function);
3852
3853 return function.functionName;
3854}
3855
Olli Etuaho9638c352015-04-01 14:34:52 +03003856TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3857{
3858 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3859 {
3860 if (constructIntoFunction.type == type)
3861 {
3862 return constructIntoFunction.functionName;
3863 }
3864 }
3865
3866 const TString &typeName = TypeString(type);
3867
3868 ArrayHelperFunction function;
3869 function.type = type;
3870
3871 TInfoSinkBase fnNameOut;
3872 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3873 function.functionName = fnNameOut.c_str();
3874
3875 TInfoSinkBase fnOut;
3876
3877 fnOut << "void " << function.functionName << "(out "
3878 << typeName << " a[" << type.getArraySize() << "]";
3879 for (int i = 0; i < type.getArraySize(); ++i)
3880 {
3881 fnOut << ", " << typeName << " b" << i;
3882 }
3883 fnOut << ")\n"
3884 "{\n";
3885
3886 for (int i = 0; i < type.getArraySize(); ++i)
3887 {
3888 fnOut << " a[" << i << "] = b" << i << ";\n";
3889 }
3890 fnOut << "}\n";
3891
3892 function.functionDefinition = fnOut.c_str();
3893
3894 mArrayConstructIntoFunctions.push_back(function);
3895
3896 return function.functionName;
3897}
3898
Jamie Madill2e295e22015-04-29 10:41:33 -04003899void OutputHLSL::ensureStructDefined(const TType &type)
3900{
3901 TStructure *structure = type.getStruct();
3902
3903 if (structure)
3904 {
3905 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3906 }
3907}
3908
3909
Olli Etuaho9638c352015-04-01 14:34:52 +03003910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003911}