blob: 065719bf9cb835d5280955d2600c6f5359206d16 [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
Jamie Madill37997142015-01-28 10:06:34 -0500176 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500177 mInfoSinkStack.push(&mBody);
178 mContext.treeRoot->traverse(this);
179 mInfoSinkStack.pop();
180
Jamie Madill37997142015-01-28 10:06:34 -0500181 mInfoSinkStack.push(&mFooter);
182 if (!mDeferredGlobalInitializers.empty())
183 {
184 writeDeferredGlobalInitializers(mFooter);
185 }
186 mInfoSinkStack.pop();
187
Jamie Madill32aab012015-01-27 14:12:26 -0500188 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200189 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500190 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000191
Olli Etuahoe17e3192015-01-02 12:47:59 +0200192 TInfoSinkBase& sink = mContext.infoSink().obj;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200193 sink << mHeader.c_str();
194 sink << mBody.c_str();
Jamie Madill37997142015-01-28 10:06:34 -0500195 sink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200196
197 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000198}
199
Jamie Madill570e04d2013-06-21 09:15:33 -0400200void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
201{
202 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
203 {
204 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
205
Jamie Madill32aab012015-01-27 14:12:26 -0500206 TInfoSinkBase structInfoSink;
207 mInfoSinkStack.push(&structInfoSink);
208
Jamie Madill570e04d2013-06-21 09:15:33 -0400209 // This will mark the necessary block elements as referenced
210 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500211
212 TString structName(structInfoSink.c_str());
213 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400214
215 mFlaggedStructOriginalNames[flaggedNode] = structName;
216
217 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
218 {
219 structName.erase(pos, 1);
220 }
221
222 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
223 }
224}
225
Jamie Madill4e1fd412014-07-10 17:50:10 -0400226const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
227{
228 return mUniformHLSL->getInterfaceBlockRegisterMap();
229}
230
Jamie Madill9fe25e92014-07-18 10:33:08 -0400231const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
232{
233 return mUniformHLSL->getUniformRegisterMap();
234}
235
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000236int OutputHLSL::vectorSize(const TType &type) const
237{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000238 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000239 int arraySize = type.isArray() ? type.getArraySize() : 1;
240
241 return elementSize * arraySize;
242}
243
Jamie Madill98493dd2013-07-08 14:39:03 -0400244TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400245{
246 TString init;
247
248 TString preIndentString;
249 TString fullIndentString;
250
251 for (int spaces = 0; spaces < (indent * 4); spaces++)
252 {
253 preIndentString += ' ';
254 }
255
256 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
257 {
258 fullIndentString += ' ';
259 }
260
261 init += preIndentString + "{\n";
262
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 const TFieldList &fields = structure.fields();
264 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400265 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400266 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400267 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400268 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400269
Jamie Madill98493dd2013-07-08 14:39:03 -0400270 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400271 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400272 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400273 }
274 else
275 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400276 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400277 }
278 }
279
280 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
281
282 return init;
283}
284
Olli Etuahoe17e3192015-01-02 12:47:59 +0200285void OutputHLSL::header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286{
Jamie Madill32aab012015-01-27 14:12:26 -0500287 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000289 TString varyings;
290 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400291 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000292
Jamie Madill829f59e2013-11-13 19:40:54 -0500293 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400294 {
295 TIntermTyped *structNode = flaggedStructIt->first;
296 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400297 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400298 const TString &originalName = mFlaggedStructOriginalNames[structNode];
299
Jamie Madill033dae62014-06-18 12:56:28 -0400300 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400301 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400302 flaggedStructs += "\n";
303 }
304
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000305 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
306 {
307 const TType &type = varying->second->getType();
308 const TString &name = varying->second->getSymbol();
309
310 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400311 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
312 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000313 }
314
315 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
316 {
317 const TType &type = attribute->second->getType();
318 const TString &name = attribute->second->getSymbol();
319
Jamie Madill033dae62014-06-18 12:56:28 -0400320 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000321 }
322
Jamie Madill8daaba12014-06-13 10:04:33 -0400323 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400324
Jamie Madillf91ce812014-06-13 10:04:34 -0400325 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
326 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
327
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500328 if (mUsesDiscardRewriting)
329 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400330 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500331 }
332
Nicolas Capens655fe362014-04-11 13:12:34 -0400333 if (mUsesNestedBreak)
334 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400335 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400336 }
337
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400338 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
339 "#define LOOP [loop]\n"
340 "#define FLATTEN [flatten]\n"
341 "#else\n"
342 "#define LOOP\n"
343 "#define FLATTEN\n"
344 "#endif\n";
345
Jamie Madill183bde52014-07-02 15:31:19 -0400346 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000348 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000349 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000350
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000351 out << "// Varyings\n";
352 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400353 out << "\n";
354
355 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000356 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500357 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000358 {
Jamie Madill46131a32013-06-20 11:55:50 -0400359 const TString &variableName = outputVariableIt->first;
360 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400361
Jamie Madill033dae62014-06-18 12:56:28 -0400362 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400363 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000364 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000365 }
Jamie Madill46131a32013-06-20 11:55:50 -0400366 else
367 {
368 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
369
370 out << "static float4 gl_Color[" << numColorValues << "] =\n"
371 "{\n";
372 for (unsigned int i = 0; i < numColorValues; i++)
373 {
374 out << " float4(0, 0, 0, 0)";
375 if (i + 1 != numColorValues)
376 {
377 out << ",";
378 }
379 out << "\n";
380 }
381
382 out << "};\n";
383 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000384
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400385 if (mUsesFragDepth)
386 {
387 out << "static float gl_Depth = 0.0;\n";
388 }
389
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000390 if (mUsesFragCoord)
391 {
392 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
393 }
394
395 if (mUsesPointCoord)
396 {
397 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
398 }
399
400 if (mUsesFrontFacing)
401 {
402 out << "static bool gl_FrontFacing = false;\n";
403 }
404
405 out << "\n";
406
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000407 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000408 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000409 out << "struct gl_DepthRangeParameters\n"
410 "{\n"
411 " float near;\n"
412 " float far;\n"
413 " float diff;\n"
414 "};\n"
415 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000416 }
417
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000418 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000419 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000420 out << "cbuffer DriverConstants : register(b1)\n"
421 "{\n";
422
423 if (mUsesDepthRange)
424 {
425 out << " float3 dx_DepthRange : packoffset(c0);\n";
426 }
427
428 if (mUsesFragCoord)
429 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000430 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000431 }
432
433 if (mUsesFragCoord || mUsesFrontFacing)
434 {
435 out << " float3 dx_DepthFront : packoffset(c2);\n";
436 }
437
438 out << "};\n";
439 }
440 else
441 {
442 if (mUsesDepthRange)
443 {
444 out << "uniform float3 dx_DepthRange : register(c0);";
445 }
446
447 if (mUsesFragCoord)
448 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000449 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000450 }
451
452 if (mUsesFragCoord || mUsesFrontFacing)
453 {
454 out << "uniform float3 dx_DepthFront : register(c2);\n";
455 }
456 }
457
458 out << "\n";
459
460 if (mUsesDepthRange)
461 {
462 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
463 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000464 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000465
Jamie Madillf91ce812014-06-13 10:04:34 -0400466 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000467 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400468 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000469 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400470 out << flaggedStructs;
471 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000472 }
473
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000474 if (usingMRTExtension && mNumRenderTargets > 1)
475 {
476 out << "#define GL_USES_MRT\n";
477 }
478
479 if (mUsesFragColor)
480 {
481 out << "#define GL_USES_FRAG_COLOR\n";
482 }
483
484 if (mUsesFragData)
485 {
486 out << "#define GL_USES_FRAG_DATA\n";
487 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000489 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000491 out << "// Attributes\n";
492 out << attributes;
493 out << "\n"
494 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400495
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000496 if (mUsesPointSize)
497 {
498 out << "static float gl_PointSize = float(1);\n";
499 }
500
501 out << "\n"
502 "// Varyings\n";
503 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000504 out << "\n";
505
506 if (mUsesDepthRange)
507 {
508 out << "struct gl_DepthRangeParameters\n"
509 "{\n"
510 " float near;\n"
511 " float far;\n"
512 " float diff;\n"
513 "};\n"
514 "\n";
515 }
516
517 if (mOutputType == SH_HLSL11_OUTPUT)
518 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800519 out << "cbuffer DriverConstants : register(b1)\n"
520 "{\n";
521
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000522 if (mUsesDepthRange)
523 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800524 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000525 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800526
Cooper Partine6664f02015-01-09 16:22:24 -0800527 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800528 // However, we declare it for all shaders (including Feature Level 10+).
529 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
530 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800531 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800532
533 out << "};\n"
534 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000535 }
536 else
537 {
538 if (mUsesDepthRange)
539 {
540 out << "uniform float3 dx_DepthRange : register(c0);\n";
541 }
542
Cooper Partine6664f02015-01-09 16:22:24 -0800543 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
544 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000545 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000546 }
547
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000548 if (mUsesDepthRange)
549 {
550 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
551 "\n";
552 }
553
Jamie Madillf91ce812014-06-13 10:04:34 -0400554 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000555 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400556 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000557 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400558 out << flaggedStructs;
559 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000560 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400561 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000562
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400563 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
564 {
565 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400566 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000567 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400568 switch(textureFunction->sampler)
569 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400570 case EbtSampler2D: out << "int2 "; break;
571 case EbtSampler3D: out << "int3 "; break;
572 case EbtSamplerCube: out << "int2 "; break;
573 case EbtSampler2DArray: out << "int3 "; break;
574 case EbtISampler2D: out << "int2 "; break;
575 case EbtISampler3D: out << "int3 "; break;
576 case EbtISamplerCube: out << "int2 "; break;
577 case EbtISampler2DArray: out << "int3 "; break;
578 case EbtUSampler2D: out << "int2 "; break;
579 case EbtUSampler3D: out << "int3 "; break;
580 case EbtUSamplerCube: out << "int2 "; break;
581 case EbtUSampler2DArray: out << "int3 "; break;
582 case EbtSampler2DShadow: out << "int2 "; break;
583 case EbtSamplerCubeShadow: out << "int2 "; break;
584 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400585 default: UNREACHABLE();
586 }
587 }
588 else // Sampling function
589 {
590 switch(textureFunction->sampler)
591 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400592 case EbtSampler2D: out << "float4 "; break;
593 case EbtSampler3D: out << "float4 "; break;
594 case EbtSamplerCube: out << "float4 "; break;
595 case EbtSampler2DArray: out << "float4 "; break;
596 case EbtISampler2D: out << "int4 "; break;
597 case EbtISampler3D: out << "int4 "; break;
598 case EbtISamplerCube: out << "int4 "; break;
599 case EbtISampler2DArray: out << "int4 "; break;
600 case EbtUSampler2D: out << "uint4 "; break;
601 case EbtUSampler3D: out << "uint4 "; break;
602 case EbtUSamplerCube: out << "uint4 "; break;
603 case EbtUSampler2DArray: out << "uint4 "; break;
604 case EbtSampler2DShadow: out << "float "; break;
605 case EbtSamplerCubeShadow: out << "float "; break;
606 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400607 default: UNREACHABLE();
608 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000609 }
610
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400611 // Function name
612 out << textureFunction->name();
613
614 // Argument list
615 int hlslCoords = 4;
616
617 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000618 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400619 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000620 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400621 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
622 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
623 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000624 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400625
Nicolas Capens75fb4752013-07-10 15:14:47 -0400626 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000627 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400628 case TextureFunction::IMPLICIT: break;
629 case TextureFunction::BIAS: hlslCoords = 4; break;
630 case TextureFunction::LOD: hlslCoords = 4; break;
631 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400632 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400633 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000634 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400635 }
636 else if (mOutputType == SH_HLSL11_OUTPUT)
637 {
638 switch(textureFunction->sampler)
639 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400640 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
641 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
642 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
643 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
644 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
645 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500646 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400647 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
648 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
649 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500650 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400651 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
652 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
653 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
654 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400655 default: UNREACHABLE();
656 }
657 }
658 else UNREACHABLE();
659
Nicolas Capensfc014542014-02-18 14:47:13 -0500660 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400661 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500662 switch(textureFunction->coords)
663 {
664 case 2: out << ", int2 t"; break;
665 case 3: out << ", int3 t"; break;
666 default: UNREACHABLE();
667 }
668 }
669 else // Floating-point coordinates (except textureSize)
670 {
671 switch(textureFunction->coords)
672 {
673 case 1: out << ", int lod"; break; // textureSize()
674 case 2: out << ", float2 t"; break;
675 case 3: out << ", float3 t"; break;
676 case 4: out << ", float4 t"; break;
677 default: UNREACHABLE();
678 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000679 }
680
Nicolas Capensd11d5492014-02-19 17:06:10 -0500681 if (textureFunction->method == TextureFunction::GRAD)
682 {
683 switch(textureFunction->sampler)
684 {
685 case EbtSampler2D:
686 case EbtISampler2D:
687 case EbtUSampler2D:
688 case EbtSampler2DArray:
689 case EbtISampler2DArray:
690 case EbtUSampler2DArray:
691 case EbtSampler2DShadow:
692 case EbtSampler2DArrayShadow:
693 out << ", float2 ddx, float2 ddy";
694 break;
695 case EbtSampler3D:
696 case EbtISampler3D:
697 case EbtUSampler3D:
698 case EbtSamplerCube:
699 case EbtISamplerCube:
700 case EbtUSamplerCube:
701 case EbtSamplerCubeShadow:
702 out << ", float3 ddx, float3 ddy";
703 break;
704 default: UNREACHABLE();
705 }
706 }
707
Nicolas Capens75fb4752013-07-10 15:14:47 -0400708 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000709 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400710 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400711 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400712 case TextureFunction::LOD: out << ", float lod"; break;
713 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400714 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400715 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500716 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500717 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400718 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000719 }
720
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500721 if (textureFunction->offset)
722 {
723 switch(textureFunction->sampler)
724 {
725 case EbtSampler2D: out << ", int2 offset"; break;
726 case EbtSampler3D: out << ", int3 offset"; break;
727 case EbtSampler2DArray: out << ", int2 offset"; break;
728 case EbtISampler2D: out << ", int2 offset"; break;
729 case EbtISampler3D: out << ", int3 offset"; break;
730 case EbtISampler2DArray: out << ", int2 offset"; break;
731 case EbtUSampler2D: out << ", int2 offset"; break;
732 case EbtUSampler3D: out << ", int3 offset"; break;
733 case EbtUSampler2DArray: out << ", int2 offset"; break;
734 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500735 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500736 default: UNREACHABLE();
737 }
738 }
739
Nicolas Capens84cfa122014-04-14 13:48:45 -0400740 if (textureFunction->method == TextureFunction::BIAS ||
741 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500742 {
743 out << ", float bias";
744 }
745
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400746 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400747 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400748
Nicolas Capens75fb4752013-07-10 15:14:47 -0400749 if (textureFunction->method == TextureFunction::SIZE)
750 {
751 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
752 {
753 if (IsSamplerArray(textureFunction->sampler))
754 {
755 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
756 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
757 }
758 else
759 {
760 out << " uint width; uint height; uint numberOfLevels;\n"
761 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
762 }
763 }
764 else if (IsSampler3D(textureFunction->sampler))
765 {
766 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
767 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
768 }
769 else UNREACHABLE();
770
771 switch(textureFunction->sampler)
772 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400773 case EbtSampler2D: out << " return int2(width, height);"; break;
774 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
775 case EbtSamplerCube: out << " return int2(width, height);"; break;
776 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
777 case EbtISampler2D: out << " return int2(width, height);"; break;
778 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
779 case EbtISamplerCube: out << " return int2(width, height);"; break;
780 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
781 case EbtUSampler2D: out << " return int2(width, height);"; break;
782 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
783 case EbtUSamplerCube: out << " return int2(width, height);"; break;
784 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
785 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
786 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
787 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400788 default: UNREACHABLE();
789 }
790 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400791 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400792 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500793 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
794 {
795 out << " float width; float height; float layers; float levels;\n";
796
797 out << " uint mip = 0;\n";
798
799 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
800
801 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
802 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
803 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
804 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
805
806 // FACE_POSITIVE_X = 000b
807 // FACE_NEGATIVE_X = 001b
808 // FACE_POSITIVE_Y = 010b
809 // FACE_NEGATIVE_Y = 011b
810 // FACE_POSITIVE_Z = 100b
811 // FACE_NEGATIVE_Z = 101b
812 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
813
814 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
815 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
816 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
817
818 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
819 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
820 }
821 else if (IsIntegerSampler(textureFunction->sampler) &&
822 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400823 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400824 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400825 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400826 if (IsSamplerArray(textureFunction->sampler))
827 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400828 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400829
Nicolas Capens9edebd62013-08-06 10:59:10 -0400830 if (textureFunction->method == TextureFunction::LOD0)
831 {
832 out << " uint mip = 0;\n";
833 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400834 else if (textureFunction->method == TextureFunction::LOD0BIAS)
835 {
836 out << " uint mip = bias;\n";
837 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400838 else
839 {
840 if (textureFunction->method == TextureFunction::IMPLICIT ||
841 textureFunction->method == TextureFunction::BIAS)
842 {
843 out << " x.GetDimensions(0, width, height, layers, levels);\n"
844 " float2 tSized = float2(t.x * width, t.y * height);\n"
845 " float dx = length(ddx(tSized));\n"
846 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500847 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400848
849 if (textureFunction->method == TextureFunction::BIAS)
850 {
851 out << " lod += bias;\n";
852 }
853 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500854 else if (textureFunction->method == TextureFunction::GRAD)
855 {
856 out << " x.GetDimensions(0, width, height, layers, levels);\n"
857 " float lod = log2(max(length(ddx), length(ddy)));\n";
858 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400859
860 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
861 }
862
863 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400864 }
865 else
866 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400867 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400868
Nicolas Capens9edebd62013-08-06 10:59:10 -0400869 if (textureFunction->method == TextureFunction::LOD0)
870 {
871 out << " uint mip = 0;\n";
872 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400873 else if (textureFunction->method == TextureFunction::LOD0BIAS)
874 {
875 out << " uint mip = bias;\n";
876 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400877 else
878 {
879 if (textureFunction->method == TextureFunction::IMPLICIT ||
880 textureFunction->method == TextureFunction::BIAS)
881 {
882 out << " x.GetDimensions(0, width, height, levels);\n"
883 " float2 tSized = float2(t.x * width, t.y * height);\n"
884 " float dx = length(ddx(tSized));\n"
885 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500886 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400887
888 if (textureFunction->method == TextureFunction::BIAS)
889 {
890 out << " lod += bias;\n";
891 }
892 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500893 else if (textureFunction->method == TextureFunction::LOD)
894 {
895 out << " x.GetDimensions(0, width, height, levels);\n";
896 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500897 else if (textureFunction->method == TextureFunction::GRAD)
898 {
899 out << " x.GetDimensions(0, width, height, levels);\n"
900 " float lod = log2(max(length(ddx), length(ddy)));\n";
901 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400902
903 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
904 }
905
906 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400907 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400908 }
909 else if (IsSampler3D(textureFunction->sampler))
910 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400911 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400912
Nicolas Capens9edebd62013-08-06 10:59:10 -0400913 if (textureFunction->method == TextureFunction::LOD0)
914 {
915 out << " uint mip = 0;\n";
916 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400917 else if (textureFunction->method == TextureFunction::LOD0BIAS)
918 {
919 out << " uint mip = bias;\n";
920 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400921 else
922 {
923 if (textureFunction->method == TextureFunction::IMPLICIT ||
924 textureFunction->method == TextureFunction::BIAS)
925 {
926 out << " x.GetDimensions(0, width, height, depth, levels);\n"
927 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
928 " float dx = length(ddx(tSized));\n"
929 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500930 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400931
932 if (textureFunction->method == TextureFunction::BIAS)
933 {
934 out << " lod += bias;\n";
935 }
936 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500937 else if (textureFunction->method == TextureFunction::GRAD)
938 {
939 out << " x.GetDimensions(0, width, height, depth, levels);\n"
940 " float lod = log2(max(length(ddx), length(ddy)));\n";
941 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400942
943 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
944 }
945
946 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400947 }
948 else UNREACHABLE();
949 }
950
951 out << " return ";
952
953 // HLSL intrinsic
954 if (mOutputType == SH_HLSL9_OUTPUT)
955 {
956 switch(textureFunction->sampler)
957 {
958 case EbtSampler2D: out << "tex2D"; break;
959 case EbtSamplerCube: out << "texCUBE"; break;
960 default: UNREACHABLE();
961 }
962
Nicolas Capens75fb4752013-07-10 15:14:47 -0400963 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400964 {
965 case TextureFunction::IMPLICIT: out << "(s, "; break;
966 case TextureFunction::BIAS: out << "bias(s, "; break;
967 case TextureFunction::LOD: out << "lod(s, "; break;
968 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400969 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400970 default: UNREACHABLE();
971 }
972 }
973 else if (mOutputType == SH_HLSL11_OUTPUT)
974 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500975 if (textureFunction->method == TextureFunction::GRAD)
976 {
977 if (IsIntegerSampler(textureFunction->sampler))
978 {
979 out << "x.Load(";
980 }
981 else if (IsShadowSampler(textureFunction->sampler))
982 {
983 out << "x.SampleCmpLevelZero(s, ";
984 }
985 else
986 {
987 out << "x.SampleGrad(s, ";
988 }
989 }
990 else if (IsIntegerSampler(textureFunction->sampler) ||
991 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400992 {
993 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400994 }
Nicolas Capenscb127d32013-07-15 17:26:18 -0400995 else if (IsShadowSampler(textureFunction->sampler))
996 {
997 out << "x.SampleCmp(s, ";
998 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400999 else
1000 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001001 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001002 {
1003 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1004 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1005 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1006 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001007 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001008 default: UNREACHABLE();
1009 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001010 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001011 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001012 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001013
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001014 // Integer sampling requires integer addresses
1015 TString addressx = "";
1016 TString addressy = "";
1017 TString addressz = "";
1018 TString close = "";
1019
Nicolas Capensfc014542014-02-18 14:47:13 -05001020 if (IsIntegerSampler(textureFunction->sampler) ||
1021 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001022 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001023 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001024 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001025 case 2: out << "int3("; break;
1026 case 3: out << "int4("; break;
1027 default: UNREACHABLE();
1028 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001029
Nicolas Capensfc014542014-02-18 14:47:13 -05001030 // Convert from normalized floating-point to integer
1031 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001032 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001033 addressx = "int(floor(width * frac((";
1034 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001035
Nicolas Capensfc014542014-02-18 14:47:13 -05001036 if (IsSamplerArray(textureFunction->sampler))
1037 {
1038 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1039 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001040 else if (IsSamplerCube(textureFunction->sampler))
1041 {
1042 addressz = "((((";
1043 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001044 else
1045 {
1046 addressz = "int(floor(depth * frac((";
1047 }
1048
1049 close = "))))";
1050 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001051 }
1052 else
1053 {
1054 switch(hlslCoords)
1055 {
1056 case 2: out << "float2("; break;
1057 case 3: out << "float3("; break;
1058 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001059 default: UNREACHABLE();
1060 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001061 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001062
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001063 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001064
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001065 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001066 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001067 switch(textureFunction->coords)
1068 {
1069 case 3: proj = " / t.z"; break;
1070 case 4: proj = " / t.w"; break;
1071 default: UNREACHABLE();
1072 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001073 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001074
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001075 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001076
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001077 if (mOutputType == SH_HLSL9_OUTPUT)
1078 {
1079 if (hlslCoords >= 3)
1080 {
1081 if (textureFunction->coords < 3)
1082 {
1083 out << ", 0";
1084 }
1085 else
1086 {
1087 out << ", t.z" + proj;
1088 }
1089 }
1090
1091 if (hlslCoords == 4)
1092 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001093 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001094 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001095 case TextureFunction::BIAS: out << ", bias"; break;
1096 case TextureFunction::LOD: out << ", lod"; break;
1097 case TextureFunction::LOD0: out << ", 0"; break;
1098 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001099 default: UNREACHABLE();
1100 }
1101 }
1102
1103 out << "));\n";
1104 }
1105 else if (mOutputType == SH_HLSL11_OUTPUT)
1106 {
1107 if (hlslCoords >= 3)
1108 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001109 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1110 {
1111 out << ", face";
1112 }
1113 else
1114 {
1115 out << ", " + addressz + ("t.z" + proj) + close;
1116 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001117 }
1118
Nicolas Capensd11d5492014-02-19 17:06:10 -05001119 if (textureFunction->method == TextureFunction::GRAD)
1120 {
1121 if (IsIntegerSampler(textureFunction->sampler))
1122 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001123 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001124 }
1125 else if (IsShadowSampler(textureFunction->sampler))
1126 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001127 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001128 switch(textureFunction->coords)
1129 {
1130 case 3: out << "), t.z"; break;
1131 case 4: out << "), t.w"; break;
1132 default: UNREACHABLE();
1133 }
1134 }
1135 else
1136 {
1137 out << "), ddx, ddy";
1138 }
1139 }
1140 else if (IsIntegerSampler(textureFunction->sampler) ||
1141 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001142 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001143 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001144 }
1145 else if (IsShadowSampler(textureFunction->sampler))
1146 {
1147 // Compare value
1148 switch(textureFunction->coords)
1149 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001150 case 3: out << "), t.z"; break;
1151 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001152 default: UNREACHABLE();
1153 }
1154 }
1155 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001156 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001157 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001158 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001159 case TextureFunction::IMPLICIT: out << ")"; break;
1160 case TextureFunction::BIAS: out << "), bias"; break;
1161 case TextureFunction::LOD: out << "), lod"; break;
1162 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001163 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001164 default: UNREACHABLE();
1165 }
1166 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001167
1168 if (textureFunction->offset)
1169 {
1170 out << ", offset";
1171 }
1172
1173 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001174 }
1175 else UNREACHABLE();
1176 }
1177
1178 out << "\n"
1179 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001180 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001181 }
1182
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001183 if (mUsesFragCoord)
1184 {
1185 out << "#define GL_USES_FRAG_COORD\n";
1186 }
1187
1188 if (mUsesPointCoord)
1189 {
1190 out << "#define GL_USES_POINT_COORD\n";
1191 }
1192
1193 if (mUsesFrontFacing)
1194 {
1195 out << "#define GL_USES_FRONT_FACING\n";
1196 }
1197
1198 if (mUsesPointSize)
1199 {
1200 out << "#define GL_USES_POINT_SIZE\n";
1201 }
1202
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001203 if (mUsesFragDepth)
1204 {
1205 out << "#define GL_USES_FRAG_DEPTH\n";
1206 }
1207
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001208 if (mUsesDepthRange)
1209 {
1210 out << "#define GL_USES_DEPTH_RANGE\n";
1211 }
1212
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001213 if (mUsesXor)
1214 {
1215 out << "bool xor(bool p, bool q)\n"
1216 "{\n"
1217 " return (p || q) && !(p && q);\n"
1218 "}\n"
1219 "\n";
1220 }
1221
Olli Etuaho80a5a6c2015-01-12 15:35:27 +02001222 builtInFunctionEmulator->OutputEmulatedFunctionDefinition(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001223}
1224
1225void OutputHLSL::visitSymbol(TIntermSymbol *node)
1226{
Jamie Madill32aab012015-01-27 14:12:26 -05001227 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001228
Jamie Madill570e04d2013-06-21 09:15:33 -04001229 // Handle accessing std140 structs by value
1230 if (mFlaggedStructMappedNames.count(node) > 0)
1231 {
1232 out << mFlaggedStructMappedNames[node];
1233 return;
1234 }
1235
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001236 TString name = node->getSymbol();
1237
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001238 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001239 {
1240 mUsesDepthRange = true;
1241 out << name;
1242 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001243 else
1244 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001245 TQualifier qualifier = node->getQualifier();
1246
1247 if (qualifier == EvqUniform)
1248 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001249 const TType& nodeType = node->getType();
1250 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1251
1252 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001253 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001254 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001255 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001256 else
1257 {
1258 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001259 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001260
Jamie Madill033dae62014-06-18 12:56:28 -04001261 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001262 }
Jamie Madill19571812013-08-12 15:26:34 -07001263 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001264 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001265 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001266 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001267 }
Jamie Madill033dae62014-06-18 12:56:28 -04001268 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001269 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001270 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001271 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001272 }
Jamie Madill19571812013-08-12 15:26:34 -07001273 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001274 {
1275 mReferencedOutputVariables[name] = node;
1276 out << "out_" << name;
1277 }
1278 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001279 {
1280 out << "gl_Color[0]";
1281 mUsesFragColor = true;
1282 }
1283 else if (qualifier == EvqFragData)
1284 {
1285 out << "gl_Color";
1286 mUsesFragData = true;
1287 }
1288 else if (qualifier == EvqFragCoord)
1289 {
1290 mUsesFragCoord = true;
1291 out << name;
1292 }
1293 else if (qualifier == EvqPointCoord)
1294 {
1295 mUsesPointCoord = true;
1296 out << name;
1297 }
1298 else if (qualifier == EvqFrontFacing)
1299 {
1300 mUsesFrontFacing = true;
1301 out << name;
1302 }
1303 else if (qualifier == EvqPointSize)
1304 {
1305 mUsesPointSize = true;
1306 out << name;
1307 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001308 else if (name == "gl_FragDepthEXT")
1309 {
1310 mUsesFragDepth = true;
1311 out << "gl_Depth";
1312 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001313 else if (qualifier == EvqInternal)
1314 {
1315 out << name;
1316 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001317 else
1318 {
Jamie Madill033dae62014-06-18 12:56:28 -04001319 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001320 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001321 }
1322}
1323
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001324void OutputHLSL::visitRaw(TIntermRaw *node)
1325{
Jamie Madill32aab012015-01-27 14:12:26 -05001326 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001327}
1328
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001329bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1330{
Jamie Madill32aab012015-01-27 14:12:26 -05001331 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001332
Jamie Madill570e04d2013-06-21 09:15:33 -04001333 // Handle accessing std140 structs by value
1334 if (mFlaggedStructMappedNames.count(node) > 0)
1335 {
1336 out << mFlaggedStructMappedNames[node];
1337 return false;
1338 }
1339
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001340 switch (node->getOp())
1341 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001342 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001343 case EOpInitialize:
1344 if (visit == PreVisit)
1345 {
1346 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1347 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1348 // new variable is created before the assignment is evaluated), so we need to convert
1349 // this to "float t = x, x = t;".
1350
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001351 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001352 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001353 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001354
Jamie Madill37997142015-01-28 10:06:34 -05001355 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1356 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001357 {
Jamie Madill37997142015-01-28 10:06:34 -05001358 // For variables which are not constant, defer their real initialization until
1359 // after we initialize other globals: uniforms, attributes and varyings.
1360 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1361 const TString &initString = initializer(node->getType());
1362 node->setRight(new TIntermRaw(node->getType(), initString));
1363 }
1364 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1365 {
1366 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001367 return false;
1368 }
1369 }
1370 else if (visit == InVisit)
1371 {
1372 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001373 }
1374 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001375 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1376 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1377 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1378 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1379 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1380 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001381 if (visit == PreVisit)
1382 {
1383 out << "(";
1384 }
1385 else if (visit == InVisit)
1386 {
1387 out << " = mul(";
1388 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001389 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001390 }
1391 else
1392 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001393 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001394 }
1395 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001396 case EOpMatrixTimesMatrixAssign:
1397 if (visit == PreVisit)
1398 {
1399 out << "(";
1400 }
1401 else if (visit == InVisit)
1402 {
1403 out << " = mul(";
1404 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001405 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001406 }
1407 else
1408 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001409 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001410 }
1411 break;
1412 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001413 case EOpModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001414 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1415 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1416 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1417 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1418 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001419 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001420 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001421 const TType& leftType = node->getLeft()->getType();
1422 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001423 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001424 if (visit == PreVisit)
1425 {
1426 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1427 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001428 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001429 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001430 return false;
1431 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001432 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001433 else
1434 {
1435 outputTriplet(visit, "", "[", "]");
1436 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001437 }
1438 break;
1439 case EOpIndexIndirect:
1440 // We do not currently support indirect references to interface blocks
1441 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1442 outputTriplet(visit, "", "[", "]");
1443 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001444 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001445 if (visit == InVisit)
1446 {
1447 const TStructure* structure = node->getLeft()->getType().getStruct();
1448 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1449 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001450 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001451
1452 return false;
1453 }
1454 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001455 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001456 if (visit == InVisit)
1457 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001458 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1459 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1460 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001461 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001462
1463 return false;
1464 }
1465 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001466 case EOpVectorSwizzle:
1467 if (visit == InVisit)
1468 {
1469 out << ".";
1470
1471 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1472
1473 if (swizzle)
1474 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001475 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001476
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001477 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001478 {
1479 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1480
1481 if (element)
1482 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001483 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001484
1485 switch (i)
1486 {
1487 case 0: out << "x"; break;
1488 case 1: out << "y"; break;
1489 case 2: out << "z"; break;
1490 case 3: out << "w"; break;
1491 default: UNREACHABLE();
1492 }
1493 }
1494 else UNREACHABLE();
1495 }
1496 }
1497 else UNREACHABLE();
1498
1499 return false; // Fully processed
1500 }
1501 break;
1502 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1503 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1504 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1505 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001506 case EOpMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001507 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1508 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1509 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1510 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1511 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001512 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001513 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001514 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001515 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001516 if (node->getOp() == EOpEqual)
1517 {
1518 outputTriplet(visit, "(", " == ", ")");
1519 }
1520 else
1521 {
1522 outputTriplet(visit, "(", " != ", ")");
1523 }
1524 }
1525 else if (node->getLeft()->getBasicType() == EbtStruct)
1526 {
1527 if (node->getOp() == EOpEqual)
1528 {
1529 out << "(";
1530 }
1531 else
1532 {
1533 out << "!(";
1534 }
1535
Jamie Madill98493dd2013-07-08 14:39:03 -04001536 const TStructure &structure = *node->getLeft()->getType().getStruct();
1537 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001538
Jamie Madill98493dd2013-07-08 14:39:03 -04001539 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001540 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001541 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001542
1543 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001544 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001545 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001546 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001547
Jamie Madill98493dd2013-07-08 14:39:03 -04001548 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001549 {
1550 out << " && ";
1551 }
1552 }
1553
1554 out << ")";
1555
1556 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001557 }
1558 else
1559 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001560 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001561
1562 if (node->getOp() == EOpEqual)
1563 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001564 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001565 }
1566 else
1567 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001568 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001569 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001570 }
1571 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001572 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1573 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1574 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1575 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1576 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001577 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001578 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1579 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001580 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001581 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001582 if (node->getRight()->hasSideEffects())
1583 {
1584 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1585 return false;
1586 }
1587 else
1588 {
1589 outputTriplet(visit, "(", " || ", ")");
1590 return true;
1591 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001592 case EOpLogicalXor:
1593 mUsesXor = true;
1594 outputTriplet(visit, "xor(", ", ", ")");
1595 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001596 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001597 if (node->getRight()->hasSideEffects())
1598 {
1599 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1600 return false;
1601 }
1602 else
1603 {
1604 outputTriplet(visit, "(", " && ", ")");
1605 return true;
1606 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001607 default: UNREACHABLE();
1608 }
1609
1610 return true;
1611}
1612
1613bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1614{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001615 switch (node->getOp())
1616 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001617 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001618 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001619 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1620 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001621 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001622 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1623 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1624 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1625 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001626 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1627 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1628 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1629 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1630 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1631 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1632 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1633 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001634 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1635 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1636 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1637 case EOpAsinh:
1638 ASSERT(node->getUseEmulatedFunction());
1639 writeEmulatedFunctionTriplet(visit, "asinh(");
1640 break;
1641 case EOpAcosh:
1642 ASSERT(node->getUseEmulatedFunction());
1643 writeEmulatedFunctionTriplet(visit, "acosh(");
1644 break;
1645 case EOpAtanh:
1646 ASSERT(node->getUseEmulatedFunction());
1647 writeEmulatedFunctionTriplet(visit, "atanh(");
1648 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001649 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1650 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1651 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1652 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1653 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1654 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1655 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1656 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1657 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1658 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1659 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001660 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1661 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1662 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1663 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001664 case EOpPackSnorm2x16:
1665 ASSERT(node->getUseEmulatedFunction());
1666 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1667 break;
1668 case EOpPackUnorm2x16:
1669 ASSERT(node->getUseEmulatedFunction());
1670 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1671 break;
1672 case EOpPackHalf2x16:
1673 ASSERT(node->getUseEmulatedFunction());
1674 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1675 break;
1676 case EOpUnpackSnorm2x16:
1677 ASSERT(node->getUseEmulatedFunction());
1678 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1679 break;
1680 case EOpUnpackUnorm2x16:
1681 ASSERT(node->getUseEmulatedFunction());
1682 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1683 break;
1684 case EOpUnpackHalf2x16:
1685 ASSERT(node->getUseEmulatedFunction());
1686 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1687 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001688 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1689 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001690 case EOpDFdx:
1691 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1692 {
1693 outputTriplet(visit, "(", "", ", 0.0)");
1694 }
1695 else
1696 {
1697 outputTriplet(visit, "ddx(", "", ")");
1698 }
1699 break;
1700 case EOpDFdy:
1701 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1702 {
1703 outputTriplet(visit, "(", "", ", 0.0)");
1704 }
1705 else
1706 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001707 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001708 }
1709 break;
1710 case EOpFwidth:
1711 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1712 {
1713 outputTriplet(visit, "(", "", ", 0.0)");
1714 }
1715 else
1716 {
1717 outputTriplet(visit, "fwidth(", "", ")");
1718 }
1719 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001720 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1721 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001722 case EOpInverse:
1723 ASSERT(node->getUseEmulatedFunction());
1724 writeEmulatedFunctionTriplet(visit, "inverse(");
1725 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001726
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001727 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1728 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729 default: UNREACHABLE();
1730 }
1731
1732 return true;
1733}
1734
1735bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1736{
Jamie Madill32aab012015-01-27 14:12:26 -05001737 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 switch (node->getOp())
1740 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001741 case EOpSequence:
1742 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001743 if (mInsideFunction)
1744 {
Jamie Madill075edd82013-07-08 13:30:19 -04001745 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001746 out << "{\n";
1747 }
1748
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001749 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001750 {
Jamie Madill075edd82013-07-08 13:30:19 -04001751 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001752
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001753 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001754
1755 out << ";\n";
1756 }
1757
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001758 if (mInsideFunction)
1759 {
Jamie Madill075edd82013-07-08 13:30:19 -04001760 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001761 out << "}\n";
1762 }
1763
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001764 return false;
1765 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 case EOpDeclaration:
1767 if (visit == PreVisit)
1768 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001769 TIntermSequence *sequence = node->getSequence();
1770 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001772 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001774 TStructure *structure = variable->getType().getStruct();
1775
1776 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001777 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001778 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001779 }
1780
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001781 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001782 {
Jamie Madill37997142015-01-28 10:06:34 -05001783 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784 {
Jamie Madill37997142015-01-28 10:06:34 -05001785 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001786 {
Jamie Madill37997142015-01-28 10:06:34 -05001787 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001788 }
1789
Nicolas Capensd974db42014-10-07 10:50:19 -04001790 if (!mInsideFunction)
1791 {
1792 out << "static ";
1793 }
1794
1795 out << TypeString(variable->getType()) + " ";
1796
Jamie Madill37997142015-01-28 10:06:34 -05001797 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001799 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001801 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001802 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001803 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001804 }
1805 else
1806 {
Jamie Madill37997142015-01-28 10:06:34 -05001807 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001808 }
1809
Jamie Madill37997142015-01-28 10:06:34 -05001810 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001811 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001812 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813 }
1814 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001815 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001816 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1817 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001818 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001819 }
1820 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001821 }
Jamie Madill033dae62014-06-18 12:56:28 -04001822 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001823 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001824 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001825 {
1826 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1827
1828 if (symbol)
1829 {
1830 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1831 mReferencedVaryings[symbol->getSymbol()] = symbol;
1832 }
1833 else
1834 {
1835 (*sit)->traverse(this);
1836 }
1837 }
1838 }
1839
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840 return false;
1841 }
1842 else if (visit == InVisit)
1843 {
1844 out << ", ";
1845 }
1846 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001847 case EOpInvariantDeclaration:
1848 // Do not do any translation
1849 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001850 case EOpPrototype:
1851 if (visit == PreVisit)
1852 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001853 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001854
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001855 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001856
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001857 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001858 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001859 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001860
1861 if (symbol)
1862 {
1863 out << argumentString(symbol);
1864
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001865 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001866 {
1867 out << ", ";
1868 }
1869 }
1870 else UNREACHABLE();
1871 }
1872
1873 out << ");\n";
1874
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001875 // Also prototype the Lod0 variant if needed
1876 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1877 {
1878 mOutputLod0Function = true;
1879 node->traverse(this);
1880 mOutputLod0Function = false;
1881 }
1882
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001883 return false;
1884 }
1885 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001886 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887 case EOpFunction:
1888 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001889 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001890
Jamie Madill033dae62014-06-18 12:56:28 -04001891 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001892
1893 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001895 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001897 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 {
Jamie Madill033dae62014-06-18 12:56:28 -04001899 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001900 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001901
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001902 TIntermSequence *sequence = node->getSequence();
1903 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001904
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001905 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001906 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001907 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001908
1909 if (symbol)
1910 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001911 TStructure *structure = symbol->getType().getStruct();
1912
1913 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001914 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001915 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001916 }
1917
1918 out << argumentString(symbol);
1919
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001920 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001921 {
1922 out << ", ";
1923 }
1924 }
1925 else UNREACHABLE();
1926 }
1927
1928 out << ")\n"
1929 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001930
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001931 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001932 {
1933 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001934 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001935 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001936 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001937
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001938 out << "}\n";
1939
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001940 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1941 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001942 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001943 {
1944 mOutputLod0Function = true;
1945 node->traverse(this);
1946 mOutputLod0Function = false;
1947 }
1948 }
1949
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001950 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951 }
1952 break;
1953 case EOpFunctionCall:
1954 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001955 TString name = TFunction::unmangleName(node->getName());
1956 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001957 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001958
1959 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960 {
Jamie Madill033dae62014-06-18 12:56:28 -04001961 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962 }
1963 else
1964 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001965 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001966
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001967 TextureFunction textureFunction;
1968 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001969 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04001970 textureFunction.method = TextureFunction::IMPLICIT;
1971 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001972 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001973
1974 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001975 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001976 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001977 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001978 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001979 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001980 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001981 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001982 }
Nicolas Capens46485082014-04-15 13:12:50 -04001983 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
1984 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001985 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001986 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001987 }
Nicolas Capens46485082014-04-15 13:12:50 -04001988 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001989 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001990 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001991 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001992 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04001993 else if (name == "textureSize")
1994 {
1995 textureFunction.method = TextureFunction::SIZE;
1996 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001997 else if (name == "textureOffset")
1998 {
1999 textureFunction.method = TextureFunction::IMPLICIT;
2000 textureFunction.offset = true;
2001 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002002 else if (name == "textureProjOffset")
2003 {
2004 textureFunction.method = TextureFunction::IMPLICIT;
2005 textureFunction.offset = true;
2006 textureFunction.proj = true;
2007 }
2008 else if (name == "textureLodOffset")
2009 {
2010 textureFunction.method = TextureFunction::LOD;
2011 textureFunction.offset = true;
2012 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002013 else if (name == "textureProjLodOffset")
2014 {
2015 textureFunction.method = TextureFunction::LOD;
2016 textureFunction.proj = true;
2017 textureFunction.offset = true;
2018 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002019 else if (name == "texelFetch")
2020 {
2021 textureFunction.method = TextureFunction::FETCH;
2022 }
2023 else if (name == "texelFetchOffset")
2024 {
2025 textureFunction.method = TextureFunction::FETCH;
2026 textureFunction.offset = true;
2027 }
Nicolas Capens46485082014-04-15 13:12:50 -04002028 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002029 {
2030 textureFunction.method = TextureFunction::GRAD;
2031 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002032 else if (name == "textureGradOffset")
2033 {
2034 textureFunction.method = TextureFunction::GRAD;
2035 textureFunction.offset = true;
2036 }
Nicolas Capens46485082014-04-15 13:12:50 -04002037 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002038 {
2039 textureFunction.method = TextureFunction::GRAD;
2040 textureFunction.proj = true;
2041 }
2042 else if (name == "textureProjGradOffset")
2043 {
2044 textureFunction.method = TextureFunction::GRAD;
2045 textureFunction.proj = true;
2046 textureFunction.offset = true;
2047 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002048 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002049
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002050 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002051 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002052 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2053
2054 if (textureFunction.offset)
2055 {
2056 mandatoryArgumentCount++;
2057 }
2058
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002059 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002060
Jamie Madill183bde52014-07-02 15:31:19 -04002061 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002062 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002063 if (bias)
2064 {
2065 textureFunction.method = TextureFunction::LOD0BIAS;
2066 }
2067 else
2068 {
2069 textureFunction.method = TextureFunction::LOD0;
2070 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002071 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002072 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002073 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002074 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002075 }
2076 }
2077
2078 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002079
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002080 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002082
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002083 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002084 {
2085 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2086 {
2087 out << "texture_";
2088 (*arg)->traverse(this);
2089 out << ", sampler_";
2090 }
2091
2092 (*arg)->traverse(this);
2093
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002094 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002095 {
2096 out << ", ";
2097 }
2098 }
2099
2100 out << ")";
2101
2102 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 }
2104 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002105 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002106 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2107 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2108 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2109 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2110 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2111 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2112 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2113 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2114 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2115 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2116 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2117 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2118 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2119 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2120 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2121 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2122 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2123 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2124 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002125 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002126 {
Jamie Madill033dae62014-06-18 12:56:28 -04002127 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002128 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002129 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2130 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002131 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002132 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2133 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2134 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2135 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2136 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2137 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002138 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002139 ASSERT(node->getUseEmulatedFunction());
2140 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002141 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002142 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002143 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002144 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002145 ASSERT(node->getUseEmulatedFunction());
2146 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147 break;
2148 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2149 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2150 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2151 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2152 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2153 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2154 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2155 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2156 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002157 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002158 ASSERT(node->getUseEmulatedFunction());
2159 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002160 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2162 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002163 case EOpOuterProduct:
2164 ASSERT(node->getUseEmulatedFunction());
2165 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2166 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 default: UNREACHABLE();
2169 }
2170
2171 return true;
2172}
2173
2174bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2175{
Jamie Madill32aab012015-01-27 14:12:26 -05002176 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002178 if (node->usesTernaryOperator())
2179 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002180 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002181 }
2182 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002183 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002184 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002185
Corentin Wallez80bacde2014-11-10 12:07:37 -08002186 // D3D errors when there is a gradient operation in a loop in an unflattened if
2187 // however flattening all the ifs in branch heavy shaders made D3D error too.
2188 // As a temporary workaround we flatten the ifs only if there is at least a loop
2189 // present somewhere in the shader.
2190 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2191 {
2192 out << "FLATTEN ";
2193 }
2194
2195 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002196
2197 node->getCondition()->traverse(this);
2198
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002199 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002200
Jamie Madill075edd82013-07-08 13:30:19 -04002201 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002202 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002203
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002204 bool discard = false;
2205
daniel@transgaming.combb885322010-04-15 20:45:24 +00002206 if (node->getTrueBlock())
2207 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002208 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002209
2210 // Detect true discard
2211 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002212 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213
Jamie Madill075edd82013-07-08 13:30:19 -04002214 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002215 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002216
2217 if (node->getFalseBlock())
2218 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002219 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002220
Jamie Madill075edd82013-07-08 13:30:19 -04002221 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002222 out << "{\n";
2223
Jamie Madill075edd82013-07-08 13:30:19 -04002224 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002225 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002226
Jamie Madill075edd82013-07-08 13:30:19 -04002227 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002228 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002229
2230 // Detect false discard
2231 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2232 }
2233
2234 // ANGLE issue 486: Detect problematic conditional discard
2235 if (discard && FindSideEffectRewriting::search(node))
2236 {
2237 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002238 }
2239 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240
2241 return false;
2242}
2243
2244void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2245{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002246 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002247}
2248
2249bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2250{
Nicolas Capens655fe362014-04-11 13:12:34 -04002251 mNestedLoopDepth++;
2252
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002253 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2254
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002255 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002256 {
2257 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2258 }
2259
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002260 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002261 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002262 if (handleExcessiveLoop(node))
2263 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002264 mInsideDiscontinuousLoop = wasDiscontinuous;
2265 mNestedLoopDepth--;
2266
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002267 return false;
2268 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002269 }
2270
Jamie Madill32aab012015-01-27 14:12:26 -05002271 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272
alokp@chromium.org52813552010-11-16 18:36:09 +00002273 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002275 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002276
Jamie Madill075edd82013-07-08 13:30:19 -04002277 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002278 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 }
2280 else
2281 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002282 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002283
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002284 if (node->getInit())
2285 {
2286 node->getInit()->traverse(this);
2287 }
2288
2289 out << "; ";
2290
alokp@chromium.org52813552010-11-16 18:36:09 +00002291 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002293 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 }
2295
2296 out << "; ";
2297
alokp@chromium.org52813552010-11-16 18:36:09 +00002298 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002300 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301 }
2302
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002303 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002304
Jamie Madill075edd82013-07-08 13:30:19 -04002305 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002306 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 }
2308
2309 if (node->getBody())
2310 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002311 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312 }
2313
Jamie Madill075edd82013-07-08 13:30:19 -04002314 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002315 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316
alokp@chromium.org52813552010-11-16 18:36:09 +00002317 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318 {
Jamie Madill075edd82013-07-08 13:30:19 -04002319 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002320 out << "while(\n";
2321
alokp@chromium.org52813552010-11-16 18:36:09 +00002322 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323
daniel@transgaming.com73536982012-03-21 20:45:49 +00002324 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325 }
2326
daniel@transgaming.com73536982012-03-21 20:45:49 +00002327 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002329 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002330 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002331
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 return false;
2333}
2334
2335bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2336{
Jamie Madill32aab012015-01-27 14:12:26 -05002337 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338
2339 switch (node->getFlowOp())
2340 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002341 case EOpKill:
2342 outputTriplet(visit, "discard;\n", "", "");
2343 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002344 case EOpBreak:
2345 if (visit == PreVisit)
2346 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002347 if (mNestedLoopDepth > 1)
2348 {
2349 mUsesNestedBreak = true;
2350 }
2351
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002352 if (mExcessiveLoopIndex)
2353 {
2354 out << "{Break";
2355 mExcessiveLoopIndex->traverse(this);
2356 out << " = true; break;}\n";
2357 }
2358 else
2359 {
2360 out << "break;\n";
2361 }
2362 }
2363 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002364 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002365 case EOpReturn:
2366 if (visit == PreVisit)
2367 {
2368 if (node->getExpression())
2369 {
2370 out << "return ";
2371 }
2372 else
2373 {
2374 out << "return;\n";
2375 }
2376 }
2377 else if (visit == PostVisit)
2378 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002379 if (node->getExpression())
2380 {
2381 out << ";\n";
2382 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383 }
2384 break;
2385 default: UNREACHABLE();
2386 }
2387
2388 return true;
2389}
2390
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002391void OutputHLSL::traverseStatements(TIntermNode *node)
2392{
2393 if (isSingleStatement(node))
2394 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002395 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002396 }
2397
2398 node->traverse(this);
2399}
2400
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002401bool OutputHLSL::isSingleStatement(TIntermNode *node)
2402{
2403 TIntermAggregate *aggregate = node->getAsAggregate();
2404
2405 if (aggregate)
2406 {
2407 if (aggregate->getOp() == EOpSequence)
2408 {
2409 return false;
2410 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002411 else if (aggregate->getOp() == EOpDeclaration)
2412 {
2413 // Declaring multiple comma-separated variables must be considered multiple statements
2414 // because each individual declaration has side effects which are visible in the next.
2415 return false;
2416 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002417 else
2418 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002419 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002420 {
2421 if (!isSingleStatement(*sit))
2422 {
2423 return false;
2424 }
2425 }
2426
2427 return true;
2428 }
2429 }
2430
2431 return true;
2432}
2433
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002434// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2435// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002436bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2437{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002438 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002439 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002440
2441 // Parse loops of the form:
2442 // for(int index = initial; index [comparator] limit; index += increment)
2443 TIntermSymbol *index = NULL;
2444 TOperator comparator = EOpNull;
2445 int initial = 0;
2446 int limit = 0;
2447 int increment = 0;
2448
2449 // Parse index name and intial value
2450 if (node->getInit())
2451 {
2452 TIntermAggregate *init = node->getInit()->getAsAggregate();
2453
2454 if (init)
2455 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002456 TIntermSequence *sequence = init->getSequence();
2457 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002458
2459 if (variable && variable->getQualifier() == EvqTemporary)
2460 {
2461 TIntermBinary *assign = variable->getAsBinaryNode();
2462
2463 if (assign->getOp() == EOpInitialize)
2464 {
2465 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2466 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2467
2468 if (symbol && constant)
2469 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002470 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002471 {
2472 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002473 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002474 }
2475 }
2476 }
2477 }
2478 }
2479 }
2480
2481 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002482 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002483 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002484 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002485
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002486 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2487 {
2488 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2489
2490 if (constant)
2491 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002492 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002493 {
2494 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002495 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002496 }
2497 }
2498 }
2499 }
2500
2501 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002502 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002503 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002504 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2505 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002506
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002507 if (binaryTerminal)
2508 {
2509 TOperator op = binaryTerminal->getOp();
2510 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2511
2512 if (constant)
2513 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002514 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002515 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002516 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002517
2518 switch (op)
2519 {
2520 case EOpAddAssign: increment = value; break;
2521 case EOpSubAssign: increment = -value; break;
2522 default: UNIMPLEMENTED();
2523 }
2524 }
2525 }
2526 }
2527 else if (unaryTerminal)
2528 {
2529 TOperator op = unaryTerminal->getOp();
2530
2531 switch (op)
2532 {
2533 case EOpPostIncrement: increment = 1; break;
2534 case EOpPostDecrement: increment = -1; break;
2535 case EOpPreIncrement: increment = 1; break;
2536 case EOpPreDecrement: increment = -1; break;
2537 default: UNIMPLEMENTED();
2538 }
2539 }
2540 }
2541
2542 if (index != NULL && comparator != EOpNull && increment != 0)
2543 {
2544 if (comparator == EOpLessThanEqual)
2545 {
2546 comparator = EOpLessThan;
2547 limit += 1;
2548 }
2549
2550 if (comparator == EOpLessThan)
2551 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002552 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002553
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002554 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002555 {
2556 return false; // Not an excessive loop
2557 }
2558
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002559 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2560 mExcessiveLoopIndex = index;
2561
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002562 out << "{int ";
2563 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002564 out << ";\n"
2565 "bool Break";
2566 index->traverse(this);
2567 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002568
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002569 bool firstLoopFragment = true;
2570
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002571 while (iterations > 0)
2572 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002573 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002574
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002575 if (!firstLoopFragment)
2576 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002577 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002578 index->traverse(this);
2579 out << ") {\n";
2580 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002581
2582 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2583 {
2584 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2585 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002586
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002587 // for(int index = initial; index < clampedLimit; index += increment)
2588
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002589 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002590 index->traverse(this);
2591 out << " = ";
2592 out << initial;
2593
2594 out << "; ";
2595 index->traverse(this);
2596 out << " < ";
2597 out << clampedLimit;
2598
2599 out << "; ";
2600 index->traverse(this);
2601 out << " += ";
2602 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002603 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002604
Jamie Madill075edd82013-07-08 13:30:19 -04002605 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002606 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002607
2608 if (node->getBody())
2609 {
2610 node->getBody()->traverse(this);
2611 }
2612
Jamie Madill075edd82013-07-08 13:30:19 -04002613 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002614 out << ";}\n";
2615
2616 if (!firstLoopFragment)
2617 {
2618 out << "}\n";
2619 }
2620
2621 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002622
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002623 initial += MAX_LOOP_ITERATIONS * increment;
2624 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002625 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002626
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002627 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002628
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002629 mExcessiveLoopIndex = restoreIndex;
2630
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002631 return true;
2632 }
2633 else UNIMPLEMENTED();
2634 }
2635
2636 return false; // Not handled as an excessive loop
2637}
2638
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002639void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002640{
Jamie Madill32aab012015-01-27 14:12:26 -05002641 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002642
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002643 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002644 {
2645 out << preString;
2646 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002647 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002648 {
2649 out << inString;
2650 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002651 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002652 {
2653 out << postString;
2654 }
2655}
2656
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002657void OutputHLSL::outputLineDirective(int line)
2658{
2659 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2660 {
Jamie Madill32aab012015-01-27 14:12:26 -05002661 TInfoSinkBase &out = getInfoSink();
2662
2663 out << "\n";
2664 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002665
2666 if (mContext.sourcePath)
2667 {
Jamie Madill32aab012015-01-27 14:12:26 -05002668 out << " \"" << mContext.sourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002669 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002670
Jamie Madill32aab012015-01-27 14:12:26 -05002671 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002672 }
2673}
2674
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002675TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2676{
2677 TQualifier qualifier = symbol->getQualifier();
2678 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002679 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002680
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002681 if (name.empty()) // HLSL demands named arguments, also for prototypes
2682 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002683 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002684 }
2685 else
2686 {
Jamie Madill033dae62014-06-18 12:56:28 -04002687 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002688 }
2689
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002690 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2691 {
Jamie Madill033dae62014-06-18 12:56:28 -04002692 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002693 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002694 }
2695
Jamie Madill033dae62014-06-18 12:56:28 -04002696 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002697}
2698
2699TString OutputHLSL::initializer(const TType &type)
2700{
2701 TString string;
2702
Jamie Madill94bf7f22013-07-08 13:31:15 -04002703 size_t size = type.getObjectSize();
2704 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002706 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707
Jamie Madill94bf7f22013-07-08 13:31:15 -04002708 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002709 {
2710 string += ", ";
2711 }
2712 }
2713
daniel@transgaming.comead23042010-04-29 03:35:36 +00002714 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002716
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002717void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2718{
Jamie Madill32aab012015-01-27 14:12:26 -05002719 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002720
2721 if (visit == PreVisit)
2722 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002723 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002724
2725 out << name + "(";
2726 }
2727 else if (visit == InVisit)
2728 {
2729 out << ", ";
2730 }
2731 else if (visit == PostVisit)
2732 {
2733 out << ")";
2734 }
2735}
2736
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002737const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2738{
Jamie Madill32aab012015-01-27 14:12:26 -05002739 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002740
Jamie Madill98493dd2013-07-08 14:39:03 -04002741 const TStructure* structure = type.getStruct();
2742 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002743 {
Jamie Madill033dae62014-06-18 12:56:28 -04002744 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002745
Jamie Madill98493dd2013-07-08 14:39:03 -04002746 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002747
Jamie Madill98493dd2013-07-08 14:39:03 -04002748 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002749 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002750 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002751 constUnion = writeConstantUnion(*fieldType, constUnion);
2752
Jamie Madill98493dd2013-07-08 14:39:03 -04002753 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002754 {
2755 out << ", ";
2756 }
2757 }
2758
2759 out << ")";
2760 }
2761 else
2762 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002763 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002764 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002765
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002766 if (writeType)
2767 {
Jamie Madill033dae62014-06-18 12:56:28 -04002768 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002769 }
2770
Jamie Madill94bf7f22013-07-08 13:31:15 -04002771 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002772 {
2773 switch (constUnion->getType())
2774 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002775 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002776 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002777 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002778 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002779 default: UNREACHABLE();
2780 }
2781
2782 if (i != size - 1)
2783 {
2784 out << ", ";
2785 }
2786 }
2787
2788 if (writeType)
2789 {
2790 out << ")";
2791 }
2792 }
2793
2794 return constUnion;
2795}
2796
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002797void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2798{
2799 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2800 outputTriplet(visit, preString.c_str(), ", ", ")");
2801}
2802
Jamie Madill37997142015-01-28 10:06:34 -05002803bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2804{
2805 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2806 expression->traverse(&searchSymbol);
2807
2808 if (searchSymbol.foundMatch())
2809 {
2810 // Type already printed
2811 out << "t" + str(mUniqueIndex) + " = ";
2812 expression->traverse(this);
2813 out << ", ";
2814 symbolNode->traverse(this);
2815 out << " = t" + str(mUniqueIndex);
2816
2817 mUniqueIndex++;
2818 return true;
2819 }
2820
2821 return false;
2822}
2823
2824void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2825{
2826 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2827 << "\n"
2828 << "void initializeDeferredGlobals()\n"
2829 << "{\n";
2830
2831 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2832 {
2833 TIntermSymbol *symbol = deferredGlobal.first;
2834 TIntermTyped *expression = deferredGlobal.second;
2835 ASSERT(symbol);
2836 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2837
2838 out << " " << Decorate(symbol->getSymbol()) << " = ";
2839
2840 if (!writeSameSymbolInitializer(out, symbol, expression))
2841 {
2842 ASSERT(mInfoSinkStack.top() == &out);
2843 expression->traverse(this);
2844 }
2845
2846 out << ";\n";
2847 }
2848
2849 out << "}\n"
2850 << "\n";
2851}
2852
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002853}