blob: 44d348a3309547d9805498b2497035403ea8342a [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 Madill3c9eeb92013-11-04 11:09:26 -0500329 if (mUsesDiscardRewriting)
330 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400331 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500332 }
333
Nicolas Capens655fe362014-04-11 13:12:34 -0400334 if (mUsesNestedBreak)
335 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400336 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400337 }
338
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400339 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
340 "#define LOOP [loop]\n"
341 "#define FLATTEN [flatten]\n"
342 "#else\n"
343 "#define LOOP\n"
344 "#define FLATTEN\n"
345 "#endif\n";
346
Jamie Madill183bde52014-07-02 15:31:19 -0400347 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000349 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000350 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000351
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000352 out << "// Varyings\n";
353 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400354 out << "\n";
355
356 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000357 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500358 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000359 {
Jamie Madill46131a32013-06-20 11:55:50 -0400360 const TString &variableName = outputVariableIt->first;
361 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400362
Jamie Madill033dae62014-06-18 12:56:28 -0400363 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400364 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000365 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000366 }
Jamie Madill46131a32013-06-20 11:55:50 -0400367 else
368 {
369 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
370
371 out << "static float4 gl_Color[" << numColorValues << "] =\n"
372 "{\n";
373 for (unsigned int i = 0; i < numColorValues; i++)
374 {
375 out << " float4(0, 0, 0, 0)";
376 if (i + 1 != numColorValues)
377 {
378 out << ",";
379 }
380 out << "\n";
381 }
382
383 out << "};\n";
384 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000385
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400386 if (mUsesFragDepth)
387 {
388 out << "static float gl_Depth = 0.0;\n";
389 }
390
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000391 if (mUsesFragCoord)
392 {
393 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
394 }
395
396 if (mUsesPointCoord)
397 {
398 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
399 }
400
401 if (mUsesFrontFacing)
402 {
403 out << "static bool gl_FrontFacing = false;\n";
404 }
405
406 out << "\n";
407
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000408 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000409 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000410 out << "struct gl_DepthRangeParameters\n"
411 "{\n"
412 " float near;\n"
413 " float far;\n"
414 " float diff;\n"
415 "};\n"
416 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000417 }
418
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000419 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000420 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000421 out << "cbuffer DriverConstants : register(b1)\n"
422 "{\n";
423
424 if (mUsesDepthRange)
425 {
426 out << " float3 dx_DepthRange : packoffset(c0);\n";
427 }
428
429 if (mUsesFragCoord)
430 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000431 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000432 }
433
434 if (mUsesFragCoord || mUsesFrontFacing)
435 {
436 out << " float3 dx_DepthFront : packoffset(c2);\n";
437 }
438
439 out << "};\n";
440 }
441 else
442 {
443 if (mUsesDepthRange)
444 {
445 out << "uniform float3 dx_DepthRange : register(c0);";
446 }
447
448 if (mUsesFragCoord)
449 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000450 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000451 }
452
453 if (mUsesFragCoord || mUsesFrontFacing)
454 {
455 out << "uniform float3 dx_DepthFront : register(c2);\n";
456 }
457 }
458
459 out << "\n";
460
461 if (mUsesDepthRange)
462 {
463 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
464 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000465 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000466
Jamie Madillf91ce812014-06-13 10:04:34 -0400467 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000468 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400469 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000470 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400471 out << flaggedStructs;
472 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000473 }
474
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000475 if (usingMRTExtension && mNumRenderTargets > 1)
476 {
477 out << "#define GL_USES_MRT\n";
478 }
479
480 if (mUsesFragColor)
481 {
482 out << "#define GL_USES_FRAG_COLOR\n";
483 }
484
485 if (mUsesFragData)
486 {
487 out << "#define GL_USES_FRAG_DATA\n";
488 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000490 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000492 out << "// Attributes\n";
493 out << attributes;
494 out << "\n"
495 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400496
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000497 if (mUsesPointSize)
498 {
499 out << "static float gl_PointSize = float(1);\n";
500 }
501
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000502 if (mUsesInstanceID)
503 {
504 out << "static int gl_InstanceID;";
505 }
506
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000507 out << "\n"
508 "// Varyings\n";
509 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000510 out << "\n";
511
512 if (mUsesDepthRange)
513 {
514 out << "struct gl_DepthRangeParameters\n"
515 "{\n"
516 " float near;\n"
517 " float far;\n"
518 " float diff;\n"
519 "};\n"
520 "\n";
521 }
522
523 if (mOutputType == SH_HLSL11_OUTPUT)
524 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800525 out << "cbuffer DriverConstants : register(b1)\n"
526 "{\n";
527
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000528 if (mUsesDepthRange)
529 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800530 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000531 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800532
Cooper Partine6664f02015-01-09 16:22:24 -0800533 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800534 // However, we declare it for all shaders (including Feature Level 10+).
535 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
536 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800537 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800538
539 out << "};\n"
540 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000541 }
542 else
543 {
544 if (mUsesDepthRange)
545 {
546 out << "uniform float3 dx_DepthRange : register(c0);\n";
547 }
548
Cooper Partine6664f02015-01-09 16:22:24 -0800549 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
550 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000551 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000552 }
553
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000554 if (mUsesDepthRange)
555 {
556 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
557 "\n";
558 }
559
Jamie Madillf91ce812014-06-13 10:04:34 -0400560 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000561 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400562 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000563 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400564 out << flaggedStructs;
565 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000566 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400567 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000568
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400569 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
570 {
571 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400572 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000573 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400574 switch(textureFunction->sampler)
575 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400576 case EbtSampler2D: out << "int2 "; break;
577 case EbtSampler3D: out << "int3 "; break;
578 case EbtSamplerCube: out << "int2 "; break;
579 case EbtSampler2DArray: out << "int3 "; break;
580 case EbtISampler2D: out << "int2 "; break;
581 case EbtISampler3D: out << "int3 "; break;
582 case EbtISamplerCube: out << "int2 "; break;
583 case EbtISampler2DArray: out << "int3 "; break;
584 case EbtUSampler2D: out << "int2 "; break;
585 case EbtUSampler3D: out << "int3 "; break;
586 case EbtUSamplerCube: out << "int2 "; break;
587 case EbtUSampler2DArray: out << "int3 "; break;
588 case EbtSampler2DShadow: out << "int2 "; break;
589 case EbtSamplerCubeShadow: out << "int2 "; break;
590 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400591 default: UNREACHABLE();
592 }
593 }
594 else // Sampling function
595 {
596 switch(textureFunction->sampler)
597 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400598 case EbtSampler2D: out << "float4 "; break;
599 case EbtSampler3D: out << "float4 "; break;
600 case EbtSamplerCube: out << "float4 "; break;
601 case EbtSampler2DArray: out << "float4 "; break;
602 case EbtISampler2D: out << "int4 "; break;
603 case EbtISampler3D: out << "int4 "; break;
604 case EbtISamplerCube: out << "int4 "; break;
605 case EbtISampler2DArray: out << "int4 "; break;
606 case EbtUSampler2D: out << "uint4 "; break;
607 case EbtUSampler3D: out << "uint4 "; break;
608 case EbtUSamplerCube: out << "uint4 "; break;
609 case EbtUSampler2DArray: out << "uint4 "; break;
610 case EbtSampler2DShadow: out << "float "; break;
611 case EbtSamplerCubeShadow: out << "float "; break;
612 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400613 default: UNREACHABLE();
614 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000615 }
616
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400617 // Function name
618 out << textureFunction->name();
619
620 // Argument list
621 int hlslCoords = 4;
622
623 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000624 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400625 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000626 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400627 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
628 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
629 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000630 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400631
Nicolas Capens75fb4752013-07-10 15:14:47 -0400632 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000633 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400634 case TextureFunction::IMPLICIT: break;
635 case TextureFunction::BIAS: hlslCoords = 4; break;
636 case TextureFunction::LOD: hlslCoords = 4; break;
637 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400638 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400639 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000640 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400641 }
642 else if (mOutputType == SH_HLSL11_OUTPUT)
643 {
644 switch(textureFunction->sampler)
645 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400646 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
647 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
648 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
649 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
650 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
651 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500652 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400653 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
654 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
655 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500656 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400657 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
658 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
659 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
660 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400661 default: UNREACHABLE();
662 }
663 }
664 else UNREACHABLE();
665
Nicolas Capensfc014542014-02-18 14:47:13 -0500666 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400667 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500668 switch(textureFunction->coords)
669 {
670 case 2: out << ", int2 t"; break;
671 case 3: out << ", int3 t"; break;
672 default: UNREACHABLE();
673 }
674 }
675 else // Floating-point coordinates (except textureSize)
676 {
677 switch(textureFunction->coords)
678 {
679 case 1: out << ", int lod"; break; // textureSize()
680 case 2: out << ", float2 t"; break;
681 case 3: out << ", float3 t"; break;
682 case 4: out << ", float4 t"; break;
683 default: UNREACHABLE();
684 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000685 }
686
Nicolas Capensd11d5492014-02-19 17:06:10 -0500687 if (textureFunction->method == TextureFunction::GRAD)
688 {
689 switch(textureFunction->sampler)
690 {
691 case EbtSampler2D:
692 case EbtISampler2D:
693 case EbtUSampler2D:
694 case EbtSampler2DArray:
695 case EbtISampler2DArray:
696 case EbtUSampler2DArray:
697 case EbtSampler2DShadow:
698 case EbtSampler2DArrayShadow:
699 out << ", float2 ddx, float2 ddy";
700 break;
701 case EbtSampler3D:
702 case EbtISampler3D:
703 case EbtUSampler3D:
704 case EbtSamplerCube:
705 case EbtISamplerCube:
706 case EbtUSamplerCube:
707 case EbtSamplerCubeShadow:
708 out << ", float3 ddx, float3 ddy";
709 break;
710 default: UNREACHABLE();
711 }
712 }
713
Nicolas Capens75fb4752013-07-10 15:14:47 -0400714 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000715 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400716 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400717 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400718 case TextureFunction::LOD: out << ", float lod"; break;
719 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400720 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400721 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500722 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500723 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400724 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000725 }
726
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500727 if (textureFunction->offset)
728 {
729 switch(textureFunction->sampler)
730 {
731 case EbtSampler2D: out << ", int2 offset"; break;
732 case EbtSampler3D: out << ", int3 offset"; break;
733 case EbtSampler2DArray: out << ", int2 offset"; break;
734 case EbtISampler2D: out << ", int2 offset"; break;
735 case EbtISampler3D: out << ", int3 offset"; break;
736 case EbtISampler2DArray: out << ", int2 offset"; break;
737 case EbtUSampler2D: out << ", int2 offset"; break;
738 case EbtUSampler3D: out << ", int3 offset"; break;
739 case EbtUSampler2DArray: out << ", int2 offset"; break;
740 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500741 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500742 default: UNREACHABLE();
743 }
744 }
745
Nicolas Capens84cfa122014-04-14 13:48:45 -0400746 if (textureFunction->method == TextureFunction::BIAS ||
747 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500748 {
749 out << ", float bias";
750 }
751
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400752 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400753 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400754
Nicolas Capens75fb4752013-07-10 15:14:47 -0400755 if (textureFunction->method == TextureFunction::SIZE)
756 {
757 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
758 {
759 if (IsSamplerArray(textureFunction->sampler))
760 {
761 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
762 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
763 }
764 else
765 {
766 out << " uint width; uint height; uint numberOfLevels;\n"
767 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
768 }
769 }
770 else if (IsSampler3D(textureFunction->sampler))
771 {
772 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
773 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
774 }
775 else UNREACHABLE();
776
777 switch(textureFunction->sampler)
778 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400779 case EbtSampler2D: out << " return int2(width, height);"; break;
780 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
781 case EbtSamplerCube: out << " return int2(width, height);"; break;
782 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
783 case EbtISampler2D: out << " return int2(width, height);"; break;
784 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
785 case EbtISamplerCube: out << " return int2(width, height);"; break;
786 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
787 case EbtUSampler2D: out << " return int2(width, height);"; break;
788 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
789 case EbtUSamplerCube: out << " return int2(width, height);"; break;
790 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
791 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
792 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
793 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400794 default: UNREACHABLE();
795 }
796 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400797 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400798 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500799 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
800 {
801 out << " float width; float height; float layers; float levels;\n";
802
803 out << " uint mip = 0;\n";
804
805 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
806
807 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
808 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
809 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
810 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
811
812 // FACE_POSITIVE_X = 000b
813 // FACE_NEGATIVE_X = 001b
814 // FACE_POSITIVE_Y = 010b
815 // FACE_NEGATIVE_Y = 011b
816 // FACE_POSITIVE_Z = 100b
817 // FACE_NEGATIVE_Z = 101b
818 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
819
820 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
821 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
822 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
823
824 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
825 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
826 }
827 else if (IsIntegerSampler(textureFunction->sampler) &&
828 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400829 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400830 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400831 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400832 if (IsSamplerArray(textureFunction->sampler))
833 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400834 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400835
Nicolas Capens9edebd62013-08-06 10:59:10 -0400836 if (textureFunction->method == TextureFunction::LOD0)
837 {
838 out << " uint mip = 0;\n";
839 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400840 else if (textureFunction->method == TextureFunction::LOD0BIAS)
841 {
842 out << " uint mip = bias;\n";
843 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400844 else
845 {
846 if (textureFunction->method == TextureFunction::IMPLICIT ||
847 textureFunction->method == TextureFunction::BIAS)
848 {
849 out << " x.GetDimensions(0, width, height, layers, levels);\n"
850 " float2 tSized = float2(t.x * width, t.y * height);\n"
851 " float dx = length(ddx(tSized));\n"
852 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500853 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400854
855 if (textureFunction->method == TextureFunction::BIAS)
856 {
857 out << " lod += bias;\n";
858 }
859 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500860 else if (textureFunction->method == TextureFunction::GRAD)
861 {
862 out << " x.GetDimensions(0, width, height, layers, levels);\n"
863 " float lod = log2(max(length(ddx), length(ddy)));\n";
864 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400865
866 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
867 }
868
869 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400870 }
871 else
872 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400873 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400874
Nicolas Capens9edebd62013-08-06 10:59:10 -0400875 if (textureFunction->method == TextureFunction::LOD0)
876 {
877 out << " uint mip = 0;\n";
878 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400879 else if (textureFunction->method == TextureFunction::LOD0BIAS)
880 {
881 out << " uint mip = bias;\n";
882 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400883 else
884 {
885 if (textureFunction->method == TextureFunction::IMPLICIT ||
886 textureFunction->method == TextureFunction::BIAS)
887 {
888 out << " x.GetDimensions(0, width, height, levels);\n"
889 " float2 tSized = float2(t.x * width, t.y * height);\n"
890 " float dx = length(ddx(tSized));\n"
891 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500892 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400893
894 if (textureFunction->method == TextureFunction::BIAS)
895 {
896 out << " lod += bias;\n";
897 }
898 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500899 else if (textureFunction->method == TextureFunction::LOD)
900 {
901 out << " x.GetDimensions(0, width, height, levels);\n";
902 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500903 else if (textureFunction->method == TextureFunction::GRAD)
904 {
905 out << " x.GetDimensions(0, width, height, levels);\n"
906 " float lod = log2(max(length(ddx), length(ddy)));\n";
907 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400908
909 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
910 }
911
912 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400913 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400914 }
915 else if (IsSampler3D(textureFunction->sampler))
916 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400917 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400918
Nicolas Capens9edebd62013-08-06 10:59:10 -0400919 if (textureFunction->method == TextureFunction::LOD0)
920 {
921 out << " uint mip = 0;\n";
922 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400923 else if (textureFunction->method == TextureFunction::LOD0BIAS)
924 {
925 out << " uint mip = bias;\n";
926 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400927 else
928 {
929 if (textureFunction->method == TextureFunction::IMPLICIT ||
930 textureFunction->method == TextureFunction::BIAS)
931 {
932 out << " x.GetDimensions(0, width, height, depth, levels);\n"
933 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
934 " float dx = length(ddx(tSized));\n"
935 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500936 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400937
938 if (textureFunction->method == TextureFunction::BIAS)
939 {
940 out << " lod += bias;\n";
941 }
942 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500943 else if (textureFunction->method == TextureFunction::GRAD)
944 {
945 out << " x.GetDimensions(0, width, height, depth, levels);\n"
946 " float lod = log2(max(length(ddx), length(ddy)));\n";
947 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400948
949 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
950 }
951
952 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400953 }
954 else UNREACHABLE();
955 }
956
957 out << " return ";
958
959 // HLSL intrinsic
960 if (mOutputType == SH_HLSL9_OUTPUT)
961 {
962 switch(textureFunction->sampler)
963 {
964 case EbtSampler2D: out << "tex2D"; break;
965 case EbtSamplerCube: out << "texCUBE"; break;
966 default: UNREACHABLE();
967 }
968
Nicolas Capens75fb4752013-07-10 15:14:47 -0400969 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400970 {
971 case TextureFunction::IMPLICIT: out << "(s, "; break;
972 case TextureFunction::BIAS: out << "bias(s, "; break;
973 case TextureFunction::LOD: out << "lod(s, "; break;
974 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400975 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400976 default: UNREACHABLE();
977 }
978 }
979 else if (mOutputType == SH_HLSL11_OUTPUT)
980 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500981 if (textureFunction->method == TextureFunction::GRAD)
982 {
983 if (IsIntegerSampler(textureFunction->sampler))
984 {
985 out << "x.Load(";
986 }
987 else if (IsShadowSampler(textureFunction->sampler))
988 {
989 out << "x.SampleCmpLevelZero(s, ";
990 }
991 else
992 {
993 out << "x.SampleGrad(s, ";
994 }
995 }
996 else if (IsIntegerSampler(textureFunction->sampler) ||
997 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400998 {
999 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001000 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001001 else if (IsShadowSampler(textureFunction->sampler))
1002 {
1003 out << "x.SampleCmp(s, ";
1004 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001005 else
1006 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001007 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001008 {
1009 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1010 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1011 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1012 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001013 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001014 default: UNREACHABLE();
1015 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001016 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001017 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001018 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001019
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001020 // Integer sampling requires integer addresses
1021 TString addressx = "";
1022 TString addressy = "";
1023 TString addressz = "";
1024 TString close = "";
1025
Nicolas Capensfc014542014-02-18 14:47:13 -05001026 if (IsIntegerSampler(textureFunction->sampler) ||
1027 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001028 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001029 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001030 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001031 case 2: out << "int3("; break;
1032 case 3: out << "int4("; break;
1033 default: UNREACHABLE();
1034 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001035
Nicolas Capensfc014542014-02-18 14:47:13 -05001036 // Convert from normalized floating-point to integer
1037 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001038 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001039 addressx = "int(floor(width * frac((";
1040 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001041
Nicolas Capensfc014542014-02-18 14:47:13 -05001042 if (IsSamplerArray(textureFunction->sampler))
1043 {
1044 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1045 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001046 else if (IsSamplerCube(textureFunction->sampler))
1047 {
1048 addressz = "((((";
1049 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001050 else
1051 {
1052 addressz = "int(floor(depth * frac((";
1053 }
1054
1055 close = "))))";
1056 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001057 }
1058 else
1059 {
1060 switch(hlslCoords)
1061 {
1062 case 2: out << "float2("; break;
1063 case 3: out << "float3("; break;
1064 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001065 default: UNREACHABLE();
1066 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001067 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001068
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001069 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001070
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001071 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001072 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001073 switch(textureFunction->coords)
1074 {
1075 case 3: proj = " / t.z"; break;
1076 case 4: proj = " / t.w"; break;
1077 default: UNREACHABLE();
1078 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001079 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001080
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001081 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001082
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001083 if (mOutputType == SH_HLSL9_OUTPUT)
1084 {
1085 if (hlslCoords >= 3)
1086 {
1087 if (textureFunction->coords < 3)
1088 {
1089 out << ", 0";
1090 }
1091 else
1092 {
1093 out << ", t.z" + proj;
1094 }
1095 }
1096
1097 if (hlslCoords == 4)
1098 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001099 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001100 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001101 case TextureFunction::BIAS: out << ", bias"; break;
1102 case TextureFunction::LOD: out << ", lod"; break;
1103 case TextureFunction::LOD0: out << ", 0"; break;
1104 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001105 default: UNREACHABLE();
1106 }
1107 }
1108
1109 out << "));\n";
1110 }
1111 else if (mOutputType == SH_HLSL11_OUTPUT)
1112 {
1113 if (hlslCoords >= 3)
1114 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001115 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1116 {
1117 out << ", face";
1118 }
1119 else
1120 {
1121 out << ", " + addressz + ("t.z" + proj) + close;
1122 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001123 }
1124
Nicolas Capensd11d5492014-02-19 17:06:10 -05001125 if (textureFunction->method == TextureFunction::GRAD)
1126 {
1127 if (IsIntegerSampler(textureFunction->sampler))
1128 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001129 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001130 }
1131 else if (IsShadowSampler(textureFunction->sampler))
1132 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001133 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001134 switch(textureFunction->coords)
1135 {
1136 case 3: out << "), t.z"; break;
1137 case 4: out << "), t.w"; break;
1138 default: UNREACHABLE();
1139 }
1140 }
1141 else
1142 {
1143 out << "), ddx, ddy";
1144 }
1145 }
1146 else if (IsIntegerSampler(textureFunction->sampler) ||
1147 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001148 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001149 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001150 }
1151 else if (IsShadowSampler(textureFunction->sampler))
1152 {
1153 // Compare value
1154 switch(textureFunction->coords)
1155 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001156 case 3: out << "), t.z"; break;
1157 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001158 default: UNREACHABLE();
1159 }
1160 }
1161 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001162 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001163 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001164 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001165 case TextureFunction::IMPLICIT: out << ")"; break;
1166 case TextureFunction::BIAS: out << "), bias"; break;
1167 case TextureFunction::LOD: out << "), lod"; break;
1168 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001169 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001170 default: UNREACHABLE();
1171 }
1172 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001173
1174 if (textureFunction->offset)
1175 {
1176 out << ", offset";
1177 }
1178
1179 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001180 }
1181 else UNREACHABLE();
1182 }
1183
1184 out << "\n"
1185 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001186 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001187 }
1188
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001189 if (mUsesFragCoord)
1190 {
1191 out << "#define GL_USES_FRAG_COORD\n";
1192 }
1193
1194 if (mUsesPointCoord)
1195 {
1196 out << "#define GL_USES_POINT_COORD\n";
1197 }
1198
1199 if (mUsesFrontFacing)
1200 {
1201 out << "#define GL_USES_FRONT_FACING\n";
1202 }
1203
1204 if (mUsesPointSize)
1205 {
1206 out << "#define GL_USES_POINT_SIZE\n";
1207 }
1208
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001209 if (mUsesFragDepth)
1210 {
1211 out << "#define GL_USES_FRAG_DEPTH\n";
1212 }
1213
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001214 if (mUsesDepthRange)
1215 {
1216 out << "#define GL_USES_DEPTH_RANGE\n";
1217 }
1218
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001219 if (mUsesXor)
1220 {
1221 out << "bool xor(bool p, bool q)\n"
1222 "{\n"
1223 " return (p || q) && !(p && q);\n"
1224 "}\n"
1225 "\n";
1226 }
1227
Olli Etuaho80a5a6c2015-01-12 15:35:27 +02001228 builtInFunctionEmulator->OutputEmulatedFunctionDefinition(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229}
1230
1231void OutputHLSL::visitSymbol(TIntermSymbol *node)
1232{
Jamie Madill32aab012015-01-27 14:12:26 -05001233 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001234
Jamie Madill570e04d2013-06-21 09:15:33 -04001235 // Handle accessing std140 structs by value
1236 if (mFlaggedStructMappedNames.count(node) > 0)
1237 {
1238 out << mFlaggedStructMappedNames[node];
1239 return;
1240 }
1241
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001242 TString name = node->getSymbol();
1243
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001244 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001245 {
1246 mUsesDepthRange = true;
1247 out << name;
1248 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001249 else
1250 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001251 TQualifier qualifier = node->getQualifier();
1252
1253 if (qualifier == EvqUniform)
1254 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001255 const TType& nodeType = node->getType();
1256 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1257
1258 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001259 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001260 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001261 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001262 else
1263 {
1264 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001265 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001266
Jamie Madill033dae62014-06-18 12:56:28 -04001267 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001268 }
Jamie Madill19571812013-08-12 15:26:34 -07001269 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001270 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001271 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001272 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001273 }
Jamie Madill033dae62014-06-18 12:56:28 -04001274 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001275 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001276 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001277 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001278 }
Jamie Madill19571812013-08-12 15:26:34 -07001279 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001280 {
1281 mReferencedOutputVariables[name] = node;
1282 out << "out_" << name;
1283 }
1284 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001285 {
1286 out << "gl_Color[0]";
1287 mUsesFragColor = true;
1288 }
1289 else if (qualifier == EvqFragData)
1290 {
1291 out << "gl_Color";
1292 mUsesFragData = true;
1293 }
1294 else if (qualifier == EvqFragCoord)
1295 {
1296 mUsesFragCoord = true;
1297 out << name;
1298 }
1299 else if (qualifier == EvqPointCoord)
1300 {
1301 mUsesPointCoord = true;
1302 out << name;
1303 }
1304 else if (qualifier == EvqFrontFacing)
1305 {
1306 mUsesFrontFacing = true;
1307 out << name;
1308 }
1309 else if (qualifier == EvqPointSize)
1310 {
1311 mUsesPointSize = true;
1312 out << name;
1313 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001314 else if (qualifier == EvqInstanceID)
1315 {
1316 mUsesInstanceID = true;
1317 out << name;
1318 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001319 else if (name == "gl_FragDepthEXT")
1320 {
1321 mUsesFragDepth = true;
1322 out << "gl_Depth";
1323 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001324 else if (qualifier == EvqInternal)
1325 {
1326 out << name;
1327 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001328 else
1329 {
Jamie Madill033dae62014-06-18 12:56:28 -04001330 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001331 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001332 }
1333}
1334
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001335void OutputHLSL::visitRaw(TIntermRaw *node)
1336{
Jamie Madill32aab012015-01-27 14:12:26 -05001337 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001338}
1339
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001340bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1341{
Jamie Madill32aab012015-01-27 14:12:26 -05001342 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001343
Jamie Madill570e04d2013-06-21 09:15:33 -04001344 // Handle accessing std140 structs by value
1345 if (mFlaggedStructMappedNames.count(node) > 0)
1346 {
1347 out << mFlaggedStructMappedNames[node];
1348 return false;
1349 }
1350
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001351 switch (node->getOp())
1352 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001353 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001354 case EOpInitialize:
1355 if (visit == PreVisit)
1356 {
1357 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1358 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1359 // new variable is created before the assignment is evaluated), so we need to convert
1360 // this to "float t = x, x = t;".
1361
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001362 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001363 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001364 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001365
Jamie Madill37997142015-01-28 10:06:34 -05001366 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1367 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001368 {
Jamie Madill37997142015-01-28 10:06:34 -05001369 // For variables which are not constant, defer their real initialization until
1370 // after we initialize other globals: uniforms, attributes and varyings.
1371 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1372 const TString &initString = initializer(node->getType());
1373 node->setRight(new TIntermRaw(node->getType(), initString));
1374 }
1375 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1376 {
1377 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001378 return false;
1379 }
1380 }
1381 else if (visit == InVisit)
1382 {
1383 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001384 }
1385 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001386 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1387 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1388 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1389 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1390 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1391 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001392 if (visit == PreVisit)
1393 {
1394 out << "(";
1395 }
1396 else if (visit == InVisit)
1397 {
1398 out << " = mul(";
1399 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001400 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001401 }
1402 else
1403 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001404 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001405 }
1406 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001407 case EOpMatrixTimesMatrixAssign:
1408 if (visit == PreVisit)
1409 {
1410 out << "(";
1411 }
1412 else if (visit == InVisit)
1413 {
1414 out << " = mul(";
1415 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001416 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001417 }
1418 else
1419 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001420 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001421 }
1422 break;
1423 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001424 case EOpModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001425 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1426 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1427 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1428 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1429 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001430 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001431 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001432 const TType& leftType = node->getLeft()->getType();
1433 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001434 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001435 if (visit == PreVisit)
1436 {
1437 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1438 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001439 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001440 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001441 return false;
1442 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001443 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001444 else
1445 {
1446 outputTriplet(visit, "", "[", "]");
1447 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001448 }
1449 break;
1450 case EOpIndexIndirect:
1451 // We do not currently support indirect references to interface blocks
1452 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1453 outputTriplet(visit, "", "[", "]");
1454 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001455 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001456 if (visit == InVisit)
1457 {
1458 const TStructure* structure = node->getLeft()->getType().getStruct();
1459 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1460 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001461 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001462
1463 return false;
1464 }
1465 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001466 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001467 if (visit == InVisit)
1468 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001469 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1470 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1471 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001472 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001473
1474 return false;
1475 }
1476 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001477 case EOpVectorSwizzle:
1478 if (visit == InVisit)
1479 {
1480 out << ".";
1481
1482 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1483
1484 if (swizzle)
1485 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001486 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001487
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001488 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001489 {
1490 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1491
1492 if (element)
1493 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001494 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001495
1496 switch (i)
1497 {
1498 case 0: out << "x"; break;
1499 case 1: out << "y"; break;
1500 case 2: out << "z"; break;
1501 case 3: out << "w"; break;
1502 default: UNREACHABLE();
1503 }
1504 }
1505 else UNREACHABLE();
1506 }
1507 }
1508 else UNREACHABLE();
1509
1510 return false; // Fully processed
1511 }
1512 break;
1513 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1514 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1515 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1516 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001517 case EOpMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001518 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1519 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1520 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1521 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1522 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001523 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001524 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001525 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001526 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001527 if (node->getOp() == EOpEqual)
1528 {
1529 outputTriplet(visit, "(", " == ", ")");
1530 }
1531 else
1532 {
1533 outputTriplet(visit, "(", " != ", ")");
1534 }
1535 }
1536 else if (node->getLeft()->getBasicType() == EbtStruct)
1537 {
1538 if (node->getOp() == EOpEqual)
1539 {
1540 out << "(";
1541 }
1542 else
1543 {
1544 out << "!(";
1545 }
1546
Jamie Madill98493dd2013-07-08 14:39:03 -04001547 const TStructure &structure = *node->getLeft()->getType().getStruct();
1548 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001549
Jamie Madill98493dd2013-07-08 14:39:03 -04001550 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001551 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001552 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001553
1554 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001555 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001556 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001557 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001558
Jamie Madill98493dd2013-07-08 14:39:03 -04001559 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001560 {
1561 out << " && ";
1562 }
1563 }
1564
1565 out << ")";
1566
1567 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001568 }
1569 else
1570 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001571 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001572
1573 if (node->getOp() == EOpEqual)
1574 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001575 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001576 }
1577 else
1578 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001579 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001580 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001581 }
1582 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001583 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1584 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1585 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1586 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1587 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001588 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001589 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1590 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001591 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001592 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001593 if (node->getRight()->hasSideEffects())
1594 {
1595 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1596 return false;
1597 }
1598 else
1599 {
1600 outputTriplet(visit, "(", " || ", ")");
1601 return true;
1602 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001603 case EOpLogicalXor:
1604 mUsesXor = true;
1605 outputTriplet(visit, "xor(", ", ", ")");
1606 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001607 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001608 if (node->getRight()->hasSideEffects())
1609 {
1610 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1611 return false;
1612 }
1613 else
1614 {
1615 outputTriplet(visit, "(", " && ", ")");
1616 return true;
1617 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618 default: UNREACHABLE();
1619 }
1620
1621 return true;
1622}
1623
1624bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1625{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626 switch (node->getOp())
1627 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001628 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001629 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001630 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1631 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001632 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001633 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1634 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1635 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1636 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001637 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1638 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1639 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1640 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1641 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1642 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1643 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1644 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001645 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1646 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1647 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1648 case EOpAsinh:
1649 ASSERT(node->getUseEmulatedFunction());
1650 writeEmulatedFunctionTriplet(visit, "asinh(");
1651 break;
1652 case EOpAcosh:
1653 ASSERT(node->getUseEmulatedFunction());
1654 writeEmulatedFunctionTriplet(visit, "acosh(");
1655 break;
1656 case EOpAtanh:
1657 ASSERT(node->getUseEmulatedFunction());
1658 writeEmulatedFunctionTriplet(visit, "atanh(");
1659 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001660 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1661 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1662 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1663 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1664 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1665 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1666 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1667 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1668 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1669 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1670 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001671 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1672 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1673 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1674 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001675 case EOpPackSnorm2x16:
1676 ASSERT(node->getUseEmulatedFunction());
1677 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1678 break;
1679 case EOpPackUnorm2x16:
1680 ASSERT(node->getUseEmulatedFunction());
1681 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1682 break;
1683 case EOpPackHalf2x16:
1684 ASSERT(node->getUseEmulatedFunction());
1685 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1686 break;
1687 case EOpUnpackSnorm2x16:
1688 ASSERT(node->getUseEmulatedFunction());
1689 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1690 break;
1691 case EOpUnpackUnorm2x16:
1692 ASSERT(node->getUseEmulatedFunction());
1693 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1694 break;
1695 case EOpUnpackHalf2x16:
1696 ASSERT(node->getUseEmulatedFunction());
1697 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1698 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001699 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1700 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001701 case EOpDFdx:
1702 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1703 {
1704 outputTriplet(visit, "(", "", ", 0.0)");
1705 }
1706 else
1707 {
1708 outputTriplet(visit, "ddx(", "", ")");
1709 }
1710 break;
1711 case EOpDFdy:
1712 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1713 {
1714 outputTriplet(visit, "(", "", ", 0.0)");
1715 }
1716 else
1717 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001718 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001719 }
1720 break;
1721 case EOpFwidth:
1722 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1723 {
1724 outputTriplet(visit, "(", "", ", 0.0)");
1725 }
1726 else
1727 {
1728 outputTriplet(visit, "fwidth(", "", ")");
1729 }
1730 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001731 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1732 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001733 case EOpInverse:
1734 ASSERT(node->getUseEmulatedFunction());
1735 writeEmulatedFunctionTriplet(visit, "inverse(");
1736 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001737
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001738 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1739 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740 default: UNREACHABLE();
1741 }
1742
1743 return true;
1744}
1745
1746bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1747{
Jamie Madill32aab012015-01-27 14:12:26 -05001748 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 switch (node->getOp())
1751 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001752 case EOpSequence:
1753 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001754 if (mInsideFunction)
1755 {
Jamie Madill075edd82013-07-08 13:30:19 -04001756 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001757 out << "{\n";
1758 }
1759
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001760 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001761 {
Jamie Madill075edd82013-07-08 13:30:19 -04001762 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001763
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001764 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001765
1766 out << ";\n";
1767 }
1768
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001769 if (mInsideFunction)
1770 {
Jamie Madill075edd82013-07-08 13:30:19 -04001771 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001772 out << "}\n";
1773 }
1774
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001775 return false;
1776 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001777 case EOpDeclaration:
1778 if (visit == PreVisit)
1779 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001780 TIntermSequence *sequence = node->getSequence();
1781 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001782
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001783 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001785 TStructure *structure = variable->getType().getStruct();
1786
1787 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001788 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001789 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001790 }
1791
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001792 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001793 {
Jamie Madill37997142015-01-28 10:06:34 -05001794 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795 {
Jamie Madill37997142015-01-28 10:06:34 -05001796 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001797 {
Jamie Madill37997142015-01-28 10:06:34 -05001798 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001799 }
1800
Nicolas Capensd974db42014-10-07 10:50:19 -04001801 if (!mInsideFunction)
1802 {
1803 out << "static ";
1804 }
1805
1806 out << TypeString(variable->getType()) + " ";
1807
Jamie Madill37997142015-01-28 10:06:34 -05001808 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001809
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001810 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001812 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001813 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001814 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001815 }
1816 else
1817 {
Jamie Madill37997142015-01-28 10:06:34 -05001818 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001819 }
1820
Jamie Madill37997142015-01-28 10:06:34 -05001821 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001822 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001823 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824 }
1825 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001826 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001827 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1828 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001829 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001830 }
1831 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832 }
Jamie Madill033dae62014-06-18 12:56:28 -04001833 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001834 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001835 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001836 {
1837 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1838
1839 if (symbol)
1840 {
1841 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1842 mReferencedVaryings[symbol->getSymbol()] = symbol;
1843 }
1844 else
1845 {
1846 (*sit)->traverse(this);
1847 }
1848 }
1849 }
1850
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001851 return false;
1852 }
1853 else if (visit == InVisit)
1854 {
1855 out << ", ";
1856 }
1857 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001858 case EOpInvariantDeclaration:
1859 // Do not do any translation
1860 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001861 case EOpPrototype:
1862 if (visit == PreVisit)
1863 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001864 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001865
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001866 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001867
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001868 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001869 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001870 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001871
1872 if (symbol)
1873 {
1874 out << argumentString(symbol);
1875
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001876 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001877 {
1878 out << ", ";
1879 }
1880 }
1881 else UNREACHABLE();
1882 }
1883
1884 out << ");\n";
1885
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001886 // Also prototype the Lod0 variant if needed
1887 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1888 {
1889 mOutputLod0Function = true;
1890 node->traverse(this);
1891 mOutputLod0Function = false;
1892 }
1893
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001894 return false;
1895 }
1896 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001897 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 case EOpFunction:
1899 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001900 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001901
Jamie Madill033dae62014-06-18 12:56:28 -04001902 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001903
1904 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001906 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001908 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909 {
Jamie Madill033dae62014-06-18 12:56:28 -04001910 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001911 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001912
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001913 TIntermSequence *sequence = node->getSequence();
1914 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001915
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001916 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001917 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001918 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001919
1920 if (symbol)
1921 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001922 TStructure *structure = symbol->getType().getStruct();
1923
1924 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001925 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001926 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001927 }
1928
1929 out << argumentString(symbol);
1930
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001931 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001932 {
1933 out << ", ";
1934 }
1935 }
1936 else UNREACHABLE();
1937 }
1938
1939 out << ")\n"
1940 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001941
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001942 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001943 {
1944 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001945 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001946 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001948
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001949 out << "}\n";
1950
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001951 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1952 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001953 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001954 {
1955 mOutputLod0Function = true;
1956 node->traverse(this);
1957 mOutputLod0Function = false;
1958 }
1959 }
1960
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001961 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962 }
1963 break;
1964 case EOpFunctionCall:
1965 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001966 TString name = TFunction::unmangleName(node->getName());
1967 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001968 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001969
1970 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971 {
Jamie Madill033dae62014-06-18 12:56:28 -04001972 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973 }
1974 else
1975 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001976 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001977
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001978 TextureFunction textureFunction;
1979 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001980 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04001981 textureFunction.method = TextureFunction::IMPLICIT;
1982 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001983 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001984
1985 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001986 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001987 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001988 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001989 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001990 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001991 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001992 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001993 }
Nicolas Capens46485082014-04-15 13:12:50 -04001994 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
1995 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001996 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001997 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001998 }
Nicolas Capens46485082014-04-15 13:12:50 -04001999 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002000 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002001 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002002 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002003 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002004 else if (name == "textureSize")
2005 {
2006 textureFunction.method = TextureFunction::SIZE;
2007 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002008 else if (name == "textureOffset")
2009 {
2010 textureFunction.method = TextureFunction::IMPLICIT;
2011 textureFunction.offset = true;
2012 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002013 else if (name == "textureProjOffset")
2014 {
2015 textureFunction.method = TextureFunction::IMPLICIT;
2016 textureFunction.offset = true;
2017 textureFunction.proj = true;
2018 }
2019 else if (name == "textureLodOffset")
2020 {
2021 textureFunction.method = TextureFunction::LOD;
2022 textureFunction.offset = true;
2023 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002024 else if (name == "textureProjLodOffset")
2025 {
2026 textureFunction.method = TextureFunction::LOD;
2027 textureFunction.proj = true;
2028 textureFunction.offset = true;
2029 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002030 else if (name == "texelFetch")
2031 {
2032 textureFunction.method = TextureFunction::FETCH;
2033 }
2034 else if (name == "texelFetchOffset")
2035 {
2036 textureFunction.method = TextureFunction::FETCH;
2037 textureFunction.offset = true;
2038 }
Nicolas Capens46485082014-04-15 13:12:50 -04002039 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002040 {
2041 textureFunction.method = TextureFunction::GRAD;
2042 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002043 else if (name == "textureGradOffset")
2044 {
2045 textureFunction.method = TextureFunction::GRAD;
2046 textureFunction.offset = true;
2047 }
Nicolas Capens46485082014-04-15 13:12:50 -04002048 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002049 {
2050 textureFunction.method = TextureFunction::GRAD;
2051 textureFunction.proj = true;
2052 }
2053 else if (name == "textureProjGradOffset")
2054 {
2055 textureFunction.method = TextureFunction::GRAD;
2056 textureFunction.proj = true;
2057 textureFunction.offset = true;
2058 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002059 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002060
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002061 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002062 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002063 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2064
2065 if (textureFunction.offset)
2066 {
2067 mandatoryArgumentCount++;
2068 }
2069
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002070 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002071
Jamie Madill183bde52014-07-02 15:31:19 -04002072 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002073 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002074 if (bias)
2075 {
2076 textureFunction.method = TextureFunction::LOD0BIAS;
2077 }
2078 else
2079 {
2080 textureFunction.method = TextureFunction::LOD0;
2081 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002082 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002083 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002084 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002085 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002086 }
2087 }
2088
2089 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002090
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002091 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002093
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002094 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002095 {
2096 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2097 {
2098 out << "texture_";
2099 (*arg)->traverse(this);
2100 out << ", sampler_";
2101 }
2102
2103 (*arg)->traverse(this);
2104
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002105 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002106 {
2107 out << ", ";
2108 }
2109 }
2110
2111 out << ")";
2112
2113 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114 }
2115 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002116 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002117 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2118 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2119 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2120 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2121 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2122 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2123 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2124 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2125 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2126 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2127 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2128 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2129 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2130 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2131 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2132 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2133 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2134 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2135 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002136 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002137 {
Jamie Madill033dae62014-06-18 12:56:28 -04002138 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002139 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002140 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2141 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002142 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002143 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2144 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2145 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2146 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2147 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2148 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002149 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002150 ASSERT(node->getUseEmulatedFunction());
2151 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002152 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002153 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002155 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002156 ASSERT(node->getUseEmulatedFunction());
2157 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 break;
2159 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2160 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2161 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2162 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2163 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2164 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2165 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2166 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2167 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002168 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002169 ASSERT(node->getUseEmulatedFunction());
2170 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002171 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2173 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002174 case EOpOuterProduct:
2175 ASSERT(node->getUseEmulatedFunction());
2176 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2177 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002178 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 default: UNREACHABLE();
2180 }
2181
2182 return true;
2183}
2184
2185bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2186{
Jamie Madill32aab012015-01-27 14:12:26 -05002187 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002189 if (node->usesTernaryOperator())
2190 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002191 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002192 }
2193 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002194 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002195 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002196
Corentin Wallez80bacde2014-11-10 12:07:37 -08002197 // D3D errors when there is a gradient operation in a loop in an unflattened if
2198 // however flattening all the ifs in branch heavy shaders made D3D error too.
2199 // As a temporary workaround we flatten the ifs only if there is at least a loop
2200 // present somewhere in the shader.
2201 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2202 {
2203 out << "FLATTEN ";
2204 }
2205
2206 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002207
2208 node->getCondition()->traverse(this);
2209
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002210 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002211
Jamie Madill075edd82013-07-08 13:30:19 -04002212 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002213 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002215 bool discard = false;
2216
daniel@transgaming.combb885322010-04-15 20:45:24 +00002217 if (node->getTrueBlock())
2218 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002219 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002220
2221 // Detect true discard
2222 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002223 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002224
Jamie Madill075edd82013-07-08 13:30:19 -04002225 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002226 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002227
2228 if (node->getFalseBlock())
2229 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002230 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002231
Jamie Madill075edd82013-07-08 13:30:19 -04002232 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002233 out << "{\n";
2234
Jamie Madill075edd82013-07-08 13:30:19 -04002235 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002236 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002237
Jamie Madill075edd82013-07-08 13:30:19 -04002238 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002239 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002240
2241 // Detect false discard
2242 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2243 }
2244
2245 // ANGLE issue 486: Detect problematic conditional discard
2246 if (discard && FindSideEffectRewriting::search(node))
2247 {
2248 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002249 }
2250 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251
2252 return false;
2253}
2254
2255void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2256{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002257 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002258}
2259
2260bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2261{
Nicolas Capens655fe362014-04-11 13:12:34 -04002262 mNestedLoopDepth++;
2263
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002264 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2265
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002266 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002267 {
2268 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2269 }
2270
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002271 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002272 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002273 if (handleExcessiveLoop(node))
2274 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002275 mInsideDiscontinuousLoop = wasDiscontinuous;
2276 mNestedLoopDepth--;
2277
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002278 return false;
2279 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002280 }
2281
Jamie Madill32aab012015-01-27 14:12:26 -05002282 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283
alokp@chromium.org52813552010-11-16 18:36:09 +00002284 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002286 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002287
Jamie Madill075edd82013-07-08 13:30:19 -04002288 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002289 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290 }
2291 else
2292 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002293 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002294
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295 if (node->getInit())
2296 {
2297 node->getInit()->traverse(this);
2298 }
2299
2300 out << "; ";
2301
alokp@chromium.org52813552010-11-16 18:36:09 +00002302 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002304 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 }
2306
2307 out << "; ";
2308
alokp@chromium.org52813552010-11-16 18:36:09 +00002309 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002311 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312 }
2313
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002314 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002315
Jamie Madill075edd82013-07-08 13:30:19 -04002316 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002317 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318 }
2319
2320 if (node->getBody())
2321 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002322 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323 }
2324
Jamie Madill075edd82013-07-08 13:30:19 -04002325 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002326 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327
alokp@chromium.org52813552010-11-16 18:36:09 +00002328 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329 {
Jamie Madill075edd82013-07-08 13:30:19 -04002330 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331 out << "while(\n";
2332
alokp@chromium.org52813552010-11-16 18:36:09 +00002333 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002334
daniel@transgaming.com73536982012-03-21 20:45:49 +00002335 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336 }
2337
daniel@transgaming.com73536982012-03-21 20:45:49 +00002338 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002340 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002341 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002342
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002343 return false;
2344}
2345
2346bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2347{
Jamie Madill32aab012015-01-27 14:12:26 -05002348 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349
2350 switch (node->getFlowOp())
2351 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002352 case EOpKill:
2353 outputTriplet(visit, "discard;\n", "", "");
2354 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002355 case EOpBreak:
2356 if (visit == PreVisit)
2357 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002358 if (mNestedLoopDepth > 1)
2359 {
2360 mUsesNestedBreak = true;
2361 }
2362
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002363 if (mExcessiveLoopIndex)
2364 {
2365 out << "{Break";
2366 mExcessiveLoopIndex->traverse(this);
2367 out << " = true; break;}\n";
2368 }
2369 else
2370 {
2371 out << "break;\n";
2372 }
2373 }
2374 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002375 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 case EOpReturn:
2377 if (visit == PreVisit)
2378 {
2379 if (node->getExpression())
2380 {
2381 out << "return ";
2382 }
2383 else
2384 {
2385 out << "return;\n";
2386 }
2387 }
2388 else if (visit == PostVisit)
2389 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002390 if (node->getExpression())
2391 {
2392 out << ";\n";
2393 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394 }
2395 break;
2396 default: UNREACHABLE();
2397 }
2398
2399 return true;
2400}
2401
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002402void OutputHLSL::traverseStatements(TIntermNode *node)
2403{
2404 if (isSingleStatement(node))
2405 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002406 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002407 }
2408
2409 node->traverse(this);
2410}
2411
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002412bool OutputHLSL::isSingleStatement(TIntermNode *node)
2413{
2414 TIntermAggregate *aggregate = node->getAsAggregate();
2415
2416 if (aggregate)
2417 {
2418 if (aggregate->getOp() == EOpSequence)
2419 {
2420 return false;
2421 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002422 else if (aggregate->getOp() == EOpDeclaration)
2423 {
2424 // Declaring multiple comma-separated variables must be considered multiple statements
2425 // because each individual declaration has side effects which are visible in the next.
2426 return false;
2427 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002428 else
2429 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002430 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002431 {
2432 if (!isSingleStatement(*sit))
2433 {
2434 return false;
2435 }
2436 }
2437
2438 return true;
2439 }
2440 }
2441
2442 return true;
2443}
2444
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002445// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2446// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002447bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2448{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002449 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002450 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002451
2452 // Parse loops of the form:
2453 // for(int index = initial; index [comparator] limit; index += increment)
2454 TIntermSymbol *index = NULL;
2455 TOperator comparator = EOpNull;
2456 int initial = 0;
2457 int limit = 0;
2458 int increment = 0;
2459
2460 // Parse index name and intial value
2461 if (node->getInit())
2462 {
2463 TIntermAggregate *init = node->getInit()->getAsAggregate();
2464
2465 if (init)
2466 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002467 TIntermSequence *sequence = init->getSequence();
2468 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002469
2470 if (variable && variable->getQualifier() == EvqTemporary)
2471 {
2472 TIntermBinary *assign = variable->getAsBinaryNode();
2473
2474 if (assign->getOp() == EOpInitialize)
2475 {
2476 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2477 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2478
2479 if (symbol && constant)
2480 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002481 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002482 {
2483 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002484 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002485 }
2486 }
2487 }
2488 }
2489 }
2490 }
2491
2492 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002493 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002494 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002495 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002496
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002497 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2498 {
2499 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2500
2501 if (constant)
2502 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002503 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002504 {
2505 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002506 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002507 }
2508 }
2509 }
2510 }
2511
2512 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002513 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002514 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002515 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2516 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002517
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002518 if (binaryTerminal)
2519 {
2520 TOperator op = binaryTerminal->getOp();
2521 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2522
2523 if (constant)
2524 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002525 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002526 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002527 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002528
2529 switch (op)
2530 {
2531 case EOpAddAssign: increment = value; break;
2532 case EOpSubAssign: increment = -value; break;
2533 default: UNIMPLEMENTED();
2534 }
2535 }
2536 }
2537 }
2538 else if (unaryTerminal)
2539 {
2540 TOperator op = unaryTerminal->getOp();
2541
2542 switch (op)
2543 {
2544 case EOpPostIncrement: increment = 1; break;
2545 case EOpPostDecrement: increment = -1; break;
2546 case EOpPreIncrement: increment = 1; break;
2547 case EOpPreDecrement: increment = -1; break;
2548 default: UNIMPLEMENTED();
2549 }
2550 }
2551 }
2552
2553 if (index != NULL && comparator != EOpNull && increment != 0)
2554 {
2555 if (comparator == EOpLessThanEqual)
2556 {
2557 comparator = EOpLessThan;
2558 limit += 1;
2559 }
2560
2561 if (comparator == EOpLessThan)
2562 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002563 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002564
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002565 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002566 {
2567 return false; // Not an excessive loop
2568 }
2569
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002570 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2571 mExcessiveLoopIndex = index;
2572
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002573 out << "{int ";
2574 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002575 out << ";\n"
2576 "bool Break";
2577 index->traverse(this);
2578 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002579
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002580 bool firstLoopFragment = true;
2581
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002582 while (iterations > 0)
2583 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002584 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002585
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002586 if (!firstLoopFragment)
2587 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002588 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002589 index->traverse(this);
2590 out << ") {\n";
2591 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002592
2593 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2594 {
2595 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2596 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002597
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002598 // for(int index = initial; index < clampedLimit; index += increment)
2599
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002600 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002601 index->traverse(this);
2602 out << " = ";
2603 out << initial;
2604
2605 out << "; ";
2606 index->traverse(this);
2607 out << " < ";
2608 out << clampedLimit;
2609
2610 out << "; ";
2611 index->traverse(this);
2612 out << " += ";
2613 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002614 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002615
Jamie Madill075edd82013-07-08 13:30:19 -04002616 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002617 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002618
2619 if (node->getBody())
2620 {
2621 node->getBody()->traverse(this);
2622 }
2623
Jamie Madill075edd82013-07-08 13:30:19 -04002624 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002625 out << ";}\n";
2626
2627 if (!firstLoopFragment)
2628 {
2629 out << "}\n";
2630 }
2631
2632 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002633
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002634 initial += MAX_LOOP_ITERATIONS * increment;
2635 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002636 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002637
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002638 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002639
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002640 mExcessiveLoopIndex = restoreIndex;
2641
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002642 return true;
2643 }
2644 else UNIMPLEMENTED();
2645 }
2646
2647 return false; // Not handled as an excessive loop
2648}
2649
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002650void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651{
Jamie Madill32aab012015-01-27 14:12:26 -05002652 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002653
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002654 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002655 {
2656 out << preString;
2657 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002658 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002659 {
2660 out << inString;
2661 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002662 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002663 {
2664 out << postString;
2665 }
2666}
2667
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002668void OutputHLSL::outputLineDirective(int line)
2669{
2670 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2671 {
Jamie Madill32aab012015-01-27 14:12:26 -05002672 TInfoSinkBase &out = getInfoSink();
2673
2674 out << "\n";
2675 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002676
2677 if (mContext.sourcePath)
2678 {
Jamie Madill32aab012015-01-27 14:12:26 -05002679 out << " \"" << mContext.sourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002680 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002681
Jamie Madill32aab012015-01-27 14:12:26 -05002682 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002683 }
2684}
2685
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002686TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2687{
2688 TQualifier qualifier = symbol->getQualifier();
2689 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002690 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002691
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002692 if (name.empty()) // HLSL demands named arguments, also for prototypes
2693 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002694 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002695 }
2696 else
2697 {
Jamie Madill033dae62014-06-18 12:56:28 -04002698 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002699 }
2700
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002701 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2702 {
Jamie Madill033dae62014-06-18 12:56:28 -04002703 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002704 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002705 }
2706
Jamie Madill033dae62014-06-18 12:56:28 -04002707 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002708}
2709
2710TString OutputHLSL::initializer(const TType &type)
2711{
2712 TString string;
2713
Jamie Madill94bf7f22013-07-08 13:31:15 -04002714 size_t size = type.getObjectSize();
2715 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002716 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002717 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002718
Jamie Madill94bf7f22013-07-08 13:31:15 -04002719 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720 {
2721 string += ", ";
2722 }
2723 }
2724
daniel@transgaming.comead23042010-04-29 03:35:36 +00002725 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002727
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002728void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2729{
Jamie Madill32aab012015-01-27 14:12:26 -05002730 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002731
2732 if (visit == PreVisit)
2733 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002734 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002735
2736 out << name + "(";
2737 }
2738 else if (visit == InVisit)
2739 {
2740 out << ", ";
2741 }
2742 else if (visit == PostVisit)
2743 {
2744 out << ")";
2745 }
2746}
2747
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002748const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2749{
Jamie Madill32aab012015-01-27 14:12:26 -05002750 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002751
Jamie Madill98493dd2013-07-08 14:39:03 -04002752 const TStructure* structure = type.getStruct();
2753 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002754 {
Jamie Madill033dae62014-06-18 12:56:28 -04002755 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002756
Jamie Madill98493dd2013-07-08 14:39:03 -04002757 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002758
Jamie Madill98493dd2013-07-08 14:39:03 -04002759 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002760 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002761 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002762 constUnion = writeConstantUnion(*fieldType, constUnion);
2763
Jamie Madill98493dd2013-07-08 14:39:03 -04002764 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002765 {
2766 out << ", ";
2767 }
2768 }
2769
2770 out << ")";
2771 }
2772 else
2773 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002774 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002775 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002776
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002777 if (writeType)
2778 {
Jamie Madill033dae62014-06-18 12:56:28 -04002779 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002780 }
2781
Jamie Madill94bf7f22013-07-08 13:31:15 -04002782 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002783 {
2784 switch (constUnion->getType())
2785 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002786 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002787 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002788 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002789 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002790 default: UNREACHABLE();
2791 }
2792
2793 if (i != size - 1)
2794 {
2795 out << ", ";
2796 }
2797 }
2798
2799 if (writeType)
2800 {
2801 out << ")";
2802 }
2803 }
2804
2805 return constUnion;
2806}
2807
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002808void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2809{
2810 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2811 outputTriplet(visit, preString.c_str(), ", ", ")");
2812}
2813
Jamie Madill37997142015-01-28 10:06:34 -05002814bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2815{
2816 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2817 expression->traverse(&searchSymbol);
2818
2819 if (searchSymbol.foundMatch())
2820 {
2821 // Type already printed
2822 out << "t" + str(mUniqueIndex) + " = ";
2823 expression->traverse(this);
2824 out << ", ";
2825 symbolNode->traverse(this);
2826 out << " = t" + str(mUniqueIndex);
2827
2828 mUniqueIndex++;
2829 return true;
2830 }
2831
2832 return false;
2833}
2834
2835void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2836{
2837 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2838 << "\n"
2839 << "void initializeDeferredGlobals()\n"
2840 << "{\n";
2841
2842 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2843 {
2844 TIntermSymbol *symbol = deferredGlobal.first;
2845 TIntermTyped *expression = deferredGlobal.second;
2846 ASSERT(symbol);
2847 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2848
2849 out << " " << Decorate(symbol->getSymbol()) << " = ";
2850
2851 if (!writeSameSymbolInitializer(out, symbol, expression))
2852 {
2853 ASSERT(mInfoSinkStack.top() == &out);
2854 expression->traverse(this);
2855 }
2856
2857 out << ";\n";
2858 }
2859
2860 out << "}\n"
2861 << "\n";
2862}
2863
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002864}