blob: 6ff6700884c4cd361fea618364a1e24606c467b8 [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;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400113 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000114 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500115 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400116 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000117
Jamie Madill54ad4f82014-09-03 09:40:46 -0400118 const ShBuiltInResources &resources = parentTranslator->getResources();
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000119 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
120
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000121 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000122
123 mContainsLoopDiscontinuity = false;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800124 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000125 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000126 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400127 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000128
129 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000130
Jamie Madill8daaba12014-06-13 10:04:33 -0400131 mStructureHLSL = new StructureHLSL;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400132 mUniformHLSL = new UniformHLSL(mStructureHLSL, parentTranslator);
Jamie Madill8daaba12014-06-13 10:04:33 -0400133
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000134 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000135 {
Jamie Madill183bde52014-07-02 15:31:19 -0400136 if (mContext.shaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000137 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400138 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
139 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000140 }
141 else
142 {
Cooper Partine6664f02015-01-09 16:22:24 -0800143 // Reserve registers for dx_DepthRange, dx_ViewAdjust and dx_ViewCoords
144 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000145 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000146 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000147
Jamie Madillf91ce812014-06-13 10:04:34 -0400148 // Reserve registers for the default uniform block and driver constants
149 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150}
151
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000152OutputHLSL::~OutputHLSL()
153{
Jamie Madill8daaba12014-06-13 10:04:33 -0400154 SafeDelete(mUnfoldShortCircuit);
155 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400156 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000157}
158
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000159void OutputHLSL::output()
160{
Jamie Madill183bde52014-07-02 15:31:19 -0400161 mContainsLoopDiscontinuity = mContext.shaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Corentin Wallez80bacde2014-11-10 12:07:37 -0800162 mContainsAnyLoop = containsAnyLoop(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400163 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
164 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000165
Jamie Madille53c98b2014-02-03 11:57:13 -0500166 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
167 // use a vertex attribute as a condition, and some related computation in the else block.
Jamie Madill183bde52014-07-02 15:31:19 -0400168 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500169 {
170 RewriteElseBlocks(mContext.treeRoot);
171 }
172
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200173 BuiltInFunctionEmulatorHLSL builtInFunctionEmulator;
174 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(mContext.treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500175
176 // Output the body first to determine what has to go in the header
177 mInfoSinkStack.push(&mBody);
178 mContext.treeRoot->traverse(this);
179 mInfoSinkStack.pop();
180
181 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200182 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500183 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000184
Olli Etuahoe17e3192015-01-02 12:47:59 +0200185 TInfoSinkBase& sink = mContext.infoSink().obj;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200186 sink << mHeader.c_str();
187 sink << mBody.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200188
189 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000190}
191
Jamie Madill570e04d2013-06-21 09:15:33 -0400192void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
193{
194 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
195 {
196 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
197
Jamie Madill32aab012015-01-27 14:12:26 -0500198 TInfoSinkBase structInfoSink;
199 mInfoSinkStack.push(&structInfoSink);
200
Jamie Madill570e04d2013-06-21 09:15:33 -0400201 // This will mark the necessary block elements as referenced
202 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500203
204 TString structName(structInfoSink.c_str());
205 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400206
207 mFlaggedStructOriginalNames[flaggedNode] = structName;
208
209 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
210 {
211 structName.erase(pos, 1);
212 }
213
214 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
215 }
216}
217
Jamie Madill4e1fd412014-07-10 17:50:10 -0400218const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
219{
220 return mUniformHLSL->getInterfaceBlockRegisterMap();
221}
222
Jamie Madill9fe25e92014-07-18 10:33:08 -0400223const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
224{
225 return mUniformHLSL->getUniformRegisterMap();
226}
227
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000228int OutputHLSL::vectorSize(const TType &type) const
229{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000230 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000231 int arraySize = type.isArray() ? type.getArraySize() : 1;
232
233 return elementSize * arraySize;
234}
235
Jamie Madill98493dd2013-07-08 14:39:03 -0400236TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400237{
238 TString init;
239
240 TString preIndentString;
241 TString fullIndentString;
242
243 for (int spaces = 0; spaces < (indent * 4); spaces++)
244 {
245 preIndentString += ' ';
246 }
247
248 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
249 {
250 fullIndentString += ' ';
251 }
252
253 init += preIndentString + "{\n";
254
Jamie Madill98493dd2013-07-08 14:39:03 -0400255 const TFieldList &fields = structure.fields();
256 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400257 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400258 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400259 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400261
Jamie Madill98493dd2013-07-08 14:39:03 -0400262 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400263 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400265 }
266 else
267 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400268 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400269 }
270 }
271
272 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
273
274 return init;
275}
276
Olli Etuahoe17e3192015-01-02 12:47:59 +0200277void OutputHLSL::header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278{
Jamie Madill32aab012015-01-27 14:12:26 -0500279 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000281 TString varyings;
282 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400283 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000284
Jamie Madill829f59e2013-11-13 19:40:54 -0500285 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 {
287 TIntermTyped *structNode = flaggedStructIt->first;
288 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400289 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400290 const TString &originalName = mFlaggedStructOriginalNames[structNode];
291
Jamie Madill033dae62014-06-18 12:56:28 -0400292 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400293 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400294 flaggedStructs += "\n";
295 }
296
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000297 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
298 {
299 const TType &type = varying->second->getType();
300 const TString &name = varying->second->getSymbol();
301
302 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400303 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
304 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000305 }
306
307 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
308 {
309 const TType &type = attribute->second->getType();
310 const TString &name = attribute->second->getSymbol();
311
Jamie Madill033dae62014-06-18 12:56:28 -0400312 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000313 }
314
Jamie Madill8daaba12014-06-13 10:04:33 -0400315 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400316
Jamie Madillf91ce812014-06-13 10:04:34 -0400317 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
318 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
319
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500320 if (mUsesDiscardRewriting)
321 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400322 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500323 }
324
Nicolas Capens655fe362014-04-11 13:12:34 -0400325 if (mUsesNestedBreak)
326 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400327 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400328 }
329
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400330 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
331 "#define LOOP [loop]\n"
332 "#define FLATTEN [flatten]\n"
333 "#else\n"
334 "#define LOOP\n"
335 "#define FLATTEN\n"
336 "#endif\n";
337
Jamie Madill183bde52014-07-02 15:31:19 -0400338 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000340 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000341 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000342
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000343 out << "// Varyings\n";
344 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400345 out << "\n";
346
347 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000348 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500349 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000350 {
Jamie Madill46131a32013-06-20 11:55:50 -0400351 const TString &variableName = outputVariableIt->first;
352 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400353
Jamie Madill033dae62014-06-18 12:56:28 -0400354 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400355 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000356 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000357 }
Jamie Madill46131a32013-06-20 11:55:50 -0400358 else
359 {
360 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
361
362 out << "static float4 gl_Color[" << numColorValues << "] =\n"
363 "{\n";
364 for (unsigned int i = 0; i < numColorValues; i++)
365 {
366 out << " float4(0, 0, 0, 0)";
367 if (i + 1 != numColorValues)
368 {
369 out << ",";
370 }
371 out << "\n";
372 }
373
374 out << "};\n";
375 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000376
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400377 if (mUsesFragDepth)
378 {
379 out << "static float gl_Depth = 0.0;\n";
380 }
381
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000382 if (mUsesFragCoord)
383 {
384 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
385 }
386
387 if (mUsesPointCoord)
388 {
389 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
390 }
391
392 if (mUsesFrontFacing)
393 {
394 out << "static bool gl_FrontFacing = false;\n";
395 }
396
397 out << "\n";
398
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000399 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000400 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000401 out << "struct gl_DepthRangeParameters\n"
402 "{\n"
403 " float near;\n"
404 " float far;\n"
405 " float diff;\n"
406 "};\n"
407 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000408 }
409
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000410 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000411 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000412 out << "cbuffer DriverConstants : register(b1)\n"
413 "{\n";
414
415 if (mUsesDepthRange)
416 {
417 out << " float3 dx_DepthRange : packoffset(c0);\n";
418 }
419
420 if (mUsesFragCoord)
421 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000422 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000423 }
424
425 if (mUsesFragCoord || mUsesFrontFacing)
426 {
427 out << " float3 dx_DepthFront : packoffset(c2);\n";
428 }
429
430 out << "};\n";
431 }
432 else
433 {
434 if (mUsesDepthRange)
435 {
436 out << "uniform float3 dx_DepthRange : register(c0);";
437 }
438
439 if (mUsesFragCoord)
440 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000441 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000442 }
443
444 if (mUsesFragCoord || mUsesFrontFacing)
445 {
446 out << "uniform float3 dx_DepthFront : register(c2);\n";
447 }
448 }
449
450 out << "\n";
451
452 if (mUsesDepthRange)
453 {
454 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
455 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000456 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000457
Jamie Madillf91ce812014-06-13 10:04:34 -0400458 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000459 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400460 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000461 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400462 out << flaggedStructs;
463 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000464 }
465
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000466 if (usingMRTExtension && mNumRenderTargets > 1)
467 {
468 out << "#define GL_USES_MRT\n";
469 }
470
471 if (mUsesFragColor)
472 {
473 out << "#define GL_USES_FRAG_COLOR\n";
474 }
475
476 if (mUsesFragData)
477 {
478 out << "#define GL_USES_FRAG_DATA\n";
479 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000481 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000483 out << "// Attributes\n";
484 out << attributes;
485 out << "\n"
486 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400487
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000488 if (mUsesPointSize)
489 {
490 out << "static float gl_PointSize = float(1);\n";
491 }
492
493 out << "\n"
494 "// Varyings\n";
495 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000496 out << "\n";
497
498 if (mUsesDepthRange)
499 {
500 out << "struct gl_DepthRangeParameters\n"
501 "{\n"
502 " float near;\n"
503 " float far;\n"
504 " float diff;\n"
505 "};\n"
506 "\n";
507 }
508
509 if (mOutputType == SH_HLSL11_OUTPUT)
510 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800511 out << "cbuffer DriverConstants : register(b1)\n"
512 "{\n";
513
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000514 if (mUsesDepthRange)
515 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800516 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000517 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800518
Cooper Partine6664f02015-01-09 16:22:24 -0800519 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800520 // However, we declare it for all shaders (including Feature Level 10+).
521 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
522 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800523 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800524
525 out << "};\n"
526 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000527 }
528 else
529 {
530 if (mUsesDepthRange)
531 {
532 out << "uniform float3 dx_DepthRange : register(c0);\n";
533 }
534
Cooper Partine6664f02015-01-09 16:22:24 -0800535 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
536 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000537 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000538 }
539
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000540 if (mUsesDepthRange)
541 {
542 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
543 "\n";
544 }
545
Jamie Madillf91ce812014-06-13 10:04:34 -0400546 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000547 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400548 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000549 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400550 out << flaggedStructs;
551 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000552 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400553 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000554
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400555 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
556 {
557 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400558 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000559 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400560 switch(textureFunction->sampler)
561 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400562 case EbtSampler2D: out << "int2 "; break;
563 case EbtSampler3D: out << "int3 "; break;
564 case EbtSamplerCube: out << "int2 "; break;
565 case EbtSampler2DArray: out << "int3 "; break;
566 case EbtISampler2D: out << "int2 "; break;
567 case EbtISampler3D: out << "int3 "; break;
568 case EbtISamplerCube: out << "int2 "; break;
569 case EbtISampler2DArray: out << "int3 "; break;
570 case EbtUSampler2D: out << "int2 "; break;
571 case EbtUSampler3D: out << "int3 "; break;
572 case EbtUSamplerCube: out << "int2 "; break;
573 case EbtUSampler2DArray: out << "int3 "; break;
574 case EbtSampler2DShadow: out << "int2 "; break;
575 case EbtSamplerCubeShadow: out << "int2 "; break;
576 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400577 default: UNREACHABLE();
578 }
579 }
580 else // Sampling function
581 {
582 switch(textureFunction->sampler)
583 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400584 case EbtSampler2D: out << "float4 "; break;
585 case EbtSampler3D: out << "float4 "; break;
586 case EbtSamplerCube: out << "float4 "; break;
587 case EbtSampler2DArray: out << "float4 "; break;
588 case EbtISampler2D: out << "int4 "; break;
589 case EbtISampler3D: out << "int4 "; break;
590 case EbtISamplerCube: out << "int4 "; break;
591 case EbtISampler2DArray: out << "int4 "; break;
592 case EbtUSampler2D: out << "uint4 "; break;
593 case EbtUSampler3D: out << "uint4 "; break;
594 case EbtUSamplerCube: out << "uint4 "; break;
595 case EbtUSampler2DArray: out << "uint4 "; break;
596 case EbtSampler2DShadow: out << "float "; break;
597 case EbtSamplerCubeShadow: out << "float "; break;
598 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400599 default: UNREACHABLE();
600 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000601 }
602
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400603 // Function name
604 out << textureFunction->name();
605
606 // Argument list
607 int hlslCoords = 4;
608
609 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000610 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400611 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000612 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400613 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
614 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
615 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000616 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400617
Nicolas Capens75fb4752013-07-10 15:14:47 -0400618 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000619 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400620 case TextureFunction::IMPLICIT: break;
621 case TextureFunction::BIAS: hlslCoords = 4; break;
622 case TextureFunction::LOD: hlslCoords = 4; break;
623 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400624 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400625 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000626 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400627 }
628 else if (mOutputType == SH_HLSL11_OUTPUT)
629 {
630 switch(textureFunction->sampler)
631 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400632 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
633 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
634 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
635 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
636 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
637 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500638 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400639 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
640 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
641 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500642 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400643 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
644 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
645 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
646 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400647 default: UNREACHABLE();
648 }
649 }
650 else UNREACHABLE();
651
Nicolas Capensfc014542014-02-18 14:47:13 -0500652 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400653 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500654 switch(textureFunction->coords)
655 {
656 case 2: out << ", int2 t"; break;
657 case 3: out << ", int3 t"; break;
658 default: UNREACHABLE();
659 }
660 }
661 else // Floating-point coordinates (except textureSize)
662 {
663 switch(textureFunction->coords)
664 {
665 case 1: out << ", int lod"; break; // textureSize()
666 case 2: out << ", float2 t"; break;
667 case 3: out << ", float3 t"; break;
668 case 4: out << ", float4 t"; break;
669 default: UNREACHABLE();
670 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000671 }
672
Nicolas Capensd11d5492014-02-19 17:06:10 -0500673 if (textureFunction->method == TextureFunction::GRAD)
674 {
675 switch(textureFunction->sampler)
676 {
677 case EbtSampler2D:
678 case EbtISampler2D:
679 case EbtUSampler2D:
680 case EbtSampler2DArray:
681 case EbtISampler2DArray:
682 case EbtUSampler2DArray:
683 case EbtSampler2DShadow:
684 case EbtSampler2DArrayShadow:
685 out << ", float2 ddx, float2 ddy";
686 break;
687 case EbtSampler3D:
688 case EbtISampler3D:
689 case EbtUSampler3D:
690 case EbtSamplerCube:
691 case EbtISamplerCube:
692 case EbtUSamplerCube:
693 case EbtSamplerCubeShadow:
694 out << ", float3 ddx, float3 ddy";
695 break;
696 default: UNREACHABLE();
697 }
698 }
699
Nicolas Capens75fb4752013-07-10 15:14:47 -0400700 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000701 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400702 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400703 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400704 case TextureFunction::LOD: out << ", float lod"; break;
705 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400706 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400707 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500708 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500709 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400710 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000711 }
712
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500713 if (textureFunction->offset)
714 {
715 switch(textureFunction->sampler)
716 {
717 case EbtSampler2D: out << ", int2 offset"; break;
718 case EbtSampler3D: out << ", int3 offset"; break;
719 case EbtSampler2DArray: out << ", int2 offset"; break;
720 case EbtISampler2D: out << ", int2 offset"; break;
721 case EbtISampler3D: out << ", int3 offset"; break;
722 case EbtISampler2DArray: out << ", int2 offset"; break;
723 case EbtUSampler2D: out << ", int2 offset"; break;
724 case EbtUSampler3D: out << ", int3 offset"; break;
725 case EbtUSampler2DArray: out << ", int2 offset"; break;
726 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500727 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500728 default: UNREACHABLE();
729 }
730 }
731
Nicolas Capens84cfa122014-04-14 13:48:45 -0400732 if (textureFunction->method == TextureFunction::BIAS ||
733 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500734 {
735 out << ", float bias";
736 }
737
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400738 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400739 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400740
Nicolas Capens75fb4752013-07-10 15:14:47 -0400741 if (textureFunction->method == TextureFunction::SIZE)
742 {
743 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
744 {
745 if (IsSamplerArray(textureFunction->sampler))
746 {
747 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
748 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
749 }
750 else
751 {
752 out << " uint width; uint height; uint numberOfLevels;\n"
753 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
754 }
755 }
756 else if (IsSampler3D(textureFunction->sampler))
757 {
758 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
759 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
760 }
761 else UNREACHABLE();
762
763 switch(textureFunction->sampler)
764 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400765 case EbtSampler2D: out << " return int2(width, height);"; break;
766 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
767 case EbtSamplerCube: out << " return int2(width, height);"; break;
768 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
769 case EbtISampler2D: out << " return int2(width, height);"; break;
770 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
771 case EbtISamplerCube: out << " return int2(width, height);"; break;
772 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
773 case EbtUSampler2D: out << " return int2(width, height);"; break;
774 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
775 case EbtUSamplerCube: out << " return int2(width, height);"; break;
776 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
777 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
778 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
779 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400780 default: UNREACHABLE();
781 }
782 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400783 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400784 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500785 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
786 {
787 out << " float width; float height; float layers; float levels;\n";
788
789 out << " uint mip = 0;\n";
790
791 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
792
793 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
794 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
795 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
796 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
797
798 // FACE_POSITIVE_X = 000b
799 // FACE_NEGATIVE_X = 001b
800 // FACE_POSITIVE_Y = 010b
801 // FACE_NEGATIVE_Y = 011b
802 // FACE_POSITIVE_Z = 100b
803 // FACE_NEGATIVE_Z = 101b
804 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
805
806 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
807 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
808 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
809
810 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
811 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
812 }
813 else if (IsIntegerSampler(textureFunction->sampler) &&
814 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400815 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400816 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400817 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400818 if (IsSamplerArray(textureFunction->sampler))
819 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400820 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400821
Nicolas Capens9edebd62013-08-06 10:59:10 -0400822 if (textureFunction->method == TextureFunction::LOD0)
823 {
824 out << " uint mip = 0;\n";
825 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400826 else if (textureFunction->method == TextureFunction::LOD0BIAS)
827 {
828 out << " uint mip = bias;\n";
829 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400830 else
831 {
832 if (textureFunction->method == TextureFunction::IMPLICIT ||
833 textureFunction->method == TextureFunction::BIAS)
834 {
835 out << " x.GetDimensions(0, width, height, layers, levels);\n"
836 " float2 tSized = float2(t.x * width, t.y * height);\n"
837 " float dx = length(ddx(tSized));\n"
838 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500839 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400840
841 if (textureFunction->method == TextureFunction::BIAS)
842 {
843 out << " lod += bias;\n";
844 }
845 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500846 else if (textureFunction->method == TextureFunction::GRAD)
847 {
848 out << " x.GetDimensions(0, width, height, layers, levels);\n"
849 " float lod = log2(max(length(ddx), length(ddy)));\n";
850 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400851
852 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
853 }
854
855 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400856 }
857 else
858 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400859 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400860
Nicolas Capens9edebd62013-08-06 10:59:10 -0400861 if (textureFunction->method == TextureFunction::LOD0)
862 {
863 out << " uint mip = 0;\n";
864 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400865 else if (textureFunction->method == TextureFunction::LOD0BIAS)
866 {
867 out << " uint mip = bias;\n";
868 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400869 else
870 {
871 if (textureFunction->method == TextureFunction::IMPLICIT ||
872 textureFunction->method == TextureFunction::BIAS)
873 {
874 out << " x.GetDimensions(0, width, height, levels);\n"
875 " float2 tSized = float2(t.x * width, t.y * height);\n"
876 " float dx = length(ddx(tSized));\n"
877 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500878 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400879
880 if (textureFunction->method == TextureFunction::BIAS)
881 {
882 out << " lod += bias;\n";
883 }
884 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500885 else if (textureFunction->method == TextureFunction::LOD)
886 {
887 out << " x.GetDimensions(0, width, height, levels);\n";
888 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500889 else if (textureFunction->method == TextureFunction::GRAD)
890 {
891 out << " x.GetDimensions(0, width, height, levels);\n"
892 " float lod = log2(max(length(ddx), length(ddy)));\n";
893 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400894
895 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
896 }
897
898 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400899 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400900 }
901 else if (IsSampler3D(textureFunction->sampler))
902 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400903 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400904
Nicolas Capens9edebd62013-08-06 10:59:10 -0400905 if (textureFunction->method == TextureFunction::LOD0)
906 {
907 out << " uint mip = 0;\n";
908 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400909 else if (textureFunction->method == TextureFunction::LOD0BIAS)
910 {
911 out << " uint mip = bias;\n";
912 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400913 else
914 {
915 if (textureFunction->method == TextureFunction::IMPLICIT ||
916 textureFunction->method == TextureFunction::BIAS)
917 {
918 out << " x.GetDimensions(0, width, height, depth, levels);\n"
919 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
920 " float dx = length(ddx(tSized));\n"
921 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500922 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400923
924 if (textureFunction->method == TextureFunction::BIAS)
925 {
926 out << " lod += bias;\n";
927 }
928 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500929 else if (textureFunction->method == TextureFunction::GRAD)
930 {
931 out << " x.GetDimensions(0, width, height, depth, levels);\n"
932 " float lod = log2(max(length(ddx), length(ddy)));\n";
933 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400934
935 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
936 }
937
938 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400939 }
940 else UNREACHABLE();
941 }
942
943 out << " return ";
944
945 // HLSL intrinsic
946 if (mOutputType == SH_HLSL9_OUTPUT)
947 {
948 switch(textureFunction->sampler)
949 {
950 case EbtSampler2D: out << "tex2D"; break;
951 case EbtSamplerCube: out << "texCUBE"; break;
952 default: UNREACHABLE();
953 }
954
Nicolas Capens75fb4752013-07-10 15:14:47 -0400955 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400956 {
957 case TextureFunction::IMPLICIT: out << "(s, "; break;
958 case TextureFunction::BIAS: out << "bias(s, "; break;
959 case TextureFunction::LOD: out << "lod(s, "; break;
960 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400961 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400962 default: UNREACHABLE();
963 }
964 }
965 else if (mOutputType == SH_HLSL11_OUTPUT)
966 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500967 if (textureFunction->method == TextureFunction::GRAD)
968 {
969 if (IsIntegerSampler(textureFunction->sampler))
970 {
971 out << "x.Load(";
972 }
973 else if (IsShadowSampler(textureFunction->sampler))
974 {
975 out << "x.SampleCmpLevelZero(s, ";
976 }
977 else
978 {
979 out << "x.SampleGrad(s, ";
980 }
981 }
982 else if (IsIntegerSampler(textureFunction->sampler) ||
983 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400984 {
985 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400986 }
Nicolas Capenscb127d32013-07-15 17:26:18 -0400987 else if (IsShadowSampler(textureFunction->sampler))
988 {
989 out << "x.SampleCmp(s, ";
990 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400991 else
992 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400993 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400994 {
995 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
996 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
997 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
998 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400999 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001000 default: UNREACHABLE();
1001 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001002 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001003 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001004 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001005
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001006 // Integer sampling requires integer addresses
1007 TString addressx = "";
1008 TString addressy = "";
1009 TString addressz = "";
1010 TString close = "";
1011
Nicolas Capensfc014542014-02-18 14:47:13 -05001012 if (IsIntegerSampler(textureFunction->sampler) ||
1013 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001014 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001015 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001016 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001017 case 2: out << "int3("; break;
1018 case 3: out << "int4("; break;
1019 default: UNREACHABLE();
1020 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001021
Nicolas Capensfc014542014-02-18 14:47:13 -05001022 // Convert from normalized floating-point to integer
1023 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001024 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001025 addressx = "int(floor(width * frac((";
1026 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001027
Nicolas Capensfc014542014-02-18 14:47:13 -05001028 if (IsSamplerArray(textureFunction->sampler))
1029 {
1030 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1031 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001032 else if (IsSamplerCube(textureFunction->sampler))
1033 {
1034 addressz = "((((";
1035 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001036 else
1037 {
1038 addressz = "int(floor(depth * frac((";
1039 }
1040
1041 close = "))))";
1042 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001043 }
1044 else
1045 {
1046 switch(hlslCoords)
1047 {
1048 case 2: out << "float2("; break;
1049 case 3: out << "float3("; break;
1050 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001051 default: UNREACHABLE();
1052 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001053 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001054
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001055 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001056
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001057 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001058 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001059 switch(textureFunction->coords)
1060 {
1061 case 3: proj = " / t.z"; break;
1062 case 4: proj = " / t.w"; break;
1063 default: UNREACHABLE();
1064 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001065 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001066
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001067 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001068
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001069 if (mOutputType == SH_HLSL9_OUTPUT)
1070 {
1071 if (hlslCoords >= 3)
1072 {
1073 if (textureFunction->coords < 3)
1074 {
1075 out << ", 0";
1076 }
1077 else
1078 {
1079 out << ", t.z" + proj;
1080 }
1081 }
1082
1083 if (hlslCoords == 4)
1084 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001085 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001086 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001087 case TextureFunction::BIAS: out << ", bias"; break;
1088 case TextureFunction::LOD: out << ", lod"; break;
1089 case TextureFunction::LOD0: out << ", 0"; break;
1090 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001091 default: UNREACHABLE();
1092 }
1093 }
1094
1095 out << "));\n";
1096 }
1097 else if (mOutputType == SH_HLSL11_OUTPUT)
1098 {
1099 if (hlslCoords >= 3)
1100 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001101 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1102 {
1103 out << ", face";
1104 }
1105 else
1106 {
1107 out << ", " + addressz + ("t.z" + proj) + close;
1108 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001109 }
1110
Nicolas Capensd11d5492014-02-19 17:06:10 -05001111 if (textureFunction->method == TextureFunction::GRAD)
1112 {
1113 if (IsIntegerSampler(textureFunction->sampler))
1114 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001115 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001116 }
1117 else if (IsShadowSampler(textureFunction->sampler))
1118 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001119 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001120 switch(textureFunction->coords)
1121 {
1122 case 3: out << "), t.z"; break;
1123 case 4: out << "), t.w"; break;
1124 default: UNREACHABLE();
1125 }
1126 }
1127 else
1128 {
1129 out << "), ddx, ddy";
1130 }
1131 }
1132 else if (IsIntegerSampler(textureFunction->sampler) ||
1133 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001134 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001135 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001136 }
1137 else if (IsShadowSampler(textureFunction->sampler))
1138 {
1139 // Compare value
1140 switch(textureFunction->coords)
1141 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001142 case 3: out << "), t.z"; break;
1143 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001144 default: UNREACHABLE();
1145 }
1146 }
1147 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001148 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001149 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001150 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001151 case TextureFunction::IMPLICIT: out << ")"; break;
1152 case TextureFunction::BIAS: out << "), bias"; break;
1153 case TextureFunction::LOD: out << "), lod"; break;
1154 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001155 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001156 default: UNREACHABLE();
1157 }
1158 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001159
1160 if (textureFunction->offset)
1161 {
1162 out << ", offset";
1163 }
1164
1165 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001166 }
1167 else UNREACHABLE();
1168 }
1169
1170 out << "\n"
1171 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001172 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001173 }
1174
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001175 if (mUsesFragCoord)
1176 {
1177 out << "#define GL_USES_FRAG_COORD\n";
1178 }
1179
1180 if (mUsesPointCoord)
1181 {
1182 out << "#define GL_USES_POINT_COORD\n";
1183 }
1184
1185 if (mUsesFrontFacing)
1186 {
1187 out << "#define GL_USES_FRONT_FACING\n";
1188 }
1189
1190 if (mUsesPointSize)
1191 {
1192 out << "#define GL_USES_POINT_SIZE\n";
1193 }
1194
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001195 if (mUsesFragDepth)
1196 {
1197 out << "#define GL_USES_FRAG_DEPTH\n";
1198 }
1199
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001200 if (mUsesDepthRange)
1201 {
1202 out << "#define GL_USES_DEPTH_RANGE\n";
1203 }
1204
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001205 if (mUsesXor)
1206 {
1207 out << "bool xor(bool p, bool q)\n"
1208 "{\n"
1209 " return (p || q) && !(p && q);\n"
1210 "}\n"
1211 "\n";
1212 }
1213
Olli Etuaho80a5a6c2015-01-12 15:35:27 +02001214 builtInFunctionEmulator->OutputEmulatedFunctionDefinition(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001215}
1216
1217void OutputHLSL::visitSymbol(TIntermSymbol *node)
1218{
Jamie Madill32aab012015-01-27 14:12:26 -05001219 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001220
Jamie Madill570e04d2013-06-21 09:15:33 -04001221 // Handle accessing std140 structs by value
1222 if (mFlaggedStructMappedNames.count(node) > 0)
1223 {
1224 out << mFlaggedStructMappedNames[node];
1225 return;
1226 }
1227
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001228 TString name = node->getSymbol();
1229
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001230 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001231 {
1232 mUsesDepthRange = true;
1233 out << name;
1234 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001235 else
1236 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001237 TQualifier qualifier = node->getQualifier();
1238
1239 if (qualifier == EvqUniform)
1240 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001241 const TType& nodeType = node->getType();
1242 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1243
1244 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001245 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001246 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001247 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001248 else
1249 {
1250 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001251 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001252
Jamie Madill033dae62014-06-18 12:56:28 -04001253 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001254 }
Jamie Madill19571812013-08-12 15:26:34 -07001255 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001256 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001257 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001258 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001259 }
Jamie Madill033dae62014-06-18 12:56:28 -04001260 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001261 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001262 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001263 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001264 }
Jamie Madill19571812013-08-12 15:26:34 -07001265 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001266 {
1267 mReferencedOutputVariables[name] = node;
1268 out << "out_" << name;
1269 }
1270 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001271 {
1272 out << "gl_Color[0]";
1273 mUsesFragColor = true;
1274 }
1275 else if (qualifier == EvqFragData)
1276 {
1277 out << "gl_Color";
1278 mUsesFragData = true;
1279 }
1280 else if (qualifier == EvqFragCoord)
1281 {
1282 mUsesFragCoord = true;
1283 out << name;
1284 }
1285 else if (qualifier == EvqPointCoord)
1286 {
1287 mUsesPointCoord = true;
1288 out << name;
1289 }
1290 else if (qualifier == EvqFrontFacing)
1291 {
1292 mUsesFrontFacing = true;
1293 out << name;
1294 }
1295 else if (qualifier == EvqPointSize)
1296 {
1297 mUsesPointSize = true;
1298 out << name;
1299 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001300 else if (name == "gl_FragDepthEXT")
1301 {
1302 mUsesFragDepth = true;
1303 out << "gl_Depth";
1304 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001305 else if (qualifier == EvqInternal)
1306 {
1307 out << name;
1308 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001309 else
1310 {
Jamie Madill033dae62014-06-18 12:56:28 -04001311 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001312 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001313 }
1314}
1315
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001316void OutputHLSL::visitRaw(TIntermRaw *node)
1317{
Jamie Madill32aab012015-01-27 14:12:26 -05001318 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001319}
1320
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001321bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1322{
Jamie Madill32aab012015-01-27 14:12:26 -05001323 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001324
Jamie Madill570e04d2013-06-21 09:15:33 -04001325 // Handle accessing std140 structs by value
1326 if (mFlaggedStructMappedNames.count(node) > 0)
1327 {
1328 out << mFlaggedStructMappedNames[node];
1329 return false;
1330 }
1331
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001332 switch (node->getOp())
1333 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001334 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001335 case EOpInitialize:
1336 if (visit == PreVisit)
1337 {
1338 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1339 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1340 // new variable is created before the assignment is evaluated), so we need to convert
1341 // this to "float t = x, x = t;".
1342
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001343 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1344 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001345
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001346 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1347 expression->traverse(&searchSymbol);
1348 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001349
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001350 if (sameSymbol)
1351 {
1352 // Type already printed
1353 out << "t" + str(mUniqueIndex) + " = ";
1354 expression->traverse(this);
1355 out << ", ";
1356 symbolNode->traverse(this);
1357 out << " = t" + str(mUniqueIndex);
1358
1359 mUniqueIndex++;
1360 return false;
1361 }
1362 }
1363 else if (visit == InVisit)
1364 {
1365 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001366 }
1367 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001368 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1369 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1370 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1371 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1372 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1373 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001374 if (visit == PreVisit)
1375 {
1376 out << "(";
1377 }
1378 else if (visit == InVisit)
1379 {
1380 out << " = mul(";
1381 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001382 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001383 }
1384 else
1385 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001386 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001387 }
1388 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001389 case EOpMatrixTimesMatrixAssign:
1390 if (visit == PreVisit)
1391 {
1392 out << "(";
1393 }
1394 else if (visit == InVisit)
1395 {
1396 out << " = mul(";
1397 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001398 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001399 }
1400 else
1401 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001402 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001403 }
1404 break;
1405 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001406 case EOpModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001407 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1408 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1409 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1410 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1411 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001412 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001413 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001414 const TType& leftType = node->getLeft()->getType();
1415 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001416 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001417 if (visit == PreVisit)
1418 {
1419 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1420 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001421 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001422 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001423 return false;
1424 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001425 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001426 else
1427 {
1428 outputTriplet(visit, "", "[", "]");
1429 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001430 }
1431 break;
1432 case EOpIndexIndirect:
1433 // We do not currently support indirect references to interface blocks
1434 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1435 outputTriplet(visit, "", "[", "]");
1436 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001437 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001438 if (visit == InVisit)
1439 {
1440 const TStructure* structure = node->getLeft()->getType().getStruct();
1441 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1442 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001443 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001444
1445 return false;
1446 }
1447 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001448 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001449 if (visit == InVisit)
1450 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001451 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1452 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1453 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001454 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001455
1456 return false;
1457 }
1458 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001459 case EOpVectorSwizzle:
1460 if (visit == InVisit)
1461 {
1462 out << ".";
1463
1464 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1465
1466 if (swizzle)
1467 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001468 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001469
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001470 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001471 {
1472 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1473
1474 if (element)
1475 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001476 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001477
1478 switch (i)
1479 {
1480 case 0: out << "x"; break;
1481 case 1: out << "y"; break;
1482 case 2: out << "z"; break;
1483 case 3: out << "w"; break;
1484 default: UNREACHABLE();
1485 }
1486 }
1487 else UNREACHABLE();
1488 }
1489 }
1490 else UNREACHABLE();
1491
1492 return false; // Fully processed
1493 }
1494 break;
1495 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1496 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1497 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1498 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001499 case EOpMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001500 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1501 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1502 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1503 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1504 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001505 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001506 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001507 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001508 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001509 if (node->getOp() == EOpEqual)
1510 {
1511 outputTriplet(visit, "(", " == ", ")");
1512 }
1513 else
1514 {
1515 outputTriplet(visit, "(", " != ", ")");
1516 }
1517 }
1518 else if (node->getLeft()->getBasicType() == EbtStruct)
1519 {
1520 if (node->getOp() == EOpEqual)
1521 {
1522 out << "(";
1523 }
1524 else
1525 {
1526 out << "!(";
1527 }
1528
Jamie Madill98493dd2013-07-08 14:39:03 -04001529 const TStructure &structure = *node->getLeft()->getType().getStruct();
1530 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001531
Jamie Madill98493dd2013-07-08 14:39:03 -04001532 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001533 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001534 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001535
1536 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001537 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001538 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001539 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001540
Jamie Madill98493dd2013-07-08 14:39:03 -04001541 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001542 {
1543 out << " && ";
1544 }
1545 }
1546
1547 out << ")";
1548
1549 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001550 }
1551 else
1552 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001553 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001554
1555 if (node->getOp() == EOpEqual)
1556 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001557 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001558 }
1559 else
1560 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001561 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001562 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001563 }
1564 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001565 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1566 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1567 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1568 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1569 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001570 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001571 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1572 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001573 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001574 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001575 if (node->getRight()->hasSideEffects())
1576 {
1577 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1578 return false;
1579 }
1580 else
1581 {
1582 outputTriplet(visit, "(", " || ", ")");
1583 return true;
1584 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001585 case EOpLogicalXor:
1586 mUsesXor = true;
1587 outputTriplet(visit, "xor(", ", ", ")");
1588 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001589 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001590 if (node->getRight()->hasSideEffects())
1591 {
1592 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1593 return false;
1594 }
1595 else
1596 {
1597 outputTriplet(visit, "(", " && ", ")");
1598 return true;
1599 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001600 default: UNREACHABLE();
1601 }
1602
1603 return true;
1604}
1605
1606bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1607{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001608 switch (node->getOp())
1609 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001610 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001611 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001612 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1613 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001614 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001615 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1616 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1617 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1618 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001619 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1620 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1621 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1622 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1623 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1624 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1625 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1626 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001627 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1628 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1629 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1630 case EOpAsinh:
1631 ASSERT(node->getUseEmulatedFunction());
1632 writeEmulatedFunctionTriplet(visit, "asinh(");
1633 break;
1634 case EOpAcosh:
1635 ASSERT(node->getUseEmulatedFunction());
1636 writeEmulatedFunctionTriplet(visit, "acosh(");
1637 break;
1638 case EOpAtanh:
1639 ASSERT(node->getUseEmulatedFunction());
1640 writeEmulatedFunctionTriplet(visit, "atanh(");
1641 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001642 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1643 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1644 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1645 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1646 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1647 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1648 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1649 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1650 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1651 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1652 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001653 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1654 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1655 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1656 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001657 case EOpPackSnorm2x16:
1658 ASSERT(node->getUseEmulatedFunction());
1659 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1660 break;
1661 case EOpPackUnorm2x16:
1662 ASSERT(node->getUseEmulatedFunction());
1663 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1664 break;
1665 case EOpPackHalf2x16:
1666 ASSERT(node->getUseEmulatedFunction());
1667 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1668 break;
1669 case EOpUnpackSnorm2x16:
1670 ASSERT(node->getUseEmulatedFunction());
1671 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1672 break;
1673 case EOpUnpackUnorm2x16:
1674 ASSERT(node->getUseEmulatedFunction());
1675 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1676 break;
1677 case EOpUnpackHalf2x16:
1678 ASSERT(node->getUseEmulatedFunction());
1679 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1680 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001681 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1682 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001683 case EOpDFdx:
1684 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1685 {
1686 outputTriplet(visit, "(", "", ", 0.0)");
1687 }
1688 else
1689 {
1690 outputTriplet(visit, "ddx(", "", ")");
1691 }
1692 break;
1693 case EOpDFdy:
1694 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1695 {
1696 outputTriplet(visit, "(", "", ", 0.0)");
1697 }
1698 else
1699 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001700 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001701 }
1702 break;
1703 case EOpFwidth:
1704 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1705 {
1706 outputTriplet(visit, "(", "", ", 0.0)");
1707 }
1708 else
1709 {
1710 outputTriplet(visit, "fwidth(", "", ")");
1711 }
1712 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001713 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1714 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001715 case EOpInverse:
1716 ASSERT(node->getUseEmulatedFunction());
1717 writeEmulatedFunctionTriplet(visit, "inverse(");
1718 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001719
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001720 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1721 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001722 default: UNREACHABLE();
1723 }
1724
1725 return true;
1726}
1727
1728bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1729{
Jamie Madill32aab012015-01-27 14:12:26 -05001730 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732 switch (node->getOp())
1733 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001734 case EOpSequence:
1735 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001736 if (mInsideFunction)
1737 {
Jamie Madill075edd82013-07-08 13:30:19 -04001738 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001739 out << "{\n";
1740 }
1741
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001742 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001743 {
Jamie Madill075edd82013-07-08 13:30:19 -04001744 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001745
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001746 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001747
1748 out << ";\n";
1749 }
1750
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001751 if (mInsideFunction)
1752 {
Jamie Madill075edd82013-07-08 13:30:19 -04001753 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001754 out << "}\n";
1755 }
1756
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001757 return false;
1758 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 case EOpDeclaration:
1760 if (visit == PreVisit)
1761 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001762 TIntermSequence *sequence = node->getSequence();
1763 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001765 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001767 TStructure *structure = variable->getType().getStruct();
1768
1769 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001770 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001771 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001772 }
1773
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001774 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001775 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001776 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001777 {
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001778 if (isSingleStatement(*sit))
1779 {
1780 mUnfoldShortCircuit->traverse(*sit);
1781 }
1782
Nicolas Capensd974db42014-10-07 10:50:19 -04001783 if (!mInsideFunction)
1784 {
1785 out << "static ";
1786 }
1787
1788 out << TypeString(variable->getType()) + " ";
1789
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001790 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001791
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001792 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001793 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001794 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001795 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001796 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001797 }
1798 else
1799 {
1800 (*sit)->traverse(this);
1801 }
1802
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001803 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001804 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001805 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806 }
1807 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001809 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1810 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001811 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001812 }
1813 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814 }
Jamie Madill033dae62014-06-18 12:56:28 -04001815 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001816 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001817 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001818 {
1819 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1820
1821 if (symbol)
1822 {
1823 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1824 mReferencedVaryings[symbol->getSymbol()] = symbol;
1825 }
1826 else
1827 {
1828 (*sit)->traverse(this);
1829 }
1830 }
1831 }
1832
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833 return false;
1834 }
1835 else if (visit == InVisit)
1836 {
1837 out << ", ";
1838 }
1839 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001840 case EOpInvariantDeclaration:
1841 // Do not do any translation
1842 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001843 case EOpPrototype:
1844 if (visit == PreVisit)
1845 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001846 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001847
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001848 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001849
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001850 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001851 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001852 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001853
1854 if (symbol)
1855 {
1856 out << argumentString(symbol);
1857
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001858 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001859 {
1860 out << ", ";
1861 }
1862 }
1863 else UNREACHABLE();
1864 }
1865
1866 out << ");\n";
1867
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001868 // Also prototype the Lod0 variant if needed
1869 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1870 {
1871 mOutputLod0Function = true;
1872 node->traverse(this);
1873 mOutputLod0Function = false;
1874 }
1875
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001876 return false;
1877 }
1878 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001879 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880 case EOpFunction:
1881 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001882 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883
Jamie Madill033dae62014-06-18 12:56:28 -04001884 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001885
1886 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001888 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001890 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891 {
Jamie Madill033dae62014-06-18 12:56:28 -04001892 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001893 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001894
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001895 TIntermSequence *sequence = node->getSequence();
1896 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001897
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001898 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001899 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001900 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001901
1902 if (symbol)
1903 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001904 TStructure *structure = symbol->getType().getStruct();
1905
1906 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001907 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001908 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001909 }
1910
1911 out << argumentString(symbol);
1912
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001913 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001914 {
1915 out << ", ";
1916 }
1917 }
1918 else UNREACHABLE();
1919 }
1920
1921 out << ")\n"
1922 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001923
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001924 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001925 {
1926 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001927 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001928 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001929 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001930
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001931 out << "}\n";
1932
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001933 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1934 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001935 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001936 {
1937 mOutputLod0Function = true;
1938 node->traverse(this);
1939 mOutputLod0Function = false;
1940 }
1941 }
1942
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001943 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944 }
1945 break;
1946 case EOpFunctionCall:
1947 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001948 TString name = TFunction::unmangleName(node->getName());
1949 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001950 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001951
1952 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953 {
Jamie Madill033dae62014-06-18 12:56:28 -04001954 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955 }
1956 else
1957 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001958 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001959
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001960 TextureFunction textureFunction;
1961 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001962 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04001963 textureFunction.method = TextureFunction::IMPLICIT;
1964 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001965 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001966
1967 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001968 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001969 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001970 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001971 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001972 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001973 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001974 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001975 }
Nicolas Capens46485082014-04-15 13:12:50 -04001976 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
1977 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001978 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001979 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001980 }
Nicolas Capens46485082014-04-15 13:12:50 -04001981 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001982 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001983 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001984 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001985 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04001986 else if (name == "textureSize")
1987 {
1988 textureFunction.method = TextureFunction::SIZE;
1989 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001990 else if (name == "textureOffset")
1991 {
1992 textureFunction.method = TextureFunction::IMPLICIT;
1993 textureFunction.offset = true;
1994 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05001995 else if (name == "textureProjOffset")
1996 {
1997 textureFunction.method = TextureFunction::IMPLICIT;
1998 textureFunction.offset = true;
1999 textureFunction.proj = true;
2000 }
2001 else if (name == "textureLodOffset")
2002 {
2003 textureFunction.method = TextureFunction::LOD;
2004 textureFunction.offset = true;
2005 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002006 else if (name == "textureProjLodOffset")
2007 {
2008 textureFunction.method = TextureFunction::LOD;
2009 textureFunction.proj = true;
2010 textureFunction.offset = true;
2011 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002012 else if (name == "texelFetch")
2013 {
2014 textureFunction.method = TextureFunction::FETCH;
2015 }
2016 else if (name == "texelFetchOffset")
2017 {
2018 textureFunction.method = TextureFunction::FETCH;
2019 textureFunction.offset = true;
2020 }
Nicolas Capens46485082014-04-15 13:12:50 -04002021 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002022 {
2023 textureFunction.method = TextureFunction::GRAD;
2024 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002025 else if (name == "textureGradOffset")
2026 {
2027 textureFunction.method = TextureFunction::GRAD;
2028 textureFunction.offset = true;
2029 }
Nicolas Capens46485082014-04-15 13:12:50 -04002030 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002031 {
2032 textureFunction.method = TextureFunction::GRAD;
2033 textureFunction.proj = true;
2034 }
2035 else if (name == "textureProjGradOffset")
2036 {
2037 textureFunction.method = TextureFunction::GRAD;
2038 textureFunction.proj = true;
2039 textureFunction.offset = true;
2040 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002041 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002042
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002043 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002044 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002045 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2046
2047 if (textureFunction.offset)
2048 {
2049 mandatoryArgumentCount++;
2050 }
2051
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002052 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002053
Jamie Madill183bde52014-07-02 15:31:19 -04002054 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002055 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002056 if (bias)
2057 {
2058 textureFunction.method = TextureFunction::LOD0BIAS;
2059 }
2060 else
2061 {
2062 textureFunction.method = TextureFunction::LOD0;
2063 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002064 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002065 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002066 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002067 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002068 }
2069 }
2070
2071 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002072
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002073 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002075
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002076 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002077 {
2078 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2079 {
2080 out << "texture_";
2081 (*arg)->traverse(this);
2082 out << ", sampler_";
2083 }
2084
2085 (*arg)->traverse(this);
2086
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002087 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002088 {
2089 out << ", ";
2090 }
2091 }
2092
2093 out << ")";
2094
2095 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096 }
2097 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002098 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002099 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2100 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2101 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2102 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2103 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2104 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2105 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2106 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2107 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2108 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2109 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2110 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2111 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2112 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2113 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2114 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2115 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2116 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2117 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002118 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002119 {
Jamie Madill033dae62014-06-18 12:56:28 -04002120 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002121 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002122 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2123 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002124 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002125 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2126 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2127 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2128 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2129 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2130 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002131 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002132 ASSERT(node->getUseEmulatedFunction());
2133 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002134 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002135 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002137 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002138 ASSERT(node->getUseEmulatedFunction());
2139 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002140 break;
2141 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2142 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2143 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2144 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2145 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2146 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2147 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2148 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2149 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002150 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002151 ASSERT(node->getUseEmulatedFunction());
2152 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002153 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2155 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002156 case EOpOuterProduct:
2157 ASSERT(node->getUseEmulatedFunction());
2158 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2159 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002160 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161 default: UNREACHABLE();
2162 }
2163
2164 return true;
2165}
2166
2167bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2168{
Jamie Madill32aab012015-01-27 14:12:26 -05002169 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002171 if (node->usesTernaryOperator())
2172 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002173 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002174 }
2175 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002177 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002178
Corentin Wallez80bacde2014-11-10 12:07:37 -08002179 // D3D errors when there is a gradient operation in a loop in an unflattened if
2180 // however flattening all the ifs in branch heavy shaders made D3D error too.
2181 // As a temporary workaround we flatten the ifs only if there is at least a loop
2182 // present somewhere in the shader.
2183 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2184 {
2185 out << "FLATTEN ";
2186 }
2187
2188 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002189
2190 node->getCondition()->traverse(this);
2191
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002192 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002193
Jamie Madill075edd82013-07-08 13:30:19 -04002194 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002195 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002196
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002197 bool discard = false;
2198
daniel@transgaming.combb885322010-04-15 20:45:24 +00002199 if (node->getTrueBlock())
2200 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002201 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002202
2203 // Detect true discard
2204 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002205 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206
Jamie Madill075edd82013-07-08 13:30:19 -04002207 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002208 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002209
2210 if (node->getFalseBlock())
2211 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002212 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002213
Jamie Madill075edd82013-07-08 13:30:19 -04002214 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002215 out << "{\n";
2216
Jamie Madill075edd82013-07-08 13:30:19 -04002217 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002218 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002219
Jamie Madill075edd82013-07-08 13:30:19 -04002220 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002221 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002222
2223 // Detect false discard
2224 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2225 }
2226
2227 // ANGLE issue 486: Detect problematic conditional discard
2228 if (discard && FindSideEffectRewriting::search(node))
2229 {
2230 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002231 }
2232 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233
2234 return false;
2235}
2236
2237void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2238{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002239 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240}
2241
2242bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2243{
Nicolas Capens655fe362014-04-11 13:12:34 -04002244 mNestedLoopDepth++;
2245
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002246 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2247
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002248 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002249 {
2250 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2251 }
2252
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002253 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002254 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002255 if (handleExcessiveLoop(node))
2256 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002257 mInsideDiscontinuousLoop = wasDiscontinuous;
2258 mNestedLoopDepth--;
2259
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002260 return false;
2261 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002262 }
2263
Jamie Madill32aab012015-01-27 14:12:26 -05002264 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265
alokp@chromium.org52813552010-11-16 18:36:09 +00002266 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002268 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002269
Jamie Madill075edd82013-07-08 13:30:19 -04002270 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002271 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272 }
2273 else
2274 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002275 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002276
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002277 if (node->getInit())
2278 {
2279 node->getInit()->traverse(this);
2280 }
2281
2282 out << "; ";
2283
alokp@chromium.org52813552010-11-16 18:36:09 +00002284 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002286 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 }
2288
2289 out << "; ";
2290
alokp@chromium.org52813552010-11-16 18:36:09 +00002291 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002293 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 }
2295
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002296 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002297
Jamie Madill075edd82013-07-08 13:30:19 -04002298 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002299 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 }
2301
2302 if (node->getBody())
2303 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002304 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 }
2306
Jamie Madill075edd82013-07-08 13:30:19 -04002307 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002308 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002309
alokp@chromium.org52813552010-11-16 18:36:09 +00002310 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 {
Jamie Madill075edd82013-07-08 13:30:19 -04002312 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313 out << "while(\n";
2314
alokp@chromium.org52813552010-11-16 18:36:09 +00002315 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316
daniel@transgaming.com73536982012-03-21 20:45:49 +00002317 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318 }
2319
daniel@transgaming.com73536982012-03-21 20:45:49 +00002320 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002322 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002323 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002324
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325 return false;
2326}
2327
2328bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2329{
Jamie Madill32aab012015-01-27 14:12:26 -05002330 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331
2332 switch (node->getFlowOp())
2333 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002334 case EOpKill:
2335 outputTriplet(visit, "discard;\n", "", "");
2336 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002337 case EOpBreak:
2338 if (visit == PreVisit)
2339 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002340 if (mNestedLoopDepth > 1)
2341 {
2342 mUsesNestedBreak = true;
2343 }
2344
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002345 if (mExcessiveLoopIndex)
2346 {
2347 out << "{Break";
2348 mExcessiveLoopIndex->traverse(this);
2349 out << " = true; break;}\n";
2350 }
2351 else
2352 {
2353 out << "break;\n";
2354 }
2355 }
2356 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002357 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358 case EOpReturn:
2359 if (visit == PreVisit)
2360 {
2361 if (node->getExpression())
2362 {
2363 out << "return ";
2364 }
2365 else
2366 {
2367 out << "return;\n";
2368 }
2369 }
2370 else if (visit == PostVisit)
2371 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002372 if (node->getExpression())
2373 {
2374 out << ";\n";
2375 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 }
2377 break;
2378 default: UNREACHABLE();
2379 }
2380
2381 return true;
2382}
2383
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002384void OutputHLSL::traverseStatements(TIntermNode *node)
2385{
2386 if (isSingleStatement(node))
2387 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002388 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002389 }
2390
2391 node->traverse(this);
2392}
2393
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002394bool OutputHLSL::isSingleStatement(TIntermNode *node)
2395{
2396 TIntermAggregate *aggregate = node->getAsAggregate();
2397
2398 if (aggregate)
2399 {
2400 if (aggregate->getOp() == EOpSequence)
2401 {
2402 return false;
2403 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002404 else if (aggregate->getOp() == EOpDeclaration)
2405 {
2406 // Declaring multiple comma-separated variables must be considered multiple statements
2407 // because each individual declaration has side effects which are visible in the next.
2408 return false;
2409 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002410 else
2411 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002412 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002413 {
2414 if (!isSingleStatement(*sit))
2415 {
2416 return false;
2417 }
2418 }
2419
2420 return true;
2421 }
2422 }
2423
2424 return true;
2425}
2426
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002427// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2428// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002429bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2430{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002431 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002432 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002433
2434 // Parse loops of the form:
2435 // for(int index = initial; index [comparator] limit; index += increment)
2436 TIntermSymbol *index = NULL;
2437 TOperator comparator = EOpNull;
2438 int initial = 0;
2439 int limit = 0;
2440 int increment = 0;
2441
2442 // Parse index name and intial value
2443 if (node->getInit())
2444 {
2445 TIntermAggregate *init = node->getInit()->getAsAggregate();
2446
2447 if (init)
2448 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002449 TIntermSequence *sequence = init->getSequence();
2450 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002451
2452 if (variable && variable->getQualifier() == EvqTemporary)
2453 {
2454 TIntermBinary *assign = variable->getAsBinaryNode();
2455
2456 if (assign->getOp() == EOpInitialize)
2457 {
2458 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2459 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2460
2461 if (symbol && constant)
2462 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002463 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002464 {
2465 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002466 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002467 }
2468 }
2469 }
2470 }
2471 }
2472 }
2473
2474 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002475 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002476 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002477 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002478
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002479 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2480 {
2481 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2482
2483 if (constant)
2484 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002485 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002486 {
2487 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002488 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002489 }
2490 }
2491 }
2492 }
2493
2494 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002495 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002496 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002497 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2498 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002499
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002500 if (binaryTerminal)
2501 {
2502 TOperator op = binaryTerminal->getOp();
2503 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2504
2505 if (constant)
2506 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002507 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002508 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002509 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002510
2511 switch (op)
2512 {
2513 case EOpAddAssign: increment = value; break;
2514 case EOpSubAssign: increment = -value; break;
2515 default: UNIMPLEMENTED();
2516 }
2517 }
2518 }
2519 }
2520 else if (unaryTerminal)
2521 {
2522 TOperator op = unaryTerminal->getOp();
2523
2524 switch (op)
2525 {
2526 case EOpPostIncrement: increment = 1; break;
2527 case EOpPostDecrement: increment = -1; break;
2528 case EOpPreIncrement: increment = 1; break;
2529 case EOpPreDecrement: increment = -1; break;
2530 default: UNIMPLEMENTED();
2531 }
2532 }
2533 }
2534
2535 if (index != NULL && comparator != EOpNull && increment != 0)
2536 {
2537 if (comparator == EOpLessThanEqual)
2538 {
2539 comparator = EOpLessThan;
2540 limit += 1;
2541 }
2542
2543 if (comparator == EOpLessThan)
2544 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002545 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002546
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002547 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002548 {
2549 return false; // Not an excessive loop
2550 }
2551
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002552 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2553 mExcessiveLoopIndex = index;
2554
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002555 out << "{int ";
2556 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002557 out << ";\n"
2558 "bool Break";
2559 index->traverse(this);
2560 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002561
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002562 bool firstLoopFragment = true;
2563
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002564 while (iterations > 0)
2565 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002566 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002567
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002568 if (!firstLoopFragment)
2569 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002570 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002571 index->traverse(this);
2572 out << ") {\n";
2573 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002574
2575 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2576 {
2577 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2578 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002579
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002580 // for(int index = initial; index < clampedLimit; index += increment)
2581
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002582 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002583 index->traverse(this);
2584 out << " = ";
2585 out << initial;
2586
2587 out << "; ";
2588 index->traverse(this);
2589 out << " < ";
2590 out << clampedLimit;
2591
2592 out << "; ";
2593 index->traverse(this);
2594 out << " += ";
2595 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002596 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002597
Jamie Madill075edd82013-07-08 13:30:19 -04002598 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002599 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002600
2601 if (node->getBody())
2602 {
2603 node->getBody()->traverse(this);
2604 }
2605
Jamie Madill075edd82013-07-08 13:30:19 -04002606 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002607 out << ";}\n";
2608
2609 if (!firstLoopFragment)
2610 {
2611 out << "}\n";
2612 }
2613
2614 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002615
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002616 initial += MAX_LOOP_ITERATIONS * increment;
2617 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002618 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002619
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002620 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002621
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002622 mExcessiveLoopIndex = restoreIndex;
2623
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002624 return true;
2625 }
2626 else UNIMPLEMENTED();
2627 }
2628
2629 return false; // Not handled as an excessive loop
2630}
2631
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002632void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002633{
Jamie Madill32aab012015-01-27 14:12:26 -05002634 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002635
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002636 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002637 {
2638 out << preString;
2639 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002640 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641 {
2642 out << inString;
2643 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002644 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002645 {
2646 out << postString;
2647 }
2648}
2649
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002650void OutputHLSL::outputLineDirective(int line)
2651{
2652 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2653 {
Jamie Madill32aab012015-01-27 14:12:26 -05002654 TInfoSinkBase &out = getInfoSink();
2655
2656 out << "\n";
2657 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002658
2659 if (mContext.sourcePath)
2660 {
Jamie Madill32aab012015-01-27 14:12:26 -05002661 out << " \"" << mContext.sourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002662 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002663
Jamie Madill32aab012015-01-27 14:12:26 -05002664 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002665 }
2666}
2667
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002668TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2669{
2670 TQualifier qualifier = symbol->getQualifier();
2671 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002672 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002673
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002674 if (name.empty()) // HLSL demands named arguments, also for prototypes
2675 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002676 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002677 }
2678 else
2679 {
Jamie Madill033dae62014-06-18 12:56:28 -04002680 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002681 }
2682
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002683 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2684 {
Jamie Madill033dae62014-06-18 12:56:28 -04002685 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002686 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002687 }
2688
Jamie Madill033dae62014-06-18 12:56:28 -04002689 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690}
2691
2692TString OutputHLSL::initializer(const TType &type)
2693{
2694 TString string;
2695
Jamie Madill94bf7f22013-07-08 13:31:15 -04002696 size_t size = type.getObjectSize();
2697 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002698 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002699 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700
Jamie Madill94bf7f22013-07-08 13:31:15 -04002701 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002702 {
2703 string += ", ";
2704 }
2705 }
2706
daniel@transgaming.comead23042010-04-29 03:35:36 +00002707 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002708}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002709
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002710void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2711{
Jamie Madill32aab012015-01-27 14:12:26 -05002712 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002713
2714 if (visit == PreVisit)
2715 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002716 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002717
2718 out << name + "(";
2719 }
2720 else if (visit == InVisit)
2721 {
2722 out << ", ";
2723 }
2724 else if (visit == PostVisit)
2725 {
2726 out << ")";
2727 }
2728}
2729
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002730const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2731{
Jamie Madill32aab012015-01-27 14:12:26 -05002732 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002733
Jamie Madill98493dd2013-07-08 14:39:03 -04002734 const TStructure* structure = type.getStruct();
2735 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002736 {
Jamie Madill033dae62014-06-18 12:56:28 -04002737 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002738
Jamie Madill98493dd2013-07-08 14:39:03 -04002739 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002740
Jamie Madill98493dd2013-07-08 14:39:03 -04002741 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002742 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002743 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002744 constUnion = writeConstantUnion(*fieldType, constUnion);
2745
Jamie Madill98493dd2013-07-08 14:39:03 -04002746 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002747 {
2748 out << ", ";
2749 }
2750 }
2751
2752 out << ")";
2753 }
2754 else
2755 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002756 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002757 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002758
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002759 if (writeType)
2760 {
Jamie Madill033dae62014-06-18 12:56:28 -04002761 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002762 }
2763
Jamie Madill94bf7f22013-07-08 13:31:15 -04002764 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002765 {
2766 switch (constUnion->getType())
2767 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002768 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002769 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002770 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002771 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002772 default: UNREACHABLE();
2773 }
2774
2775 if (i != size - 1)
2776 {
2777 out << ", ";
2778 }
2779 }
2780
2781 if (writeType)
2782 {
2783 out << ")";
2784 }
2785 }
2786
2787 return constUnion;
2788}
2789
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002790void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2791{
2792 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2793 outputTriplet(visit, preString.c_str(), ", ", ")");
2794}
2795
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796}