blob: fce12f177cbf31f703eb9b6116258688d41ee0ec [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"
14#include "common/utilities.h"
15#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
16#include "compiler/translator/DetectDiscontinuity.h"
17#include "compiler/translator/FlagStd140Structs.h"
18#include "compiler/translator/InfoSink.h"
19#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020020#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050021#include "compiler/translator/RewriteElseBlocks.h"
22#include "compiler/translator/SearchSymbol.h"
23#include "compiler/translator/StructureHLSL.h"
24#include "compiler/translator/TranslatorHLSL.h"
25#include "compiler/translator/UnfoldShortCircuit.h"
26#include "compiler/translator/UniformHLSL.h"
27#include "compiler/translator/UtilsHLSL.h"
28#include "compiler/translator/blocklayout.h"
29#include "compiler/translator/compilerdebug.h"
30#include "compiler/translator/util.h"
31
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032namespace sh
33{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000034
Nicolas Capense0ba27a2013-06-24 16:10:52 -040035TString OutputHLSL::TextureFunction::name() const
36{
37 TString name = "gl_texture";
38
Nicolas Capens6d232bb2013-07-08 15:56:38 -040039 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040040 {
41 name += "2D";
42 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040043 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040044 {
45 name += "3D";
46 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040047 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040048 {
49 name += "Cube";
50 }
51 else UNREACHABLE();
52
53 if (proj)
54 {
55 name += "Proj";
56 }
57
Nicolas Capensb1f45b72013-12-19 17:37:19 -050058 if (offset)
59 {
60 name += "Offset";
61 }
62
Nicolas Capens75fb4752013-07-10 15:14:47 -040063 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040064 {
Nicolas Capensfc014542014-02-18 14:47:13 -050065 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040066 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050067 case LOD: name += "Lod"; break;
68 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040069 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050070 case SIZE: name += "Size"; break;
71 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050072 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040073 default: UNREACHABLE();
74 }
75
76 return name + "(";
77}
78
79bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
80{
81 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040082 if (sampler > rhs.sampler) return false;
83
Nicolas Capense0ba27a2013-06-24 16:10:52 -040084 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040085 if (coords > rhs.coords) return false;
86
Nicolas Capense0ba27a2013-06-24 16:10:52 -040087 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040088 if (proj && !rhs.proj) return false;
89
90 if (!offset && rhs.offset) return true;
91 if (offset && !rhs.offset) return false;
92
Nicolas Capens75fb4752013-07-10 15:14:47 -040093 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040094 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040095
96 return false;
97}
98
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020099OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
100 const TExtensionBehavior &extensionBehavior,
101 const char *sourcePath, ShShaderOutput outputType,
102 int numRenderTargets, const std::vector<Uniform> &uniforms,
103 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400104 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200105 mShaderType(shaderType),
106 mShaderVersion(shaderVersion),
107 mExtensionBehavior(extensionBehavior),
108 mSourcePath(sourcePath),
109 mOutputType(outputType),
110 mNumRenderTargets(numRenderTargets),
111 mCompileOptions(compileOptions)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200113 mUnfoldShortCircuit = new UnfoldShortCircuit(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000114 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000115
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000116 mUsesFragColor = false;
117 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000118 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000119 mUsesFragCoord = false;
120 mUsesPointCoord = false;
121 mUsesFrontFacing = false;
122 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000123 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400124 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000125 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500126 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400127 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000128
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000129 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000130
131 mContainsLoopDiscontinuity = false;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800132 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000133 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000134 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400135 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000136
137 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000138
Jamie Madill8daaba12014-06-13 10:04:33 -0400139 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200140 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400141
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000142 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000143 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200144 if (mShaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000145 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400146 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
147 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000148 }
149 else
150 {
Cooper Partine6664f02015-01-09 16:22:24 -0800151 // Reserve registers for dx_DepthRange, dx_ViewAdjust and dx_ViewCoords
152 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000153 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000154 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000155
Jamie Madillf91ce812014-06-13 10:04:34 -0400156 // Reserve registers for the default uniform block and driver constants
157 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158}
159
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000160OutputHLSL::~OutputHLSL()
161{
Jamie Madill8daaba12014-06-13 10:04:33 -0400162 SafeDelete(mUnfoldShortCircuit);
163 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400164 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000165}
166
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200167void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000168{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200169 mContainsLoopDiscontinuity = mShaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(treeRoot);
170 mContainsAnyLoop = containsAnyLoop(treeRoot);
171 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400172 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000173
Jamie Madille53c98b2014-02-03 11:57:13 -0500174 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
175 // use a vertex attribute as a condition, and some related computation in the else block.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200176 if (mOutputType == SH_HLSL9_OUTPUT && mShaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500177 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200178 RewriteElseBlocks(treeRoot);
Jamie Madille53c98b2014-02-03 11:57:13 -0500179 }
180
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200181 BuiltInFunctionEmulatorHLSL builtInFunctionEmulator;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200182 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500183
Jamie Madill37997142015-01-28 10:06:34 -0500184 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500185 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200186 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500187 mInfoSinkStack.pop();
188
Jamie Madill37997142015-01-28 10:06:34 -0500189 mInfoSinkStack.push(&mFooter);
190 if (!mDeferredGlobalInitializers.empty())
191 {
192 writeDeferredGlobalInitializers(mFooter);
193 }
194 mInfoSinkStack.pop();
195
Jamie Madill32aab012015-01-27 14:12:26 -0500196 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200197 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500198 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000199
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200200 objSink << mHeader.c_str();
201 objSink << mBody.c_str();
202 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200203
204 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000205}
206
Jamie Madill570e04d2013-06-21 09:15:33 -0400207void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
208{
209 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
210 {
211 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
212
Jamie Madill32aab012015-01-27 14:12:26 -0500213 TInfoSinkBase structInfoSink;
214 mInfoSinkStack.push(&structInfoSink);
215
Jamie Madill570e04d2013-06-21 09:15:33 -0400216 // This will mark the necessary block elements as referenced
217 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500218
219 TString structName(structInfoSink.c_str());
220 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400221
222 mFlaggedStructOriginalNames[flaggedNode] = structName;
223
224 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
225 {
226 structName.erase(pos, 1);
227 }
228
229 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
230 }
231}
232
Jamie Madill4e1fd412014-07-10 17:50:10 -0400233const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
234{
235 return mUniformHLSL->getInterfaceBlockRegisterMap();
236}
237
Jamie Madill9fe25e92014-07-18 10:33:08 -0400238const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
239{
240 return mUniformHLSL->getUniformRegisterMap();
241}
242
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000243int OutputHLSL::vectorSize(const TType &type) const
244{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000245 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000246 int arraySize = type.isArray() ? type.getArraySize() : 1;
247
248 return elementSize * arraySize;
249}
250
Jamie Madill98493dd2013-07-08 14:39:03 -0400251TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400252{
253 TString init;
254
255 TString preIndentString;
256 TString fullIndentString;
257
258 for (int spaces = 0; spaces < (indent * 4); spaces++)
259 {
260 preIndentString += ' ';
261 }
262
263 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
264 {
265 fullIndentString += ' ';
266 }
267
268 init += preIndentString + "{\n";
269
Jamie Madill98493dd2013-07-08 14:39:03 -0400270 const TFieldList &fields = structure.fields();
271 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400272 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400274 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400276
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400278 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400279 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400280 }
281 else
282 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400283 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400284 }
285 }
286
287 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
288
289 return init;
290}
291
Olli Etuahoe17e3192015-01-02 12:47:59 +0200292void OutputHLSL::header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293{
Jamie Madill32aab012015-01-27 14:12:26 -0500294 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000296 TString varyings;
297 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400298 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000299
Jamie Madill829f59e2013-11-13 19:40:54 -0500300 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400301 {
302 TIntermTyped *structNode = flaggedStructIt->first;
303 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400304 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400305 const TString &originalName = mFlaggedStructOriginalNames[structNode];
306
Jamie Madill033dae62014-06-18 12:56:28 -0400307 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400308 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400309 flaggedStructs += "\n";
310 }
311
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000312 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
313 {
314 const TType &type = varying->second->getType();
315 const TString &name = varying->second->getSymbol();
316
317 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400318 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
319 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000320 }
321
322 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
323 {
324 const TType &type = attribute->second->getType();
325 const TString &name = attribute->second->getSymbol();
326
Jamie Madill033dae62014-06-18 12:56:28 -0400327 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000328 }
329
Jamie Madill8daaba12014-06-13 10:04:33 -0400330 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400331
Jamie Madillf91ce812014-06-13 10:04:34 -0400332 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
333 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
334
Jamie Madill55e79e02015-02-09 15:35:00 -0500335 if (!mStructEqualityFunctions.empty())
336 {
337 out << "\n// Structure equality functions\n\n";
338 for (const auto &eqFunction : mStructEqualityFunctions)
339 {
340 out << eqFunction.functionDefinition << "\n";
341 }
342 }
343
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500344 if (mUsesDiscardRewriting)
345 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400346 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500347 }
348
Nicolas Capens655fe362014-04-11 13:12:34 -0400349 if (mUsesNestedBreak)
350 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400351 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400352 }
353
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400354 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
355 "#define LOOP [loop]\n"
356 "#define FLATTEN [flatten]\n"
357 "#else\n"
358 "#define LOOP\n"
359 "#define FLATTEN\n"
360 "#endif\n";
361
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200362 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200364 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
365 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000366
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000367 out << "// Varyings\n";
368 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400369 out << "\n";
370
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200371 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000372 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500373 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000374 {
Jamie Madill46131a32013-06-20 11:55:50 -0400375 const TString &variableName = outputVariableIt->first;
376 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400377
Jamie Madill033dae62014-06-18 12:56:28 -0400378 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400379 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000380 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000381 }
Jamie Madill46131a32013-06-20 11:55:50 -0400382 else
383 {
384 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
385
386 out << "static float4 gl_Color[" << numColorValues << "] =\n"
387 "{\n";
388 for (unsigned int i = 0; i < numColorValues; i++)
389 {
390 out << " float4(0, 0, 0, 0)";
391 if (i + 1 != numColorValues)
392 {
393 out << ",";
394 }
395 out << "\n";
396 }
397
398 out << "};\n";
399 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000400
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400401 if (mUsesFragDepth)
402 {
403 out << "static float gl_Depth = 0.0;\n";
404 }
405
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000406 if (mUsesFragCoord)
407 {
408 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
409 }
410
411 if (mUsesPointCoord)
412 {
413 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
414 }
415
416 if (mUsesFrontFacing)
417 {
418 out << "static bool gl_FrontFacing = false;\n";
419 }
420
421 out << "\n";
422
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000423 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000424 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000425 out << "struct gl_DepthRangeParameters\n"
426 "{\n"
427 " float near;\n"
428 " float far;\n"
429 " float diff;\n"
430 "};\n"
431 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000432 }
433
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000434 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000435 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000436 out << "cbuffer DriverConstants : register(b1)\n"
437 "{\n";
438
439 if (mUsesDepthRange)
440 {
441 out << " float3 dx_DepthRange : packoffset(c0);\n";
442 }
443
444 if (mUsesFragCoord)
445 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000446 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000447 }
448
449 if (mUsesFragCoord || mUsesFrontFacing)
450 {
451 out << " float3 dx_DepthFront : packoffset(c2);\n";
452 }
453
454 out << "};\n";
455 }
456 else
457 {
458 if (mUsesDepthRange)
459 {
460 out << "uniform float3 dx_DepthRange : register(c0);";
461 }
462
463 if (mUsesFragCoord)
464 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000465 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000466 }
467
468 if (mUsesFragCoord || mUsesFrontFacing)
469 {
470 out << "uniform float3 dx_DepthFront : register(c2);\n";
471 }
472 }
473
474 out << "\n";
475
476 if (mUsesDepthRange)
477 {
478 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
479 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000480 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000481
Jamie Madillf91ce812014-06-13 10:04:34 -0400482 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000483 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400484 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000485 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400486 out << flaggedStructs;
487 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000488 }
489
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000490 if (usingMRTExtension && mNumRenderTargets > 1)
491 {
492 out << "#define GL_USES_MRT\n";
493 }
494
495 if (mUsesFragColor)
496 {
497 out << "#define GL_USES_FRAG_COLOR\n";
498 }
499
500 if (mUsesFragData)
501 {
502 out << "#define GL_USES_FRAG_DATA\n";
503 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000504 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000505 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000507 out << "// Attributes\n";
508 out << attributes;
509 out << "\n"
510 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400511
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000512 if (mUsesPointSize)
513 {
514 out << "static float gl_PointSize = float(1);\n";
515 }
516
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000517 if (mUsesInstanceID)
518 {
519 out << "static int gl_InstanceID;";
520 }
521
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000522 out << "\n"
523 "// Varyings\n";
524 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000525 out << "\n";
526
527 if (mUsesDepthRange)
528 {
529 out << "struct gl_DepthRangeParameters\n"
530 "{\n"
531 " float near;\n"
532 " float far;\n"
533 " float diff;\n"
534 "};\n"
535 "\n";
536 }
537
538 if (mOutputType == SH_HLSL11_OUTPUT)
539 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800540 out << "cbuffer DriverConstants : register(b1)\n"
541 "{\n";
542
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000543 if (mUsesDepthRange)
544 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800545 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000546 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800547
Cooper Partine6664f02015-01-09 16:22:24 -0800548 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800549 // However, we declare it for all shaders (including Feature Level 10+).
550 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
551 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800552 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800553
554 out << "};\n"
555 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000556 }
557 else
558 {
559 if (mUsesDepthRange)
560 {
561 out << "uniform float3 dx_DepthRange : register(c0);\n";
562 }
563
Cooper Partine6664f02015-01-09 16:22:24 -0800564 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
565 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000566 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000567 }
568
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000569 if (mUsesDepthRange)
570 {
571 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
572 "\n";
573 }
574
Jamie Madillf91ce812014-06-13 10:04:34 -0400575 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000576 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400577 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000578 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400579 out << flaggedStructs;
580 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000581 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400582 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000583
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400584 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
585 {
586 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400587 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000588 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400589 switch(textureFunction->sampler)
590 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400591 case EbtSampler2D: out << "int2 "; break;
592 case EbtSampler3D: out << "int3 "; break;
593 case EbtSamplerCube: out << "int2 "; break;
594 case EbtSampler2DArray: out << "int3 "; break;
595 case EbtISampler2D: out << "int2 "; break;
596 case EbtISampler3D: out << "int3 "; break;
597 case EbtISamplerCube: out << "int2 "; break;
598 case EbtISampler2DArray: out << "int3 "; break;
599 case EbtUSampler2D: out << "int2 "; break;
600 case EbtUSampler3D: out << "int3 "; break;
601 case EbtUSamplerCube: out << "int2 "; break;
602 case EbtUSampler2DArray: out << "int3 "; break;
603 case EbtSampler2DShadow: out << "int2 "; break;
604 case EbtSamplerCubeShadow: out << "int2 "; break;
605 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400606 default: UNREACHABLE();
607 }
608 }
609 else // Sampling function
610 {
611 switch(textureFunction->sampler)
612 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400613 case EbtSampler2D: out << "float4 "; break;
614 case EbtSampler3D: out << "float4 "; break;
615 case EbtSamplerCube: out << "float4 "; break;
616 case EbtSampler2DArray: out << "float4 "; break;
617 case EbtISampler2D: out << "int4 "; break;
618 case EbtISampler3D: out << "int4 "; break;
619 case EbtISamplerCube: out << "int4 "; break;
620 case EbtISampler2DArray: out << "int4 "; break;
621 case EbtUSampler2D: out << "uint4 "; break;
622 case EbtUSampler3D: out << "uint4 "; break;
623 case EbtUSamplerCube: out << "uint4 "; break;
624 case EbtUSampler2DArray: out << "uint4 "; break;
625 case EbtSampler2DShadow: out << "float "; break;
626 case EbtSamplerCubeShadow: out << "float "; break;
627 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400628 default: UNREACHABLE();
629 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000630 }
631
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400632 // Function name
633 out << textureFunction->name();
634
635 // Argument list
636 int hlslCoords = 4;
637
638 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000639 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400640 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000641 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400642 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
643 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
644 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000645 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400646
Nicolas Capens75fb4752013-07-10 15:14:47 -0400647 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000648 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400649 case TextureFunction::IMPLICIT: break;
650 case TextureFunction::BIAS: hlslCoords = 4; break;
651 case TextureFunction::LOD: hlslCoords = 4; break;
652 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400653 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400654 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000655 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400656 }
657 else if (mOutputType == SH_HLSL11_OUTPUT)
658 {
659 switch(textureFunction->sampler)
660 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400661 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
662 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
663 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
664 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
665 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
666 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500667 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400668 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
669 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
670 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500671 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400672 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
673 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
674 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
675 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400676 default: UNREACHABLE();
677 }
678 }
679 else UNREACHABLE();
680
Nicolas Capensfc014542014-02-18 14:47:13 -0500681 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400682 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500683 switch(textureFunction->coords)
684 {
685 case 2: out << ", int2 t"; break;
686 case 3: out << ", int3 t"; break;
687 default: UNREACHABLE();
688 }
689 }
690 else // Floating-point coordinates (except textureSize)
691 {
692 switch(textureFunction->coords)
693 {
694 case 1: out << ", int lod"; break; // textureSize()
695 case 2: out << ", float2 t"; break;
696 case 3: out << ", float3 t"; break;
697 case 4: out << ", float4 t"; break;
698 default: UNREACHABLE();
699 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000700 }
701
Nicolas Capensd11d5492014-02-19 17:06:10 -0500702 if (textureFunction->method == TextureFunction::GRAD)
703 {
704 switch(textureFunction->sampler)
705 {
706 case EbtSampler2D:
707 case EbtISampler2D:
708 case EbtUSampler2D:
709 case EbtSampler2DArray:
710 case EbtISampler2DArray:
711 case EbtUSampler2DArray:
712 case EbtSampler2DShadow:
713 case EbtSampler2DArrayShadow:
714 out << ", float2 ddx, float2 ddy";
715 break;
716 case EbtSampler3D:
717 case EbtISampler3D:
718 case EbtUSampler3D:
719 case EbtSamplerCube:
720 case EbtISamplerCube:
721 case EbtUSamplerCube:
722 case EbtSamplerCubeShadow:
723 out << ", float3 ddx, float3 ddy";
724 break;
725 default: UNREACHABLE();
726 }
727 }
728
Nicolas Capens75fb4752013-07-10 15:14:47 -0400729 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000730 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400731 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400732 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400733 case TextureFunction::LOD: out << ", float lod"; break;
734 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400735 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400736 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500737 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500738 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400739 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000740 }
741
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500742 if (textureFunction->offset)
743 {
744 switch(textureFunction->sampler)
745 {
746 case EbtSampler2D: out << ", int2 offset"; break;
747 case EbtSampler3D: out << ", int3 offset"; break;
748 case EbtSampler2DArray: out << ", int2 offset"; break;
749 case EbtISampler2D: out << ", int2 offset"; break;
750 case EbtISampler3D: out << ", int3 offset"; break;
751 case EbtISampler2DArray: out << ", int2 offset"; break;
752 case EbtUSampler2D: out << ", int2 offset"; break;
753 case EbtUSampler3D: out << ", int3 offset"; break;
754 case EbtUSampler2DArray: out << ", int2 offset"; break;
755 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500756 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500757 default: UNREACHABLE();
758 }
759 }
760
Nicolas Capens84cfa122014-04-14 13:48:45 -0400761 if (textureFunction->method == TextureFunction::BIAS ||
762 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500763 {
764 out << ", float bias";
765 }
766
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400767 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400768 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400769
Nicolas Capens75fb4752013-07-10 15:14:47 -0400770 if (textureFunction->method == TextureFunction::SIZE)
771 {
772 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
773 {
774 if (IsSamplerArray(textureFunction->sampler))
775 {
776 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
777 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
778 }
779 else
780 {
781 out << " uint width; uint height; uint numberOfLevels;\n"
782 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
783 }
784 }
785 else if (IsSampler3D(textureFunction->sampler))
786 {
787 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
788 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
789 }
790 else UNREACHABLE();
791
792 switch(textureFunction->sampler)
793 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400794 case EbtSampler2D: out << " return int2(width, height);"; break;
795 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
796 case EbtSamplerCube: out << " return int2(width, height);"; break;
797 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
798 case EbtISampler2D: out << " return int2(width, height);"; break;
799 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
800 case EbtISamplerCube: out << " return int2(width, height);"; break;
801 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
802 case EbtUSampler2D: out << " return int2(width, height);"; break;
803 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
804 case EbtUSamplerCube: out << " return int2(width, height);"; break;
805 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
806 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
807 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
808 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400809 default: UNREACHABLE();
810 }
811 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400812 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400813 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500814 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
815 {
816 out << " float width; float height; float layers; float levels;\n";
817
818 out << " uint mip = 0;\n";
819
820 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
821
822 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
823 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
824 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
825 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
826
827 // FACE_POSITIVE_X = 000b
828 // FACE_NEGATIVE_X = 001b
829 // FACE_POSITIVE_Y = 010b
830 // FACE_NEGATIVE_Y = 011b
831 // FACE_POSITIVE_Z = 100b
832 // FACE_NEGATIVE_Z = 101b
833 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
834
835 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
836 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
837 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
838
839 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
840 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
841 }
842 else if (IsIntegerSampler(textureFunction->sampler) &&
843 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400844 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400845 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400846 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400847 if (IsSamplerArray(textureFunction->sampler))
848 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400849 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400850
Nicolas Capens9edebd62013-08-06 10:59:10 -0400851 if (textureFunction->method == TextureFunction::LOD0)
852 {
853 out << " uint mip = 0;\n";
854 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400855 else if (textureFunction->method == TextureFunction::LOD0BIAS)
856 {
857 out << " uint mip = bias;\n";
858 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400859 else
860 {
861 if (textureFunction->method == TextureFunction::IMPLICIT ||
862 textureFunction->method == TextureFunction::BIAS)
863 {
864 out << " x.GetDimensions(0, width, height, layers, levels);\n"
865 " float2 tSized = float2(t.x * width, t.y * height);\n"
866 " float dx = length(ddx(tSized));\n"
867 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500868 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400869
870 if (textureFunction->method == TextureFunction::BIAS)
871 {
872 out << " lod += bias;\n";
873 }
874 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500875 else if (textureFunction->method == TextureFunction::GRAD)
876 {
877 out << " x.GetDimensions(0, width, height, layers, levels);\n"
878 " float lod = log2(max(length(ddx), length(ddy)));\n";
879 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400880
881 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
882 }
883
884 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400885 }
886 else
887 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400888 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400889
Nicolas Capens9edebd62013-08-06 10:59:10 -0400890 if (textureFunction->method == TextureFunction::LOD0)
891 {
892 out << " uint mip = 0;\n";
893 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400894 else if (textureFunction->method == TextureFunction::LOD0BIAS)
895 {
896 out << " uint mip = bias;\n";
897 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400898 else
899 {
900 if (textureFunction->method == TextureFunction::IMPLICIT ||
901 textureFunction->method == TextureFunction::BIAS)
902 {
903 out << " x.GetDimensions(0, width, height, levels);\n"
904 " float2 tSized = float2(t.x * width, t.y * height);\n"
905 " float dx = length(ddx(tSized));\n"
906 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500907 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400908
909 if (textureFunction->method == TextureFunction::BIAS)
910 {
911 out << " lod += bias;\n";
912 }
913 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500914 else if (textureFunction->method == TextureFunction::LOD)
915 {
916 out << " x.GetDimensions(0, width, height, levels);\n";
917 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500918 else if (textureFunction->method == TextureFunction::GRAD)
919 {
920 out << " x.GetDimensions(0, width, height, levels);\n"
921 " float lod = log2(max(length(ddx), length(ddy)));\n";
922 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400923
924 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
925 }
926
927 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400928 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400929 }
930 else if (IsSampler3D(textureFunction->sampler))
931 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400932 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400933
Nicolas Capens9edebd62013-08-06 10:59:10 -0400934 if (textureFunction->method == TextureFunction::LOD0)
935 {
936 out << " uint mip = 0;\n";
937 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400938 else if (textureFunction->method == TextureFunction::LOD0BIAS)
939 {
940 out << " uint mip = bias;\n";
941 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400942 else
943 {
944 if (textureFunction->method == TextureFunction::IMPLICIT ||
945 textureFunction->method == TextureFunction::BIAS)
946 {
947 out << " x.GetDimensions(0, width, height, depth, levels);\n"
948 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
949 " float dx = length(ddx(tSized));\n"
950 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500951 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400952
953 if (textureFunction->method == TextureFunction::BIAS)
954 {
955 out << " lod += bias;\n";
956 }
957 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500958 else if (textureFunction->method == TextureFunction::GRAD)
959 {
960 out << " x.GetDimensions(0, width, height, depth, levels);\n"
961 " float lod = log2(max(length(ddx), length(ddy)));\n";
962 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400963
964 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
965 }
966
967 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400968 }
969 else UNREACHABLE();
970 }
971
972 out << " return ";
973
974 // HLSL intrinsic
975 if (mOutputType == SH_HLSL9_OUTPUT)
976 {
977 switch(textureFunction->sampler)
978 {
979 case EbtSampler2D: out << "tex2D"; break;
980 case EbtSamplerCube: out << "texCUBE"; break;
981 default: UNREACHABLE();
982 }
983
Nicolas Capens75fb4752013-07-10 15:14:47 -0400984 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400985 {
986 case TextureFunction::IMPLICIT: out << "(s, "; break;
987 case TextureFunction::BIAS: out << "bias(s, "; break;
988 case TextureFunction::LOD: out << "lod(s, "; break;
989 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400990 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400991 default: UNREACHABLE();
992 }
993 }
994 else if (mOutputType == SH_HLSL11_OUTPUT)
995 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500996 if (textureFunction->method == TextureFunction::GRAD)
997 {
998 if (IsIntegerSampler(textureFunction->sampler))
999 {
1000 out << "x.Load(";
1001 }
1002 else if (IsShadowSampler(textureFunction->sampler))
1003 {
1004 out << "x.SampleCmpLevelZero(s, ";
1005 }
1006 else
1007 {
1008 out << "x.SampleGrad(s, ";
1009 }
1010 }
1011 else if (IsIntegerSampler(textureFunction->sampler) ||
1012 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001013 {
1014 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001015 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001016 else if (IsShadowSampler(textureFunction->sampler))
1017 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001018 switch(textureFunction->method)
1019 {
1020 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1021 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1022 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1023 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1024 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1025 default: UNREACHABLE();
1026 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001027 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001028 else
1029 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001030 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001031 {
1032 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1033 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1034 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1035 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001036 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001037 default: UNREACHABLE();
1038 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001039 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001040 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001041 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001042
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001043 // Integer sampling requires integer addresses
1044 TString addressx = "";
1045 TString addressy = "";
1046 TString addressz = "";
1047 TString close = "";
1048
Nicolas Capensfc014542014-02-18 14:47:13 -05001049 if (IsIntegerSampler(textureFunction->sampler) ||
1050 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001051 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001052 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001053 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001054 case 2: out << "int3("; break;
1055 case 3: out << "int4("; break;
1056 default: UNREACHABLE();
1057 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001058
Nicolas Capensfc014542014-02-18 14:47:13 -05001059 // Convert from normalized floating-point to integer
1060 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001061 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001062 addressx = "int(floor(width * frac((";
1063 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001064
Nicolas Capensfc014542014-02-18 14:47:13 -05001065 if (IsSamplerArray(textureFunction->sampler))
1066 {
1067 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1068 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001069 else if (IsSamplerCube(textureFunction->sampler))
1070 {
1071 addressz = "((((";
1072 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001073 else
1074 {
1075 addressz = "int(floor(depth * frac((";
1076 }
1077
1078 close = "))))";
1079 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001080 }
1081 else
1082 {
1083 switch(hlslCoords)
1084 {
1085 case 2: out << "float2("; break;
1086 case 3: out << "float3("; break;
1087 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001088 default: UNREACHABLE();
1089 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001090 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001091
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001092 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001093
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001094 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001095 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001096 switch(textureFunction->coords)
1097 {
1098 case 3: proj = " / t.z"; break;
1099 case 4: proj = " / t.w"; break;
1100 default: UNREACHABLE();
1101 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001102 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001103
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001104 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001105
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001106 if (mOutputType == SH_HLSL9_OUTPUT)
1107 {
1108 if (hlslCoords >= 3)
1109 {
1110 if (textureFunction->coords < 3)
1111 {
1112 out << ", 0";
1113 }
1114 else
1115 {
1116 out << ", t.z" + proj;
1117 }
1118 }
1119
1120 if (hlslCoords == 4)
1121 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001122 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001123 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001124 case TextureFunction::BIAS: out << ", bias"; break;
1125 case TextureFunction::LOD: out << ", lod"; break;
1126 case TextureFunction::LOD0: out << ", 0"; break;
1127 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001128 default: UNREACHABLE();
1129 }
1130 }
1131
1132 out << "));\n";
1133 }
1134 else if (mOutputType == SH_HLSL11_OUTPUT)
1135 {
1136 if (hlslCoords >= 3)
1137 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001138 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1139 {
1140 out << ", face";
1141 }
1142 else
1143 {
1144 out << ", " + addressz + ("t.z" + proj) + close;
1145 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001146 }
1147
Nicolas Capensd11d5492014-02-19 17:06:10 -05001148 if (textureFunction->method == TextureFunction::GRAD)
1149 {
1150 if (IsIntegerSampler(textureFunction->sampler))
1151 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001152 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001153 }
1154 else if (IsShadowSampler(textureFunction->sampler))
1155 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001156 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001157 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001158 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001159 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1160 // The resulting third component of P' in the shadow forms is used as Dref
1161 out << "), t.z" << proj;
1162 }
1163 else
1164 {
1165 switch(textureFunction->coords)
1166 {
1167 case 3: out << "), t.z"; break;
1168 case 4: out << "), t.w"; break;
1169 default: UNREACHABLE();
1170 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001171 }
1172 }
1173 else
1174 {
1175 out << "), ddx, ddy";
1176 }
1177 }
1178 else if (IsIntegerSampler(textureFunction->sampler) ||
1179 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001180 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001181 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001182 }
1183 else if (IsShadowSampler(textureFunction->sampler))
1184 {
1185 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001186 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001187 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001188 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1189 // The resulting third component of P' in the shadow forms is used as Dref
1190 out << "), t.z" << proj;
1191 }
1192 else
1193 {
1194 switch(textureFunction->coords)
1195 {
1196 case 3: out << "), t.z"; break;
1197 case 4: out << "), t.w"; break;
1198 default: UNREACHABLE();
1199 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001200 }
1201 }
1202 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001203 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001204 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001205 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001206 case TextureFunction::IMPLICIT: out << ")"; break;
1207 case TextureFunction::BIAS: out << "), bias"; break;
1208 case TextureFunction::LOD: out << "), lod"; break;
1209 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001210 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001211 default: UNREACHABLE();
1212 }
1213 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001214
1215 if (textureFunction->offset)
1216 {
1217 out << ", offset";
1218 }
1219
1220 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001221 }
1222 else UNREACHABLE();
1223 }
1224
1225 out << "\n"
1226 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001227 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001228 }
1229
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001230 if (mUsesFragCoord)
1231 {
1232 out << "#define GL_USES_FRAG_COORD\n";
1233 }
1234
1235 if (mUsesPointCoord)
1236 {
1237 out << "#define GL_USES_POINT_COORD\n";
1238 }
1239
1240 if (mUsesFrontFacing)
1241 {
1242 out << "#define GL_USES_FRONT_FACING\n";
1243 }
1244
1245 if (mUsesPointSize)
1246 {
1247 out << "#define GL_USES_POINT_SIZE\n";
1248 }
1249
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001250 if (mUsesFragDepth)
1251 {
1252 out << "#define GL_USES_FRAG_DEPTH\n";
1253 }
1254
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001255 if (mUsesDepthRange)
1256 {
1257 out << "#define GL_USES_DEPTH_RANGE\n";
1258 }
1259
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001260 if (mUsesXor)
1261 {
1262 out << "bool xor(bool p, bool q)\n"
1263 "{\n"
1264 " return (p || q) && !(p && q);\n"
1265 "}\n"
1266 "\n";
1267 }
1268
Olli Etuaho80a5a6c2015-01-12 15:35:27 +02001269 builtInFunctionEmulator->OutputEmulatedFunctionDefinition(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001270}
1271
1272void OutputHLSL::visitSymbol(TIntermSymbol *node)
1273{
Jamie Madill32aab012015-01-27 14:12:26 -05001274 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001275
Jamie Madill570e04d2013-06-21 09:15:33 -04001276 // Handle accessing std140 structs by value
1277 if (mFlaggedStructMappedNames.count(node) > 0)
1278 {
1279 out << mFlaggedStructMappedNames[node];
1280 return;
1281 }
1282
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001283 TString name = node->getSymbol();
1284
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001285 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001286 {
1287 mUsesDepthRange = true;
1288 out << name;
1289 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001290 else
1291 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001292 TQualifier qualifier = node->getQualifier();
1293
1294 if (qualifier == EvqUniform)
1295 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001296 const TType& nodeType = node->getType();
1297 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1298
1299 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001300 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001301 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001302 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001303 else
1304 {
1305 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001306 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001307
Jamie Madill033dae62014-06-18 12:56:28 -04001308 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001309 }
Jamie Madill19571812013-08-12 15:26:34 -07001310 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001311 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001312 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001313 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001314 }
Jamie Madill033dae62014-06-18 12:56:28 -04001315 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001316 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001317 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001318 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001319 }
Jamie Madill19571812013-08-12 15:26:34 -07001320 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001321 {
1322 mReferencedOutputVariables[name] = node;
1323 out << "out_" << name;
1324 }
1325 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001326 {
1327 out << "gl_Color[0]";
1328 mUsesFragColor = true;
1329 }
1330 else if (qualifier == EvqFragData)
1331 {
1332 out << "gl_Color";
1333 mUsesFragData = true;
1334 }
1335 else if (qualifier == EvqFragCoord)
1336 {
1337 mUsesFragCoord = true;
1338 out << name;
1339 }
1340 else if (qualifier == EvqPointCoord)
1341 {
1342 mUsesPointCoord = true;
1343 out << name;
1344 }
1345 else if (qualifier == EvqFrontFacing)
1346 {
1347 mUsesFrontFacing = true;
1348 out << name;
1349 }
1350 else if (qualifier == EvqPointSize)
1351 {
1352 mUsesPointSize = true;
1353 out << name;
1354 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001355 else if (qualifier == EvqInstanceID)
1356 {
1357 mUsesInstanceID = true;
1358 out << name;
1359 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001360 else if (name == "gl_FragDepthEXT")
1361 {
1362 mUsesFragDepth = true;
1363 out << "gl_Depth";
1364 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001365 else if (qualifier == EvqInternal)
1366 {
1367 out << name;
1368 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001369 else
1370 {
Jamie Madill033dae62014-06-18 12:56:28 -04001371 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001372 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001373 }
1374}
1375
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001376void OutputHLSL::visitRaw(TIntermRaw *node)
1377{
Jamie Madill32aab012015-01-27 14:12:26 -05001378 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001379}
1380
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001381bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1382{
Jamie Madill32aab012015-01-27 14:12:26 -05001383 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001384
Jamie Madill570e04d2013-06-21 09:15:33 -04001385 // Handle accessing std140 structs by value
1386 if (mFlaggedStructMappedNames.count(node) > 0)
1387 {
1388 out << mFlaggedStructMappedNames[node];
1389 return false;
1390 }
1391
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001392 switch (node->getOp())
1393 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001394 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001395 case EOpInitialize:
1396 if (visit == PreVisit)
1397 {
1398 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1399 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1400 // new variable is created before the assignment is evaluated), so we need to convert
1401 // this to "float t = x, x = t;".
1402
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001403 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001404 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001405 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001406
Jamie Madill37997142015-01-28 10:06:34 -05001407 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1408 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001409 {
Jamie Madill37997142015-01-28 10:06:34 -05001410 // For variables which are not constant, defer their real initialization until
1411 // after we initialize other globals: uniforms, attributes and varyings.
1412 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1413 const TString &initString = initializer(node->getType());
1414 node->setRight(new TIntermRaw(node->getType(), initString));
1415 }
1416 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1417 {
1418 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001419 return false;
1420 }
1421 }
1422 else if (visit == InVisit)
1423 {
1424 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001425 }
1426 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001427 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1428 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1429 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1430 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1431 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1432 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001433 if (visit == PreVisit)
1434 {
1435 out << "(";
1436 }
1437 else if (visit == InVisit)
1438 {
1439 out << " = mul(";
1440 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001441 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001442 }
1443 else
1444 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001445 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001446 }
1447 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001448 case EOpMatrixTimesMatrixAssign:
1449 if (visit == PreVisit)
1450 {
1451 out << "(";
1452 }
1453 else if (visit == InVisit)
1454 {
1455 out << " = mul(";
1456 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001457 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001458 }
1459 else
1460 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001461 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001462 }
1463 break;
1464 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001465 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001466 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1467 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1468 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1469 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1470 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001471 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001472 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001473 const TType& leftType = node->getLeft()->getType();
1474 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001475 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001476 if (visit == PreVisit)
1477 {
1478 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1479 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001480 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001481 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001482 return false;
1483 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001484 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001485 else
1486 {
1487 outputTriplet(visit, "", "[", "]");
1488 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001489 }
1490 break;
1491 case EOpIndexIndirect:
1492 // We do not currently support indirect references to interface blocks
1493 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1494 outputTriplet(visit, "", "[", "]");
1495 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001496 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001497 if (visit == InVisit)
1498 {
1499 const TStructure* structure = node->getLeft()->getType().getStruct();
1500 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1501 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001502 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001503
1504 return false;
1505 }
1506 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001507 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001508 if (visit == InVisit)
1509 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001510 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1511 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1512 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001513 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001514
1515 return false;
1516 }
1517 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001518 case EOpVectorSwizzle:
1519 if (visit == InVisit)
1520 {
1521 out << ".";
1522
1523 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1524
1525 if (swizzle)
1526 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001527 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001528
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001529 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530 {
1531 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1532
1533 if (element)
1534 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001535 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001536
1537 switch (i)
1538 {
1539 case 0: out << "x"; break;
1540 case 1: out << "y"; break;
1541 case 2: out << "z"; break;
1542 case 3: out << "w"; break;
1543 default: UNREACHABLE();
1544 }
1545 }
1546 else UNREACHABLE();
1547 }
1548 }
1549 else UNREACHABLE();
1550
1551 return false; // Fully processed
1552 }
1553 break;
1554 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1555 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1556 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1557 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001558 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001559 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1560 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1561 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1562 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1563 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001564 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001565 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001566 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001567 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001568 if (node->getOp() == EOpEqual)
1569 {
1570 outputTriplet(visit, "(", " == ", ")");
1571 }
1572 else
1573 {
1574 outputTriplet(visit, "(", " != ", ")");
1575 }
1576 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001577 else
1578 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001579 if (visit == PreVisit && node->getOp() == EOpNotEqual)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001580 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001581 out << "!";
1582 }
1583
1584 if (node->getLeft()->getBasicType() == EbtStruct)
1585 {
1586 const TStructure &structure = *node->getLeft()->getType().getStruct();
1587 const TString &functionName = addStructEqualityFunction(structure);
Daniel Bratell29190082015-02-20 16:42:54 +01001588 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001589 }
1590 else
1591 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001592 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
1593 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001594 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001595 }
1596 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1598 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1599 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1600 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1601 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001602 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001603 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1604 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001605 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001606 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001607 if (node->getRight()->hasSideEffects())
1608 {
1609 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1610 return false;
1611 }
1612 else
1613 {
1614 outputTriplet(visit, "(", " || ", ")");
1615 return true;
1616 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001617 case EOpLogicalXor:
1618 mUsesXor = true;
1619 outputTriplet(visit, "xor(", ", ", ")");
1620 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001621 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001622 if (node->getRight()->hasSideEffects())
1623 {
1624 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1625 return false;
1626 }
1627 else
1628 {
1629 outputTriplet(visit, "(", " && ", ")");
1630 return true;
1631 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632 default: UNREACHABLE();
1633 }
1634
1635 return true;
1636}
1637
1638bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1639{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001640 switch (node->getOp())
1641 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001642 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001643 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001644 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1645 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001646 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001647 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1648 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1649 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1650 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001651 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1652 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1653 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1654 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1655 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1656 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1657 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1658 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001659 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1660 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1661 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1662 case EOpAsinh:
1663 ASSERT(node->getUseEmulatedFunction());
1664 writeEmulatedFunctionTriplet(visit, "asinh(");
1665 break;
1666 case EOpAcosh:
1667 ASSERT(node->getUseEmulatedFunction());
1668 writeEmulatedFunctionTriplet(visit, "acosh(");
1669 break;
1670 case EOpAtanh:
1671 ASSERT(node->getUseEmulatedFunction());
1672 writeEmulatedFunctionTriplet(visit, "atanh(");
1673 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001674 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1675 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1676 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1677 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1678 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1679 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1680 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1681 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1682 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1683 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1684 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301685 case EOpIsNan: outputTriplet(visit, "isnan(", "", ")"); break;
1686 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001687 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1688 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1689 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1690 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001691 case EOpPackSnorm2x16:
1692 ASSERT(node->getUseEmulatedFunction());
1693 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1694 break;
1695 case EOpPackUnorm2x16:
1696 ASSERT(node->getUseEmulatedFunction());
1697 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1698 break;
1699 case EOpPackHalf2x16:
1700 ASSERT(node->getUseEmulatedFunction());
1701 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1702 break;
1703 case EOpUnpackSnorm2x16:
1704 ASSERT(node->getUseEmulatedFunction());
1705 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1706 break;
1707 case EOpUnpackUnorm2x16:
1708 ASSERT(node->getUseEmulatedFunction());
1709 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1710 break;
1711 case EOpUnpackHalf2x16:
1712 ASSERT(node->getUseEmulatedFunction());
1713 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1714 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001715 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1716 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001717 case EOpDFdx:
1718 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1719 {
1720 outputTriplet(visit, "(", "", ", 0.0)");
1721 }
1722 else
1723 {
1724 outputTriplet(visit, "ddx(", "", ")");
1725 }
1726 break;
1727 case EOpDFdy:
1728 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1729 {
1730 outputTriplet(visit, "(", "", ", 0.0)");
1731 }
1732 else
1733 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001734 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001735 }
1736 break;
1737 case EOpFwidth:
1738 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1739 {
1740 outputTriplet(visit, "(", "", ", 0.0)");
1741 }
1742 else
1743 {
1744 outputTriplet(visit, "fwidth(", "", ")");
1745 }
1746 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001747 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1748 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001749 case EOpInverse:
1750 ASSERT(node->getUseEmulatedFunction());
1751 writeEmulatedFunctionTriplet(visit, "inverse(");
1752 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001753
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001754 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1755 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 default: UNREACHABLE();
1757 }
1758
1759 return true;
1760}
1761
1762bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1763{
Jamie Madill32aab012015-01-27 14:12:26 -05001764 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001765
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 switch (node->getOp())
1767 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001768 case EOpSequence:
1769 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001770 if (mInsideFunction)
1771 {
Jamie Madill075edd82013-07-08 13:30:19 -04001772 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001773 out << "{\n";
1774 }
1775
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001776 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001777 {
Jamie Madill075edd82013-07-08 13:30:19 -04001778 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001779
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001780 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001781
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001782 // Don't output ; after case labels, they're terminated by :
1783 // This is needed especially since outputting a ; after a case statement would turn empty
1784 // case statements into non-empty case statements, disallowing fall-through from them.
1785 if ((*sit)->getAsCaseNode() == nullptr)
1786 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001787 }
1788
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001789 if (mInsideFunction)
1790 {
Jamie Madill075edd82013-07-08 13:30:19 -04001791 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001792 out << "}\n";
1793 }
1794
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001795 return false;
1796 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001797 case EOpDeclaration:
1798 if (visit == PreVisit)
1799 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001800 TIntermSequence *sequence = node->getSequence();
1801 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001802
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001803 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001805 TStructure *structure = variable->getType().getStruct();
1806
1807 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001808 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001809 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001810 }
1811
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001812 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813 {
Jamie Madill37997142015-01-28 10:06:34 -05001814 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001815 {
Jamie Madill37997142015-01-28 10:06:34 -05001816 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001817 {
Jamie Madill37997142015-01-28 10:06:34 -05001818 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001819 }
1820
Nicolas Capensd974db42014-10-07 10:50:19 -04001821 if (!mInsideFunction)
1822 {
1823 out << "static ";
1824 }
1825
1826 out << TypeString(variable->getType()) + " ";
1827
Jamie Madill37997142015-01-28 10:06:34 -05001828 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001830 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001832 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001833 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001834 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001835 }
1836 else
1837 {
Jamie Madill37997142015-01-28 10:06:34 -05001838 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001839 }
1840
Jamie Madill37997142015-01-28 10:06:34 -05001841 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001842 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001843 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001844 }
1845 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001847 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1848 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001849 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001850 }
1851 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001852 }
Jamie Madill033dae62014-06-18 12:56:28 -04001853 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001854 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001855 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001856 {
1857 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1858
1859 if (symbol)
1860 {
1861 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1862 mReferencedVaryings[symbol->getSymbol()] = symbol;
1863 }
1864 else
1865 {
1866 (*sit)->traverse(this);
1867 }
1868 }
1869 }
1870
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871 return false;
1872 }
1873 else if (visit == InVisit)
1874 {
1875 out << ", ";
1876 }
1877 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001878 case EOpInvariantDeclaration:
1879 // Do not do any translation
1880 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001881 case EOpPrototype:
1882 if (visit == PreVisit)
1883 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001884 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001885
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001886 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001887
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001888 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001889 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001890 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001891
1892 if (symbol)
1893 {
1894 out << argumentString(symbol);
1895
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001896 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001897 {
1898 out << ", ";
1899 }
1900 }
1901 else UNREACHABLE();
1902 }
1903
1904 out << ");\n";
1905
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001906 // Also prototype the Lod0 variant if needed
1907 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1908 {
1909 mOutputLod0Function = true;
1910 node->traverse(this);
1911 mOutputLod0Function = false;
1912 }
1913
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001914 return false;
1915 }
1916 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001917 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 case EOpFunction:
1919 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001920 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921
Jamie Madill033dae62014-06-18 12:56:28 -04001922 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001923
1924 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001926 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001928 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001929 {
Jamie Madill033dae62014-06-18 12:56:28 -04001930 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001931 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001932
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001933 TIntermSequence *sequence = node->getSequence();
1934 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001935
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001936 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001937 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001938 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001939
1940 if (symbol)
1941 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001942 TStructure *structure = symbol->getType().getStruct();
1943
1944 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001945 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001946 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001947 }
1948
1949 out << argumentString(symbol);
1950
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001951 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001952 {
1953 out << ", ";
1954 }
1955 }
1956 else UNREACHABLE();
1957 }
1958
1959 out << ")\n"
1960 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001961
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001962 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001963 {
1964 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001965 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001966 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001968
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001969 out << "}\n";
1970
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001971 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1972 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001973 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001974 {
1975 mOutputLod0Function = true;
1976 node->traverse(this);
1977 mOutputLod0Function = false;
1978 }
1979 }
1980
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001981 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982 }
1983 break;
1984 case EOpFunctionCall:
1985 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001986 TString name = TFunction::unmangleName(node->getName());
1987 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001988 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001989
1990 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991 {
Jamie Madill033dae62014-06-18 12:56:28 -04001992 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 }
1994 else
1995 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001996 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001997
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001998 TextureFunction textureFunction;
1999 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002000 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002001 textureFunction.method = TextureFunction::IMPLICIT;
2002 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002003 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002004
2005 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002006 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002007 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002008 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002009 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002010 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002011 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002012 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002013 }
Nicolas Capens46485082014-04-15 13:12:50 -04002014 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2015 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002016 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002017 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002018 }
Nicolas Capens46485082014-04-15 13:12:50 -04002019 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002020 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002021 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002022 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002023 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002024 else if (name == "textureSize")
2025 {
2026 textureFunction.method = TextureFunction::SIZE;
2027 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002028 else if (name == "textureOffset")
2029 {
2030 textureFunction.method = TextureFunction::IMPLICIT;
2031 textureFunction.offset = true;
2032 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002033 else if (name == "textureProjOffset")
2034 {
2035 textureFunction.method = TextureFunction::IMPLICIT;
2036 textureFunction.offset = true;
2037 textureFunction.proj = true;
2038 }
2039 else if (name == "textureLodOffset")
2040 {
2041 textureFunction.method = TextureFunction::LOD;
2042 textureFunction.offset = true;
2043 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002044 else if (name == "textureProjLodOffset")
2045 {
2046 textureFunction.method = TextureFunction::LOD;
2047 textureFunction.proj = true;
2048 textureFunction.offset = true;
2049 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002050 else if (name == "texelFetch")
2051 {
2052 textureFunction.method = TextureFunction::FETCH;
2053 }
2054 else if (name == "texelFetchOffset")
2055 {
2056 textureFunction.method = TextureFunction::FETCH;
2057 textureFunction.offset = true;
2058 }
Nicolas Capens46485082014-04-15 13:12:50 -04002059 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002060 {
2061 textureFunction.method = TextureFunction::GRAD;
2062 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002063 else if (name == "textureGradOffset")
2064 {
2065 textureFunction.method = TextureFunction::GRAD;
2066 textureFunction.offset = true;
2067 }
Nicolas Capens46485082014-04-15 13:12:50 -04002068 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002069 {
2070 textureFunction.method = TextureFunction::GRAD;
2071 textureFunction.proj = true;
2072 }
2073 else if (name == "textureProjGradOffset")
2074 {
2075 textureFunction.method = TextureFunction::GRAD;
2076 textureFunction.proj = true;
2077 textureFunction.offset = true;
2078 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002079 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002080
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002081 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002082 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002083 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2084
2085 if (textureFunction.offset)
2086 {
2087 mandatoryArgumentCount++;
2088 }
2089
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002090 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002091
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002092 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002093 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002094 if (bias)
2095 {
2096 textureFunction.method = TextureFunction::LOD0BIAS;
2097 }
2098 else
2099 {
2100 textureFunction.method = TextureFunction::LOD0;
2101 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002102 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002103 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002104 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002105 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002106 }
2107 }
2108
2109 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002110
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002111 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002113
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002114 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002115 {
2116 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2117 {
2118 out << "texture_";
2119 (*arg)->traverse(this);
2120 out << ", sampler_";
2121 }
2122
2123 (*arg)->traverse(this);
2124
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002125 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002126 {
2127 out << ", ";
2128 }
2129 }
2130
2131 out << ")";
2132
2133 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134 }
2135 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002136 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002137 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2138 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2139 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2140 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2141 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2142 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2143 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2144 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2145 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2146 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2147 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2148 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2149 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2150 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2151 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2152 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2153 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2154 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2155 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002156 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002157 {
Jamie Madill033dae62014-06-18 12:56:28 -04002158 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002159 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002160 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002161 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002162 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002163 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2164 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2165 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2166 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2167 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2168 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002169 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002170 ASSERT(node->getUseEmulatedFunction());
2171 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002172 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002173 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002174 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002176 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002177 ASSERT(node->getUseEmulatedFunction());
2178 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 break;
2180 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2181 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2182 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2183 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2184 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2185 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2186 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2187 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2188 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002189 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002190 ASSERT(node->getUseEmulatedFunction());
2191 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002192 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002193 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2194 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002195 case EOpOuterProduct:
2196 ASSERT(node->getUseEmulatedFunction());
2197 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2198 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002200 default: UNREACHABLE();
2201 }
2202
2203 return true;
2204}
2205
2206bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2207{
Jamie Madill32aab012015-01-27 14:12:26 -05002208 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002210 if (node->usesTernaryOperator())
2211 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002212 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002213 }
2214 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002215 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002216 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002217
Corentin Wallez80bacde2014-11-10 12:07:37 -08002218 // D3D errors when there is a gradient operation in a loop in an unflattened if
2219 // however flattening all the ifs in branch heavy shaders made D3D error too.
2220 // As a temporary workaround we flatten the ifs only if there is at least a loop
2221 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002222 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002223 {
2224 out << "FLATTEN ";
2225 }
2226
2227 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002228
2229 node->getCondition()->traverse(this);
2230
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002231 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002232
Jamie Madill075edd82013-07-08 13:30:19 -04002233 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002234 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002235
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002236 bool discard = false;
2237
daniel@transgaming.combb885322010-04-15 20:45:24 +00002238 if (node->getTrueBlock())
2239 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002240 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002241
2242 // Detect true discard
2243 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002244 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245
Jamie Madill075edd82013-07-08 13:30:19 -04002246 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002247 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002248
2249 if (node->getFalseBlock())
2250 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002251 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002252
Jamie Madill075edd82013-07-08 13:30:19 -04002253 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002254 out << "{\n";
2255
Jamie Madill075edd82013-07-08 13:30:19 -04002256 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002257 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002258
Jamie Madill075edd82013-07-08 13:30:19 -04002259 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002260 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002261
2262 // Detect false discard
2263 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2264 }
2265
2266 // ANGLE issue 486: Detect problematic conditional discard
2267 if (discard && FindSideEffectRewriting::search(node))
2268 {
2269 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002270 }
2271 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272
2273 return false;
2274}
2275
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002276bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002277{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002278 if (node->getStatementList())
2279 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002280 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002281 outputTriplet(visit, "switch (", ") ", "");
2282 // The curly braces get written when visiting the statementList aggregate
2283 }
2284 else
2285 {
2286 // No statementList, so it won't output curly braces
2287 outputTriplet(visit, "switch (", ") {", "}\n");
2288 }
2289 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002290}
2291
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002292bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002293{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002294 if (node->hasCondition())
2295 {
2296 outputTriplet(visit, "case (", "", "):\n");
2297 return true;
2298 }
2299 else
2300 {
2301 TInfoSinkBase &out = getInfoSink();
2302 out << "default:\n";
2303 return false;
2304 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002305}
2306
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2308{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002309 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310}
2311
2312bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2313{
Nicolas Capens655fe362014-04-11 13:12:34 -04002314 mNestedLoopDepth++;
2315
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002316 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2317
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002318 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002319 {
2320 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2321 }
2322
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002323 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002324 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002325 if (handleExcessiveLoop(node))
2326 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002327 mInsideDiscontinuousLoop = wasDiscontinuous;
2328 mNestedLoopDepth--;
2329
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002330 return false;
2331 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002332 }
2333
Jamie Madill32aab012015-01-27 14:12:26 -05002334 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335
alokp@chromium.org52813552010-11-16 18:36:09 +00002336 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002338 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002339
Jamie Madill075edd82013-07-08 13:30:19 -04002340 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002341 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002342 }
2343 else
2344 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002345 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002346
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002347 if (node->getInit())
2348 {
2349 node->getInit()->traverse(this);
2350 }
2351
2352 out << "; ";
2353
alokp@chromium.org52813552010-11-16 18:36:09 +00002354 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002356 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357 }
2358
2359 out << "; ";
2360
alokp@chromium.org52813552010-11-16 18:36:09 +00002361 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002363 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002364 }
2365
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002366 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002367
Jamie Madill075edd82013-07-08 13:30:19 -04002368 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002369 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 }
2371
2372 if (node->getBody())
2373 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002374 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 }
2376
Jamie Madill075edd82013-07-08 13:30:19 -04002377 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002378 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379
alokp@chromium.org52813552010-11-16 18:36:09 +00002380 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 {
Jamie Madill075edd82013-07-08 13:30:19 -04002382 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383 out << "while(\n";
2384
alokp@chromium.org52813552010-11-16 18:36:09 +00002385 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386
daniel@transgaming.com73536982012-03-21 20:45:49 +00002387 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388 }
2389
daniel@transgaming.com73536982012-03-21 20:45:49 +00002390 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002392 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002393 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002394
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395 return false;
2396}
2397
2398bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2399{
Jamie Madill32aab012015-01-27 14:12:26 -05002400 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002401
2402 switch (node->getFlowOp())
2403 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002404 case EOpKill:
2405 outputTriplet(visit, "discard;\n", "", "");
2406 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002407 case EOpBreak:
2408 if (visit == PreVisit)
2409 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002410 if (mNestedLoopDepth > 1)
2411 {
2412 mUsesNestedBreak = true;
2413 }
2414
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002415 if (mExcessiveLoopIndex)
2416 {
2417 out << "{Break";
2418 mExcessiveLoopIndex->traverse(this);
2419 out << " = true; break;}\n";
2420 }
2421 else
2422 {
2423 out << "break;\n";
2424 }
2425 }
2426 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002427 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 case EOpReturn:
2429 if (visit == PreVisit)
2430 {
2431 if (node->getExpression())
2432 {
2433 out << "return ";
2434 }
2435 else
2436 {
2437 out << "return;\n";
2438 }
2439 }
2440 else if (visit == PostVisit)
2441 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002442 if (node->getExpression())
2443 {
2444 out << ";\n";
2445 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446 }
2447 break;
2448 default: UNREACHABLE();
2449 }
2450
2451 return true;
2452}
2453
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002454void OutputHLSL::traverseStatements(TIntermNode *node)
2455{
2456 if (isSingleStatement(node))
2457 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002458 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002459 }
2460
2461 node->traverse(this);
2462}
2463
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002464bool OutputHLSL::isSingleStatement(TIntermNode *node)
2465{
2466 TIntermAggregate *aggregate = node->getAsAggregate();
2467
2468 if (aggregate)
2469 {
2470 if (aggregate->getOp() == EOpSequence)
2471 {
2472 return false;
2473 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002474 else if (aggregate->getOp() == EOpDeclaration)
2475 {
2476 // Declaring multiple comma-separated variables must be considered multiple statements
2477 // because each individual declaration has side effects which are visible in the next.
2478 return false;
2479 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002480 else
2481 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002482 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002483 {
2484 if (!isSingleStatement(*sit))
2485 {
2486 return false;
2487 }
2488 }
2489
2490 return true;
2491 }
2492 }
2493
2494 return true;
2495}
2496
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002497// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2498// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002499bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2500{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002501 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002502 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002503
2504 // Parse loops of the form:
2505 // for(int index = initial; index [comparator] limit; index += increment)
2506 TIntermSymbol *index = NULL;
2507 TOperator comparator = EOpNull;
2508 int initial = 0;
2509 int limit = 0;
2510 int increment = 0;
2511
2512 // Parse index name and intial value
2513 if (node->getInit())
2514 {
2515 TIntermAggregate *init = node->getInit()->getAsAggregate();
2516
2517 if (init)
2518 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002519 TIntermSequence *sequence = init->getSequence();
2520 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002521
2522 if (variable && variable->getQualifier() == EvqTemporary)
2523 {
2524 TIntermBinary *assign = variable->getAsBinaryNode();
2525
2526 if (assign->getOp() == EOpInitialize)
2527 {
2528 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2529 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2530
2531 if (symbol && constant)
2532 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002533 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002534 {
2535 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002536 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002537 }
2538 }
2539 }
2540 }
2541 }
2542 }
2543
2544 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002545 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002546 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002547 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002548
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002549 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2550 {
2551 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2552
2553 if (constant)
2554 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002555 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002556 {
2557 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002558 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002559 }
2560 }
2561 }
2562 }
2563
2564 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002565 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002566 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002567 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2568 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002569
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002570 if (binaryTerminal)
2571 {
2572 TOperator op = binaryTerminal->getOp();
2573 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2574
2575 if (constant)
2576 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002577 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002578 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002579 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002580
2581 switch (op)
2582 {
2583 case EOpAddAssign: increment = value; break;
2584 case EOpSubAssign: increment = -value; break;
2585 default: UNIMPLEMENTED();
2586 }
2587 }
2588 }
2589 }
2590 else if (unaryTerminal)
2591 {
2592 TOperator op = unaryTerminal->getOp();
2593
2594 switch (op)
2595 {
2596 case EOpPostIncrement: increment = 1; break;
2597 case EOpPostDecrement: increment = -1; break;
2598 case EOpPreIncrement: increment = 1; break;
2599 case EOpPreDecrement: increment = -1; break;
2600 default: UNIMPLEMENTED();
2601 }
2602 }
2603 }
2604
2605 if (index != NULL && comparator != EOpNull && increment != 0)
2606 {
2607 if (comparator == EOpLessThanEqual)
2608 {
2609 comparator = EOpLessThan;
2610 limit += 1;
2611 }
2612
2613 if (comparator == EOpLessThan)
2614 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002615 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002616
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002617 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002618 {
2619 return false; // Not an excessive loop
2620 }
2621
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002622 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2623 mExcessiveLoopIndex = index;
2624
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002625 out << "{int ";
2626 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002627 out << ";\n"
2628 "bool Break";
2629 index->traverse(this);
2630 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002631
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002632 bool firstLoopFragment = true;
2633
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002634 while (iterations > 0)
2635 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002636 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002637
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002638 if (!firstLoopFragment)
2639 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002640 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002641 index->traverse(this);
2642 out << ") {\n";
2643 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002644
2645 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2646 {
2647 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2648 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002649
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002650 // for(int index = initial; index < clampedLimit; index += increment)
2651
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002652 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002653 index->traverse(this);
2654 out << " = ";
2655 out << initial;
2656
2657 out << "; ";
2658 index->traverse(this);
2659 out << " < ";
2660 out << clampedLimit;
2661
2662 out << "; ";
2663 index->traverse(this);
2664 out << " += ";
2665 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002666 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002667
Jamie Madill075edd82013-07-08 13:30:19 -04002668 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002669 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002670
2671 if (node->getBody())
2672 {
2673 node->getBody()->traverse(this);
2674 }
2675
Jamie Madill075edd82013-07-08 13:30:19 -04002676 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002677 out << ";}\n";
2678
2679 if (!firstLoopFragment)
2680 {
2681 out << "}\n";
2682 }
2683
2684 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002685
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002686 initial += MAX_LOOP_ITERATIONS * increment;
2687 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002688 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002689
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002690 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002691
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002692 mExcessiveLoopIndex = restoreIndex;
2693
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002694 return true;
2695 }
2696 else UNIMPLEMENTED();
2697 }
2698
2699 return false; // Not handled as an excessive loop
2700}
2701
Daniel Bratell29190082015-02-20 16:42:54 +01002702void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703{
Jamie Madill32aab012015-01-27 14:12:26 -05002704 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002706 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707 {
2708 out << preString;
2709 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002710 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711 {
2712 out << inString;
2713 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002714 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715 {
2716 out << postString;
2717 }
2718}
2719
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002720void OutputHLSL::outputLineDirective(int line)
2721{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002722 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002723 {
Jamie Madill32aab012015-01-27 14:12:26 -05002724 TInfoSinkBase &out = getInfoSink();
2725
2726 out << "\n";
2727 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002728
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002729 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002730 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002731 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002732 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002733
Jamie Madill32aab012015-01-27 14:12:26 -05002734 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002735 }
2736}
2737
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002738TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2739{
2740 TQualifier qualifier = symbol->getQualifier();
2741 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002742 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002743
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002744 if (name.empty()) // HLSL demands named arguments, also for prototypes
2745 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002746 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002747 }
2748 else
2749 {
Jamie Madill033dae62014-06-18 12:56:28 -04002750 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002751 }
2752
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002753 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2754 {
Jamie Madill033dae62014-06-18 12:56:28 -04002755 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002756 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002757 }
2758
Jamie Madill033dae62014-06-18 12:56:28 -04002759 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760}
2761
2762TString OutputHLSL::initializer(const TType &type)
2763{
2764 TString string;
2765
Jamie Madill94bf7f22013-07-08 13:31:15 -04002766 size_t size = type.getObjectSize();
2767 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002769 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002770
Jamie Madill94bf7f22013-07-08 13:31:15 -04002771 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002772 {
2773 string += ", ";
2774 }
2775 }
2776
daniel@transgaming.comead23042010-04-29 03:35:36 +00002777 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002779
Daniel Bratell29190082015-02-20 16:42:54 +01002780void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002781{
Jamie Madill32aab012015-01-27 14:12:26 -05002782 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002783
2784 if (visit == PreVisit)
2785 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002786 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002787
Daniel Bratell29190082015-02-20 16:42:54 +01002788 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002789 }
2790 else if (visit == InVisit)
2791 {
2792 out << ", ";
2793 }
2794 else if (visit == PostVisit)
2795 {
2796 out << ")";
2797 }
2798}
2799
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002800const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2801{
Jamie Madill32aab012015-01-27 14:12:26 -05002802 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002803
Jamie Madill98493dd2013-07-08 14:39:03 -04002804 const TStructure* structure = type.getStruct();
2805 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002806 {
Jamie Madill033dae62014-06-18 12:56:28 -04002807 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002808
Jamie Madill98493dd2013-07-08 14:39:03 -04002809 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002810
Jamie Madill98493dd2013-07-08 14:39:03 -04002811 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002812 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002813 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002814 constUnion = writeConstantUnion(*fieldType, constUnion);
2815
Jamie Madill98493dd2013-07-08 14:39:03 -04002816 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002817 {
2818 out << ", ";
2819 }
2820 }
2821
2822 out << ")";
2823 }
2824 else
2825 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002826 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002827 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002828
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002829 if (writeType)
2830 {
Jamie Madill033dae62014-06-18 12:56:28 -04002831 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002832 }
2833
Jamie Madill94bf7f22013-07-08 13:31:15 -04002834 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002835 {
2836 switch (constUnion->getType())
2837 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002838 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002839 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002840 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002841 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002842 default: UNREACHABLE();
2843 }
2844
2845 if (i != size - 1)
2846 {
2847 out << ", ";
2848 }
2849 }
2850
2851 if (writeType)
2852 {
2853 out << ")";
2854 }
2855 }
2856
2857 return constUnion;
2858}
2859
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002860void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2861{
2862 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2863 outputTriplet(visit, preString.c_str(), ", ", ")");
2864}
2865
Jamie Madill37997142015-01-28 10:06:34 -05002866bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2867{
2868 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2869 expression->traverse(&searchSymbol);
2870
2871 if (searchSymbol.foundMatch())
2872 {
2873 // Type already printed
2874 out << "t" + str(mUniqueIndex) + " = ";
2875 expression->traverse(this);
2876 out << ", ";
2877 symbolNode->traverse(this);
2878 out << " = t" + str(mUniqueIndex);
2879
2880 mUniqueIndex++;
2881 return true;
2882 }
2883
2884 return false;
2885}
2886
2887void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2888{
2889 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2890 << "\n"
2891 << "void initializeDeferredGlobals()\n"
2892 << "{\n";
2893
2894 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2895 {
2896 TIntermSymbol *symbol = deferredGlobal.first;
2897 TIntermTyped *expression = deferredGlobal.second;
2898 ASSERT(symbol);
2899 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2900
2901 out << " " << Decorate(symbol->getSymbol()) << " = ";
2902
2903 if (!writeSameSymbolInitializer(out, symbol, expression))
2904 {
2905 ASSERT(mInfoSinkStack.top() == &out);
2906 expression->traverse(this);
2907 }
2908
2909 out << ";\n";
2910 }
2911
2912 out << "}\n"
2913 << "\n";
2914}
2915
Jamie Madill55e79e02015-02-09 15:35:00 -05002916TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2917{
2918 const TFieldList &fields = structure.fields();
2919
2920 for (const auto &eqFunction : mStructEqualityFunctions)
2921 {
2922 if (eqFunction.structure == &structure)
2923 {
2924 return eqFunction.functionName;
2925 }
2926 }
2927
2928 const TString &structNameString = StructNameString(structure);
2929
2930 StructEqualityFunction function;
2931 function.structure = &structure;
2932 function.functionName = "angle_eq_" + structNameString;
2933
2934 TString &func = function.functionDefinition;
2935
2936 func = "bool " + function.functionName + "(" + structNameString + " a, " + structNameString + " b)\n" +
2937 "{\n"
2938 " return ";
2939
2940 for (size_t i = 0; i < fields.size(); i++)
2941 {
2942 const TField *field = fields[i];
2943 const TType *fieldType = field->type();
2944
2945 const TString &fieldNameA = "a." + Decorate(field->name());
2946 const TString &fieldNameB = "b." + Decorate(field->name());
2947
2948 if (i > 0)
2949 {
2950 func += " && ";
2951 }
2952
2953 if (fieldType->getBasicType() == EbtStruct)
2954 {
2955 const TStructure &fieldStruct = *fieldType->getStruct();
2956 const TString &functionName = addStructEqualityFunction(fieldStruct);
2957 func += functionName + "(" + fieldNameA + ", " + fieldNameB + ")";
2958 }
2959 else if (fieldType->isScalar())
2960 {
2961 func += "(" + fieldNameA + " == " + fieldNameB + ")";
2962 }
2963 else
2964 {
2965 ASSERT(fieldType->isMatrix() || fieldType->isVector());
2966 func += "all(" + fieldNameA + " == " + fieldNameB + ")";
2967 }
2968 }
2969
2970 func = func + ";\n" + "}\n";
2971
2972 mStructEqualityFunctions.push_back(function);
2973
2974 return function.functionName;
2975}
2976
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002977}