blob: d8aeccdb2f247e2e602480942c320f26a3e96001 [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
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030namespace sh
31{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000032
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033TString OutputHLSL::TextureFunction::name() const
34{
35 TString name = "gl_texture";
36
Nicolas Capens6d232bb2013-07-08 15:56:38 -040037 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040038 {
39 name += "2D";
40 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040041 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040042 {
43 name += "3D";
44 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040045 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040046 {
47 name += "Cube";
48 }
49 else UNREACHABLE();
50
51 if (proj)
52 {
53 name += "Proj";
54 }
55
Nicolas Capensb1f45b72013-12-19 17:37:19 -050056 if (offset)
57 {
58 name += "Offset";
59 }
60
Nicolas Capens75fb4752013-07-10 15:14:47 -040061 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040062 {
Nicolas Capensfc014542014-02-18 14:47:13 -050063 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040064 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050065 case LOD: name += "Lod"; break;
66 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040067 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050068 case SIZE: name += "Size"; break;
69 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050070 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040071 default: UNREACHABLE();
72 }
73
74 return name + "(";
75}
76
77bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
78{
79 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040080 if (sampler > rhs.sampler) return false;
81
Nicolas Capense0ba27a2013-06-24 16:10:52 -040082 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040083 if (coords > rhs.coords) return false;
84
Nicolas Capense0ba27a2013-06-24 16:10:52 -040085 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040086 if (proj && !rhs.proj) return false;
87
88 if (!offset && rhs.offset) return true;
89 if (offset && !rhs.offset) return false;
90
Nicolas Capens75fb4752013-07-10 15:14:47 -040091 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040092 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040093
94 return false;
95}
96
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020097OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
98 const TExtensionBehavior &extensionBehavior,
99 const char *sourcePath, ShShaderOutput outputType,
100 int numRenderTargets, const std::vector<Uniform> &uniforms,
101 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400102 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200103 mShaderType(shaderType),
104 mShaderVersion(shaderVersion),
105 mExtensionBehavior(extensionBehavior),
106 mSourcePath(sourcePath),
107 mOutputType(outputType),
108 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700109 mCompileOptions(compileOptions),
110 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000112 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000113
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000114 mUsesFragColor = false;
115 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000116 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000117 mUsesFragCoord = false;
118 mUsesPointCoord = false;
119 mUsesFrontFacing = false;
120 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000121 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400122 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000123 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500124 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400125 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530126 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000127
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000128 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000129
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000130 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000131 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400132 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000133
134 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000135
Jamie Madill8daaba12014-06-13 10:04:33 -0400136 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200137 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400138
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000139 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000140 {
Arun Patole63419392015-03-13 11:51:07 +0530141 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
142 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
143 // In both cases total 3 uniform registers need to be reserved.
144 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000145 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000146
Jamie Madillf91ce812014-06-13 10:04:34 -0400147 // Reserve registers for the default uniform block and driver constants
148 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149}
150
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000151OutputHLSL::~OutputHLSL()
152{
Jamie Madill8daaba12014-06-13 10:04:33 -0400153 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400154 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200155 for (auto &eqFunction : mStructEqualityFunctions)
156 {
157 SafeDelete(eqFunction);
158 }
159 for (auto &eqFunction : mArrayEqualityFunctions)
160 {
161 SafeDelete(eqFunction);
162 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000163}
164
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200165void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000166{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200167 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400168 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000169
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200170 BuiltInFunctionEmulator builtInFunctionEmulator;
171 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200172 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500173
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700174 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700175 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
176 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300177 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700178 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700179
Jamie Madill37997142015-01-28 10:06:34 -0500180 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500181 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200182 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500183 mInfoSinkStack.pop();
184
Jamie Madill37997142015-01-28 10:06:34 -0500185 mInfoSinkStack.push(&mFooter);
186 if (!mDeferredGlobalInitializers.empty())
187 {
188 writeDeferredGlobalInitializers(mFooter);
189 }
190 mInfoSinkStack.pop();
191
Jamie Madill32aab012015-01-27 14:12:26 -0500192 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200193 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500194 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000195
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200196 objSink << mHeader.c_str();
197 objSink << mBody.c_str();
198 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200199
200 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000201}
202
Jamie Madill570e04d2013-06-21 09:15:33 -0400203void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
204{
205 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
206 {
207 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
208
Jamie Madill32aab012015-01-27 14:12:26 -0500209 TInfoSinkBase structInfoSink;
210 mInfoSinkStack.push(&structInfoSink);
211
Jamie Madill570e04d2013-06-21 09:15:33 -0400212 // This will mark the necessary block elements as referenced
213 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500214
215 TString structName(structInfoSink.c_str());
216 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400217
218 mFlaggedStructOriginalNames[flaggedNode] = structName;
219
220 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
221 {
222 structName.erase(pos, 1);
223 }
224
225 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
226 }
227}
228
Jamie Madill4e1fd412014-07-10 17:50:10 -0400229const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
230{
231 return mUniformHLSL->getInterfaceBlockRegisterMap();
232}
233
Jamie Madill9fe25e92014-07-18 10:33:08 -0400234const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
235{
236 return mUniformHLSL->getUniformRegisterMap();
237}
238
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000239int OutputHLSL::vectorSize(const TType &type) const
240{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000241 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000242 int arraySize = type.isArray() ? type.getArraySize() : 1;
243
244 return elementSize * arraySize;
245}
246
Jamie Madill98493dd2013-07-08 14:39:03 -0400247TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400248{
249 TString init;
250
251 TString preIndentString;
252 TString fullIndentString;
253
254 for (int spaces = 0; spaces < (indent * 4); spaces++)
255 {
256 preIndentString += ' ';
257 }
258
259 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
260 {
261 fullIndentString += ' ';
262 }
263
264 init += preIndentString + "{\n";
265
Jamie Madill98493dd2013-07-08 14:39:03 -0400266 const TFieldList &fields = structure.fields();
267 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400268 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400270 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400271 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400272
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400274 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400276 }
277 else
278 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400279 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400280 }
281 }
282
283 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
284
285 return init;
286}
287
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200288void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289{
Jamie Madill32aab012015-01-27 14:12:26 -0500290 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000292 TString varyings;
293 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400294 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000295
Jamie Madill829f59e2013-11-13 19:40:54 -0500296 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400297 {
298 TIntermTyped *structNode = flaggedStructIt->first;
299 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400300 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400301 const TString &originalName = mFlaggedStructOriginalNames[structNode];
302
Jamie Madill033dae62014-06-18 12:56:28 -0400303 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400304 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400305 flaggedStructs += "\n";
306 }
307
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000308 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
309 {
310 const TType &type = varying->second->getType();
311 const TString &name = varying->second->getSymbol();
312
313 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400314 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
315 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000316 }
317
318 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
319 {
320 const TType &type = attribute->second->getType();
321 const TString &name = attribute->second->getSymbol();
322
Jamie Madill033dae62014-06-18 12:56:28 -0400323 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000324 }
325
Jamie Madill8daaba12014-06-13 10:04:33 -0400326 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400327
Jamie Madillf91ce812014-06-13 10:04:34 -0400328 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
329 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
330
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200331 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500332 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200333 out << "\n// Equality functions\n\n";
334 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500335 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200336 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200337 }
338 }
Olli Etuaho12690762015-03-31 12:55:28 +0300339 if (!mArrayAssignmentFunctions.empty())
340 {
341 out << "\n// Assignment functions\n\n";
342 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
343 {
344 out << assignmentFunction.functionDefinition << "\n";
345 }
346 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300347 if (!mArrayConstructIntoFunctions.empty())
348 {
349 out << "\n// Array constructor functions\n\n";
350 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
351 {
352 out << constructIntoFunction.functionDefinition << "\n";
353 }
354 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200355
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500356 if (mUsesDiscardRewriting)
357 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400358 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500359 }
360
Nicolas Capens655fe362014-04-11 13:12:34 -0400361 if (mUsesNestedBreak)
362 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400363 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400364 }
365
Arun Patole44efa0b2015-03-04 17:11:05 +0530366 if (mRequiresIEEEStrictCompiling)
367 {
368 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
369 }
370
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400371 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
372 "#define LOOP [loop]\n"
373 "#define FLATTEN [flatten]\n"
374 "#else\n"
375 "#define LOOP\n"
376 "#define FLATTEN\n"
377 "#endif\n";
378
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200379 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200381 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
382 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000383
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000384 out << "// Varyings\n";
385 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400386 out << "\n";
387
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200388 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000389 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500390 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000391 {
Jamie Madill46131a32013-06-20 11:55:50 -0400392 const TString &variableName = outputVariableIt->first;
393 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400394
Jamie Madill033dae62014-06-18 12:56:28 -0400395 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400396 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000397 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000398 }
Jamie Madill46131a32013-06-20 11:55:50 -0400399 else
400 {
401 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
402
403 out << "static float4 gl_Color[" << numColorValues << "] =\n"
404 "{\n";
405 for (unsigned int i = 0; i < numColorValues; i++)
406 {
407 out << " float4(0, 0, 0, 0)";
408 if (i + 1 != numColorValues)
409 {
410 out << ",";
411 }
412 out << "\n";
413 }
414
415 out << "};\n";
416 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000417
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400418 if (mUsesFragDepth)
419 {
420 out << "static float gl_Depth = 0.0;\n";
421 }
422
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000423 if (mUsesFragCoord)
424 {
425 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
426 }
427
428 if (mUsesPointCoord)
429 {
430 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
431 }
432
433 if (mUsesFrontFacing)
434 {
435 out << "static bool gl_FrontFacing = false;\n";
436 }
437
438 out << "\n";
439
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000440 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000441 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000442 out << "struct gl_DepthRangeParameters\n"
443 "{\n"
444 " float near;\n"
445 " float far;\n"
446 " float diff;\n"
447 "};\n"
448 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000449 }
450
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000451 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000452 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000453 out << "cbuffer DriverConstants : register(b1)\n"
454 "{\n";
455
456 if (mUsesDepthRange)
457 {
458 out << " float3 dx_DepthRange : packoffset(c0);\n";
459 }
460
461 if (mUsesFragCoord)
462 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000463 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000464 }
465
466 if (mUsesFragCoord || mUsesFrontFacing)
467 {
468 out << " float3 dx_DepthFront : packoffset(c2);\n";
469 }
470
471 out << "};\n";
472 }
473 else
474 {
475 if (mUsesDepthRange)
476 {
477 out << "uniform float3 dx_DepthRange : register(c0);";
478 }
479
480 if (mUsesFragCoord)
481 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000482 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000483 }
484
485 if (mUsesFragCoord || mUsesFrontFacing)
486 {
487 out << "uniform float3 dx_DepthFront : register(c2);\n";
488 }
489 }
490
491 out << "\n";
492
493 if (mUsesDepthRange)
494 {
495 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
496 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000497 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000498
Jamie Madillf91ce812014-06-13 10:04:34 -0400499 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000500 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400501 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000502 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400503 out << flaggedStructs;
504 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000505 }
506
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000507 if (usingMRTExtension && mNumRenderTargets > 1)
508 {
509 out << "#define GL_USES_MRT\n";
510 }
511
512 if (mUsesFragColor)
513 {
514 out << "#define GL_USES_FRAG_COLOR\n";
515 }
516
517 if (mUsesFragData)
518 {
519 out << "#define GL_USES_FRAG_DATA\n";
520 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000522 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000523 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000524 out << "// Attributes\n";
525 out << attributes;
526 out << "\n"
527 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400528
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000529 if (mUsesPointSize)
530 {
531 out << "static float gl_PointSize = float(1);\n";
532 }
533
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000534 if (mUsesInstanceID)
535 {
536 out << "static int gl_InstanceID;";
537 }
538
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000539 out << "\n"
540 "// Varyings\n";
541 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000542 out << "\n";
543
544 if (mUsesDepthRange)
545 {
546 out << "struct gl_DepthRangeParameters\n"
547 "{\n"
548 " float near;\n"
549 " float far;\n"
550 " float diff;\n"
551 "};\n"
552 "\n";
553 }
554
555 if (mOutputType == SH_HLSL11_OUTPUT)
556 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800557 out << "cbuffer DriverConstants : register(b1)\n"
558 "{\n";
559
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000560 if (mUsesDepthRange)
561 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800562 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000563 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800564
Cooper Partine6664f02015-01-09 16:22:24 -0800565 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800566 // However, we declare it for all shaders (including Feature Level 10+).
567 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
568 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800569 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800570
571 out << "};\n"
572 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000573 }
574 else
575 {
576 if (mUsesDepthRange)
577 {
578 out << "uniform float3 dx_DepthRange : register(c0);\n";
579 }
580
Cooper Partine6664f02015-01-09 16:22:24 -0800581 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
582 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000583 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000584 }
585
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000586 if (mUsesDepthRange)
587 {
588 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
589 "\n";
590 }
591
Jamie Madillf91ce812014-06-13 10:04:34 -0400592 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000593 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400594 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000595 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400596 out << flaggedStructs;
597 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000598 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400599 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000600
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400601 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
602 {
603 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400604 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000605 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400606 switch(textureFunction->sampler)
607 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400608 case EbtSampler2D: out << "int2 "; break;
609 case EbtSampler3D: out << "int3 "; break;
610 case EbtSamplerCube: out << "int2 "; break;
611 case EbtSampler2DArray: out << "int3 "; break;
612 case EbtISampler2D: out << "int2 "; break;
613 case EbtISampler3D: out << "int3 "; break;
614 case EbtISamplerCube: out << "int2 "; break;
615 case EbtISampler2DArray: out << "int3 "; break;
616 case EbtUSampler2D: out << "int2 "; break;
617 case EbtUSampler3D: out << "int3 "; break;
618 case EbtUSamplerCube: out << "int2 "; break;
619 case EbtUSampler2DArray: out << "int3 "; break;
620 case EbtSampler2DShadow: out << "int2 "; break;
621 case EbtSamplerCubeShadow: out << "int2 "; break;
622 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400623 default: UNREACHABLE();
624 }
625 }
626 else // Sampling function
627 {
628 switch(textureFunction->sampler)
629 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400630 case EbtSampler2D: out << "float4 "; break;
631 case EbtSampler3D: out << "float4 "; break;
632 case EbtSamplerCube: out << "float4 "; break;
633 case EbtSampler2DArray: out << "float4 "; break;
634 case EbtISampler2D: out << "int4 "; break;
635 case EbtISampler3D: out << "int4 "; break;
636 case EbtISamplerCube: out << "int4 "; break;
637 case EbtISampler2DArray: out << "int4 "; break;
638 case EbtUSampler2D: out << "uint4 "; break;
639 case EbtUSampler3D: out << "uint4 "; break;
640 case EbtUSamplerCube: out << "uint4 "; break;
641 case EbtUSampler2DArray: out << "uint4 "; break;
642 case EbtSampler2DShadow: out << "float "; break;
643 case EbtSamplerCubeShadow: out << "float "; break;
644 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400645 default: UNREACHABLE();
646 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000647 }
648
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400649 // Function name
650 out << textureFunction->name();
651
652 // Argument list
653 int hlslCoords = 4;
654
655 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000656 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400657 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000658 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400659 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
660 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
661 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000662 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400663
Nicolas Capens75fb4752013-07-10 15:14:47 -0400664 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000665 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400666 case TextureFunction::IMPLICIT: break;
667 case TextureFunction::BIAS: hlslCoords = 4; break;
668 case TextureFunction::LOD: hlslCoords = 4; break;
669 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400670 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400671 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000672 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400673 }
674 else if (mOutputType == SH_HLSL11_OUTPUT)
675 {
676 switch(textureFunction->sampler)
677 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400678 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
679 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
680 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
681 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
682 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
683 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500684 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400685 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
686 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
687 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500688 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400689 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
690 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
691 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
692 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400693 default: UNREACHABLE();
694 }
695 }
696 else UNREACHABLE();
697
Nicolas Capensfc014542014-02-18 14:47:13 -0500698 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400699 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500700 switch(textureFunction->coords)
701 {
702 case 2: out << ", int2 t"; break;
703 case 3: out << ", int3 t"; break;
704 default: UNREACHABLE();
705 }
706 }
707 else // Floating-point coordinates (except textureSize)
708 {
709 switch(textureFunction->coords)
710 {
711 case 1: out << ", int lod"; break; // textureSize()
712 case 2: out << ", float2 t"; break;
713 case 3: out << ", float3 t"; break;
714 case 4: out << ", float4 t"; break;
715 default: UNREACHABLE();
716 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000717 }
718
Nicolas Capensd11d5492014-02-19 17:06:10 -0500719 if (textureFunction->method == TextureFunction::GRAD)
720 {
721 switch(textureFunction->sampler)
722 {
723 case EbtSampler2D:
724 case EbtISampler2D:
725 case EbtUSampler2D:
726 case EbtSampler2DArray:
727 case EbtISampler2DArray:
728 case EbtUSampler2DArray:
729 case EbtSampler2DShadow:
730 case EbtSampler2DArrayShadow:
731 out << ", float2 ddx, float2 ddy";
732 break;
733 case EbtSampler3D:
734 case EbtISampler3D:
735 case EbtUSampler3D:
736 case EbtSamplerCube:
737 case EbtISamplerCube:
738 case EbtUSamplerCube:
739 case EbtSamplerCubeShadow:
740 out << ", float3 ddx, float3 ddy";
741 break;
742 default: UNREACHABLE();
743 }
744 }
745
Nicolas Capens75fb4752013-07-10 15:14:47 -0400746 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000747 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400748 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400749 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400750 case TextureFunction::LOD: out << ", float lod"; break;
751 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400752 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400753 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500754 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500755 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400756 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000757 }
758
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500759 if (textureFunction->offset)
760 {
761 switch(textureFunction->sampler)
762 {
763 case EbtSampler2D: out << ", int2 offset"; break;
764 case EbtSampler3D: out << ", int3 offset"; break;
765 case EbtSampler2DArray: out << ", int2 offset"; break;
766 case EbtISampler2D: out << ", int2 offset"; break;
767 case EbtISampler3D: out << ", int3 offset"; break;
768 case EbtISampler2DArray: out << ", int2 offset"; break;
769 case EbtUSampler2D: out << ", int2 offset"; break;
770 case EbtUSampler3D: out << ", int3 offset"; break;
771 case EbtUSampler2DArray: out << ", int2 offset"; break;
772 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500773 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500774 default: UNREACHABLE();
775 }
776 }
777
Nicolas Capens84cfa122014-04-14 13:48:45 -0400778 if (textureFunction->method == TextureFunction::BIAS ||
779 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500780 {
781 out << ", float bias";
782 }
783
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400784 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400785 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400786
Nicolas Capens75fb4752013-07-10 15:14:47 -0400787 if (textureFunction->method == TextureFunction::SIZE)
788 {
789 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
790 {
791 if (IsSamplerArray(textureFunction->sampler))
792 {
793 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
794 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
795 }
796 else
797 {
798 out << " uint width; uint height; uint numberOfLevels;\n"
799 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
800 }
801 }
802 else if (IsSampler3D(textureFunction->sampler))
803 {
804 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
805 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
806 }
807 else UNREACHABLE();
808
809 switch(textureFunction->sampler)
810 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400811 case EbtSampler2D: out << " return int2(width, height);"; break;
812 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
813 case EbtSamplerCube: out << " return int2(width, height);"; break;
814 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
815 case EbtISampler2D: out << " return int2(width, height);"; break;
816 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
817 case EbtISamplerCube: out << " return int2(width, height);"; break;
818 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
819 case EbtUSampler2D: out << " return int2(width, height);"; break;
820 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
821 case EbtUSamplerCube: out << " return int2(width, height);"; break;
822 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
823 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
824 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
825 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400826 default: UNREACHABLE();
827 }
828 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400829 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400830 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500831 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
832 {
833 out << " float width; float height; float layers; float levels;\n";
834
835 out << " uint mip = 0;\n";
836
837 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
838
839 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
840 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
841 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
842 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
843
844 // FACE_POSITIVE_X = 000b
845 // FACE_NEGATIVE_X = 001b
846 // FACE_POSITIVE_Y = 010b
847 // FACE_NEGATIVE_Y = 011b
848 // FACE_POSITIVE_Z = 100b
849 // FACE_NEGATIVE_Z = 101b
850 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
851
852 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
853 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
854 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
855
856 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
857 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
858 }
859 else if (IsIntegerSampler(textureFunction->sampler) &&
860 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400861 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400862 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400863 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400864 if (IsSamplerArray(textureFunction->sampler))
865 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400866 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400867
Nicolas Capens9edebd62013-08-06 10:59:10 -0400868 if (textureFunction->method == TextureFunction::LOD0)
869 {
870 out << " uint mip = 0;\n";
871 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400872 else if (textureFunction->method == TextureFunction::LOD0BIAS)
873 {
874 out << " uint mip = bias;\n";
875 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400876 else
877 {
878 if (textureFunction->method == TextureFunction::IMPLICIT ||
879 textureFunction->method == TextureFunction::BIAS)
880 {
881 out << " x.GetDimensions(0, width, height, layers, levels);\n"
882 " float2 tSized = float2(t.x * width, t.y * height);\n"
883 " float dx = length(ddx(tSized));\n"
884 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500885 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400886
887 if (textureFunction->method == TextureFunction::BIAS)
888 {
889 out << " lod += bias;\n";
890 }
891 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500892 else if (textureFunction->method == TextureFunction::GRAD)
893 {
894 out << " x.GetDimensions(0, width, height, layers, levels);\n"
895 " float lod = log2(max(length(ddx), length(ddy)));\n";
896 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400897
898 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
899 }
900
901 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400902 }
903 else
904 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400905 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400906
Nicolas Capens9edebd62013-08-06 10:59:10 -0400907 if (textureFunction->method == TextureFunction::LOD0)
908 {
909 out << " uint mip = 0;\n";
910 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400911 else if (textureFunction->method == TextureFunction::LOD0BIAS)
912 {
913 out << " uint mip = bias;\n";
914 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400915 else
916 {
917 if (textureFunction->method == TextureFunction::IMPLICIT ||
918 textureFunction->method == TextureFunction::BIAS)
919 {
920 out << " x.GetDimensions(0, width, height, levels);\n"
921 " float2 tSized = float2(t.x * width, t.y * height);\n"
922 " float dx = length(ddx(tSized));\n"
923 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500924 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400925
926 if (textureFunction->method == TextureFunction::BIAS)
927 {
928 out << " lod += bias;\n";
929 }
930 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500931 else if (textureFunction->method == TextureFunction::LOD)
932 {
933 out << " x.GetDimensions(0, width, height, levels);\n";
934 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500935 else if (textureFunction->method == TextureFunction::GRAD)
936 {
937 out << " x.GetDimensions(0, width, height, levels);\n"
938 " float lod = log2(max(length(ddx), length(ddy)));\n";
939 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400940
941 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
942 }
943
944 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400945 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400946 }
947 else if (IsSampler3D(textureFunction->sampler))
948 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400949 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400950
Nicolas Capens9edebd62013-08-06 10:59:10 -0400951 if (textureFunction->method == TextureFunction::LOD0)
952 {
953 out << " uint mip = 0;\n";
954 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400955 else if (textureFunction->method == TextureFunction::LOD0BIAS)
956 {
957 out << " uint mip = bias;\n";
958 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400959 else
960 {
961 if (textureFunction->method == TextureFunction::IMPLICIT ||
962 textureFunction->method == TextureFunction::BIAS)
963 {
964 out << " x.GetDimensions(0, width, height, depth, levels);\n"
965 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
966 " float dx = length(ddx(tSized));\n"
967 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500968 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400969
970 if (textureFunction->method == TextureFunction::BIAS)
971 {
972 out << " lod += bias;\n";
973 }
974 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500975 else if (textureFunction->method == TextureFunction::GRAD)
976 {
977 out << " x.GetDimensions(0, width, height, depth, levels);\n"
978 " float lod = log2(max(length(ddx), length(ddy)));\n";
979 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400980
981 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
982 }
983
984 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400985 }
986 else UNREACHABLE();
987 }
988
989 out << " return ";
990
991 // HLSL intrinsic
992 if (mOutputType == SH_HLSL9_OUTPUT)
993 {
994 switch(textureFunction->sampler)
995 {
996 case EbtSampler2D: out << "tex2D"; break;
997 case EbtSamplerCube: out << "texCUBE"; break;
998 default: UNREACHABLE();
999 }
1000
Nicolas Capens75fb4752013-07-10 15:14:47 -04001001 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001002 {
1003 case TextureFunction::IMPLICIT: out << "(s, "; break;
1004 case TextureFunction::BIAS: out << "bias(s, "; break;
1005 case TextureFunction::LOD: out << "lod(s, "; break;
1006 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001007 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001008 default: UNREACHABLE();
1009 }
1010 }
1011 else if (mOutputType == SH_HLSL11_OUTPUT)
1012 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001013 if (textureFunction->method == TextureFunction::GRAD)
1014 {
1015 if (IsIntegerSampler(textureFunction->sampler))
1016 {
1017 out << "x.Load(";
1018 }
1019 else if (IsShadowSampler(textureFunction->sampler))
1020 {
1021 out << "x.SampleCmpLevelZero(s, ";
1022 }
1023 else
1024 {
1025 out << "x.SampleGrad(s, ";
1026 }
1027 }
1028 else if (IsIntegerSampler(textureFunction->sampler) ||
1029 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001030 {
1031 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001032 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001033 else if (IsShadowSampler(textureFunction->sampler))
1034 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001035 switch(textureFunction->method)
1036 {
1037 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1038 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1039 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1040 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1041 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1042 default: UNREACHABLE();
1043 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001044 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001045 else
1046 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001047 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001048 {
1049 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1050 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1051 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1052 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001053 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001054 default: UNREACHABLE();
1055 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001056 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001057 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001058 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001059
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001060 // Integer sampling requires integer addresses
1061 TString addressx = "";
1062 TString addressy = "";
1063 TString addressz = "";
1064 TString close = "";
1065
Nicolas Capensfc014542014-02-18 14:47:13 -05001066 if (IsIntegerSampler(textureFunction->sampler) ||
1067 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001068 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001069 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001070 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001071 case 2: out << "int3("; break;
1072 case 3: out << "int4("; break;
1073 default: UNREACHABLE();
1074 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001075
Nicolas Capensfc014542014-02-18 14:47:13 -05001076 // Convert from normalized floating-point to integer
1077 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001078 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001079 addressx = "int(floor(width * frac((";
1080 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001081
Nicolas Capensfc014542014-02-18 14:47:13 -05001082 if (IsSamplerArray(textureFunction->sampler))
1083 {
1084 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1085 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001086 else if (IsSamplerCube(textureFunction->sampler))
1087 {
1088 addressz = "((((";
1089 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001090 else
1091 {
1092 addressz = "int(floor(depth * frac((";
1093 }
1094
1095 close = "))))";
1096 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001097 }
1098 else
1099 {
1100 switch(hlslCoords)
1101 {
1102 case 2: out << "float2("; break;
1103 case 3: out << "float3("; break;
1104 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001105 default: UNREACHABLE();
1106 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001107 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001108
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001109 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001110
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001111 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001112 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001113 switch(textureFunction->coords)
1114 {
1115 case 3: proj = " / t.z"; break;
1116 case 4: proj = " / t.w"; break;
1117 default: UNREACHABLE();
1118 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001119 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001120
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001121 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001122
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001123 if (mOutputType == SH_HLSL9_OUTPUT)
1124 {
1125 if (hlslCoords >= 3)
1126 {
1127 if (textureFunction->coords < 3)
1128 {
1129 out << ", 0";
1130 }
1131 else
1132 {
1133 out << ", t.z" + proj;
1134 }
1135 }
1136
1137 if (hlslCoords == 4)
1138 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001139 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001140 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001141 case TextureFunction::BIAS: out << ", bias"; break;
1142 case TextureFunction::LOD: out << ", lod"; break;
1143 case TextureFunction::LOD0: out << ", 0"; break;
1144 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001145 default: UNREACHABLE();
1146 }
1147 }
1148
1149 out << "));\n";
1150 }
1151 else if (mOutputType == SH_HLSL11_OUTPUT)
1152 {
1153 if (hlslCoords >= 3)
1154 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001155 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1156 {
1157 out << ", face";
1158 }
1159 else
1160 {
1161 out << ", " + addressz + ("t.z" + proj) + close;
1162 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001163 }
1164
Nicolas Capensd11d5492014-02-19 17:06:10 -05001165 if (textureFunction->method == TextureFunction::GRAD)
1166 {
1167 if (IsIntegerSampler(textureFunction->sampler))
1168 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001169 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001170 }
1171 else if (IsShadowSampler(textureFunction->sampler))
1172 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001173 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001174 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001175 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001176 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1177 // The resulting third component of P' in the shadow forms is used as Dref
1178 out << "), t.z" << proj;
1179 }
1180 else
1181 {
1182 switch(textureFunction->coords)
1183 {
1184 case 3: out << "), t.z"; break;
1185 case 4: out << "), t.w"; break;
1186 default: UNREACHABLE();
1187 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001188 }
1189 }
1190 else
1191 {
1192 out << "), ddx, ddy";
1193 }
1194 }
1195 else if (IsIntegerSampler(textureFunction->sampler) ||
1196 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001197 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001198 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001199 }
1200 else if (IsShadowSampler(textureFunction->sampler))
1201 {
1202 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001203 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001204 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001205 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1206 // The resulting third component of P' in the shadow forms is used as Dref
1207 out << "), t.z" << proj;
1208 }
1209 else
1210 {
1211 switch(textureFunction->coords)
1212 {
1213 case 3: out << "), t.z"; break;
1214 case 4: out << "), t.w"; break;
1215 default: UNREACHABLE();
1216 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001217 }
1218 }
1219 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001220 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001221 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001222 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001223 case TextureFunction::IMPLICIT: out << ")"; break;
1224 case TextureFunction::BIAS: out << "), bias"; break;
1225 case TextureFunction::LOD: out << "), lod"; break;
1226 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001227 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001228 default: UNREACHABLE();
1229 }
1230 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001231
1232 if (textureFunction->offset)
1233 {
1234 out << ", offset";
1235 }
1236
1237 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001238 }
1239 else UNREACHABLE();
1240 }
1241
1242 out << "\n"
1243 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001244 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001245 }
1246
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001247 if (mUsesFragCoord)
1248 {
1249 out << "#define GL_USES_FRAG_COORD\n";
1250 }
1251
1252 if (mUsesPointCoord)
1253 {
1254 out << "#define GL_USES_POINT_COORD\n";
1255 }
1256
1257 if (mUsesFrontFacing)
1258 {
1259 out << "#define GL_USES_FRONT_FACING\n";
1260 }
1261
1262 if (mUsesPointSize)
1263 {
1264 out << "#define GL_USES_POINT_SIZE\n";
1265 }
1266
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001267 if (mUsesFragDepth)
1268 {
1269 out << "#define GL_USES_FRAG_DEPTH\n";
1270 }
1271
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001272 if (mUsesDepthRange)
1273 {
1274 out << "#define GL_USES_DEPTH_RANGE\n";
1275 }
1276
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001277 if (mUsesXor)
1278 {
1279 out << "bool xor(bool p, bool q)\n"
1280 "{\n"
1281 " return (p || q) && !(p && q);\n"
1282 "}\n"
1283 "\n";
1284 }
1285
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001286 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287}
1288
1289void OutputHLSL::visitSymbol(TIntermSymbol *node)
1290{
Jamie Madill32aab012015-01-27 14:12:26 -05001291 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292
Jamie Madill570e04d2013-06-21 09:15:33 -04001293 // Handle accessing std140 structs by value
1294 if (mFlaggedStructMappedNames.count(node) > 0)
1295 {
1296 out << mFlaggedStructMappedNames[node];
1297 return;
1298 }
1299
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300 TString name = node->getSymbol();
1301
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001302 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001303 {
1304 mUsesDepthRange = true;
1305 out << name;
1306 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001307 else
1308 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001309 TQualifier qualifier = node->getQualifier();
1310
1311 if (qualifier == EvqUniform)
1312 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001313 const TType &nodeType = node->getType();
1314 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001315
1316 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001317 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001318 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001319 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001320 else
1321 {
1322 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001323 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001324
Jamie Madill2e295e22015-04-29 10:41:33 -04001325 ensureStructDefined(nodeType);
1326
Jamie Madill033dae62014-06-18 12:56:28 -04001327 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001328 }
Jamie Madill19571812013-08-12 15:26:34 -07001329 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001330 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001331 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001332 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001333 }
Jamie Madill033dae62014-06-18 12:56:28 -04001334 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001335 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001336 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001337 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001338 }
Jamie Madill19571812013-08-12 15:26:34 -07001339 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001340 {
1341 mReferencedOutputVariables[name] = node;
1342 out << "out_" << name;
1343 }
1344 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001345 {
1346 out << "gl_Color[0]";
1347 mUsesFragColor = true;
1348 }
1349 else if (qualifier == EvqFragData)
1350 {
1351 out << "gl_Color";
1352 mUsesFragData = true;
1353 }
1354 else if (qualifier == EvqFragCoord)
1355 {
1356 mUsesFragCoord = true;
1357 out << name;
1358 }
1359 else if (qualifier == EvqPointCoord)
1360 {
1361 mUsesPointCoord = true;
1362 out << name;
1363 }
1364 else if (qualifier == EvqFrontFacing)
1365 {
1366 mUsesFrontFacing = true;
1367 out << name;
1368 }
1369 else if (qualifier == EvqPointSize)
1370 {
1371 mUsesPointSize = true;
1372 out << name;
1373 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001374 else if (qualifier == EvqInstanceID)
1375 {
1376 mUsesInstanceID = true;
1377 out << name;
1378 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001379 else if (name == "gl_FragDepthEXT")
1380 {
1381 mUsesFragDepth = true;
1382 out << "gl_Depth";
1383 }
Olli Etuaho78174db2015-04-21 16:14:00 +03001384 else if (node->isInternal())
Jamie Madille53c98b2014-02-03 11:57:13 -05001385 {
1386 out << name;
1387 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001388 else
1389 {
Jamie Madill033dae62014-06-18 12:56:28 -04001390 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001391 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001392 }
1393}
1394
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001395void OutputHLSL::visitRaw(TIntermRaw *node)
1396{
Jamie Madill32aab012015-01-27 14:12:26 -05001397 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001398}
1399
Olli Etuaho7fb49552015-03-18 17:27:44 +02001400void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1401{
1402 if (type.isScalar() && !type.isArray())
1403 {
1404 if (op == EOpEqual)
1405 {
1406 outputTriplet(visit, "(", " == ", ")", out);
1407 }
1408 else
1409 {
1410 outputTriplet(visit, "(", " != ", ")", out);
1411 }
1412 }
1413 else
1414 {
1415 if (visit == PreVisit && op == EOpNotEqual)
1416 {
1417 out << "!";
1418 }
1419
1420 if (type.isArray())
1421 {
1422 const TString &functionName = addArrayEqualityFunction(type);
1423 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1424 }
1425 else if (type.getBasicType() == EbtStruct)
1426 {
1427 const TStructure &structure = *type.getStruct();
1428 const TString &functionName = addStructEqualityFunction(structure);
1429 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1430 }
1431 else
1432 {
1433 ASSERT(type.isMatrix() || type.isVector());
1434 outputTriplet(visit, "all(", " == ", ")", out);
1435 }
1436 }
1437}
1438
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001439bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1440{
Jamie Madill32aab012015-01-27 14:12:26 -05001441 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001442
Jamie Madill570e04d2013-06-21 09:15:33 -04001443 // Handle accessing std140 structs by value
1444 if (mFlaggedStructMappedNames.count(node) > 0)
1445 {
1446 out << mFlaggedStructMappedNames[node];
1447 return false;
1448 }
1449
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001450 switch (node->getOp())
1451 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001452 case EOpAssign:
1453 if (node->getLeft()->isArray())
1454 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001455 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1456 if (rightAgg != nullptr && rightAgg->isConstructor())
1457 {
1458 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1459 out << functionName << "(";
1460 node->getLeft()->traverse(this);
1461 TIntermSequence *seq = rightAgg->getSequence();
1462 for (auto &arrayElement : *seq)
1463 {
1464 out << ", ";
1465 arrayElement->traverse(this);
1466 }
1467 out << ")";
1468 return false;
1469 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001470 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1471 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1472
1473 const TString &functionName = addArrayAssignmentFunction(node->getType());
1474 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001475 }
1476 else
1477 {
1478 outputTriplet(visit, "(", " = ", ")");
1479 }
1480 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001481 case EOpInitialize:
1482 if (visit == PreVisit)
1483 {
1484 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1485 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1486 // new variable is created before the assignment is evaluated), so we need to convert
1487 // this to "float t = x, x = t;".
1488
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001489 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001490 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001491 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001492
Jamie Madill37997142015-01-28 10:06:34 -05001493 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1494 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001495 {
Jamie Madill37997142015-01-28 10:06:34 -05001496 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001497 // after we initialize uniforms.
1498 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1499 deferredInit->setLeft(node->getLeft());
1500 deferredInit->setRight(node->getRight());
1501 deferredInit->setType(node->getType());
1502 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001503 const TString &initString = initializer(node->getType());
1504 node->setRight(new TIntermRaw(node->getType(), initString));
1505 }
1506 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1507 {
1508 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001509 return false;
1510 }
1511 }
1512 else if (visit == InVisit)
1513 {
1514 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001515 }
1516 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001517 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1518 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1519 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1520 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1521 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1522 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001523 if (visit == PreVisit)
1524 {
1525 out << "(";
1526 }
1527 else if (visit == InVisit)
1528 {
1529 out << " = mul(";
1530 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001531 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001532 }
1533 else
1534 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001535 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001536 }
1537 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001538 case EOpMatrixTimesMatrixAssign:
1539 if (visit == PreVisit)
1540 {
1541 out << "(";
1542 }
1543 else if (visit == InVisit)
1544 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001545 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001546 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001547 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001548 }
1549 else
1550 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001551 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001552 }
1553 break;
1554 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001555 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001556 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1557 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1558 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1559 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1560 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001561 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001562 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001563 const TType& leftType = node->getLeft()->getType();
1564 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001565 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001566 if (visit == PreVisit)
1567 {
1568 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1569 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001570 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001571 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001572 return false;
1573 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001574 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001575 else
1576 {
1577 outputTriplet(visit, "", "[", "]");
1578 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001579 }
1580 break;
1581 case EOpIndexIndirect:
1582 // We do not currently support indirect references to interface blocks
1583 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1584 outputTriplet(visit, "", "[", "]");
1585 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001586 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001587 if (visit == InVisit)
1588 {
1589 const TStructure* structure = node->getLeft()->getType().getStruct();
1590 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1591 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001592 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001593
1594 return false;
1595 }
1596 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001597 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001598 if (visit == InVisit)
1599 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001600 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1601 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1602 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001603 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001604
1605 return false;
1606 }
1607 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001608 case EOpVectorSwizzle:
1609 if (visit == InVisit)
1610 {
1611 out << ".";
1612
1613 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1614
1615 if (swizzle)
1616 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001617 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001619 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001620 {
1621 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1622
1623 if (element)
1624 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001625 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626
1627 switch (i)
1628 {
1629 case 0: out << "x"; break;
1630 case 1: out << "y"; break;
1631 case 2: out << "z"; break;
1632 case 3: out << "w"; break;
1633 default: UNREACHABLE();
1634 }
1635 }
1636 else UNREACHABLE();
1637 }
1638 }
1639 else UNREACHABLE();
1640
1641 return false; // Fully processed
1642 }
1643 break;
1644 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1645 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1646 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1647 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001648 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001649 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1650 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1651 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1652 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1653 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001654 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001655 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001656 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001657 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001658 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1659 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1660 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1661 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1662 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001663 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001664 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1665 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001666 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001667 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001668 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1669 ASSERT(!node->getRight()->hasSideEffects());
1670 outputTriplet(visit, "(", " || ", ")");
1671 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001672 case EOpLogicalXor:
1673 mUsesXor = true;
1674 outputTriplet(visit, "xor(", ", ", ")");
1675 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001676 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001677 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1678 ASSERT(!node->getRight()->hasSideEffects());
1679 outputTriplet(visit, "(", " && ", ")");
1680 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001681 default: UNREACHABLE();
1682 }
1683
1684 return true;
1685}
1686
1687bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1688{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689 switch (node->getOp())
1690 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001691 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001692 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001693 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1694 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001695 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001696 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1697 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1698 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1699 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001700 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1701 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1702 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1703 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1704 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1705 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1706 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1707 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001708 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1709 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1710 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1711 case EOpAsinh:
1712 ASSERT(node->getUseEmulatedFunction());
1713 writeEmulatedFunctionTriplet(visit, "asinh(");
1714 break;
1715 case EOpAcosh:
1716 ASSERT(node->getUseEmulatedFunction());
1717 writeEmulatedFunctionTriplet(visit, "acosh(");
1718 break;
1719 case EOpAtanh:
1720 ASSERT(node->getUseEmulatedFunction());
1721 writeEmulatedFunctionTriplet(visit, "atanh(");
1722 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001723 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1724 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1725 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1726 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1727 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1728 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1729 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1730 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1731 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001732 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1733 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1734 case EOpRoundEven:
1735 ASSERT(node->getUseEmulatedFunction());
1736 writeEmulatedFunctionTriplet(visit, "roundEven(");
1737 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001738 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1739 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301740 case EOpIsNan:
1741 outputTriplet(visit, "isnan(", "", ")");
1742 mRequiresIEEEStrictCompiling = true;
1743 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301744 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001745 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1746 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1747 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1748 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001749 case EOpPackSnorm2x16:
1750 ASSERT(node->getUseEmulatedFunction());
1751 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1752 break;
1753 case EOpPackUnorm2x16:
1754 ASSERT(node->getUseEmulatedFunction());
1755 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1756 break;
1757 case EOpPackHalf2x16:
1758 ASSERT(node->getUseEmulatedFunction());
1759 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1760 break;
1761 case EOpUnpackSnorm2x16:
1762 ASSERT(node->getUseEmulatedFunction());
1763 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1764 break;
1765 case EOpUnpackUnorm2x16:
1766 ASSERT(node->getUseEmulatedFunction());
1767 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1768 break;
1769 case EOpUnpackHalf2x16:
1770 ASSERT(node->getUseEmulatedFunction());
1771 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1772 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001773 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1774 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001775 case EOpDFdx:
1776 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1777 {
1778 outputTriplet(visit, "(", "", ", 0.0)");
1779 }
1780 else
1781 {
1782 outputTriplet(visit, "ddx(", "", ")");
1783 }
1784 break;
1785 case EOpDFdy:
1786 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1787 {
1788 outputTriplet(visit, "(", "", ", 0.0)");
1789 }
1790 else
1791 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001792 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001793 }
1794 break;
1795 case EOpFwidth:
1796 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1797 {
1798 outputTriplet(visit, "(", "", ", 0.0)");
1799 }
1800 else
1801 {
1802 outputTriplet(visit, "fwidth(", "", ")");
1803 }
1804 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001805 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1806 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001807 case EOpInverse:
1808 ASSERT(node->getUseEmulatedFunction());
1809 writeEmulatedFunctionTriplet(visit, "inverse(");
1810 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001811
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001812 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1813 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814 default: UNREACHABLE();
1815 }
1816
1817 return true;
1818}
1819
1820bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1821{
Jamie Madill32aab012015-01-27 14:12:26 -05001822 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001823
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824 switch (node->getOp())
1825 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001826 case EOpSequence:
1827 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001828 if (mInsideFunction)
1829 {
Jamie Madill075edd82013-07-08 13:30:19 -04001830 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001831 out << "{\n";
1832 }
1833
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001834 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001835 {
Jamie Madill075edd82013-07-08 13:30:19 -04001836 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001837
Olli Etuahoa6f22092015-05-08 18:31:10 +03001838 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001839
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001840 // Don't output ; after case labels, they're terminated by :
1841 // This is needed especially since outputting a ; after a case statement would turn empty
1842 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001843 // Also no need to output ; after selection (if) statements. This is done just for code clarity.
1844 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
1845 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
1846 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001847 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001848 }
1849
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001850 if (mInsideFunction)
1851 {
Jamie Madill075edd82013-07-08 13:30:19 -04001852 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001853 out << "}\n";
1854 }
1855
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001856 return false;
1857 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001858 case EOpDeclaration:
1859 if (visit == PreVisit)
1860 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001861 TIntermSequence *sequence = node->getSequence();
1862 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001863 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001865 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001867 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001868
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001869 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001871 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001872 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001873 out << "static ";
1874 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001875
Olli Etuahoa6f22092015-05-08 18:31:10 +03001876 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001877
Olli Etuahoa6f22092015-05-08 18:31:10 +03001878 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001879
Olli Etuahoa6f22092015-05-08 18:31:10 +03001880 if (symbol)
1881 {
1882 symbol->traverse(this);
1883 out << ArrayString(symbol->getType());
1884 out << " = " + initializer(symbol->getType());
1885 }
1886 else
1887 {
1888 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001890 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001891 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1892 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001893 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001894 }
1895 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896 }
Jamie Madill033dae62014-06-18 12:56:28 -04001897 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001898 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001899 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001900 {
1901 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1902
1903 if (symbol)
1904 {
1905 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1906 mReferencedVaryings[symbol->getSymbol()] = symbol;
1907 }
1908 else
1909 {
1910 (*sit)->traverse(this);
1911 }
1912 }
1913 }
1914
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915 return false;
1916 }
1917 else if (visit == InVisit)
1918 {
1919 out << ", ";
1920 }
1921 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001922 case EOpInvariantDeclaration:
1923 // Do not do any translation
1924 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001925 case EOpPrototype:
1926 if (visit == PreVisit)
1927 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001928 size_t index = mCallDag.findIndex(node);
1929 // Skip the prototype if it is not implemented (and thus not used)
1930 if (index == CallDAG::InvalidIndex)
1931 {
1932 return false;
1933 }
1934
Olli Etuaho76acee82014-11-04 13:44:03 +02001935 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001936
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001937 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001938
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001939 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001940 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001941 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001942
1943 if (symbol)
1944 {
1945 out << argumentString(symbol);
1946
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001947 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001948 {
1949 out << ", ";
1950 }
1951 }
1952 else UNREACHABLE();
1953 }
1954
1955 out << ");\n";
1956
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001957 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001958 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1959 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001960 {
1961 mOutputLod0Function = true;
1962 node->traverse(this);
1963 mOutputLod0Function = false;
1964 }
1965
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001966 return false;
1967 }
1968 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001969 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001970 case EOpFunction:
1971 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001972 ASSERT(mCurrentFunctionMetadata == nullptr);
alokp@chromium.org43884872010-03-30 00:08:52 +00001973 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001974
Corentin Wallez1239ee92015-03-19 14:38:02 -07001975 size_t index = mCallDag.findIndex(node);
1976 ASSERT(index != CallDAG::InvalidIndex);
1977 mCurrentFunctionMetadata = &mASTMetadataList[index];
1978
Jamie Madill033dae62014-06-18 12:56:28 -04001979 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001980
1981 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001983 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001984 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001985 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001986 {
Jamie Madill033dae62014-06-18 12:56:28 -04001987 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001988 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001989
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001990 TIntermSequence *sequence = node->getSequence();
1991 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001992
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001993 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001994 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001995 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001996
1997 if (symbol)
1998 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001999 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002000
2001 out << argumentString(symbol);
2002
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002003 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002004 {
2005 out << ", ";
2006 }
2007 }
2008 else UNREACHABLE();
2009 }
2010
2011 out << ")\n"
2012 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002013
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002014 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002015 {
2016 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002017 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002018 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002020
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002021 out << "}\n";
2022
Corentin Wallez1239ee92015-03-19 14:38:02 -07002023 mCurrentFunctionMetadata = nullptr;
2024
2025 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2026 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002027 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002028 ASSERT(name != "main");
2029 mOutputLod0Function = true;
2030 node->traverse(this);
2031 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002032 }
2033
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002034 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035 }
2036 break;
2037 case EOpFunctionCall:
2038 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002039 TString name = TFunction::unmangleName(node->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002040 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002041
Corentin Wallez1239ee92015-03-19 14:38:02 -07002042 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002043 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002044 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002045 if (node->isArray())
2046 {
2047 UNIMPLEMENTED();
2048 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002049 size_t index = mCallDag.findIndex(node);
2050 ASSERT(index != CallDAG::InvalidIndex);
2051 lod0 &= mASTMetadataList[index].mNeedsLod0;
2052
Jamie Madill033dae62014-06-18 12:56:28 -04002053 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002054 }
2055 else
2056 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002057 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002058
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002059 TextureFunction textureFunction;
2060 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002061 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002062 textureFunction.method = TextureFunction::IMPLICIT;
2063 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002064 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002065
2066 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002067 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002068 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002069 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002070 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002071 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002072 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002073 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002074 }
Nicolas Capens46485082014-04-15 13:12:50 -04002075 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2076 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002077 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002078 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002079 }
Nicolas Capens46485082014-04-15 13:12:50 -04002080 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002081 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002082 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002083 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002084 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002085 else if (name == "textureSize")
2086 {
2087 textureFunction.method = TextureFunction::SIZE;
2088 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002089 else if (name == "textureOffset")
2090 {
2091 textureFunction.method = TextureFunction::IMPLICIT;
2092 textureFunction.offset = true;
2093 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002094 else if (name == "textureProjOffset")
2095 {
2096 textureFunction.method = TextureFunction::IMPLICIT;
2097 textureFunction.offset = true;
2098 textureFunction.proj = true;
2099 }
2100 else if (name == "textureLodOffset")
2101 {
2102 textureFunction.method = TextureFunction::LOD;
2103 textureFunction.offset = true;
2104 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002105 else if (name == "textureProjLodOffset")
2106 {
2107 textureFunction.method = TextureFunction::LOD;
2108 textureFunction.proj = true;
2109 textureFunction.offset = true;
2110 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002111 else if (name == "texelFetch")
2112 {
2113 textureFunction.method = TextureFunction::FETCH;
2114 }
2115 else if (name == "texelFetchOffset")
2116 {
2117 textureFunction.method = TextureFunction::FETCH;
2118 textureFunction.offset = true;
2119 }
Nicolas Capens46485082014-04-15 13:12:50 -04002120 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002121 {
2122 textureFunction.method = TextureFunction::GRAD;
2123 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002124 else if (name == "textureGradOffset")
2125 {
2126 textureFunction.method = TextureFunction::GRAD;
2127 textureFunction.offset = true;
2128 }
Nicolas Capens46485082014-04-15 13:12:50 -04002129 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002130 {
2131 textureFunction.method = TextureFunction::GRAD;
2132 textureFunction.proj = true;
2133 }
2134 else if (name == "textureProjGradOffset")
2135 {
2136 textureFunction.method = TextureFunction::GRAD;
2137 textureFunction.proj = true;
2138 textureFunction.offset = true;
2139 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002140 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002141
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002142 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002143 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002144 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2145
2146 if (textureFunction.offset)
2147 {
2148 mandatoryArgumentCount++;
2149 }
2150
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002151 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002152
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002153 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002154 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002155 if (bias)
2156 {
2157 textureFunction.method = TextureFunction::LOD0BIAS;
2158 }
2159 else
2160 {
2161 textureFunction.method = TextureFunction::LOD0;
2162 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002163 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002164 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002165 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002166 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002167 }
2168 }
2169
2170 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002171
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002172 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002174
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002175 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002176 {
2177 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2178 {
2179 out << "texture_";
2180 (*arg)->traverse(this);
2181 out << ", sampler_";
2182 }
2183
2184 (*arg)->traverse(this);
2185
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002186 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002187 {
2188 out << ", ";
2189 }
2190 }
2191
2192 out << ")";
2193
2194 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002195 }
2196 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002197 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002198 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2199 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2200 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2201 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2202 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2203 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2204 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2205 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2206 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2207 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2208 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2209 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2210 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2211 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2212 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2213 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2214 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2215 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2216 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002217 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002218 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002219 if (node->getType().isArray())
2220 {
2221 UNIMPLEMENTED();
2222 }
Jamie Madill033dae62014-06-18 12:56:28 -04002223 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002224 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002225 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002226 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002227 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002228 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2229 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2230 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2231 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2232 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2233 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002234 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002235 ASSERT(node->getUseEmulatedFunction());
2236 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002237 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002238 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002239 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002241 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002242 ASSERT(node->getUseEmulatedFunction());
2243 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 break;
2245 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2246 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2247 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
Arun Patoled94f6642015-05-18 16:25:12 +05302248 case EOpMix:
2249 {
2250 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2251 if (lastParamNode->getType().getBasicType() == EbtBool)
2252 {
2253 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2254 // so use emulated version.
2255 ASSERT(node->getUseEmulatedFunction());
2256 writeEmulatedFunctionTriplet(visit, "mix(");
2257 }
2258 else
2259 {
2260 outputTriplet(visit, "lerp(", ", ", ")");
2261 }
2262 }
2263 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2265 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2266 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2267 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2268 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002269 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002270 ASSERT(node->getUseEmulatedFunction());
2271 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002272 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002273 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2274 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002275 case EOpOuterProduct:
2276 ASSERT(node->getUseEmulatedFunction());
2277 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2278 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280 default: UNREACHABLE();
2281 }
2282
2283 return true;
2284}
2285
Olli Etuahod81ed842015-05-12 12:46:35 +03002286void OutputHLSL::writeSelection(TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287{
Jamie Madill32aab012015-01-27 14:12:26 -05002288 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289
Olli Etuahoa6f22092015-05-08 18:31:10 +03002290 out << "if (";
2291
2292 node->getCondition()->traverse(this);
2293
2294 out << ")\n";
2295
2296 outputLineDirective(node->getLine().first_line);
2297 out << "{\n";
2298
2299 bool discard = false;
2300
2301 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002303 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002304
Olli Etuahoa6f22092015-05-08 18:31:10 +03002305 // Detect true discard
2306 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2307 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002308
Olli Etuahoa6f22092015-05-08 18:31:10 +03002309 outputLineDirective(node->getLine().first_line);
2310 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002311
Olli Etuahoa6f22092015-05-08 18:31:10 +03002312 if (node->getFalseBlock())
2313 {
2314 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002315
Olli Etuahoa6f22092015-05-08 18:31:10 +03002316 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002317 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318
Olli Etuahoa6f22092015-05-08 18:31:10 +03002319 outputLineDirective(node->getFalseBlock()->getLine().first_line);
2320 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002321
Olli Etuahoa6f22092015-05-08 18:31:10 +03002322 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002323 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002324
Olli Etuahoa6f22092015-05-08 18:31:10 +03002325 // Detect false discard
2326 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2327 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002328
Olli Etuahoa6f22092015-05-08 18:31:10 +03002329 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002330 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002331 {
2332 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002333 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002334}
2335
2336bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2337{
2338 TInfoSinkBase &out = getInfoSink();
2339
2340 ASSERT(!node->usesTernaryOperator());
2341
2342 if (!mInsideFunction)
2343 {
2344 // This is part of unfolded global initialization.
2345 mDeferredGlobalInitializers.push_back(node);
2346 return false;
2347 }
2348
2349 // D3D errors when there is a gradient operation in a loop in an unflattened if.
2350 if (mShaderType == GL_FRAGMENT_SHADER &&
2351 mCurrentFunctionMetadata->hasDiscontinuousLoop(node) &&
2352 mCurrentFunctionMetadata->hasGradientInCallGraph(node))
2353 {
2354 out << "FLATTEN ";
2355 }
2356
2357 writeSelection(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358
2359 return false;
2360}
2361
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002362bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002363{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002364 if (node->getStatementList())
2365 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002366 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002367 outputTriplet(visit, "switch (", ") ", "");
2368 // The curly braces get written when visiting the statementList aggregate
2369 }
2370 else
2371 {
2372 // No statementList, so it won't output curly braces
2373 outputTriplet(visit, "switch (", ") {", "}\n");
2374 }
2375 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002376}
2377
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002378bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002379{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002380 if (node->hasCondition())
2381 {
2382 outputTriplet(visit, "case (", "", "):\n");
2383 return true;
2384 }
2385 else
2386 {
2387 TInfoSinkBase &out = getInfoSink();
2388 out << "default:\n";
2389 return false;
2390 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002391}
2392
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2394{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002395 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396}
2397
2398bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2399{
Nicolas Capens655fe362014-04-11 13:12:34 -04002400 mNestedLoopDepth++;
2401
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002402 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002403 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002404 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002405
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002406 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002407 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002408 if (handleExcessiveLoop(node))
2409 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002410 mInsideDiscontinuousLoop = wasDiscontinuous;
2411 mNestedLoopDepth--;
2412
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002413 return false;
2414 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002415 }
2416
Jamie Madill32aab012015-01-27 14:12:26 -05002417 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418
Corentin Wallez1239ee92015-03-19 14:38:02 -07002419 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002420 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002422 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002423
Jamie Madill075edd82013-07-08 13:30:19 -04002424 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002425 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 }
2427 else
2428 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002429 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002430
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431 if (node->getInit())
2432 {
2433 node->getInit()->traverse(this);
2434 }
2435
2436 out << "; ";
2437
alokp@chromium.org52813552010-11-16 18:36:09 +00002438 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002440 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 }
2442
2443 out << "; ";
2444
alokp@chromium.org52813552010-11-16 18:36:09 +00002445 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002447 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448 }
2449
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002450 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002451
Jamie Madill075edd82013-07-08 13:30:19 -04002452 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002453 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 }
2455
2456 if (node->getBody())
2457 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002458 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002459 }
2460
Jamie Madill075edd82013-07-08 13:30:19 -04002461 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002462 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463
alokp@chromium.org52813552010-11-16 18:36:09 +00002464 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 {
Jamie Madill075edd82013-07-08 13:30:19 -04002466 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 out << "while(\n";
2468
alokp@chromium.org52813552010-11-16 18:36:09 +00002469 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002470
daniel@transgaming.com73536982012-03-21 20:45:49 +00002471 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002472 }
2473
daniel@transgaming.com73536982012-03-21 20:45:49 +00002474 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002475
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002476 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002477 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002478
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479 return false;
2480}
2481
2482bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2483{
Jamie Madill32aab012015-01-27 14:12:26 -05002484 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485
2486 switch (node->getFlowOp())
2487 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002488 case EOpKill:
2489 outputTriplet(visit, "discard;\n", "", "");
2490 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002491 case EOpBreak:
2492 if (visit == PreVisit)
2493 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002494 if (mNestedLoopDepth > 1)
2495 {
2496 mUsesNestedBreak = true;
2497 }
2498
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002499 if (mExcessiveLoopIndex)
2500 {
2501 out << "{Break";
2502 mExcessiveLoopIndex->traverse(this);
2503 out << " = true; break;}\n";
2504 }
2505 else
2506 {
2507 out << "break;\n";
2508 }
2509 }
2510 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002511 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512 case EOpReturn:
2513 if (visit == PreVisit)
2514 {
2515 if (node->getExpression())
2516 {
2517 out << "return ";
2518 }
2519 else
2520 {
2521 out << "return;\n";
2522 }
2523 }
2524 else if (visit == PostVisit)
2525 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002526 if (node->getExpression())
2527 {
2528 out << ";\n";
2529 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530 }
2531 break;
2532 default: UNREACHABLE();
2533 }
2534
2535 return true;
2536}
2537
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002538bool OutputHLSL::isSingleStatement(TIntermNode *node)
2539{
2540 TIntermAggregate *aggregate = node->getAsAggregate();
2541
2542 if (aggregate)
2543 {
2544 if (aggregate->getOp() == EOpSequence)
2545 {
2546 return false;
2547 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002548 else if (aggregate->getOp() == EOpDeclaration)
2549 {
2550 // Declaring multiple comma-separated variables must be considered multiple statements
2551 // because each individual declaration has side effects which are visible in the next.
2552 return false;
2553 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002554 else
2555 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002556 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002557 {
2558 if (!isSingleStatement(*sit))
2559 {
2560 return false;
2561 }
2562 }
2563
2564 return true;
2565 }
2566 }
2567
2568 return true;
2569}
2570
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002571// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2572// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002573bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2574{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002575 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002576 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002577
2578 // Parse loops of the form:
2579 // for(int index = initial; index [comparator] limit; index += increment)
2580 TIntermSymbol *index = NULL;
2581 TOperator comparator = EOpNull;
2582 int initial = 0;
2583 int limit = 0;
2584 int increment = 0;
2585
2586 // Parse index name and intial value
2587 if (node->getInit())
2588 {
2589 TIntermAggregate *init = node->getInit()->getAsAggregate();
2590
2591 if (init)
2592 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002593 TIntermSequence *sequence = init->getSequence();
2594 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002595
2596 if (variable && variable->getQualifier() == EvqTemporary)
2597 {
2598 TIntermBinary *assign = variable->getAsBinaryNode();
2599
2600 if (assign->getOp() == EOpInitialize)
2601 {
2602 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2603 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2604
2605 if (symbol && constant)
2606 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002607 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002608 {
2609 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002610 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002611 }
2612 }
2613 }
2614 }
2615 }
2616 }
2617
2618 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002619 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002620 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002621 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002622
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002623 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2624 {
2625 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2626
2627 if (constant)
2628 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002629 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002630 {
2631 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002632 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002633 }
2634 }
2635 }
2636 }
2637
2638 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002639 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002640 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002641 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2642 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002643
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002644 if (binaryTerminal)
2645 {
2646 TOperator op = binaryTerminal->getOp();
2647 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2648
2649 if (constant)
2650 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002651 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002652 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002653 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002654
2655 switch (op)
2656 {
2657 case EOpAddAssign: increment = value; break;
2658 case EOpSubAssign: increment = -value; break;
2659 default: UNIMPLEMENTED();
2660 }
2661 }
2662 }
2663 }
2664 else if (unaryTerminal)
2665 {
2666 TOperator op = unaryTerminal->getOp();
2667
2668 switch (op)
2669 {
2670 case EOpPostIncrement: increment = 1; break;
2671 case EOpPostDecrement: increment = -1; break;
2672 case EOpPreIncrement: increment = 1; break;
2673 case EOpPreDecrement: increment = -1; break;
2674 default: UNIMPLEMENTED();
2675 }
2676 }
2677 }
2678
2679 if (index != NULL && comparator != EOpNull && increment != 0)
2680 {
2681 if (comparator == EOpLessThanEqual)
2682 {
2683 comparator = EOpLessThan;
2684 limit += 1;
2685 }
2686
2687 if (comparator == EOpLessThan)
2688 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002689 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002690
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002691 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002692 {
2693 return false; // Not an excessive loop
2694 }
2695
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002696 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2697 mExcessiveLoopIndex = index;
2698
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002699 out << "{int ";
2700 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002701 out << ";\n"
2702 "bool Break";
2703 index->traverse(this);
2704 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002705
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002706 bool firstLoopFragment = true;
2707
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002708 while (iterations > 0)
2709 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002710 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002711
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002712 if (!firstLoopFragment)
2713 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002714 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002715 index->traverse(this);
2716 out << ") {\n";
2717 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002718
2719 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2720 {
2721 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2722 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002723
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002724 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002725 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002726
Corentin Wallez1239ee92015-03-19 14:38:02 -07002727 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002728 index->traverse(this);
2729 out << " = ";
2730 out << initial;
2731
2732 out << "; ";
2733 index->traverse(this);
2734 out << " < ";
2735 out << clampedLimit;
2736
2737 out << "; ";
2738 index->traverse(this);
2739 out << " += ";
2740 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002741 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002742
Jamie Madill075edd82013-07-08 13:30:19 -04002743 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002744 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002745
2746 if (node->getBody())
2747 {
2748 node->getBody()->traverse(this);
2749 }
2750
Jamie Madill075edd82013-07-08 13:30:19 -04002751 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002752 out << ";}\n";
2753
2754 if (!firstLoopFragment)
2755 {
2756 out << "}\n";
2757 }
2758
2759 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002760
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002761 initial += MAX_LOOP_ITERATIONS * increment;
2762 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002763 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002764
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002765 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002766
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002767 mExcessiveLoopIndex = restoreIndex;
2768
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002769 return true;
2770 }
2771 else UNIMPLEMENTED();
2772 }
2773
2774 return false; // Not handled as an excessive loop
2775}
2776
Olli Etuaho7fb49552015-03-18 17:27:44 +02002777void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002779 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002780 {
2781 out << preString;
2782 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002783 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784 {
2785 out << inString;
2786 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002787 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002788 {
2789 out << postString;
2790 }
2791}
2792
Olli Etuaho7fb49552015-03-18 17:27:44 +02002793void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2794{
2795 outputTriplet(visit, preString, inString, postString, getInfoSink());
2796}
2797
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002798void OutputHLSL::outputLineDirective(int line)
2799{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002800 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002801 {
Jamie Madill32aab012015-01-27 14:12:26 -05002802 TInfoSinkBase &out = getInfoSink();
2803
2804 out << "\n";
2805 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002806
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002807 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002808 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002809 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002810 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002811
Jamie Madill32aab012015-01-27 14:12:26 -05002812 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002813 }
2814}
2815
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002816TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2817{
2818 TQualifier qualifier = symbol->getQualifier();
2819 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002820 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002821
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002822 if (name.empty()) // HLSL demands named arguments, also for prototypes
2823 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002824 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002825 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002826 else if (!symbol->isInternal())
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002827 {
Jamie Madill033dae62014-06-18 12:56:28 -04002828 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002829 }
2830
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002831 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2832 {
Jamie Madill033dae62014-06-18 12:56:28 -04002833 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002834 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002835 }
2836
Jamie Madill033dae62014-06-18 12:56:28 -04002837 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002838}
2839
2840TString OutputHLSL::initializer(const TType &type)
2841{
2842 TString string;
2843
Jamie Madill94bf7f22013-07-08 13:31:15 -04002844 size_t size = type.getObjectSize();
2845 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002846 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002847 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002848
Jamie Madill94bf7f22013-07-08 13:31:15 -04002849 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002850 {
2851 string += ", ";
2852 }
2853 }
2854
daniel@transgaming.comead23042010-04-29 03:35:36 +00002855 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002856}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002857
Daniel Bratell29190082015-02-20 16:42:54 +01002858void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002859{
Olli Etuahof40319e2015-03-10 14:33:00 +02002860 if (type.isArray())
2861 {
2862 UNIMPLEMENTED();
2863 }
Jamie Madill32aab012015-01-27 14:12:26 -05002864 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002865
2866 if (visit == PreVisit)
2867 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002868 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002869
Daniel Bratell29190082015-02-20 16:42:54 +01002870 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002871 }
2872 else if (visit == InVisit)
2873 {
2874 out << ", ";
2875 }
2876 else if (visit == PostVisit)
2877 {
2878 out << ")";
2879 }
2880}
2881
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002882const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const TConstantUnion *constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002883{
Jamie Madill32aab012015-01-27 14:12:26 -05002884 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002885
Jamie Madill98493dd2013-07-08 14:39:03 -04002886 const TStructure* structure = type.getStruct();
2887 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002888 {
Jamie Madill033dae62014-06-18 12:56:28 -04002889 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002890
Jamie Madill98493dd2013-07-08 14:39:03 -04002891 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002892
Jamie Madill98493dd2013-07-08 14:39:03 -04002893 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002894 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002895 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002896 constUnion = writeConstantUnion(*fieldType, constUnion);
2897
Jamie Madill98493dd2013-07-08 14:39:03 -04002898 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002899 {
2900 out << ", ";
2901 }
2902 }
2903
2904 out << ")";
2905 }
2906 else
2907 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002908 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002909 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002910
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002911 if (writeType)
2912 {
Jamie Madill033dae62014-06-18 12:56:28 -04002913 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002914 }
2915
Jamie Madill94bf7f22013-07-08 13:31:15 -04002916 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002917 {
2918 switch (constUnion->getType())
2919 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002920 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002921 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002922 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002923 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002924 default: UNREACHABLE();
2925 }
2926
2927 if (i != size - 1)
2928 {
2929 out << ", ";
2930 }
2931 }
2932
2933 if (writeType)
2934 {
2935 out << ")";
2936 }
2937 }
2938
2939 return constUnion;
2940}
2941
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002942void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2943{
2944 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2945 outputTriplet(visit, preString.c_str(), ", ", ")");
2946}
2947
Jamie Madill37997142015-01-28 10:06:34 -05002948bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2949{
2950 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2951 expression->traverse(&searchSymbol);
2952
2953 if (searchSymbol.foundMatch())
2954 {
2955 // Type already printed
2956 out << "t" + str(mUniqueIndex) + " = ";
2957 expression->traverse(this);
2958 out << ", ";
2959 symbolNode->traverse(this);
2960 out << " = t" + str(mUniqueIndex);
2961
2962 mUniqueIndex++;
2963 return true;
2964 }
2965
2966 return false;
2967}
2968
2969void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2970{
2971 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2972 << "\n"
2973 << "void initializeDeferredGlobals()\n"
2974 << "{\n";
2975
2976 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2977 {
Olli Etuahod81ed842015-05-12 12:46:35 +03002978 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
2979 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
2980 if (binary != nullptr)
2981 {
2982 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
2983 TIntermTyped *expression = binary->getRight();
2984 ASSERT(symbol);
2985 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05002986
Olli Etuahod81ed842015-05-12 12:46:35 +03002987 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05002988
Olli Etuahod81ed842015-05-12 12:46:35 +03002989 if (!writeSameSymbolInitializer(out, symbol, expression))
2990 {
2991 ASSERT(mInfoSinkStack.top() == &out);
2992 expression->traverse(this);
2993 }
2994 out << ";\n";
2995 }
2996 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05002997 {
2998 ASSERT(mInfoSinkStack.top() == &out);
Olli Etuahod81ed842015-05-12 12:46:35 +03002999 writeSelection(selection);
Jamie Madill37997142015-01-28 10:06:34 -05003000 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003001 else
3002 {
3003 UNREACHABLE();
3004 }
Jamie Madill37997142015-01-28 10:06:34 -05003005 }
3006
3007 out << "}\n"
3008 << "\n";
3009}
3010
Jamie Madill55e79e02015-02-09 15:35:00 -05003011TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3012{
3013 const TFieldList &fields = structure.fields();
3014
3015 for (const auto &eqFunction : mStructEqualityFunctions)
3016 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003017 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003018 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003019 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003020 }
3021 }
3022
3023 const TString &structNameString = StructNameString(structure);
3024
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003025 StructEqualityFunction *function = new StructEqualityFunction();
3026 function->structure = &structure;
3027 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003028
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003029 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003030
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003031 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3032 << "{\n"
3033 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003034
3035 for (size_t i = 0; i < fields.size(); i++)
3036 {
3037 const TField *field = fields[i];
3038 const TType *fieldType = field->type();
3039
3040 const TString &fieldNameA = "a." + Decorate(field->name());
3041 const TString &fieldNameB = "b." + Decorate(field->name());
3042
3043 if (i > 0)
3044 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003045 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003046 }
3047
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003048 fnOut << "(";
3049 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3050 fnOut << fieldNameA;
3051 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3052 fnOut << fieldNameB;
3053 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3054 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003055 }
3056
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003057 fnOut << ";\n" << "}\n";
3058
3059 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003060
3061 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003062 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003063
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003064 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003065}
3066
Olli Etuaho7fb49552015-03-18 17:27:44 +02003067TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3068{
3069 for (const auto &eqFunction : mArrayEqualityFunctions)
3070 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003071 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003072 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003073 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003074 }
3075 }
3076
3077 const TString &typeName = TypeString(type);
3078
Olli Etuaho12690762015-03-31 12:55:28 +03003079 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003080 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003081
3082 TInfoSinkBase fnNameOut;
3083 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003084 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003085
3086 TType nonArrayType = type;
3087 nonArrayType.clearArrayness();
3088
3089 TInfoSinkBase fnOut;
3090
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003091 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003092 << typeName << " a[" << type.getArraySize() << "], "
3093 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003094 << "{\n"
3095 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3096 " {\n"
3097 " if (";
3098
3099 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3100 fnOut << "a[i]";
3101 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3102 fnOut << "b[i]";
3103 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3104
3105 fnOut << ") { return false; }\n"
3106 " }\n"
3107 " return true;\n"
3108 "}\n";
3109
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003110 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003111
3112 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003113 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003114
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003115 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003116}
3117
Olli Etuaho12690762015-03-31 12:55:28 +03003118TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3119{
3120 for (const auto &assignFunction : mArrayAssignmentFunctions)
3121 {
3122 if (assignFunction.type == type)
3123 {
3124 return assignFunction.functionName;
3125 }
3126 }
3127
3128 const TString &typeName = TypeString(type);
3129
3130 ArrayHelperFunction function;
3131 function.type = type;
3132
3133 TInfoSinkBase fnNameOut;
3134 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3135 function.functionName = fnNameOut.c_str();
3136
3137 TInfoSinkBase fnOut;
3138
3139 fnOut << "void " << function.functionName << "(out "
3140 << typeName << " a[" << type.getArraySize() << "], "
3141 << typeName << " b[" << type.getArraySize() << "])\n"
3142 << "{\n"
3143 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3144 " {\n"
3145 " a[i] = b[i];\n"
3146 " }\n"
3147 "}\n";
3148
3149 function.functionDefinition = fnOut.c_str();
3150
3151 mArrayAssignmentFunctions.push_back(function);
3152
3153 return function.functionName;
3154}
3155
Olli Etuaho9638c352015-04-01 14:34:52 +03003156TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3157{
3158 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3159 {
3160 if (constructIntoFunction.type == type)
3161 {
3162 return constructIntoFunction.functionName;
3163 }
3164 }
3165
3166 const TString &typeName = TypeString(type);
3167
3168 ArrayHelperFunction function;
3169 function.type = type;
3170
3171 TInfoSinkBase fnNameOut;
3172 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3173 function.functionName = fnNameOut.c_str();
3174
3175 TInfoSinkBase fnOut;
3176
3177 fnOut << "void " << function.functionName << "(out "
3178 << typeName << " a[" << type.getArraySize() << "]";
3179 for (int i = 0; i < type.getArraySize(); ++i)
3180 {
3181 fnOut << ", " << typeName << " b" << i;
3182 }
3183 fnOut << ")\n"
3184 "{\n";
3185
3186 for (int i = 0; i < type.getArraySize(); ++i)
3187 {
3188 fnOut << " a[" << i << "] = b" << i << ";\n";
3189 }
3190 fnOut << "}\n";
3191
3192 function.functionDefinition = fnOut.c_str();
3193
3194 mArrayConstructIntoFunctions.push_back(function);
3195
3196 return function.functionName;
3197}
3198
Jamie Madill2e295e22015-04-29 10:41:33 -04003199void OutputHLSL::ensureStructDefined(const TType &type)
3200{
3201 TStructure *structure = type.getStruct();
3202
3203 if (structure)
3204 {
3205 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3206 }
3207}
3208
3209
Olli Etuaho9638c352015-04-01 14:34:52 +03003210
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003211}