blob: faa760fe5975c571b72e12afc375a4a0c7ee829f [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;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001683 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1684 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1685 case EOpRoundEven:
1686 ASSERT(node->getUseEmulatedFunction());
1687 writeEmulatedFunctionTriplet(visit, "roundEven(");
1688 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001689 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1690 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301691 case EOpIsNan: outputTriplet(visit, "isnan(", "", ")"); break;
1692 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001693 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1694 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1695 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1696 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001697 case EOpPackSnorm2x16:
1698 ASSERT(node->getUseEmulatedFunction());
1699 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1700 break;
1701 case EOpPackUnorm2x16:
1702 ASSERT(node->getUseEmulatedFunction());
1703 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1704 break;
1705 case EOpPackHalf2x16:
1706 ASSERT(node->getUseEmulatedFunction());
1707 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1708 break;
1709 case EOpUnpackSnorm2x16:
1710 ASSERT(node->getUseEmulatedFunction());
1711 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1712 break;
1713 case EOpUnpackUnorm2x16:
1714 ASSERT(node->getUseEmulatedFunction());
1715 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1716 break;
1717 case EOpUnpackHalf2x16:
1718 ASSERT(node->getUseEmulatedFunction());
1719 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1720 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001721 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1722 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001723 case EOpDFdx:
1724 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1725 {
1726 outputTriplet(visit, "(", "", ", 0.0)");
1727 }
1728 else
1729 {
1730 outputTriplet(visit, "ddx(", "", ")");
1731 }
1732 break;
1733 case EOpDFdy:
1734 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1735 {
1736 outputTriplet(visit, "(", "", ", 0.0)");
1737 }
1738 else
1739 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001740 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001741 }
1742 break;
1743 case EOpFwidth:
1744 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1745 {
1746 outputTriplet(visit, "(", "", ", 0.0)");
1747 }
1748 else
1749 {
1750 outputTriplet(visit, "fwidth(", "", ")");
1751 }
1752 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001753 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1754 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001755 case EOpInverse:
1756 ASSERT(node->getUseEmulatedFunction());
1757 writeEmulatedFunctionTriplet(visit, "inverse(");
1758 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001759
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001760 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1761 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762 default: UNREACHABLE();
1763 }
1764
1765 return true;
1766}
1767
1768bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1769{
Jamie Madill32aab012015-01-27 14:12:26 -05001770 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001772 switch (node->getOp())
1773 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001774 case EOpSequence:
1775 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001776 if (mInsideFunction)
1777 {
Jamie Madill075edd82013-07-08 13:30:19 -04001778 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001779 out << "{\n";
1780 }
1781
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001782 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001783 {
Jamie Madill075edd82013-07-08 13:30:19 -04001784 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001785
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001786 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001787
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001788 // Don't output ; after case labels, they're terminated by :
1789 // This is needed especially since outputting a ; after a case statement would turn empty
1790 // case statements into non-empty case statements, disallowing fall-through from them.
1791 if ((*sit)->getAsCaseNode() == nullptr)
1792 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001793 }
1794
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001795 if (mInsideFunction)
1796 {
Jamie Madill075edd82013-07-08 13:30:19 -04001797 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001798 out << "}\n";
1799 }
1800
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001801 return false;
1802 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803 case EOpDeclaration:
1804 if (visit == PreVisit)
1805 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001806 TIntermSequence *sequence = node->getSequence();
1807 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001809 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001811 TStructure *structure = variable->getType().getStruct();
1812
1813 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001814 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001815 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001816 }
1817
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001818 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819 {
Jamie Madill37997142015-01-28 10:06:34 -05001820 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001821 {
Jamie Madill37997142015-01-28 10:06:34 -05001822 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001823 {
Jamie Madill37997142015-01-28 10:06:34 -05001824 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001825 }
1826
Nicolas Capensd974db42014-10-07 10:50:19 -04001827 if (!mInsideFunction)
1828 {
1829 out << "static ";
1830 }
1831
1832 out << TypeString(variable->getType()) + " ";
1833
Jamie Madill37997142015-01-28 10:06:34 -05001834 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001835
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001836 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001838 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001839 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001840 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001841 }
1842 else
1843 {
Jamie Madill37997142015-01-28 10:06:34 -05001844 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001845 }
1846
Jamie Madill37997142015-01-28 10:06:34 -05001847 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001848 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001849 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850 }
1851 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001852 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001853 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1854 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001855 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001856 }
1857 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001858 }
Jamie Madill033dae62014-06-18 12:56:28 -04001859 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001860 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001861 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001862 {
1863 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1864
1865 if (symbol)
1866 {
1867 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1868 mReferencedVaryings[symbol->getSymbol()] = symbol;
1869 }
1870 else
1871 {
1872 (*sit)->traverse(this);
1873 }
1874 }
1875 }
1876
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877 return false;
1878 }
1879 else if (visit == InVisit)
1880 {
1881 out << ", ";
1882 }
1883 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001884 case EOpInvariantDeclaration:
1885 // Do not do any translation
1886 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001887 case EOpPrototype:
1888 if (visit == PreVisit)
1889 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001890 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001891
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001892 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001893
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001894 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001895 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001896 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001897
1898 if (symbol)
1899 {
1900 out << argumentString(symbol);
1901
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001902 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001903 {
1904 out << ", ";
1905 }
1906 }
1907 else UNREACHABLE();
1908 }
1909
1910 out << ");\n";
1911
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001912 // Also prototype the Lod0 variant if needed
1913 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1914 {
1915 mOutputLod0Function = true;
1916 node->traverse(this);
1917 mOutputLod0Function = false;
1918 }
1919
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001920 return false;
1921 }
1922 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001923 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924 case EOpFunction:
1925 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001926 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927
Jamie Madill033dae62014-06-18 12:56:28 -04001928 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001929
1930 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001931 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001932 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001934 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935 {
Jamie Madill033dae62014-06-18 12:56:28 -04001936 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001937 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001938
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001939 TIntermSequence *sequence = node->getSequence();
1940 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001941
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001942 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001943 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001944 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001945
1946 if (symbol)
1947 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001948 TStructure *structure = symbol->getType().getStruct();
1949
1950 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001951 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001952 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001953 }
1954
1955 out << argumentString(symbol);
1956
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001957 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001958 {
1959 out << ", ";
1960 }
1961 }
1962 else UNREACHABLE();
1963 }
1964
1965 out << ")\n"
1966 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001967
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001968 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001969 {
1970 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001971 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001972 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001974
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001975 out << "}\n";
1976
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001977 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1978 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001979 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001980 {
1981 mOutputLod0Function = true;
1982 node->traverse(this);
1983 mOutputLod0Function = false;
1984 }
1985 }
1986
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001987 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001988 }
1989 break;
1990 case EOpFunctionCall:
1991 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001992 TString name = TFunction::unmangleName(node->getName());
1993 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001994 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001995
1996 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997 {
Jamie Madill033dae62014-06-18 12:56:28 -04001998 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 }
2000 else
2001 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002002 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002003
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002004 TextureFunction textureFunction;
2005 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002006 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002007 textureFunction.method = TextureFunction::IMPLICIT;
2008 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002009 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002010
2011 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002012 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002013 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002014 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002015 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002016 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002017 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002018 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002019 }
Nicolas Capens46485082014-04-15 13:12:50 -04002020 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2021 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002022 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002023 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002024 }
Nicolas Capens46485082014-04-15 13:12:50 -04002025 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002026 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002027 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002028 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002029 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002030 else if (name == "textureSize")
2031 {
2032 textureFunction.method = TextureFunction::SIZE;
2033 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002034 else if (name == "textureOffset")
2035 {
2036 textureFunction.method = TextureFunction::IMPLICIT;
2037 textureFunction.offset = true;
2038 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002039 else if (name == "textureProjOffset")
2040 {
2041 textureFunction.method = TextureFunction::IMPLICIT;
2042 textureFunction.offset = true;
2043 textureFunction.proj = true;
2044 }
2045 else if (name == "textureLodOffset")
2046 {
2047 textureFunction.method = TextureFunction::LOD;
2048 textureFunction.offset = true;
2049 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002050 else if (name == "textureProjLodOffset")
2051 {
2052 textureFunction.method = TextureFunction::LOD;
2053 textureFunction.proj = true;
2054 textureFunction.offset = true;
2055 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002056 else if (name == "texelFetch")
2057 {
2058 textureFunction.method = TextureFunction::FETCH;
2059 }
2060 else if (name == "texelFetchOffset")
2061 {
2062 textureFunction.method = TextureFunction::FETCH;
2063 textureFunction.offset = true;
2064 }
Nicolas Capens46485082014-04-15 13:12:50 -04002065 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002066 {
2067 textureFunction.method = TextureFunction::GRAD;
2068 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002069 else if (name == "textureGradOffset")
2070 {
2071 textureFunction.method = TextureFunction::GRAD;
2072 textureFunction.offset = true;
2073 }
Nicolas Capens46485082014-04-15 13:12:50 -04002074 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002075 {
2076 textureFunction.method = TextureFunction::GRAD;
2077 textureFunction.proj = true;
2078 }
2079 else if (name == "textureProjGradOffset")
2080 {
2081 textureFunction.method = TextureFunction::GRAD;
2082 textureFunction.proj = true;
2083 textureFunction.offset = true;
2084 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002085 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002086
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002087 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002088 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002089 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2090
2091 if (textureFunction.offset)
2092 {
2093 mandatoryArgumentCount++;
2094 }
2095
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002096 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002097
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002098 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002099 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002100 if (bias)
2101 {
2102 textureFunction.method = TextureFunction::LOD0BIAS;
2103 }
2104 else
2105 {
2106 textureFunction.method = TextureFunction::LOD0;
2107 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002108 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002109 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002110 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002111 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002112 }
2113 }
2114
2115 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002116
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002117 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002118 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002119
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002120 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002121 {
2122 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2123 {
2124 out << "texture_";
2125 (*arg)->traverse(this);
2126 out << ", sampler_";
2127 }
2128
2129 (*arg)->traverse(this);
2130
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002131 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002132 {
2133 out << ", ";
2134 }
2135 }
2136
2137 out << ")";
2138
2139 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002140 }
2141 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002142 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002143 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2144 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2145 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2146 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2147 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2148 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2149 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2150 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2151 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2152 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2153 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2154 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2155 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2156 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2157 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2158 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2159 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2160 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2161 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002162 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002163 {
Jamie Madill033dae62014-06-18 12:56:28 -04002164 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002165 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002166 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002167 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002168 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002169 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2170 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2171 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2172 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2173 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2174 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002175 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002176 ASSERT(node->getUseEmulatedFunction());
2177 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002178 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002179 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002180 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002182 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002183 ASSERT(node->getUseEmulatedFunction());
2184 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 break;
2186 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2187 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2188 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2189 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2190 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2191 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2192 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2193 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2194 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002195 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002196 ASSERT(node->getUseEmulatedFunction());
2197 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002198 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2200 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002201 case EOpOuterProduct:
2202 ASSERT(node->getUseEmulatedFunction());
2203 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2204 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206 default: UNREACHABLE();
2207 }
2208
2209 return true;
2210}
2211
2212bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2213{
Jamie Madill32aab012015-01-27 14:12:26 -05002214 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002215
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002216 if (node->usesTernaryOperator())
2217 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002218 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002219 }
2220 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002221 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002222 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002223
Corentin Wallez80bacde2014-11-10 12:07:37 -08002224 // D3D errors when there is a gradient operation in a loop in an unflattened if
2225 // however flattening all the ifs in branch heavy shaders made D3D error too.
2226 // As a temporary workaround we flatten the ifs only if there is at least a loop
2227 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002228 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002229 {
2230 out << "FLATTEN ";
2231 }
2232
2233 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002234
2235 node->getCondition()->traverse(this);
2236
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002237 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002238
Jamie Madill075edd82013-07-08 13:30:19 -04002239 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002240 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002241
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002242 bool discard = false;
2243
daniel@transgaming.combb885322010-04-15 20:45:24 +00002244 if (node->getTrueBlock())
2245 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002246 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002247
2248 // Detect true discard
2249 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002250 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251
Jamie Madill075edd82013-07-08 13:30:19 -04002252 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002253 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002254
2255 if (node->getFalseBlock())
2256 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002257 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002258
Jamie Madill075edd82013-07-08 13:30:19 -04002259 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002260 out << "{\n";
2261
Jamie Madill075edd82013-07-08 13:30:19 -04002262 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002263 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002264
Jamie Madill075edd82013-07-08 13:30:19 -04002265 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002266 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002267
2268 // Detect false discard
2269 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2270 }
2271
2272 // ANGLE issue 486: Detect problematic conditional discard
2273 if (discard && FindSideEffectRewriting::search(node))
2274 {
2275 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002276 }
2277 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278
2279 return false;
2280}
2281
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002282bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002283{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002284 if (node->getStatementList())
2285 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002286 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002287 outputTriplet(visit, "switch (", ") ", "");
2288 // The curly braces get written when visiting the statementList aggregate
2289 }
2290 else
2291 {
2292 // No statementList, so it won't output curly braces
2293 outputTriplet(visit, "switch (", ") {", "}\n");
2294 }
2295 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002296}
2297
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002298bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002299{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002300 if (node->hasCondition())
2301 {
2302 outputTriplet(visit, "case (", "", "):\n");
2303 return true;
2304 }
2305 else
2306 {
2307 TInfoSinkBase &out = getInfoSink();
2308 out << "default:\n";
2309 return false;
2310 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002311}
2312
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2314{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002315 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316}
2317
2318bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2319{
Nicolas Capens655fe362014-04-11 13:12:34 -04002320 mNestedLoopDepth++;
2321
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002322 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2323
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002324 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002325 {
2326 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2327 }
2328
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002329 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002330 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002331 if (handleExcessiveLoop(node))
2332 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002333 mInsideDiscontinuousLoop = wasDiscontinuous;
2334 mNestedLoopDepth--;
2335
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002336 return false;
2337 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002338 }
2339
Jamie Madill32aab012015-01-27 14:12:26 -05002340 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341
alokp@chromium.org52813552010-11-16 18:36:09 +00002342 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002343 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002344 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002345
Jamie Madill075edd82013-07-08 13:30:19 -04002346 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002347 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 }
2349 else
2350 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002351 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002352
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353 if (node->getInit())
2354 {
2355 node->getInit()->traverse(this);
2356 }
2357
2358 out << "; ";
2359
alokp@chromium.org52813552010-11-16 18:36:09 +00002360 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002362 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 }
2364
2365 out << "; ";
2366
alokp@chromium.org52813552010-11-16 18:36:09 +00002367 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002369 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 }
2371
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002372 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002373
Jamie Madill075edd82013-07-08 13:30:19 -04002374 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002375 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 }
2377
2378 if (node->getBody())
2379 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002380 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 }
2382
Jamie Madill075edd82013-07-08 13:30:19 -04002383 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002384 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385
alokp@chromium.org52813552010-11-16 18:36:09 +00002386 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 {
Jamie Madill075edd82013-07-08 13:30:19 -04002388 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 out << "while(\n";
2390
alokp@chromium.org52813552010-11-16 18:36:09 +00002391 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392
daniel@transgaming.com73536982012-03-21 20:45:49 +00002393 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394 }
2395
daniel@transgaming.com73536982012-03-21 20:45:49 +00002396 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002397
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002398 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002399 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002400
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002401 return false;
2402}
2403
2404bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2405{
Jamie Madill32aab012015-01-27 14:12:26 -05002406 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407
2408 switch (node->getFlowOp())
2409 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002410 case EOpKill:
2411 outputTriplet(visit, "discard;\n", "", "");
2412 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002413 case EOpBreak:
2414 if (visit == PreVisit)
2415 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002416 if (mNestedLoopDepth > 1)
2417 {
2418 mUsesNestedBreak = true;
2419 }
2420
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002421 if (mExcessiveLoopIndex)
2422 {
2423 out << "{Break";
2424 mExcessiveLoopIndex->traverse(this);
2425 out << " = true; break;}\n";
2426 }
2427 else
2428 {
2429 out << "break;\n";
2430 }
2431 }
2432 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002433 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 case EOpReturn:
2435 if (visit == PreVisit)
2436 {
2437 if (node->getExpression())
2438 {
2439 out << "return ";
2440 }
2441 else
2442 {
2443 out << "return;\n";
2444 }
2445 }
2446 else if (visit == PostVisit)
2447 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002448 if (node->getExpression())
2449 {
2450 out << ";\n";
2451 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 }
2453 break;
2454 default: UNREACHABLE();
2455 }
2456
2457 return true;
2458}
2459
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002460void OutputHLSL::traverseStatements(TIntermNode *node)
2461{
2462 if (isSingleStatement(node))
2463 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002464 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002465 }
2466
2467 node->traverse(this);
2468}
2469
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002470bool OutputHLSL::isSingleStatement(TIntermNode *node)
2471{
2472 TIntermAggregate *aggregate = node->getAsAggregate();
2473
2474 if (aggregate)
2475 {
2476 if (aggregate->getOp() == EOpSequence)
2477 {
2478 return false;
2479 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002480 else if (aggregate->getOp() == EOpDeclaration)
2481 {
2482 // Declaring multiple comma-separated variables must be considered multiple statements
2483 // because each individual declaration has side effects which are visible in the next.
2484 return false;
2485 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002486 else
2487 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002488 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002489 {
2490 if (!isSingleStatement(*sit))
2491 {
2492 return false;
2493 }
2494 }
2495
2496 return true;
2497 }
2498 }
2499
2500 return true;
2501}
2502
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002503// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2504// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002505bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2506{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002507 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002508 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002509
2510 // Parse loops of the form:
2511 // for(int index = initial; index [comparator] limit; index += increment)
2512 TIntermSymbol *index = NULL;
2513 TOperator comparator = EOpNull;
2514 int initial = 0;
2515 int limit = 0;
2516 int increment = 0;
2517
2518 // Parse index name and intial value
2519 if (node->getInit())
2520 {
2521 TIntermAggregate *init = node->getInit()->getAsAggregate();
2522
2523 if (init)
2524 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002525 TIntermSequence *sequence = init->getSequence();
2526 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002527
2528 if (variable && variable->getQualifier() == EvqTemporary)
2529 {
2530 TIntermBinary *assign = variable->getAsBinaryNode();
2531
2532 if (assign->getOp() == EOpInitialize)
2533 {
2534 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2535 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2536
2537 if (symbol && constant)
2538 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002539 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002540 {
2541 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002542 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002543 }
2544 }
2545 }
2546 }
2547 }
2548 }
2549
2550 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002551 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002552 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002553 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002554
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002555 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2556 {
2557 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2558
2559 if (constant)
2560 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002561 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002562 {
2563 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002564 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002565 }
2566 }
2567 }
2568 }
2569
2570 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002571 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002572 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002573 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2574 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002575
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002576 if (binaryTerminal)
2577 {
2578 TOperator op = binaryTerminal->getOp();
2579 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2580
2581 if (constant)
2582 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002583 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002584 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002585 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002586
2587 switch (op)
2588 {
2589 case EOpAddAssign: increment = value; break;
2590 case EOpSubAssign: increment = -value; break;
2591 default: UNIMPLEMENTED();
2592 }
2593 }
2594 }
2595 }
2596 else if (unaryTerminal)
2597 {
2598 TOperator op = unaryTerminal->getOp();
2599
2600 switch (op)
2601 {
2602 case EOpPostIncrement: increment = 1; break;
2603 case EOpPostDecrement: increment = -1; break;
2604 case EOpPreIncrement: increment = 1; break;
2605 case EOpPreDecrement: increment = -1; break;
2606 default: UNIMPLEMENTED();
2607 }
2608 }
2609 }
2610
2611 if (index != NULL && comparator != EOpNull && increment != 0)
2612 {
2613 if (comparator == EOpLessThanEqual)
2614 {
2615 comparator = EOpLessThan;
2616 limit += 1;
2617 }
2618
2619 if (comparator == EOpLessThan)
2620 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002621 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002622
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002623 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002624 {
2625 return false; // Not an excessive loop
2626 }
2627
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002628 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2629 mExcessiveLoopIndex = index;
2630
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002631 out << "{int ";
2632 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002633 out << ";\n"
2634 "bool Break";
2635 index->traverse(this);
2636 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002637
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002638 bool firstLoopFragment = true;
2639
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002640 while (iterations > 0)
2641 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002642 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002643
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002644 if (!firstLoopFragment)
2645 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002646 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002647 index->traverse(this);
2648 out << ") {\n";
2649 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002650
2651 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2652 {
2653 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2654 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002655
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002656 // for(int index = initial; index < clampedLimit; index += increment)
2657
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002658 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002659 index->traverse(this);
2660 out << " = ";
2661 out << initial;
2662
2663 out << "; ";
2664 index->traverse(this);
2665 out << " < ";
2666 out << clampedLimit;
2667
2668 out << "; ";
2669 index->traverse(this);
2670 out << " += ";
2671 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002672 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002673
Jamie Madill075edd82013-07-08 13:30:19 -04002674 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002675 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002676
2677 if (node->getBody())
2678 {
2679 node->getBody()->traverse(this);
2680 }
2681
Jamie Madill075edd82013-07-08 13:30:19 -04002682 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002683 out << ";}\n";
2684
2685 if (!firstLoopFragment)
2686 {
2687 out << "}\n";
2688 }
2689
2690 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002691
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002692 initial += MAX_LOOP_ITERATIONS * increment;
2693 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002694 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002695
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002696 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002697
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002698 mExcessiveLoopIndex = restoreIndex;
2699
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002700 return true;
2701 }
2702 else UNIMPLEMENTED();
2703 }
2704
2705 return false; // Not handled as an excessive loop
2706}
2707
Daniel Bratell29190082015-02-20 16:42:54 +01002708void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002709{
Jamie Madill32aab012015-01-27 14:12:26 -05002710 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002712 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713 {
2714 out << preString;
2715 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002716 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002717 {
2718 out << inString;
2719 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002720 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721 {
2722 out << postString;
2723 }
2724}
2725
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002726void OutputHLSL::outputLineDirective(int line)
2727{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002728 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002729 {
Jamie Madill32aab012015-01-27 14:12:26 -05002730 TInfoSinkBase &out = getInfoSink();
2731
2732 out << "\n";
2733 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002734
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002735 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002736 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002737 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002738 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002739
Jamie Madill32aab012015-01-27 14:12:26 -05002740 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002741 }
2742}
2743
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002744TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2745{
2746 TQualifier qualifier = symbol->getQualifier();
2747 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002748 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002749
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002750 if (name.empty()) // HLSL demands named arguments, also for prototypes
2751 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002752 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002753 }
2754 else
2755 {
Jamie Madill033dae62014-06-18 12:56:28 -04002756 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002757 }
2758
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002759 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2760 {
Jamie Madill033dae62014-06-18 12:56:28 -04002761 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002762 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002763 }
2764
Jamie Madill033dae62014-06-18 12:56:28 -04002765 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766}
2767
2768TString OutputHLSL::initializer(const TType &type)
2769{
2770 TString string;
2771
Jamie Madill94bf7f22013-07-08 13:31:15 -04002772 size_t size = type.getObjectSize();
2773 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002774 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002775 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776
Jamie Madill94bf7f22013-07-08 13:31:15 -04002777 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778 {
2779 string += ", ";
2780 }
2781 }
2782
daniel@transgaming.comead23042010-04-29 03:35:36 +00002783 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002785
Daniel Bratell29190082015-02-20 16:42:54 +01002786void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002787{
Jamie Madill32aab012015-01-27 14:12:26 -05002788 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002789
2790 if (visit == PreVisit)
2791 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002792 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002793
Daniel Bratell29190082015-02-20 16:42:54 +01002794 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002795 }
2796 else if (visit == InVisit)
2797 {
2798 out << ", ";
2799 }
2800 else if (visit == PostVisit)
2801 {
2802 out << ")";
2803 }
2804}
2805
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002806const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2807{
Jamie Madill32aab012015-01-27 14:12:26 -05002808 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002809
Jamie Madill98493dd2013-07-08 14:39:03 -04002810 const TStructure* structure = type.getStruct();
2811 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002812 {
Jamie Madill033dae62014-06-18 12:56:28 -04002813 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002814
Jamie Madill98493dd2013-07-08 14:39:03 -04002815 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002816
Jamie Madill98493dd2013-07-08 14:39:03 -04002817 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002818 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002819 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002820 constUnion = writeConstantUnion(*fieldType, constUnion);
2821
Jamie Madill98493dd2013-07-08 14:39:03 -04002822 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002823 {
2824 out << ", ";
2825 }
2826 }
2827
2828 out << ")";
2829 }
2830 else
2831 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002832 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002833 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002834
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002835 if (writeType)
2836 {
Jamie Madill033dae62014-06-18 12:56:28 -04002837 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002838 }
2839
Jamie Madill94bf7f22013-07-08 13:31:15 -04002840 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002841 {
2842 switch (constUnion->getType())
2843 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002844 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002845 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002846 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002847 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002848 default: UNREACHABLE();
2849 }
2850
2851 if (i != size - 1)
2852 {
2853 out << ", ";
2854 }
2855 }
2856
2857 if (writeType)
2858 {
2859 out << ")";
2860 }
2861 }
2862
2863 return constUnion;
2864}
2865
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002866void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2867{
2868 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2869 outputTriplet(visit, preString.c_str(), ", ", ")");
2870}
2871
Jamie Madill37997142015-01-28 10:06:34 -05002872bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2873{
2874 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2875 expression->traverse(&searchSymbol);
2876
2877 if (searchSymbol.foundMatch())
2878 {
2879 // Type already printed
2880 out << "t" + str(mUniqueIndex) + " = ";
2881 expression->traverse(this);
2882 out << ", ";
2883 symbolNode->traverse(this);
2884 out << " = t" + str(mUniqueIndex);
2885
2886 mUniqueIndex++;
2887 return true;
2888 }
2889
2890 return false;
2891}
2892
2893void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2894{
2895 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2896 << "\n"
2897 << "void initializeDeferredGlobals()\n"
2898 << "{\n";
2899
2900 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2901 {
2902 TIntermSymbol *symbol = deferredGlobal.first;
2903 TIntermTyped *expression = deferredGlobal.second;
2904 ASSERT(symbol);
2905 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2906
2907 out << " " << Decorate(symbol->getSymbol()) << " = ";
2908
2909 if (!writeSameSymbolInitializer(out, symbol, expression))
2910 {
2911 ASSERT(mInfoSinkStack.top() == &out);
2912 expression->traverse(this);
2913 }
2914
2915 out << ";\n";
2916 }
2917
2918 out << "}\n"
2919 << "\n";
2920}
2921
Jamie Madill55e79e02015-02-09 15:35:00 -05002922TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2923{
2924 const TFieldList &fields = structure.fields();
2925
2926 for (const auto &eqFunction : mStructEqualityFunctions)
2927 {
2928 if (eqFunction.structure == &structure)
2929 {
2930 return eqFunction.functionName;
2931 }
2932 }
2933
2934 const TString &structNameString = StructNameString(structure);
2935
2936 StructEqualityFunction function;
2937 function.structure = &structure;
2938 function.functionName = "angle_eq_" + structNameString;
2939
2940 TString &func = function.functionDefinition;
2941
2942 func = "bool " + function.functionName + "(" + structNameString + " a, " + structNameString + " b)\n" +
2943 "{\n"
2944 " return ";
2945
2946 for (size_t i = 0; i < fields.size(); i++)
2947 {
2948 const TField *field = fields[i];
2949 const TType *fieldType = field->type();
2950
2951 const TString &fieldNameA = "a." + Decorate(field->name());
2952 const TString &fieldNameB = "b." + Decorate(field->name());
2953
2954 if (i > 0)
2955 {
2956 func += " && ";
2957 }
2958
2959 if (fieldType->getBasicType() == EbtStruct)
2960 {
2961 const TStructure &fieldStruct = *fieldType->getStruct();
2962 const TString &functionName = addStructEqualityFunction(fieldStruct);
2963 func += functionName + "(" + fieldNameA + ", " + fieldNameB + ")";
2964 }
2965 else if (fieldType->isScalar())
2966 {
2967 func += "(" + fieldNameA + " == " + fieldNameB + ")";
2968 }
2969 else
2970 {
2971 ASSERT(fieldType->isMatrix() || fieldType->isVector());
2972 func += "all(" + fieldNameA + " == " + fieldNameB + ")";
2973 }
2974 }
2975
2976 func = func + ";\n" + "}\n";
2977
2978 mStructEqualityFunctions.push_back(function);
2979
2980 return function.functionName;
2981}
2982
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002983}