blob: 3ee58cea7af62b085aa55b8ac92a9edab94ecfd4 [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"
20#include "compiler/translator/RewriteElseBlocks.h"
21#include "compiler/translator/SearchSymbol.h"
22#include "compiler/translator/StructureHLSL.h"
23#include "compiler/translator/TranslatorHLSL.h"
24#include "compiler/translator/UnfoldShortCircuit.h"
25#include "compiler/translator/UniformHLSL.h"
26#include "compiler/translator/UtilsHLSL.h"
27#include "compiler/translator/blocklayout.h"
28#include "compiler/translator/compilerdebug.h"
29#include "compiler/translator/util.h"
30
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031namespace sh
32{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000033
Nicolas Capense0ba27a2013-06-24 16:10:52 -040034TString OutputHLSL::TextureFunction::name() const
35{
36 TString name = "gl_texture";
37
Nicolas Capens6d232bb2013-07-08 15:56:38 -040038 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040039 {
40 name += "2D";
41 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040042 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040043 {
44 name += "3D";
45 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040046 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040047 {
48 name += "Cube";
49 }
50 else UNREACHABLE();
51
52 if (proj)
53 {
54 name += "Proj";
55 }
56
Nicolas Capensb1f45b72013-12-19 17:37:19 -050057 if (offset)
58 {
59 name += "Offset";
60 }
61
Nicolas Capens75fb4752013-07-10 15:14:47 -040062 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040063 {
Nicolas Capensfc014542014-02-18 14:47:13 -050064 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040065 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050066 case LOD: name += "Lod"; break;
67 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040068 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050069 case SIZE: name += "Size"; break;
70 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050071 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040072 default: UNREACHABLE();
73 }
74
75 return name + "(";
76}
77
78bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
79{
80 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040081 if (sampler > rhs.sampler) return false;
82
Nicolas Capense0ba27a2013-06-24 16:10:52 -040083 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040084 if (coords > rhs.coords) return false;
85
Nicolas Capense0ba27a2013-06-24 16:10:52 -040086 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040087 if (proj && !rhs.proj) return false;
88
89 if (!offset && rhs.offset) return true;
90 if (offset && !rhs.offset) return false;
91
Nicolas Capens75fb4752013-07-10 15:14:47 -040092 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040093 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040094
95 return false;
96}
97
Jamie Madill54ad4f82014-09-03 09:40:46 -040098OutputHLSL::OutputHLSL(TParseContext &context, TranslatorHLSL *parentTranslator)
99 : TIntermTraverser(true, true, true),
100 mContext(context),
101 mOutputType(parentTranslator->getOutputType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000103 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000104 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000105
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000106 mUsesFragColor = false;
107 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000108 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000109 mUsesFragCoord = false;
110 mUsesPointCoord = false;
111 mUsesFrontFacing = false;
112 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000113 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400114 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000115 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500116 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400117 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000118
Jamie Madill54ad4f82014-09-03 09:40:46 -0400119 const ShBuiltInResources &resources = parentTranslator->getResources();
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000120 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
121
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000122 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000123
124 mContainsLoopDiscontinuity = false;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800125 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000126 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000127 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400128 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000129
130 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000131
Jamie Madill8daaba12014-06-13 10:04:33 -0400132 mStructureHLSL = new StructureHLSL;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400133 mUniformHLSL = new UniformHLSL(mStructureHLSL, parentTranslator);
Jamie Madill8daaba12014-06-13 10:04:33 -0400134
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000135 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000136 {
Jamie Madill183bde52014-07-02 15:31:19 -0400137 if (mContext.shaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000138 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400139 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
140 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000141 }
142 else
143 {
Cooper Partine6664f02015-01-09 16:22:24 -0800144 // Reserve registers for dx_DepthRange, dx_ViewAdjust and dx_ViewCoords
145 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000146 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000147 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000148
Jamie Madillf91ce812014-06-13 10:04:34 -0400149 // Reserve registers for the default uniform block and driver constants
150 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151}
152
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000153OutputHLSL::~OutputHLSL()
154{
Jamie Madill8daaba12014-06-13 10:04:33 -0400155 SafeDelete(mUnfoldShortCircuit);
156 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400157 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000158}
159
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000160void OutputHLSL::output()
161{
Jamie Madill183bde52014-07-02 15:31:19 -0400162 mContainsLoopDiscontinuity = mContext.shaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Corentin Wallez80bacde2014-11-10 12:07:37 -0800163 mContainsAnyLoop = containsAnyLoop(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400164 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
165 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000166
Jamie Madille53c98b2014-02-03 11:57:13 -0500167 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
168 // use a vertex attribute as a condition, and some related computation in the else block.
Jamie Madill183bde52014-07-02 15:31:19 -0400169 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500170 {
171 RewriteElseBlocks(mContext.treeRoot);
172 }
173
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200174 BuiltInFunctionEmulatorHLSL builtInFunctionEmulator;
175 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(mContext.treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500176
Jamie Madill37997142015-01-28 10:06:34 -0500177 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500178 mInfoSinkStack.push(&mBody);
179 mContext.treeRoot->traverse(this);
180 mInfoSinkStack.pop();
181
Jamie Madill37997142015-01-28 10:06:34 -0500182 mInfoSinkStack.push(&mFooter);
183 if (!mDeferredGlobalInitializers.empty())
184 {
185 writeDeferredGlobalInitializers(mFooter);
186 }
187 mInfoSinkStack.pop();
188
Jamie Madill32aab012015-01-27 14:12:26 -0500189 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200190 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500191 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000192
Olli Etuahoe17e3192015-01-02 12:47:59 +0200193 TInfoSinkBase& sink = mContext.infoSink().obj;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200194 sink << mHeader.c_str();
195 sink << mBody.c_str();
Jamie Madill37997142015-01-28 10:06:34 -0500196 sink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200197
198 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000199}
200
Jamie Madill570e04d2013-06-21 09:15:33 -0400201void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
202{
203 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
204 {
205 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
206
Jamie Madill32aab012015-01-27 14:12:26 -0500207 TInfoSinkBase structInfoSink;
208 mInfoSinkStack.push(&structInfoSink);
209
Jamie Madill570e04d2013-06-21 09:15:33 -0400210 // This will mark the necessary block elements as referenced
211 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500212
213 TString structName(structInfoSink.c_str());
214 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400215
216 mFlaggedStructOriginalNames[flaggedNode] = structName;
217
218 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
219 {
220 structName.erase(pos, 1);
221 }
222
223 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
224 }
225}
226
Jamie Madill4e1fd412014-07-10 17:50:10 -0400227const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
228{
229 return mUniformHLSL->getInterfaceBlockRegisterMap();
230}
231
Jamie Madill9fe25e92014-07-18 10:33:08 -0400232const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
233{
234 return mUniformHLSL->getUniformRegisterMap();
235}
236
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000237int OutputHLSL::vectorSize(const TType &type) const
238{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000239 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000240 int arraySize = type.isArray() ? type.getArraySize() : 1;
241
242 return elementSize * arraySize;
243}
244
Jamie Madill98493dd2013-07-08 14:39:03 -0400245TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400246{
247 TString init;
248
249 TString preIndentString;
250 TString fullIndentString;
251
252 for (int spaces = 0; spaces < (indent * 4); spaces++)
253 {
254 preIndentString += ' ';
255 }
256
257 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
258 {
259 fullIndentString += ' ';
260 }
261
262 init += preIndentString + "{\n";
263
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 const TFieldList &fields = structure.fields();
265 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400266 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400268 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400270
Jamie Madill98493dd2013-07-08 14:39:03 -0400271 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400272 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400274 }
275 else
276 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400278 }
279 }
280
281 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
282
283 return init;
284}
285
Olli Etuahoe17e3192015-01-02 12:47:59 +0200286void OutputHLSL::header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287{
Jamie Madill32aab012015-01-27 14:12:26 -0500288 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000290 TString varyings;
291 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400292 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000293
Jamie Madill829f59e2013-11-13 19:40:54 -0500294 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400295 {
296 TIntermTyped *structNode = flaggedStructIt->first;
297 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400298 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400299 const TString &originalName = mFlaggedStructOriginalNames[structNode];
300
Jamie Madill033dae62014-06-18 12:56:28 -0400301 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400302 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400303 flaggedStructs += "\n";
304 }
305
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000306 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
307 {
308 const TType &type = varying->second->getType();
309 const TString &name = varying->second->getSymbol();
310
311 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400312 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
313 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000314 }
315
316 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
317 {
318 const TType &type = attribute->second->getType();
319 const TString &name = attribute->second->getSymbol();
320
Jamie Madill033dae62014-06-18 12:56:28 -0400321 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000322 }
323
Jamie Madill8daaba12014-06-13 10:04:33 -0400324 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400325
Jamie Madillf91ce812014-06-13 10:04:34 -0400326 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
327 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
328
Jamie Madill55e79e02015-02-09 15:35:00 -0500329 if (!mStructEqualityFunctions.empty())
330 {
331 out << "\n// Structure equality functions\n\n";
332 for (const auto &eqFunction : mStructEqualityFunctions)
333 {
334 out << eqFunction.functionDefinition << "\n";
335 }
336 }
337
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500338 if (mUsesDiscardRewriting)
339 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400340 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500341 }
342
Nicolas Capens655fe362014-04-11 13:12:34 -0400343 if (mUsesNestedBreak)
344 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400345 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400346 }
347
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400348 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
349 "#define LOOP [loop]\n"
350 "#define FLATTEN [flatten]\n"
351 "#else\n"
352 "#define LOOP\n"
353 "#define FLATTEN\n"
354 "#endif\n";
355
Jamie Madill183bde52014-07-02 15:31:19 -0400356 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000358 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000359 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000360
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000361 out << "// Varyings\n";
362 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400363 out << "\n";
364
365 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000366 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500367 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000368 {
Jamie Madill46131a32013-06-20 11:55:50 -0400369 const TString &variableName = outputVariableIt->first;
370 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400371
Jamie Madill033dae62014-06-18 12:56:28 -0400372 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400373 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000374 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000375 }
Jamie Madill46131a32013-06-20 11:55:50 -0400376 else
377 {
378 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
379
380 out << "static float4 gl_Color[" << numColorValues << "] =\n"
381 "{\n";
382 for (unsigned int i = 0; i < numColorValues; i++)
383 {
384 out << " float4(0, 0, 0, 0)";
385 if (i + 1 != numColorValues)
386 {
387 out << ",";
388 }
389 out << "\n";
390 }
391
392 out << "};\n";
393 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000394
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400395 if (mUsesFragDepth)
396 {
397 out << "static float gl_Depth = 0.0;\n";
398 }
399
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000400 if (mUsesFragCoord)
401 {
402 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
403 }
404
405 if (mUsesPointCoord)
406 {
407 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
408 }
409
410 if (mUsesFrontFacing)
411 {
412 out << "static bool gl_FrontFacing = false;\n";
413 }
414
415 out << "\n";
416
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000417 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000418 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000419 out << "struct gl_DepthRangeParameters\n"
420 "{\n"
421 " float near;\n"
422 " float far;\n"
423 " float diff;\n"
424 "};\n"
425 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000426 }
427
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000428 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000429 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000430 out << "cbuffer DriverConstants : register(b1)\n"
431 "{\n";
432
433 if (mUsesDepthRange)
434 {
435 out << " float3 dx_DepthRange : packoffset(c0);\n";
436 }
437
438 if (mUsesFragCoord)
439 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000440 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000441 }
442
443 if (mUsesFragCoord || mUsesFrontFacing)
444 {
445 out << " float3 dx_DepthFront : packoffset(c2);\n";
446 }
447
448 out << "};\n";
449 }
450 else
451 {
452 if (mUsesDepthRange)
453 {
454 out << "uniform float3 dx_DepthRange : register(c0);";
455 }
456
457 if (mUsesFragCoord)
458 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000459 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000460 }
461
462 if (mUsesFragCoord || mUsesFrontFacing)
463 {
464 out << "uniform float3 dx_DepthFront : register(c2);\n";
465 }
466 }
467
468 out << "\n";
469
470 if (mUsesDepthRange)
471 {
472 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
473 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000474 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000475
Jamie Madillf91ce812014-06-13 10:04:34 -0400476 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000477 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400478 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000479 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400480 out << flaggedStructs;
481 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000482 }
483
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000484 if (usingMRTExtension && mNumRenderTargets > 1)
485 {
486 out << "#define GL_USES_MRT\n";
487 }
488
489 if (mUsesFragColor)
490 {
491 out << "#define GL_USES_FRAG_COLOR\n";
492 }
493
494 if (mUsesFragData)
495 {
496 out << "#define GL_USES_FRAG_DATA\n";
497 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000498 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000499 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000501 out << "// Attributes\n";
502 out << attributes;
503 out << "\n"
504 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400505
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000506 if (mUsesPointSize)
507 {
508 out << "static float gl_PointSize = float(1);\n";
509 }
510
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000511 if (mUsesInstanceID)
512 {
513 out << "static int gl_InstanceID;";
514 }
515
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000516 out << "\n"
517 "// Varyings\n";
518 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000519 out << "\n";
520
521 if (mUsesDepthRange)
522 {
523 out << "struct gl_DepthRangeParameters\n"
524 "{\n"
525 " float near;\n"
526 " float far;\n"
527 " float diff;\n"
528 "};\n"
529 "\n";
530 }
531
532 if (mOutputType == SH_HLSL11_OUTPUT)
533 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800534 out << "cbuffer DriverConstants : register(b1)\n"
535 "{\n";
536
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000537 if (mUsesDepthRange)
538 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800539 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000540 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800541
Cooper Partine6664f02015-01-09 16:22:24 -0800542 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800543 // However, we declare it for all shaders (including Feature Level 10+).
544 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
545 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800546 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800547
548 out << "};\n"
549 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000550 }
551 else
552 {
553 if (mUsesDepthRange)
554 {
555 out << "uniform float3 dx_DepthRange : register(c0);\n";
556 }
557
Cooper Partine6664f02015-01-09 16:22:24 -0800558 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
559 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000560 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000561 }
562
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000563 if (mUsesDepthRange)
564 {
565 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
566 "\n";
567 }
568
Jamie Madillf91ce812014-06-13 10:04:34 -0400569 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000570 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400571 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000572 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400573 out << flaggedStructs;
574 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000575 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400576 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000577
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400578 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
579 {
580 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400581 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000582 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400583 switch(textureFunction->sampler)
584 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400585 case EbtSampler2D: out << "int2 "; break;
586 case EbtSampler3D: out << "int3 "; break;
587 case EbtSamplerCube: out << "int2 "; break;
588 case EbtSampler2DArray: out << "int3 "; break;
589 case EbtISampler2D: out << "int2 "; break;
590 case EbtISampler3D: out << "int3 "; break;
591 case EbtISamplerCube: out << "int2 "; break;
592 case EbtISampler2DArray: out << "int3 "; break;
593 case EbtUSampler2D: out << "int2 "; break;
594 case EbtUSampler3D: out << "int3 "; break;
595 case EbtUSamplerCube: out << "int2 "; break;
596 case EbtUSampler2DArray: out << "int3 "; break;
597 case EbtSampler2DShadow: out << "int2 "; break;
598 case EbtSamplerCubeShadow: out << "int2 "; break;
599 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400600 default: UNREACHABLE();
601 }
602 }
603 else // Sampling function
604 {
605 switch(textureFunction->sampler)
606 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400607 case EbtSampler2D: out << "float4 "; break;
608 case EbtSampler3D: out << "float4 "; break;
609 case EbtSamplerCube: out << "float4 "; break;
610 case EbtSampler2DArray: out << "float4 "; break;
611 case EbtISampler2D: out << "int4 "; break;
612 case EbtISampler3D: out << "int4 "; break;
613 case EbtISamplerCube: out << "int4 "; break;
614 case EbtISampler2DArray: out << "int4 "; break;
615 case EbtUSampler2D: out << "uint4 "; break;
616 case EbtUSampler3D: out << "uint4 "; break;
617 case EbtUSamplerCube: out << "uint4 "; break;
618 case EbtUSampler2DArray: out << "uint4 "; break;
619 case EbtSampler2DShadow: out << "float "; break;
620 case EbtSamplerCubeShadow: out << "float "; break;
621 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400622 default: UNREACHABLE();
623 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000624 }
625
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400626 // Function name
627 out << textureFunction->name();
628
629 // Argument list
630 int hlslCoords = 4;
631
632 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000633 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400634 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000635 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400636 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
637 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
638 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000639 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400640
Nicolas Capens75fb4752013-07-10 15:14:47 -0400641 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000642 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400643 case TextureFunction::IMPLICIT: break;
644 case TextureFunction::BIAS: hlslCoords = 4; break;
645 case TextureFunction::LOD: hlslCoords = 4; break;
646 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400647 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400648 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000649 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400650 }
651 else if (mOutputType == SH_HLSL11_OUTPUT)
652 {
653 switch(textureFunction->sampler)
654 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400655 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
656 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
657 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
658 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
659 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
660 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500661 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400662 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
663 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
664 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500665 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400666 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
667 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
668 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
669 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400670 default: UNREACHABLE();
671 }
672 }
673 else UNREACHABLE();
674
Nicolas Capensfc014542014-02-18 14:47:13 -0500675 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400676 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500677 switch(textureFunction->coords)
678 {
679 case 2: out << ", int2 t"; break;
680 case 3: out << ", int3 t"; break;
681 default: UNREACHABLE();
682 }
683 }
684 else // Floating-point coordinates (except textureSize)
685 {
686 switch(textureFunction->coords)
687 {
688 case 1: out << ", int lod"; break; // textureSize()
689 case 2: out << ", float2 t"; break;
690 case 3: out << ", float3 t"; break;
691 case 4: out << ", float4 t"; break;
692 default: UNREACHABLE();
693 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000694 }
695
Nicolas Capensd11d5492014-02-19 17:06:10 -0500696 if (textureFunction->method == TextureFunction::GRAD)
697 {
698 switch(textureFunction->sampler)
699 {
700 case EbtSampler2D:
701 case EbtISampler2D:
702 case EbtUSampler2D:
703 case EbtSampler2DArray:
704 case EbtISampler2DArray:
705 case EbtUSampler2DArray:
706 case EbtSampler2DShadow:
707 case EbtSampler2DArrayShadow:
708 out << ", float2 ddx, float2 ddy";
709 break;
710 case EbtSampler3D:
711 case EbtISampler3D:
712 case EbtUSampler3D:
713 case EbtSamplerCube:
714 case EbtISamplerCube:
715 case EbtUSamplerCube:
716 case EbtSamplerCubeShadow:
717 out << ", float3 ddx, float3 ddy";
718 break;
719 default: UNREACHABLE();
720 }
721 }
722
Nicolas Capens75fb4752013-07-10 15:14:47 -0400723 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000724 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400725 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400726 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400727 case TextureFunction::LOD: out << ", float lod"; break;
728 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400729 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400730 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500731 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500732 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400733 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000734 }
735
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500736 if (textureFunction->offset)
737 {
738 switch(textureFunction->sampler)
739 {
740 case EbtSampler2D: out << ", int2 offset"; break;
741 case EbtSampler3D: out << ", int3 offset"; break;
742 case EbtSampler2DArray: out << ", int2 offset"; break;
743 case EbtISampler2D: out << ", int2 offset"; break;
744 case EbtISampler3D: out << ", int3 offset"; break;
745 case EbtISampler2DArray: out << ", int2 offset"; break;
746 case EbtUSampler2D: out << ", int2 offset"; break;
747 case EbtUSampler3D: out << ", int3 offset"; break;
748 case EbtUSampler2DArray: out << ", int2 offset"; break;
749 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500750 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500751 default: UNREACHABLE();
752 }
753 }
754
Nicolas Capens84cfa122014-04-14 13:48:45 -0400755 if (textureFunction->method == TextureFunction::BIAS ||
756 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500757 {
758 out << ", float bias";
759 }
760
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400761 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400762 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400763
Nicolas Capens75fb4752013-07-10 15:14:47 -0400764 if (textureFunction->method == TextureFunction::SIZE)
765 {
766 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
767 {
768 if (IsSamplerArray(textureFunction->sampler))
769 {
770 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
771 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
772 }
773 else
774 {
775 out << " uint width; uint height; uint numberOfLevels;\n"
776 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
777 }
778 }
779 else if (IsSampler3D(textureFunction->sampler))
780 {
781 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
782 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
783 }
784 else UNREACHABLE();
785
786 switch(textureFunction->sampler)
787 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400788 case EbtSampler2D: out << " return int2(width, height);"; break;
789 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
790 case EbtSamplerCube: out << " return int2(width, height);"; break;
791 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
792 case EbtISampler2D: out << " return int2(width, height);"; break;
793 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
794 case EbtISamplerCube: out << " return int2(width, height);"; break;
795 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
796 case EbtUSampler2D: out << " return int2(width, height);"; break;
797 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
798 case EbtUSamplerCube: out << " return int2(width, height);"; break;
799 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
800 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
801 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
802 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400803 default: UNREACHABLE();
804 }
805 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400806 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400807 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500808 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
809 {
810 out << " float width; float height; float layers; float levels;\n";
811
812 out << " uint mip = 0;\n";
813
814 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
815
816 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
817 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
818 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
819 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
820
821 // FACE_POSITIVE_X = 000b
822 // FACE_NEGATIVE_X = 001b
823 // FACE_POSITIVE_Y = 010b
824 // FACE_NEGATIVE_Y = 011b
825 // FACE_POSITIVE_Z = 100b
826 // FACE_NEGATIVE_Z = 101b
827 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
828
829 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
830 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
831 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
832
833 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
834 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
835 }
836 else if (IsIntegerSampler(textureFunction->sampler) &&
837 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400838 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400839 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400840 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400841 if (IsSamplerArray(textureFunction->sampler))
842 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400843 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400844
Nicolas Capens9edebd62013-08-06 10:59:10 -0400845 if (textureFunction->method == TextureFunction::LOD0)
846 {
847 out << " uint mip = 0;\n";
848 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400849 else if (textureFunction->method == TextureFunction::LOD0BIAS)
850 {
851 out << " uint mip = bias;\n";
852 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400853 else
854 {
855 if (textureFunction->method == TextureFunction::IMPLICIT ||
856 textureFunction->method == TextureFunction::BIAS)
857 {
858 out << " x.GetDimensions(0, width, height, layers, levels);\n"
859 " float2 tSized = float2(t.x * width, t.y * height);\n"
860 " float dx = length(ddx(tSized));\n"
861 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500862 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400863
864 if (textureFunction->method == TextureFunction::BIAS)
865 {
866 out << " lod += bias;\n";
867 }
868 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500869 else if (textureFunction->method == TextureFunction::GRAD)
870 {
871 out << " x.GetDimensions(0, width, height, layers, levels);\n"
872 " float lod = log2(max(length(ddx), length(ddy)));\n";
873 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400874
875 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
876 }
877
878 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400879 }
880 else
881 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400882 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400883
Nicolas Capens9edebd62013-08-06 10:59:10 -0400884 if (textureFunction->method == TextureFunction::LOD0)
885 {
886 out << " uint mip = 0;\n";
887 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400888 else if (textureFunction->method == TextureFunction::LOD0BIAS)
889 {
890 out << " uint mip = bias;\n";
891 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400892 else
893 {
894 if (textureFunction->method == TextureFunction::IMPLICIT ||
895 textureFunction->method == TextureFunction::BIAS)
896 {
897 out << " x.GetDimensions(0, width, height, levels);\n"
898 " float2 tSized = float2(t.x * width, t.y * height);\n"
899 " float dx = length(ddx(tSized));\n"
900 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500901 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400902
903 if (textureFunction->method == TextureFunction::BIAS)
904 {
905 out << " lod += bias;\n";
906 }
907 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500908 else if (textureFunction->method == TextureFunction::LOD)
909 {
910 out << " x.GetDimensions(0, width, height, levels);\n";
911 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500912 else if (textureFunction->method == TextureFunction::GRAD)
913 {
914 out << " x.GetDimensions(0, width, height, levels);\n"
915 " float lod = log2(max(length(ddx), length(ddy)));\n";
916 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400917
918 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
919 }
920
921 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400922 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400923 }
924 else if (IsSampler3D(textureFunction->sampler))
925 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400926 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400927
Nicolas Capens9edebd62013-08-06 10:59:10 -0400928 if (textureFunction->method == TextureFunction::LOD0)
929 {
930 out << " uint mip = 0;\n";
931 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400932 else if (textureFunction->method == TextureFunction::LOD0BIAS)
933 {
934 out << " uint mip = bias;\n";
935 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400936 else
937 {
938 if (textureFunction->method == TextureFunction::IMPLICIT ||
939 textureFunction->method == TextureFunction::BIAS)
940 {
941 out << " x.GetDimensions(0, width, height, depth, levels);\n"
942 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
943 " float dx = length(ddx(tSized));\n"
944 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500945 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400946
947 if (textureFunction->method == TextureFunction::BIAS)
948 {
949 out << " lod += bias;\n";
950 }
951 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500952 else if (textureFunction->method == TextureFunction::GRAD)
953 {
954 out << " x.GetDimensions(0, width, height, depth, levels);\n"
955 " float lod = log2(max(length(ddx), length(ddy)));\n";
956 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400957
958 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
959 }
960
961 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400962 }
963 else UNREACHABLE();
964 }
965
966 out << " return ";
967
968 // HLSL intrinsic
969 if (mOutputType == SH_HLSL9_OUTPUT)
970 {
971 switch(textureFunction->sampler)
972 {
973 case EbtSampler2D: out << "tex2D"; break;
974 case EbtSamplerCube: out << "texCUBE"; break;
975 default: UNREACHABLE();
976 }
977
Nicolas Capens75fb4752013-07-10 15:14:47 -0400978 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400979 {
980 case TextureFunction::IMPLICIT: out << "(s, "; break;
981 case TextureFunction::BIAS: out << "bias(s, "; break;
982 case TextureFunction::LOD: out << "lod(s, "; break;
983 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400984 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400985 default: UNREACHABLE();
986 }
987 }
988 else if (mOutputType == SH_HLSL11_OUTPUT)
989 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500990 if (textureFunction->method == TextureFunction::GRAD)
991 {
992 if (IsIntegerSampler(textureFunction->sampler))
993 {
994 out << "x.Load(";
995 }
996 else if (IsShadowSampler(textureFunction->sampler))
997 {
998 out << "x.SampleCmpLevelZero(s, ";
999 }
1000 else
1001 {
1002 out << "x.SampleGrad(s, ";
1003 }
1004 }
1005 else if (IsIntegerSampler(textureFunction->sampler) ||
1006 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001007 {
1008 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001009 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001010 else if (IsShadowSampler(textureFunction->sampler))
1011 {
1012 out << "x.SampleCmp(s, ";
1013 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001014 else
1015 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001016 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001017 {
1018 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1019 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1020 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1021 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001022 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001023 default: UNREACHABLE();
1024 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001025 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001026 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001027 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001028
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001029 // Integer sampling requires integer addresses
1030 TString addressx = "";
1031 TString addressy = "";
1032 TString addressz = "";
1033 TString close = "";
1034
Nicolas Capensfc014542014-02-18 14:47:13 -05001035 if (IsIntegerSampler(textureFunction->sampler) ||
1036 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001037 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001038 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001039 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001040 case 2: out << "int3("; break;
1041 case 3: out << "int4("; break;
1042 default: UNREACHABLE();
1043 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001044
Nicolas Capensfc014542014-02-18 14:47:13 -05001045 // Convert from normalized floating-point to integer
1046 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001047 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001048 addressx = "int(floor(width * frac((";
1049 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001050
Nicolas Capensfc014542014-02-18 14:47:13 -05001051 if (IsSamplerArray(textureFunction->sampler))
1052 {
1053 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1054 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001055 else if (IsSamplerCube(textureFunction->sampler))
1056 {
1057 addressz = "((((";
1058 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001059 else
1060 {
1061 addressz = "int(floor(depth * frac((";
1062 }
1063
1064 close = "))))";
1065 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001066 }
1067 else
1068 {
1069 switch(hlslCoords)
1070 {
1071 case 2: out << "float2("; break;
1072 case 3: out << "float3("; break;
1073 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001074 default: UNREACHABLE();
1075 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001076 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001077
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001078 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001079
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001080 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001081 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001082 switch(textureFunction->coords)
1083 {
1084 case 3: proj = " / t.z"; break;
1085 case 4: proj = " / t.w"; break;
1086 default: UNREACHABLE();
1087 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001088 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001089
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001090 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001091
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001092 if (mOutputType == SH_HLSL9_OUTPUT)
1093 {
1094 if (hlslCoords >= 3)
1095 {
1096 if (textureFunction->coords < 3)
1097 {
1098 out << ", 0";
1099 }
1100 else
1101 {
1102 out << ", t.z" + proj;
1103 }
1104 }
1105
1106 if (hlslCoords == 4)
1107 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001108 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001109 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001110 case TextureFunction::BIAS: out << ", bias"; break;
1111 case TextureFunction::LOD: out << ", lod"; break;
1112 case TextureFunction::LOD0: out << ", 0"; break;
1113 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001114 default: UNREACHABLE();
1115 }
1116 }
1117
1118 out << "));\n";
1119 }
1120 else if (mOutputType == SH_HLSL11_OUTPUT)
1121 {
1122 if (hlslCoords >= 3)
1123 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001124 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1125 {
1126 out << ", face";
1127 }
1128 else
1129 {
1130 out << ", " + addressz + ("t.z" + proj) + close;
1131 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001132 }
1133
Nicolas Capensd11d5492014-02-19 17:06:10 -05001134 if (textureFunction->method == TextureFunction::GRAD)
1135 {
1136 if (IsIntegerSampler(textureFunction->sampler))
1137 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001138 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001139 }
1140 else if (IsShadowSampler(textureFunction->sampler))
1141 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001142 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001143 switch(textureFunction->coords)
1144 {
1145 case 3: out << "), t.z"; break;
1146 case 4: out << "), t.w"; break;
1147 default: UNREACHABLE();
1148 }
1149 }
1150 else
1151 {
1152 out << "), ddx, ddy";
1153 }
1154 }
1155 else if (IsIntegerSampler(textureFunction->sampler) ||
1156 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001157 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001158 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001159 }
1160 else if (IsShadowSampler(textureFunction->sampler))
1161 {
1162 // Compare value
1163 switch(textureFunction->coords)
1164 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001165 case 3: out << "), t.z"; break;
1166 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001167 default: UNREACHABLE();
1168 }
1169 }
1170 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001171 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001172 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001173 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001174 case TextureFunction::IMPLICIT: out << ")"; break;
1175 case TextureFunction::BIAS: out << "), bias"; break;
1176 case TextureFunction::LOD: out << "), lod"; break;
1177 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001178 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001179 default: UNREACHABLE();
1180 }
1181 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001182
1183 if (textureFunction->offset)
1184 {
1185 out << ", offset";
1186 }
1187
1188 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001189 }
1190 else UNREACHABLE();
1191 }
1192
1193 out << "\n"
1194 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001195 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001196 }
1197
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001198 if (mUsesFragCoord)
1199 {
1200 out << "#define GL_USES_FRAG_COORD\n";
1201 }
1202
1203 if (mUsesPointCoord)
1204 {
1205 out << "#define GL_USES_POINT_COORD\n";
1206 }
1207
1208 if (mUsesFrontFacing)
1209 {
1210 out << "#define GL_USES_FRONT_FACING\n";
1211 }
1212
1213 if (mUsesPointSize)
1214 {
1215 out << "#define GL_USES_POINT_SIZE\n";
1216 }
1217
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001218 if (mUsesFragDepth)
1219 {
1220 out << "#define GL_USES_FRAG_DEPTH\n";
1221 }
1222
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001223 if (mUsesDepthRange)
1224 {
1225 out << "#define GL_USES_DEPTH_RANGE\n";
1226 }
1227
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001228 if (mUsesXor)
1229 {
1230 out << "bool xor(bool p, bool q)\n"
1231 "{\n"
1232 " return (p || q) && !(p && q);\n"
1233 "}\n"
1234 "\n";
1235 }
1236
Olli Etuaho80a5a6c2015-01-12 15:35:27 +02001237 builtInFunctionEmulator->OutputEmulatedFunctionDefinition(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238}
1239
1240void OutputHLSL::visitSymbol(TIntermSymbol *node)
1241{
Jamie Madill32aab012015-01-27 14:12:26 -05001242 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001243
Jamie Madill570e04d2013-06-21 09:15:33 -04001244 // Handle accessing std140 structs by value
1245 if (mFlaggedStructMappedNames.count(node) > 0)
1246 {
1247 out << mFlaggedStructMappedNames[node];
1248 return;
1249 }
1250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001251 TString name = node->getSymbol();
1252
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001253 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001254 {
1255 mUsesDepthRange = true;
1256 out << name;
1257 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001258 else
1259 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001260 TQualifier qualifier = node->getQualifier();
1261
1262 if (qualifier == EvqUniform)
1263 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001264 const TType& nodeType = node->getType();
1265 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1266
1267 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001268 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001269 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001270 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001271 else
1272 {
1273 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001274 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001275
Jamie Madill033dae62014-06-18 12:56:28 -04001276 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001277 }
Jamie Madill19571812013-08-12 15:26:34 -07001278 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001279 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001280 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001281 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001282 }
Jamie Madill033dae62014-06-18 12:56:28 -04001283 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001284 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001285 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001286 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001287 }
Jamie Madill19571812013-08-12 15:26:34 -07001288 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001289 {
1290 mReferencedOutputVariables[name] = node;
1291 out << "out_" << name;
1292 }
1293 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001294 {
1295 out << "gl_Color[0]";
1296 mUsesFragColor = true;
1297 }
1298 else if (qualifier == EvqFragData)
1299 {
1300 out << "gl_Color";
1301 mUsesFragData = true;
1302 }
1303 else if (qualifier == EvqFragCoord)
1304 {
1305 mUsesFragCoord = true;
1306 out << name;
1307 }
1308 else if (qualifier == EvqPointCoord)
1309 {
1310 mUsesPointCoord = true;
1311 out << name;
1312 }
1313 else if (qualifier == EvqFrontFacing)
1314 {
1315 mUsesFrontFacing = true;
1316 out << name;
1317 }
1318 else if (qualifier == EvqPointSize)
1319 {
1320 mUsesPointSize = true;
1321 out << name;
1322 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001323 else if (qualifier == EvqInstanceID)
1324 {
1325 mUsesInstanceID = true;
1326 out << name;
1327 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001328 else if (name == "gl_FragDepthEXT")
1329 {
1330 mUsesFragDepth = true;
1331 out << "gl_Depth";
1332 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001333 else if (qualifier == EvqInternal)
1334 {
1335 out << name;
1336 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001337 else
1338 {
Jamie Madill033dae62014-06-18 12:56:28 -04001339 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001340 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001341 }
1342}
1343
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001344void OutputHLSL::visitRaw(TIntermRaw *node)
1345{
Jamie Madill32aab012015-01-27 14:12:26 -05001346 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001347}
1348
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001349bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1350{
Jamie Madill32aab012015-01-27 14:12:26 -05001351 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001352
Jamie Madill570e04d2013-06-21 09:15:33 -04001353 // Handle accessing std140 structs by value
1354 if (mFlaggedStructMappedNames.count(node) > 0)
1355 {
1356 out << mFlaggedStructMappedNames[node];
1357 return false;
1358 }
1359
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001360 switch (node->getOp())
1361 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001362 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001363 case EOpInitialize:
1364 if (visit == PreVisit)
1365 {
1366 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1367 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1368 // new variable is created before the assignment is evaluated), so we need to convert
1369 // this to "float t = x, x = t;".
1370
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001371 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001372 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001373 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001374
Jamie Madill37997142015-01-28 10:06:34 -05001375 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1376 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001377 {
Jamie Madill37997142015-01-28 10:06:34 -05001378 // For variables which are not constant, defer their real initialization until
1379 // after we initialize other globals: uniforms, attributes and varyings.
1380 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1381 const TString &initString = initializer(node->getType());
1382 node->setRight(new TIntermRaw(node->getType(), initString));
1383 }
1384 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1385 {
1386 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001387 return false;
1388 }
1389 }
1390 else if (visit == InVisit)
1391 {
1392 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001393 }
1394 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001395 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1396 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1397 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1398 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1399 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1400 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001401 if (visit == PreVisit)
1402 {
1403 out << "(";
1404 }
1405 else if (visit == InVisit)
1406 {
1407 out << " = mul(";
1408 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001409 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001410 }
1411 else
1412 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001413 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001414 }
1415 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001416 case EOpMatrixTimesMatrixAssign:
1417 if (visit == PreVisit)
1418 {
1419 out << "(";
1420 }
1421 else if (visit == InVisit)
1422 {
1423 out << " = mul(";
1424 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001425 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001426 }
1427 else
1428 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001429 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001430 }
1431 break;
1432 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001433 case EOpModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001434 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1435 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1436 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1437 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1438 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001439 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001440 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001441 const TType& leftType = node->getLeft()->getType();
1442 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001443 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001444 if (visit == PreVisit)
1445 {
1446 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1447 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001448 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001449 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001450 return false;
1451 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001452 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001453 else
1454 {
1455 outputTriplet(visit, "", "[", "]");
1456 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001457 }
1458 break;
1459 case EOpIndexIndirect:
1460 // We do not currently support indirect references to interface blocks
1461 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1462 outputTriplet(visit, "", "[", "]");
1463 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001464 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001465 if (visit == InVisit)
1466 {
1467 const TStructure* structure = node->getLeft()->getType().getStruct();
1468 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1469 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001470 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001471
1472 return false;
1473 }
1474 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001475 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001476 if (visit == InVisit)
1477 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001478 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1479 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1480 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001481 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001482
1483 return false;
1484 }
1485 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001486 case EOpVectorSwizzle:
1487 if (visit == InVisit)
1488 {
1489 out << ".";
1490
1491 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1492
1493 if (swizzle)
1494 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001495 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001496
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001497 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001498 {
1499 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1500
1501 if (element)
1502 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001503 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001504
1505 switch (i)
1506 {
1507 case 0: out << "x"; break;
1508 case 1: out << "y"; break;
1509 case 2: out << "z"; break;
1510 case 3: out << "w"; break;
1511 default: UNREACHABLE();
1512 }
1513 }
1514 else UNREACHABLE();
1515 }
1516 }
1517 else UNREACHABLE();
1518
1519 return false; // Fully processed
1520 }
1521 break;
1522 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1523 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1524 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1525 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001526 case EOpMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001527 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1528 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1529 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1530 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1531 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001532 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001533 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001534 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001535 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001536 if (node->getOp() == EOpEqual)
1537 {
1538 outputTriplet(visit, "(", " == ", ")");
1539 }
1540 else
1541 {
1542 outputTriplet(visit, "(", " != ", ")");
1543 }
1544 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001545 else
1546 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001547 if (visit == PreVisit && node->getOp() == EOpNotEqual)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001548 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001549 out << "!";
1550 }
1551
1552 if (node->getLeft()->getBasicType() == EbtStruct)
1553 {
1554 const TStructure &structure = *node->getLeft()->getType().getStruct();
1555 const TString &functionName = addStructEqualityFunction(structure);
1556 outputTriplet(visit, functionName + "(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001557 }
1558 else
1559 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001560 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
1561 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001562 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001563 }
1564 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001565 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1566 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1567 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1568 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1569 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001570 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001571 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1572 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001573 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001574 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001575 if (node->getRight()->hasSideEffects())
1576 {
1577 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1578 return false;
1579 }
1580 else
1581 {
1582 outputTriplet(visit, "(", " || ", ")");
1583 return true;
1584 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001585 case EOpLogicalXor:
1586 mUsesXor = true;
1587 outputTriplet(visit, "xor(", ", ", ")");
1588 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001589 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001590 if (node->getRight()->hasSideEffects())
1591 {
1592 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1593 return false;
1594 }
1595 else
1596 {
1597 outputTriplet(visit, "(", " && ", ")");
1598 return true;
1599 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001600 default: UNREACHABLE();
1601 }
1602
1603 return true;
1604}
1605
1606bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1607{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001608 switch (node->getOp())
1609 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001610 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001611 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001612 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1613 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001614 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001615 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1616 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1617 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1618 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001619 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1620 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1621 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1622 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1623 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1624 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1625 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1626 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001627 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1628 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1629 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1630 case EOpAsinh:
1631 ASSERT(node->getUseEmulatedFunction());
1632 writeEmulatedFunctionTriplet(visit, "asinh(");
1633 break;
1634 case EOpAcosh:
1635 ASSERT(node->getUseEmulatedFunction());
1636 writeEmulatedFunctionTriplet(visit, "acosh(");
1637 break;
1638 case EOpAtanh:
1639 ASSERT(node->getUseEmulatedFunction());
1640 writeEmulatedFunctionTriplet(visit, "atanh(");
1641 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001642 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1643 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1644 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1645 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1646 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1647 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1648 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1649 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1650 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1651 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1652 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001653 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1654 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1655 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1656 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001657 case EOpPackSnorm2x16:
1658 ASSERT(node->getUseEmulatedFunction());
1659 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1660 break;
1661 case EOpPackUnorm2x16:
1662 ASSERT(node->getUseEmulatedFunction());
1663 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1664 break;
1665 case EOpPackHalf2x16:
1666 ASSERT(node->getUseEmulatedFunction());
1667 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1668 break;
1669 case EOpUnpackSnorm2x16:
1670 ASSERT(node->getUseEmulatedFunction());
1671 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1672 break;
1673 case EOpUnpackUnorm2x16:
1674 ASSERT(node->getUseEmulatedFunction());
1675 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1676 break;
1677 case EOpUnpackHalf2x16:
1678 ASSERT(node->getUseEmulatedFunction());
1679 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1680 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001681 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1682 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001683 case EOpDFdx:
1684 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1685 {
1686 outputTriplet(visit, "(", "", ", 0.0)");
1687 }
1688 else
1689 {
1690 outputTriplet(visit, "ddx(", "", ")");
1691 }
1692 break;
1693 case EOpDFdy:
1694 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1695 {
1696 outputTriplet(visit, "(", "", ", 0.0)");
1697 }
1698 else
1699 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001700 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001701 }
1702 break;
1703 case EOpFwidth:
1704 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1705 {
1706 outputTriplet(visit, "(", "", ", 0.0)");
1707 }
1708 else
1709 {
1710 outputTriplet(visit, "fwidth(", "", ")");
1711 }
1712 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001713 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1714 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001715 case EOpInverse:
1716 ASSERT(node->getUseEmulatedFunction());
1717 writeEmulatedFunctionTriplet(visit, "inverse(");
1718 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001719
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001720 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1721 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001722 default: UNREACHABLE();
1723 }
1724
1725 return true;
1726}
1727
1728bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1729{
Jamie Madill32aab012015-01-27 14:12:26 -05001730 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732 switch (node->getOp())
1733 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001734 case EOpSequence:
1735 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001736 if (mInsideFunction)
1737 {
Jamie Madill075edd82013-07-08 13:30:19 -04001738 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001739 out << "{\n";
1740 }
1741
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001742 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001743 {
Jamie Madill075edd82013-07-08 13:30:19 -04001744 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001745
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001746 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001747
1748 out << ";\n";
1749 }
1750
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001751 if (mInsideFunction)
1752 {
Jamie Madill075edd82013-07-08 13:30:19 -04001753 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001754 out << "}\n";
1755 }
1756
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001757 return false;
1758 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 case EOpDeclaration:
1760 if (visit == PreVisit)
1761 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001762 TIntermSequence *sequence = node->getSequence();
1763 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001765 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001767 TStructure *structure = variable->getType().getStruct();
1768
1769 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001770 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001771 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001772 }
1773
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001774 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001775 {
Jamie Madill37997142015-01-28 10:06:34 -05001776 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001777 {
Jamie Madill37997142015-01-28 10:06:34 -05001778 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001779 {
Jamie Madill37997142015-01-28 10:06:34 -05001780 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001781 }
1782
Nicolas Capensd974db42014-10-07 10:50:19 -04001783 if (!mInsideFunction)
1784 {
1785 out << "static ";
1786 }
1787
1788 out << TypeString(variable->getType()) + " ";
1789
Jamie Madill37997142015-01-28 10:06:34 -05001790 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001791
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001792 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001793 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001794 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001795 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001796 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001797 }
1798 else
1799 {
Jamie Madill37997142015-01-28 10:06:34 -05001800 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001801 }
1802
Jamie Madill37997142015-01-28 10:06:34 -05001803 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001804 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001805 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806 }
1807 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001809 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1810 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001811 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001812 }
1813 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814 }
Jamie Madill033dae62014-06-18 12:56:28 -04001815 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001816 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001817 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001818 {
1819 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1820
1821 if (symbol)
1822 {
1823 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1824 mReferencedVaryings[symbol->getSymbol()] = symbol;
1825 }
1826 else
1827 {
1828 (*sit)->traverse(this);
1829 }
1830 }
1831 }
1832
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833 return false;
1834 }
1835 else if (visit == InVisit)
1836 {
1837 out << ", ";
1838 }
1839 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001840 case EOpInvariantDeclaration:
1841 // Do not do any translation
1842 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001843 case EOpPrototype:
1844 if (visit == PreVisit)
1845 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001846 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001847
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001848 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001849
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001850 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001851 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001852 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001853
1854 if (symbol)
1855 {
1856 out << argumentString(symbol);
1857
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001858 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001859 {
1860 out << ", ";
1861 }
1862 }
1863 else UNREACHABLE();
1864 }
1865
1866 out << ");\n";
1867
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001868 // Also prototype the Lod0 variant if needed
1869 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1870 {
1871 mOutputLod0Function = true;
1872 node->traverse(this);
1873 mOutputLod0Function = false;
1874 }
1875
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001876 return false;
1877 }
1878 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001879 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880 case EOpFunction:
1881 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001882 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883
Jamie Madill033dae62014-06-18 12:56:28 -04001884 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001885
1886 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001888 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001890 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891 {
Jamie Madill033dae62014-06-18 12:56:28 -04001892 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001893 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001894
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001895 TIntermSequence *sequence = node->getSequence();
1896 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001897
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001898 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001899 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001900 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001901
1902 if (symbol)
1903 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001904 TStructure *structure = symbol->getType().getStruct();
1905
1906 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001907 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001908 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001909 }
1910
1911 out << argumentString(symbol);
1912
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001913 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001914 {
1915 out << ", ";
1916 }
1917 }
1918 else UNREACHABLE();
1919 }
1920
1921 out << ")\n"
1922 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001923
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001924 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001925 {
1926 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001927 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001928 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001929 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001930
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001931 out << "}\n";
1932
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001933 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1934 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001935 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001936 {
1937 mOutputLod0Function = true;
1938 node->traverse(this);
1939 mOutputLod0Function = false;
1940 }
1941 }
1942
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001943 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944 }
1945 break;
1946 case EOpFunctionCall:
1947 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001948 TString name = TFunction::unmangleName(node->getName());
1949 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001950 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001951
1952 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953 {
Jamie Madill033dae62014-06-18 12:56:28 -04001954 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955 }
1956 else
1957 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001958 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001959
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001960 TextureFunction textureFunction;
1961 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001962 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04001963 textureFunction.method = TextureFunction::IMPLICIT;
1964 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001965 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001966
1967 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001968 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001969 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001970 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001971 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001972 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001973 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001974 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001975 }
Nicolas Capens46485082014-04-15 13:12:50 -04001976 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
1977 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001978 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001979 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001980 }
Nicolas Capens46485082014-04-15 13:12:50 -04001981 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001982 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001983 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001984 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001985 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04001986 else if (name == "textureSize")
1987 {
1988 textureFunction.method = TextureFunction::SIZE;
1989 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001990 else if (name == "textureOffset")
1991 {
1992 textureFunction.method = TextureFunction::IMPLICIT;
1993 textureFunction.offset = true;
1994 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05001995 else if (name == "textureProjOffset")
1996 {
1997 textureFunction.method = TextureFunction::IMPLICIT;
1998 textureFunction.offset = true;
1999 textureFunction.proj = true;
2000 }
2001 else if (name == "textureLodOffset")
2002 {
2003 textureFunction.method = TextureFunction::LOD;
2004 textureFunction.offset = true;
2005 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002006 else if (name == "textureProjLodOffset")
2007 {
2008 textureFunction.method = TextureFunction::LOD;
2009 textureFunction.proj = true;
2010 textureFunction.offset = true;
2011 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002012 else if (name == "texelFetch")
2013 {
2014 textureFunction.method = TextureFunction::FETCH;
2015 }
2016 else if (name == "texelFetchOffset")
2017 {
2018 textureFunction.method = TextureFunction::FETCH;
2019 textureFunction.offset = true;
2020 }
Nicolas Capens46485082014-04-15 13:12:50 -04002021 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002022 {
2023 textureFunction.method = TextureFunction::GRAD;
2024 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002025 else if (name == "textureGradOffset")
2026 {
2027 textureFunction.method = TextureFunction::GRAD;
2028 textureFunction.offset = true;
2029 }
Nicolas Capens46485082014-04-15 13:12:50 -04002030 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002031 {
2032 textureFunction.method = TextureFunction::GRAD;
2033 textureFunction.proj = true;
2034 }
2035 else if (name == "textureProjGradOffset")
2036 {
2037 textureFunction.method = TextureFunction::GRAD;
2038 textureFunction.proj = true;
2039 textureFunction.offset = true;
2040 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002041 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002042
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002043 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002044 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002045 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2046
2047 if (textureFunction.offset)
2048 {
2049 mandatoryArgumentCount++;
2050 }
2051
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002052 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002053
Jamie Madill183bde52014-07-02 15:31:19 -04002054 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002055 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002056 if (bias)
2057 {
2058 textureFunction.method = TextureFunction::LOD0BIAS;
2059 }
2060 else
2061 {
2062 textureFunction.method = TextureFunction::LOD0;
2063 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002064 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002065 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002066 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002067 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002068 }
2069 }
2070
2071 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002072
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002073 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002075
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002076 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002077 {
2078 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2079 {
2080 out << "texture_";
2081 (*arg)->traverse(this);
2082 out << ", sampler_";
2083 }
2084
2085 (*arg)->traverse(this);
2086
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002087 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002088 {
2089 out << ", ";
2090 }
2091 }
2092
2093 out << ")";
2094
2095 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096 }
2097 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002098 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002099 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2100 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2101 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2102 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2103 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2104 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2105 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2106 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2107 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2108 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2109 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2110 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2111 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2112 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2113 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2114 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2115 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2116 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2117 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002118 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002119 {
Jamie Madill033dae62014-06-18 12:56:28 -04002120 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002121 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002122 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2123 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002124 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002125 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2126 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2127 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2128 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2129 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2130 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002131 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002132 ASSERT(node->getUseEmulatedFunction());
2133 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002134 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002135 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002137 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002138 ASSERT(node->getUseEmulatedFunction());
2139 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002140 break;
2141 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2142 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2143 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2144 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2145 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2146 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2147 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2148 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2149 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002150 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002151 ASSERT(node->getUseEmulatedFunction());
2152 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002153 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2155 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002156 case EOpOuterProduct:
2157 ASSERT(node->getUseEmulatedFunction());
2158 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2159 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002160 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161 default: UNREACHABLE();
2162 }
2163
2164 return true;
2165}
2166
2167bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2168{
Jamie Madill32aab012015-01-27 14:12:26 -05002169 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002171 if (node->usesTernaryOperator())
2172 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002173 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002174 }
2175 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002177 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002178
Corentin Wallez80bacde2014-11-10 12:07:37 -08002179 // D3D errors when there is a gradient operation in a loop in an unflattened if
2180 // however flattening all the ifs in branch heavy shaders made D3D error too.
2181 // As a temporary workaround we flatten the ifs only if there is at least a loop
2182 // present somewhere in the shader.
2183 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2184 {
2185 out << "FLATTEN ";
2186 }
2187
2188 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002189
2190 node->getCondition()->traverse(this);
2191
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002192 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002193
Jamie Madill075edd82013-07-08 13:30:19 -04002194 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002195 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002196
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002197 bool discard = false;
2198
daniel@transgaming.combb885322010-04-15 20:45:24 +00002199 if (node->getTrueBlock())
2200 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002201 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002202
2203 // Detect true discard
2204 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002205 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206
Jamie Madill075edd82013-07-08 13:30:19 -04002207 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002208 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002209
2210 if (node->getFalseBlock())
2211 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002212 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002213
Jamie Madill075edd82013-07-08 13:30:19 -04002214 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002215 out << "{\n";
2216
Jamie Madill075edd82013-07-08 13:30:19 -04002217 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002218 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002219
Jamie Madill075edd82013-07-08 13:30:19 -04002220 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002221 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002222
2223 // Detect false discard
2224 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2225 }
2226
2227 // ANGLE issue 486: Detect problematic conditional discard
2228 if (discard && FindSideEffectRewriting::search(node))
2229 {
2230 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002231 }
2232 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233
2234 return false;
2235}
2236
2237void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2238{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002239 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240}
2241
2242bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2243{
Nicolas Capens655fe362014-04-11 13:12:34 -04002244 mNestedLoopDepth++;
2245
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002246 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2247
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002248 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002249 {
2250 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2251 }
2252
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002253 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002254 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002255 if (handleExcessiveLoop(node))
2256 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002257 mInsideDiscontinuousLoop = wasDiscontinuous;
2258 mNestedLoopDepth--;
2259
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002260 return false;
2261 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002262 }
2263
Jamie Madill32aab012015-01-27 14:12:26 -05002264 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265
alokp@chromium.org52813552010-11-16 18:36:09 +00002266 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002268 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002269
Jamie Madill075edd82013-07-08 13:30:19 -04002270 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002271 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272 }
2273 else
2274 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002275 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002276
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002277 if (node->getInit())
2278 {
2279 node->getInit()->traverse(this);
2280 }
2281
2282 out << "; ";
2283
alokp@chromium.org52813552010-11-16 18:36:09 +00002284 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002286 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 }
2288
2289 out << "; ";
2290
alokp@chromium.org52813552010-11-16 18:36:09 +00002291 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002293 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 }
2295
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002296 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002297
Jamie Madill075edd82013-07-08 13:30:19 -04002298 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002299 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 }
2301
2302 if (node->getBody())
2303 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002304 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 }
2306
Jamie Madill075edd82013-07-08 13:30:19 -04002307 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002308 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002309
alokp@chromium.org52813552010-11-16 18:36:09 +00002310 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 {
Jamie Madill075edd82013-07-08 13:30:19 -04002312 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313 out << "while(\n";
2314
alokp@chromium.org52813552010-11-16 18:36:09 +00002315 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316
daniel@transgaming.com73536982012-03-21 20:45:49 +00002317 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318 }
2319
daniel@transgaming.com73536982012-03-21 20:45:49 +00002320 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002322 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002323 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002324
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325 return false;
2326}
2327
2328bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2329{
Jamie Madill32aab012015-01-27 14:12:26 -05002330 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331
2332 switch (node->getFlowOp())
2333 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002334 case EOpKill:
2335 outputTriplet(visit, "discard;\n", "", "");
2336 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002337 case EOpBreak:
2338 if (visit == PreVisit)
2339 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002340 if (mNestedLoopDepth > 1)
2341 {
2342 mUsesNestedBreak = true;
2343 }
2344
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002345 if (mExcessiveLoopIndex)
2346 {
2347 out << "{Break";
2348 mExcessiveLoopIndex->traverse(this);
2349 out << " = true; break;}\n";
2350 }
2351 else
2352 {
2353 out << "break;\n";
2354 }
2355 }
2356 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002357 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358 case EOpReturn:
2359 if (visit == PreVisit)
2360 {
2361 if (node->getExpression())
2362 {
2363 out << "return ";
2364 }
2365 else
2366 {
2367 out << "return;\n";
2368 }
2369 }
2370 else if (visit == PostVisit)
2371 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002372 if (node->getExpression())
2373 {
2374 out << ";\n";
2375 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 }
2377 break;
2378 default: UNREACHABLE();
2379 }
2380
2381 return true;
2382}
2383
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002384void OutputHLSL::traverseStatements(TIntermNode *node)
2385{
2386 if (isSingleStatement(node))
2387 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002388 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002389 }
2390
2391 node->traverse(this);
2392}
2393
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002394bool OutputHLSL::isSingleStatement(TIntermNode *node)
2395{
2396 TIntermAggregate *aggregate = node->getAsAggregate();
2397
2398 if (aggregate)
2399 {
2400 if (aggregate->getOp() == EOpSequence)
2401 {
2402 return false;
2403 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002404 else if (aggregate->getOp() == EOpDeclaration)
2405 {
2406 // Declaring multiple comma-separated variables must be considered multiple statements
2407 // because each individual declaration has side effects which are visible in the next.
2408 return false;
2409 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002410 else
2411 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002412 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002413 {
2414 if (!isSingleStatement(*sit))
2415 {
2416 return false;
2417 }
2418 }
2419
2420 return true;
2421 }
2422 }
2423
2424 return true;
2425}
2426
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002427// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2428// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002429bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2430{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002431 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002432 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002433
2434 // Parse loops of the form:
2435 // for(int index = initial; index [comparator] limit; index += increment)
2436 TIntermSymbol *index = NULL;
2437 TOperator comparator = EOpNull;
2438 int initial = 0;
2439 int limit = 0;
2440 int increment = 0;
2441
2442 // Parse index name and intial value
2443 if (node->getInit())
2444 {
2445 TIntermAggregate *init = node->getInit()->getAsAggregate();
2446
2447 if (init)
2448 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002449 TIntermSequence *sequence = init->getSequence();
2450 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002451
2452 if (variable && variable->getQualifier() == EvqTemporary)
2453 {
2454 TIntermBinary *assign = variable->getAsBinaryNode();
2455
2456 if (assign->getOp() == EOpInitialize)
2457 {
2458 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2459 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2460
2461 if (symbol && constant)
2462 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002463 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002464 {
2465 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002466 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002467 }
2468 }
2469 }
2470 }
2471 }
2472 }
2473
2474 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002475 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002476 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002477 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002478
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002479 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2480 {
2481 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2482
2483 if (constant)
2484 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002485 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002486 {
2487 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002488 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002489 }
2490 }
2491 }
2492 }
2493
2494 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002495 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002496 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002497 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2498 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002499
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002500 if (binaryTerminal)
2501 {
2502 TOperator op = binaryTerminal->getOp();
2503 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2504
2505 if (constant)
2506 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002507 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002508 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002509 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002510
2511 switch (op)
2512 {
2513 case EOpAddAssign: increment = value; break;
2514 case EOpSubAssign: increment = -value; break;
2515 default: UNIMPLEMENTED();
2516 }
2517 }
2518 }
2519 }
2520 else if (unaryTerminal)
2521 {
2522 TOperator op = unaryTerminal->getOp();
2523
2524 switch (op)
2525 {
2526 case EOpPostIncrement: increment = 1; break;
2527 case EOpPostDecrement: increment = -1; break;
2528 case EOpPreIncrement: increment = 1; break;
2529 case EOpPreDecrement: increment = -1; break;
2530 default: UNIMPLEMENTED();
2531 }
2532 }
2533 }
2534
2535 if (index != NULL && comparator != EOpNull && increment != 0)
2536 {
2537 if (comparator == EOpLessThanEqual)
2538 {
2539 comparator = EOpLessThan;
2540 limit += 1;
2541 }
2542
2543 if (comparator == EOpLessThan)
2544 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002545 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002546
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002547 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002548 {
2549 return false; // Not an excessive loop
2550 }
2551
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002552 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2553 mExcessiveLoopIndex = index;
2554
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002555 out << "{int ";
2556 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002557 out << ";\n"
2558 "bool Break";
2559 index->traverse(this);
2560 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002561
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002562 bool firstLoopFragment = true;
2563
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002564 while (iterations > 0)
2565 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002566 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002567
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002568 if (!firstLoopFragment)
2569 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002570 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002571 index->traverse(this);
2572 out << ") {\n";
2573 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002574
2575 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2576 {
2577 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2578 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002579
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002580 // for(int index = initial; index < clampedLimit; index += increment)
2581
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002582 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002583 index->traverse(this);
2584 out << " = ";
2585 out << initial;
2586
2587 out << "; ";
2588 index->traverse(this);
2589 out << " < ";
2590 out << clampedLimit;
2591
2592 out << "; ";
2593 index->traverse(this);
2594 out << " += ";
2595 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002596 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002597
Jamie Madill075edd82013-07-08 13:30:19 -04002598 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002599 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002600
2601 if (node->getBody())
2602 {
2603 node->getBody()->traverse(this);
2604 }
2605
Jamie Madill075edd82013-07-08 13:30:19 -04002606 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002607 out << ";}\n";
2608
2609 if (!firstLoopFragment)
2610 {
2611 out << "}\n";
2612 }
2613
2614 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002615
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002616 initial += MAX_LOOP_ITERATIONS * increment;
2617 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002618 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002619
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002620 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002621
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002622 mExcessiveLoopIndex = restoreIndex;
2623
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002624 return true;
2625 }
2626 else UNIMPLEMENTED();
2627 }
2628
2629 return false; // Not handled as an excessive loop
2630}
2631
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002632void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002633{
Jamie Madill32aab012015-01-27 14:12:26 -05002634 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002635
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002636 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002637 {
2638 out << preString;
2639 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002640 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641 {
2642 out << inString;
2643 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002644 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002645 {
2646 out << postString;
2647 }
2648}
2649
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002650void OutputHLSL::outputLineDirective(int line)
2651{
2652 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2653 {
Jamie Madill32aab012015-01-27 14:12:26 -05002654 TInfoSinkBase &out = getInfoSink();
2655
2656 out << "\n";
2657 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002658
2659 if (mContext.sourcePath)
2660 {
Jamie Madill32aab012015-01-27 14:12:26 -05002661 out << " \"" << mContext.sourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002662 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002663
Jamie Madill32aab012015-01-27 14:12:26 -05002664 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002665 }
2666}
2667
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002668TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2669{
2670 TQualifier qualifier = symbol->getQualifier();
2671 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002672 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002673
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002674 if (name.empty()) // HLSL demands named arguments, also for prototypes
2675 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002676 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002677 }
2678 else
2679 {
Jamie Madill033dae62014-06-18 12:56:28 -04002680 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002681 }
2682
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002683 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2684 {
Jamie Madill033dae62014-06-18 12:56:28 -04002685 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002686 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002687 }
2688
Jamie Madill033dae62014-06-18 12:56:28 -04002689 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690}
2691
2692TString OutputHLSL::initializer(const TType &type)
2693{
2694 TString string;
2695
Jamie Madill94bf7f22013-07-08 13:31:15 -04002696 size_t size = type.getObjectSize();
2697 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002698 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002699 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700
Jamie Madill94bf7f22013-07-08 13:31:15 -04002701 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002702 {
2703 string += ", ";
2704 }
2705 }
2706
daniel@transgaming.comead23042010-04-29 03:35:36 +00002707 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002708}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002709
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002710void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2711{
Jamie Madill32aab012015-01-27 14:12:26 -05002712 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002713
2714 if (visit == PreVisit)
2715 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002716 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002717
2718 out << name + "(";
2719 }
2720 else if (visit == InVisit)
2721 {
2722 out << ", ";
2723 }
2724 else if (visit == PostVisit)
2725 {
2726 out << ")";
2727 }
2728}
2729
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002730const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2731{
Jamie Madill32aab012015-01-27 14:12:26 -05002732 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002733
Jamie Madill98493dd2013-07-08 14:39:03 -04002734 const TStructure* structure = type.getStruct();
2735 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002736 {
Jamie Madill033dae62014-06-18 12:56:28 -04002737 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002738
Jamie Madill98493dd2013-07-08 14:39:03 -04002739 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002740
Jamie Madill98493dd2013-07-08 14:39:03 -04002741 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002742 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002743 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002744 constUnion = writeConstantUnion(*fieldType, constUnion);
2745
Jamie Madill98493dd2013-07-08 14:39:03 -04002746 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002747 {
2748 out << ", ";
2749 }
2750 }
2751
2752 out << ")";
2753 }
2754 else
2755 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002756 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002757 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002758
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002759 if (writeType)
2760 {
Jamie Madill033dae62014-06-18 12:56:28 -04002761 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002762 }
2763
Jamie Madill94bf7f22013-07-08 13:31:15 -04002764 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002765 {
2766 switch (constUnion->getType())
2767 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002768 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002769 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002770 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002771 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002772 default: UNREACHABLE();
2773 }
2774
2775 if (i != size - 1)
2776 {
2777 out << ", ";
2778 }
2779 }
2780
2781 if (writeType)
2782 {
2783 out << ")";
2784 }
2785 }
2786
2787 return constUnion;
2788}
2789
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002790void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2791{
2792 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2793 outputTriplet(visit, preString.c_str(), ", ", ")");
2794}
2795
Jamie Madill37997142015-01-28 10:06:34 -05002796bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2797{
2798 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2799 expression->traverse(&searchSymbol);
2800
2801 if (searchSymbol.foundMatch())
2802 {
2803 // Type already printed
2804 out << "t" + str(mUniqueIndex) + " = ";
2805 expression->traverse(this);
2806 out << ", ";
2807 symbolNode->traverse(this);
2808 out << " = t" + str(mUniqueIndex);
2809
2810 mUniqueIndex++;
2811 return true;
2812 }
2813
2814 return false;
2815}
2816
2817void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2818{
2819 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2820 << "\n"
2821 << "void initializeDeferredGlobals()\n"
2822 << "{\n";
2823
2824 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2825 {
2826 TIntermSymbol *symbol = deferredGlobal.first;
2827 TIntermTyped *expression = deferredGlobal.second;
2828 ASSERT(symbol);
2829 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2830
2831 out << " " << Decorate(symbol->getSymbol()) << " = ";
2832
2833 if (!writeSameSymbolInitializer(out, symbol, expression))
2834 {
2835 ASSERT(mInfoSinkStack.top() == &out);
2836 expression->traverse(this);
2837 }
2838
2839 out << ";\n";
2840 }
2841
2842 out << "}\n"
2843 << "\n";
2844}
2845
Jamie Madill55e79e02015-02-09 15:35:00 -05002846TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2847{
2848 const TFieldList &fields = structure.fields();
2849
2850 for (const auto &eqFunction : mStructEqualityFunctions)
2851 {
2852 if (eqFunction.structure == &structure)
2853 {
2854 return eqFunction.functionName;
2855 }
2856 }
2857
2858 const TString &structNameString = StructNameString(structure);
2859
2860 StructEqualityFunction function;
2861 function.structure = &structure;
2862 function.functionName = "angle_eq_" + structNameString;
2863
2864 TString &func = function.functionDefinition;
2865
2866 func = "bool " + function.functionName + "(" + structNameString + " a, " + structNameString + " b)\n" +
2867 "{\n"
2868 " return ";
2869
2870 for (size_t i = 0; i < fields.size(); i++)
2871 {
2872 const TField *field = fields[i];
2873 const TType *fieldType = field->type();
2874
2875 const TString &fieldNameA = "a." + Decorate(field->name());
2876 const TString &fieldNameB = "b." + Decorate(field->name());
2877
2878 if (i > 0)
2879 {
2880 func += " && ";
2881 }
2882
2883 if (fieldType->getBasicType() == EbtStruct)
2884 {
2885 const TStructure &fieldStruct = *fieldType->getStruct();
2886 const TString &functionName = addStructEqualityFunction(fieldStruct);
2887 func += functionName + "(" + fieldNameA + ", " + fieldNameB + ")";
2888 }
2889 else if (fieldType->isScalar())
2890 {
2891 func += "(" + fieldNameA + " == " + fieldNameB + ")";
2892 }
2893 else
2894 {
2895 ASSERT(fieldType->isMatrix() || fieldType->isVector());
2896 func += "all(" + fieldNameA + " == " + fieldNameB + ")";
2897 }
2898 }
2899
2900 func = func + ";\n" + "}\n";
2901
2902 mStructEqualityFunctions.push_back(function);
2903
2904 return function.functionName;
2905}
2906
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002907}