blob: 4410bb036aa823df811e6f89f05568492f806527 [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"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020015#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050016#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
17#include "compiler/translator/DetectDiscontinuity.h"
18#include "compiler/translator/FlagStd140Structs.h"
19#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/RewriteElseBlocks.h"
23#include "compiler/translator/SearchSymbol.h"
24#include "compiler/translator/StructureHLSL.h"
25#include "compiler/translator/TranslatorHLSL.h"
26#include "compiler/translator/UnfoldShortCircuit.h"
27#include "compiler/translator/UniformHLSL.h"
28#include "compiler/translator/UtilsHLSL.h"
29#include "compiler/translator/blocklayout.h"
30#include "compiler/translator/compilerdebug.h"
31#include "compiler/translator/util.h"
32
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033namespace sh
34{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000035
Nicolas Capense0ba27a2013-06-24 16:10:52 -040036TString OutputHLSL::TextureFunction::name() const
37{
38 TString name = "gl_texture";
39
Nicolas Capens6d232bb2013-07-08 15:56:38 -040040 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040041 {
42 name += "2D";
43 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040044 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040045 {
46 name += "3D";
47 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040048 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040049 {
50 name += "Cube";
51 }
52 else UNREACHABLE();
53
54 if (proj)
55 {
56 name += "Proj";
57 }
58
Nicolas Capensb1f45b72013-12-19 17:37:19 -050059 if (offset)
60 {
61 name += "Offset";
62 }
63
Nicolas Capens75fb4752013-07-10 15:14:47 -040064 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040065 {
Nicolas Capensfc014542014-02-18 14:47:13 -050066 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040067 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050068 case LOD: name += "Lod"; break;
69 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040070 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050071 case SIZE: name += "Size"; break;
72 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050073 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040074 default: UNREACHABLE();
75 }
76
77 return name + "(";
78}
79
80bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
81{
82 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040083 if (sampler > rhs.sampler) return false;
84
Nicolas Capense0ba27a2013-06-24 16:10:52 -040085 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040086 if (coords > rhs.coords) return false;
87
Nicolas Capense0ba27a2013-06-24 16:10:52 -040088 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040089 if (proj && !rhs.proj) return false;
90
91 if (!offset && rhs.offset) return true;
92 if (offset && !rhs.offset) return false;
93
Nicolas Capens75fb4752013-07-10 15:14:47 -040094 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040095 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040096
97 return false;
98}
99
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200100OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
101 const TExtensionBehavior &extensionBehavior,
102 const char *sourcePath, ShShaderOutput outputType,
103 int numRenderTargets, const std::vector<Uniform> &uniforms,
104 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400105 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200106 mShaderType(shaderType),
107 mShaderVersion(shaderVersion),
108 mExtensionBehavior(extensionBehavior),
109 mSourcePath(sourcePath),
110 mOutputType(outputType),
111 mNumRenderTargets(numRenderTargets),
112 mCompileOptions(compileOptions)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200114 mUnfoldShortCircuit = new UnfoldShortCircuit(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000115 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000116
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000117 mUsesFragColor = false;
118 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000119 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000120 mUsesFragCoord = false;
121 mUsesPointCoord = false;
122 mUsesFrontFacing = false;
123 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000124 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400125 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000126 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500127 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400128 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530129 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000130
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000131 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000132
133 mContainsLoopDiscontinuity = false;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800134 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000135 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000136 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400137 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000138
139 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000140
Jamie Madill8daaba12014-06-13 10:04:33 -0400141 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200142 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400143
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000144 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000145 {
Arun Patole63419392015-03-13 11:51:07 +0530146 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
147 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
148 // In both cases total 3 uniform registers need to be reserved.
149 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000150 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000151
Jamie Madillf91ce812014-06-13 10:04:34 -0400152 // Reserve registers for the default uniform block and driver constants
153 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154}
155
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000156OutputHLSL::~OutputHLSL()
157{
Jamie Madill8daaba12014-06-13 10:04:33 -0400158 SafeDelete(mUnfoldShortCircuit);
159 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400160 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000161}
162
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200163void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000164{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200165 mContainsLoopDiscontinuity = mShaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(treeRoot);
166 mContainsAnyLoop = containsAnyLoop(treeRoot);
167 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400168 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000169
Jamie Madille53c98b2014-02-03 11:57:13 -0500170 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
171 // use a vertex attribute as a condition, and some related computation in the else block.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200172 if (mOutputType == SH_HLSL9_OUTPUT && mShaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500173 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200174 RewriteElseBlocks(treeRoot);
Jamie Madille53c98b2014-02-03 11:57:13 -0500175 }
176
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200177 BuiltInFunctionEmulator builtInFunctionEmulator;
178 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200179 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500180
Jamie Madill37997142015-01-28 10:06:34 -0500181 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500182 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200183 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500184 mInfoSinkStack.pop();
185
Jamie Madill37997142015-01-28 10:06:34 -0500186 mInfoSinkStack.push(&mFooter);
187 if (!mDeferredGlobalInitializers.empty())
188 {
189 writeDeferredGlobalInitializers(mFooter);
190 }
191 mInfoSinkStack.pop();
192
Jamie Madill32aab012015-01-27 14:12:26 -0500193 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200194 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500195 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000196
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200197 objSink << mHeader.c_str();
198 objSink << mBody.c_str();
199 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200200
201 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000202}
203
Jamie Madill570e04d2013-06-21 09:15:33 -0400204void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
205{
206 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
207 {
208 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
209
Jamie Madill32aab012015-01-27 14:12:26 -0500210 TInfoSinkBase structInfoSink;
211 mInfoSinkStack.push(&structInfoSink);
212
Jamie Madill570e04d2013-06-21 09:15:33 -0400213 // This will mark the necessary block elements as referenced
214 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500215
216 TString structName(structInfoSink.c_str());
217 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400218
219 mFlaggedStructOriginalNames[flaggedNode] = structName;
220
221 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
222 {
223 structName.erase(pos, 1);
224 }
225
226 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
227 }
228}
229
Jamie Madill4e1fd412014-07-10 17:50:10 -0400230const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
231{
232 return mUniformHLSL->getInterfaceBlockRegisterMap();
233}
234
Jamie Madill9fe25e92014-07-18 10:33:08 -0400235const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
236{
237 return mUniformHLSL->getUniformRegisterMap();
238}
239
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000240int OutputHLSL::vectorSize(const TType &type) const
241{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000242 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000243 int arraySize = type.isArray() ? type.getArraySize() : 1;
244
245 return elementSize * arraySize;
246}
247
Jamie Madill98493dd2013-07-08 14:39:03 -0400248TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400249{
250 TString init;
251
252 TString preIndentString;
253 TString fullIndentString;
254
255 for (int spaces = 0; spaces < (indent * 4); spaces++)
256 {
257 preIndentString += ' ';
258 }
259
260 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
261 {
262 fullIndentString += ' ';
263 }
264
265 init += preIndentString + "{\n";
266
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 const TFieldList &fields = structure.fields();
268 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400269 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400270 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400271 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400272 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400273
Jamie Madill98493dd2013-07-08 14:39:03 -0400274 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400275 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400276 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400277 }
278 else
279 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400280 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400281 }
282 }
283
284 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
285
286 return init;
287}
288
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200289void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290{
Jamie Madill32aab012015-01-27 14:12:26 -0500291 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000293 TString varyings;
294 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400295 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000296
Jamie Madill829f59e2013-11-13 19:40:54 -0500297 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400298 {
299 TIntermTyped *structNode = flaggedStructIt->first;
300 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400301 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400302 const TString &originalName = mFlaggedStructOriginalNames[structNode];
303
Jamie Madill033dae62014-06-18 12:56:28 -0400304 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400305 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400306 flaggedStructs += "\n";
307 }
308
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000309 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
310 {
311 const TType &type = varying->second->getType();
312 const TString &name = varying->second->getSymbol();
313
314 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400315 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
316 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000317 }
318
319 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
320 {
321 const TType &type = attribute->second->getType();
322 const TString &name = attribute->second->getSymbol();
323
Jamie Madill033dae62014-06-18 12:56:28 -0400324 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000325 }
326
Jamie Madill8daaba12014-06-13 10:04:33 -0400327 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400328
Jamie Madillf91ce812014-06-13 10:04:34 -0400329 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
330 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
331
Jamie Madill55e79e02015-02-09 15:35:00 -0500332 if (!mStructEqualityFunctions.empty())
333 {
334 out << "\n// Structure equality functions\n\n";
335 for (const auto &eqFunction : mStructEqualityFunctions)
336 {
337 out << eqFunction.functionDefinition << "\n";
338 }
339 }
340
Olli Etuaho7fb49552015-03-18 17:27:44 +0200341 if (!mArrayEqualityFunctions.empty())
342 {
343 out << "\n// Array equality functions\n\n";
344 for (const auto &eqFunction : mArrayEqualityFunctions)
345 {
346 out << eqFunction.functionDefinition << "\n";
347 }
348 }
349
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500350 if (mUsesDiscardRewriting)
351 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400352 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500353 }
354
Nicolas Capens655fe362014-04-11 13:12:34 -0400355 if (mUsesNestedBreak)
356 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400357 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400358 }
359
Arun Patole44efa0b2015-03-04 17:11:05 +0530360 if (mRequiresIEEEStrictCompiling)
361 {
362 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
363 }
364
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400365 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
366 "#define LOOP [loop]\n"
367 "#define FLATTEN [flatten]\n"
368 "#else\n"
369 "#define LOOP\n"
370 "#define FLATTEN\n"
371 "#endif\n";
372
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200373 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000374 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200375 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
376 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000377
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000378 out << "// Varyings\n";
379 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400380 out << "\n";
381
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200382 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000383 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500384 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000385 {
Jamie Madill46131a32013-06-20 11:55:50 -0400386 const TString &variableName = outputVariableIt->first;
387 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400388
Jamie Madill033dae62014-06-18 12:56:28 -0400389 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400390 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000391 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000392 }
Jamie Madill46131a32013-06-20 11:55:50 -0400393 else
394 {
395 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
396
397 out << "static float4 gl_Color[" << numColorValues << "] =\n"
398 "{\n";
399 for (unsigned int i = 0; i < numColorValues; i++)
400 {
401 out << " float4(0, 0, 0, 0)";
402 if (i + 1 != numColorValues)
403 {
404 out << ",";
405 }
406 out << "\n";
407 }
408
409 out << "};\n";
410 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000411
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400412 if (mUsesFragDepth)
413 {
414 out << "static float gl_Depth = 0.0;\n";
415 }
416
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000417 if (mUsesFragCoord)
418 {
419 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
420 }
421
422 if (mUsesPointCoord)
423 {
424 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
425 }
426
427 if (mUsesFrontFacing)
428 {
429 out << "static bool gl_FrontFacing = false;\n";
430 }
431
432 out << "\n";
433
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000434 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000435 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000436 out << "struct gl_DepthRangeParameters\n"
437 "{\n"
438 " float near;\n"
439 " float far;\n"
440 " float diff;\n"
441 "};\n"
442 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000443 }
444
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000445 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000446 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000447 out << "cbuffer DriverConstants : register(b1)\n"
448 "{\n";
449
450 if (mUsesDepthRange)
451 {
452 out << " float3 dx_DepthRange : packoffset(c0);\n";
453 }
454
455 if (mUsesFragCoord)
456 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000457 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000458 }
459
460 if (mUsesFragCoord || mUsesFrontFacing)
461 {
462 out << " float3 dx_DepthFront : packoffset(c2);\n";
463 }
464
465 out << "};\n";
466 }
467 else
468 {
469 if (mUsesDepthRange)
470 {
471 out << "uniform float3 dx_DepthRange : register(c0);";
472 }
473
474 if (mUsesFragCoord)
475 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000476 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000477 }
478
479 if (mUsesFragCoord || mUsesFrontFacing)
480 {
481 out << "uniform float3 dx_DepthFront : register(c2);\n";
482 }
483 }
484
485 out << "\n";
486
487 if (mUsesDepthRange)
488 {
489 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
490 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000491 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000492
Jamie Madillf91ce812014-06-13 10:04:34 -0400493 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000494 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400495 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000496 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400497 out << flaggedStructs;
498 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000499 }
500
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000501 if (usingMRTExtension && mNumRenderTargets > 1)
502 {
503 out << "#define GL_USES_MRT\n";
504 }
505
506 if (mUsesFragColor)
507 {
508 out << "#define GL_USES_FRAG_COLOR\n";
509 }
510
511 if (mUsesFragData)
512 {
513 out << "#define GL_USES_FRAG_DATA\n";
514 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000516 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000518 out << "// Attributes\n";
519 out << attributes;
520 out << "\n"
521 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400522
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000523 if (mUsesPointSize)
524 {
525 out << "static float gl_PointSize = float(1);\n";
526 }
527
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000528 if (mUsesInstanceID)
529 {
530 out << "static int gl_InstanceID;";
531 }
532
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000533 out << "\n"
534 "// Varyings\n";
535 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000536 out << "\n";
537
538 if (mUsesDepthRange)
539 {
540 out << "struct gl_DepthRangeParameters\n"
541 "{\n"
542 " float near;\n"
543 " float far;\n"
544 " float diff;\n"
545 "};\n"
546 "\n";
547 }
548
549 if (mOutputType == SH_HLSL11_OUTPUT)
550 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800551 out << "cbuffer DriverConstants : register(b1)\n"
552 "{\n";
553
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000554 if (mUsesDepthRange)
555 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800556 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000557 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800558
Cooper Partine6664f02015-01-09 16:22:24 -0800559 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800560 // However, we declare it for all shaders (including Feature Level 10+).
561 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
562 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800563 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800564
565 out << "};\n"
566 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000567 }
568 else
569 {
570 if (mUsesDepthRange)
571 {
572 out << "uniform float3 dx_DepthRange : register(c0);\n";
573 }
574
Cooper Partine6664f02015-01-09 16:22:24 -0800575 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
576 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000577 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000578 }
579
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000580 if (mUsesDepthRange)
581 {
582 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
583 "\n";
584 }
585
Jamie Madillf91ce812014-06-13 10:04:34 -0400586 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000587 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400588 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000589 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400590 out << flaggedStructs;
591 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000592 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400593 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000594
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400595 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
596 {
597 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400598 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000599 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400600 switch(textureFunction->sampler)
601 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400602 case EbtSampler2D: out << "int2 "; break;
603 case EbtSampler3D: out << "int3 "; break;
604 case EbtSamplerCube: out << "int2 "; break;
605 case EbtSampler2DArray: out << "int3 "; break;
606 case EbtISampler2D: out << "int2 "; break;
607 case EbtISampler3D: out << "int3 "; break;
608 case EbtISamplerCube: out << "int2 "; break;
609 case EbtISampler2DArray: out << "int3 "; break;
610 case EbtUSampler2D: out << "int2 "; break;
611 case EbtUSampler3D: out << "int3 "; break;
612 case EbtUSamplerCube: out << "int2 "; break;
613 case EbtUSampler2DArray: out << "int3 "; break;
614 case EbtSampler2DShadow: out << "int2 "; break;
615 case EbtSamplerCubeShadow: out << "int2 "; break;
616 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400617 default: UNREACHABLE();
618 }
619 }
620 else // Sampling function
621 {
622 switch(textureFunction->sampler)
623 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400624 case EbtSampler2D: out << "float4 "; break;
625 case EbtSampler3D: out << "float4 "; break;
626 case EbtSamplerCube: out << "float4 "; break;
627 case EbtSampler2DArray: out << "float4 "; break;
628 case EbtISampler2D: out << "int4 "; break;
629 case EbtISampler3D: out << "int4 "; break;
630 case EbtISamplerCube: out << "int4 "; break;
631 case EbtISampler2DArray: out << "int4 "; break;
632 case EbtUSampler2D: out << "uint4 "; break;
633 case EbtUSampler3D: out << "uint4 "; break;
634 case EbtUSamplerCube: out << "uint4 "; break;
635 case EbtUSampler2DArray: out << "uint4 "; break;
636 case EbtSampler2DShadow: out << "float "; break;
637 case EbtSamplerCubeShadow: out << "float "; break;
638 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400639 default: UNREACHABLE();
640 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000641 }
642
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400643 // Function name
644 out << textureFunction->name();
645
646 // Argument list
647 int hlslCoords = 4;
648
649 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000650 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400651 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000652 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400653 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
654 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
655 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000656 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400657
Nicolas Capens75fb4752013-07-10 15:14:47 -0400658 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000659 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400660 case TextureFunction::IMPLICIT: break;
661 case TextureFunction::BIAS: hlslCoords = 4; break;
662 case TextureFunction::LOD: hlslCoords = 4; break;
663 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400664 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400665 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000666 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400667 }
668 else if (mOutputType == SH_HLSL11_OUTPUT)
669 {
670 switch(textureFunction->sampler)
671 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400672 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
673 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
674 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
675 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
676 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
677 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500678 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400679 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
680 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
681 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500682 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400683 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
684 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
685 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
686 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400687 default: UNREACHABLE();
688 }
689 }
690 else UNREACHABLE();
691
Nicolas Capensfc014542014-02-18 14:47:13 -0500692 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400693 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500694 switch(textureFunction->coords)
695 {
696 case 2: out << ", int2 t"; break;
697 case 3: out << ", int3 t"; break;
698 default: UNREACHABLE();
699 }
700 }
701 else // Floating-point coordinates (except textureSize)
702 {
703 switch(textureFunction->coords)
704 {
705 case 1: out << ", int lod"; break; // textureSize()
706 case 2: out << ", float2 t"; break;
707 case 3: out << ", float3 t"; break;
708 case 4: out << ", float4 t"; break;
709 default: UNREACHABLE();
710 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000711 }
712
Nicolas Capensd11d5492014-02-19 17:06:10 -0500713 if (textureFunction->method == TextureFunction::GRAD)
714 {
715 switch(textureFunction->sampler)
716 {
717 case EbtSampler2D:
718 case EbtISampler2D:
719 case EbtUSampler2D:
720 case EbtSampler2DArray:
721 case EbtISampler2DArray:
722 case EbtUSampler2DArray:
723 case EbtSampler2DShadow:
724 case EbtSampler2DArrayShadow:
725 out << ", float2 ddx, float2 ddy";
726 break;
727 case EbtSampler3D:
728 case EbtISampler3D:
729 case EbtUSampler3D:
730 case EbtSamplerCube:
731 case EbtISamplerCube:
732 case EbtUSamplerCube:
733 case EbtSamplerCubeShadow:
734 out << ", float3 ddx, float3 ddy";
735 break;
736 default: UNREACHABLE();
737 }
738 }
739
Nicolas Capens75fb4752013-07-10 15:14:47 -0400740 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000741 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400742 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400743 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400744 case TextureFunction::LOD: out << ", float lod"; break;
745 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400746 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400747 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500748 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500749 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400750 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000751 }
752
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500753 if (textureFunction->offset)
754 {
755 switch(textureFunction->sampler)
756 {
757 case EbtSampler2D: out << ", int2 offset"; break;
758 case EbtSampler3D: out << ", int3 offset"; break;
759 case EbtSampler2DArray: out << ", int2 offset"; break;
760 case EbtISampler2D: out << ", int2 offset"; break;
761 case EbtISampler3D: out << ", int3 offset"; break;
762 case EbtISampler2DArray: out << ", int2 offset"; break;
763 case EbtUSampler2D: out << ", int2 offset"; break;
764 case EbtUSampler3D: out << ", int3 offset"; break;
765 case EbtUSampler2DArray: out << ", int2 offset"; break;
766 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500767 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500768 default: UNREACHABLE();
769 }
770 }
771
Nicolas Capens84cfa122014-04-14 13:48:45 -0400772 if (textureFunction->method == TextureFunction::BIAS ||
773 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500774 {
775 out << ", float bias";
776 }
777
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400778 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400779 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400780
Nicolas Capens75fb4752013-07-10 15:14:47 -0400781 if (textureFunction->method == TextureFunction::SIZE)
782 {
783 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
784 {
785 if (IsSamplerArray(textureFunction->sampler))
786 {
787 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
788 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
789 }
790 else
791 {
792 out << " uint width; uint height; uint numberOfLevels;\n"
793 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
794 }
795 }
796 else if (IsSampler3D(textureFunction->sampler))
797 {
798 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
799 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
800 }
801 else UNREACHABLE();
802
803 switch(textureFunction->sampler)
804 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400805 case EbtSampler2D: out << " return int2(width, height);"; break;
806 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
807 case EbtSamplerCube: out << " return int2(width, height);"; break;
808 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
809 case EbtISampler2D: out << " return int2(width, height);"; break;
810 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
811 case EbtISamplerCube: out << " return int2(width, height);"; break;
812 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
813 case EbtUSampler2D: out << " return int2(width, height);"; break;
814 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
815 case EbtUSamplerCube: out << " return int2(width, height);"; break;
816 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
817 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
818 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
819 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400820 default: UNREACHABLE();
821 }
822 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400823 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400824 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500825 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
826 {
827 out << " float width; float height; float layers; float levels;\n";
828
829 out << " uint mip = 0;\n";
830
831 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
832
833 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
834 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
835 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
836 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
837
838 // FACE_POSITIVE_X = 000b
839 // FACE_NEGATIVE_X = 001b
840 // FACE_POSITIVE_Y = 010b
841 // FACE_NEGATIVE_Y = 011b
842 // FACE_POSITIVE_Z = 100b
843 // FACE_NEGATIVE_Z = 101b
844 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
845
846 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
847 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
848 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
849
850 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
851 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
852 }
853 else if (IsIntegerSampler(textureFunction->sampler) &&
854 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400855 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400856 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400857 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400858 if (IsSamplerArray(textureFunction->sampler))
859 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400860 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400861
Nicolas Capens9edebd62013-08-06 10:59:10 -0400862 if (textureFunction->method == TextureFunction::LOD0)
863 {
864 out << " uint mip = 0;\n";
865 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400866 else if (textureFunction->method == TextureFunction::LOD0BIAS)
867 {
868 out << " uint mip = bias;\n";
869 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400870 else
871 {
872 if (textureFunction->method == TextureFunction::IMPLICIT ||
873 textureFunction->method == TextureFunction::BIAS)
874 {
875 out << " x.GetDimensions(0, width, height, layers, levels);\n"
876 " float2 tSized = float2(t.x * width, t.y * height);\n"
877 " float dx = length(ddx(tSized));\n"
878 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500879 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400880
881 if (textureFunction->method == TextureFunction::BIAS)
882 {
883 out << " lod += bias;\n";
884 }
885 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500886 else if (textureFunction->method == TextureFunction::GRAD)
887 {
888 out << " x.GetDimensions(0, width, height, layers, levels);\n"
889 " float lod = log2(max(length(ddx), length(ddy)));\n";
890 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400891
892 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
893 }
894
895 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400896 }
897 else
898 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400899 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400900
Nicolas Capens9edebd62013-08-06 10:59:10 -0400901 if (textureFunction->method == TextureFunction::LOD0)
902 {
903 out << " uint mip = 0;\n";
904 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400905 else if (textureFunction->method == TextureFunction::LOD0BIAS)
906 {
907 out << " uint mip = bias;\n";
908 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400909 else
910 {
911 if (textureFunction->method == TextureFunction::IMPLICIT ||
912 textureFunction->method == TextureFunction::BIAS)
913 {
914 out << " x.GetDimensions(0, width, height, levels);\n"
915 " float2 tSized = float2(t.x * width, t.y * height);\n"
916 " float dx = length(ddx(tSized));\n"
917 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500918 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400919
920 if (textureFunction->method == TextureFunction::BIAS)
921 {
922 out << " lod += bias;\n";
923 }
924 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500925 else if (textureFunction->method == TextureFunction::LOD)
926 {
927 out << " x.GetDimensions(0, width, height, levels);\n";
928 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500929 else if (textureFunction->method == TextureFunction::GRAD)
930 {
931 out << " x.GetDimensions(0, width, height, levels);\n"
932 " float lod = log2(max(length(ddx), length(ddy)));\n";
933 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400934
935 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
936 }
937
938 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400939 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400940 }
941 else if (IsSampler3D(textureFunction->sampler))
942 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400943 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400944
Nicolas Capens9edebd62013-08-06 10:59:10 -0400945 if (textureFunction->method == TextureFunction::LOD0)
946 {
947 out << " uint mip = 0;\n";
948 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400949 else if (textureFunction->method == TextureFunction::LOD0BIAS)
950 {
951 out << " uint mip = bias;\n";
952 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400953 else
954 {
955 if (textureFunction->method == TextureFunction::IMPLICIT ||
956 textureFunction->method == TextureFunction::BIAS)
957 {
958 out << " x.GetDimensions(0, width, height, depth, levels);\n"
959 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
960 " float dx = length(ddx(tSized));\n"
961 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500962 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400963
964 if (textureFunction->method == TextureFunction::BIAS)
965 {
966 out << " lod += bias;\n";
967 }
968 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500969 else if (textureFunction->method == TextureFunction::GRAD)
970 {
971 out << " x.GetDimensions(0, width, height, depth, levels);\n"
972 " float lod = log2(max(length(ddx), length(ddy)));\n";
973 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400974
975 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
976 }
977
978 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400979 }
980 else UNREACHABLE();
981 }
982
983 out << " return ";
984
985 // HLSL intrinsic
986 if (mOutputType == SH_HLSL9_OUTPUT)
987 {
988 switch(textureFunction->sampler)
989 {
990 case EbtSampler2D: out << "tex2D"; break;
991 case EbtSamplerCube: out << "texCUBE"; break;
992 default: UNREACHABLE();
993 }
994
Nicolas Capens75fb4752013-07-10 15:14:47 -0400995 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400996 {
997 case TextureFunction::IMPLICIT: out << "(s, "; break;
998 case TextureFunction::BIAS: out << "bias(s, "; break;
999 case TextureFunction::LOD: out << "lod(s, "; break;
1000 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001001 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001002 default: UNREACHABLE();
1003 }
1004 }
1005 else if (mOutputType == SH_HLSL11_OUTPUT)
1006 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001007 if (textureFunction->method == TextureFunction::GRAD)
1008 {
1009 if (IsIntegerSampler(textureFunction->sampler))
1010 {
1011 out << "x.Load(";
1012 }
1013 else if (IsShadowSampler(textureFunction->sampler))
1014 {
1015 out << "x.SampleCmpLevelZero(s, ";
1016 }
1017 else
1018 {
1019 out << "x.SampleGrad(s, ";
1020 }
1021 }
1022 else if (IsIntegerSampler(textureFunction->sampler) ||
1023 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001024 {
1025 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001026 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001027 else if (IsShadowSampler(textureFunction->sampler))
1028 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001029 switch(textureFunction->method)
1030 {
1031 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1032 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1033 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1034 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1035 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1036 default: UNREACHABLE();
1037 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001038 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001039 else
1040 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001041 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001042 {
1043 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1044 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1045 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1046 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001047 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001048 default: UNREACHABLE();
1049 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001050 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001051 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001052 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001053
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001054 // Integer sampling requires integer addresses
1055 TString addressx = "";
1056 TString addressy = "";
1057 TString addressz = "";
1058 TString close = "";
1059
Nicolas Capensfc014542014-02-18 14:47:13 -05001060 if (IsIntegerSampler(textureFunction->sampler) ||
1061 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001062 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001063 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001064 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001065 case 2: out << "int3("; break;
1066 case 3: out << "int4("; break;
1067 default: UNREACHABLE();
1068 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001069
Nicolas Capensfc014542014-02-18 14:47:13 -05001070 // Convert from normalized floating-point to integer
1071 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001072 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001073 addressx = "int(floor(width * frac((";
1074 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001075
Nicolas Capensfc014542014-02-18 14:47:13 -05001076 if (IsSamplerArray(textureFunction->sampler))
1077 {
1078 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1079 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001080 else if (IsSamplerCube(textureFunction->sampler))
1081 {
1082 addressz = "((((";
1083 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001084 else
1085 {
1086 addressz = "int(floor(depth * frac((";
1087 }
1088
1089 close = "))))";
1090 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001091 }
1092 else
1093 {
1094 switch(hlslCoords)
1095 {
1096 case 2: out << "float2("; break;
1097 case 3: out << "float3("; break;
1098 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001099 default: UNREACHABLE();
1100 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001101 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001102
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001103 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001104
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001105 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001106 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001107 switch(textureFunction->coords)
1108 {
1109 case 3: proj = " / t.z"; break;
1110 case 4: proj = " / t.w"; break;
1111 default: UNREACHABLE();
1112 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001113 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001114
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001115 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001116
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001117 if (mOutputType == SH_HLSL9_OUTPUT)
1118 {
1119 if (hlslCoords >= 3)
1120 {
1121 if (textureFunction->coords < 3)
1122 {
1123 out << ", 0";
1124 }
1125 else
1126 {
1127 out << ", t.z" + proj;
1128 }
1129 }
1130
1131 if (hlslCoords == 4)
1132 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001133 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001134 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001135 case TextureFunction::BIAS: out << ", bias"; break;
1136 case TextureFunction::LOD: out << ", lod"; break;
1137 case TextureFunction::LOD0: out << ", 0"; break;
1138 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001139 default: UNREACHABLE();
1140 }
1141 }
1142
1143 out << "));\n";
1144 }
1145 else if (mOutputType == SH_HLSL11_OUTPUT)
1146 {
1147 if (hlslCoords >= 3)
1148 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001149 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1150 {
1151 out << ", face";
1152 }
1153 else
1154 {
1155 out << ", " + addressz + ("t.z" + proj) + close;
1156 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001157 }
1158
Nicolas Capensd11d5492014-02-19 17:06:10 -05001159 if (textureFunction->method == TextureFunction::GRAD)
1160 {
1161 if (IsIntegerSampler(textureFunction->sampler))
1162 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001163 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001164 }
1165 else if (IsShadowSampler(textureFunction->sampler))
1166 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001167 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001168 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001169 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001170 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1171 // The resulting third component of P' in the shadow forms is used as Dref
1172 out << "), t.z" << proj;
1173 }
1174 else
1175 {
1176 switch(textureFunction->coords)
1177 {
1178 case 3: out << "), t.z"; break;
1179 case 4: out << "), t.w"; break;
1180 default: UNREACHABLE();
1181 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001182 }
1183 }
1184 else
1185 {
1186 out << "), ddx, ddy";
1187 }
1188 }
1189 else if (IsIntegerSampler(textureFunction->sampler) ||
1190 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001191 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001192 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001193 }
1194 else if (IsShadowSampler(textureFunction->sampler))
1195 {
1196 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001197 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001198 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001199 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1200 // The resulting third component of P' in the shadow forms is used as Dref
1201 out << "), t.z" << proj;
1202 }
1203 else
1204 {
1205 switch(textureFunction->coords)
1206 {
1207 case 3: out << "), t.z"; break;
1208 case 4: out << "), t.w"; break;
1209 default: UNREACHABLE();
1210 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001211 }
1212 }
1213 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001214 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001215 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001216 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001217 case TextureFunction::IMPLICIT: out << ")"; break;
1218 case TextureFunction::BIAS: out << "), bias"; break;
1219 case TextureFunction::LOD: out << "), lod"; break;
1220 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001221 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001222 default: UNREACHABLE();
1223 }
1224 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001225
1226 if (textureFunction->offset)
1227 {
1228 out << ", offset";
1229 }
1230
1231 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001232 }
1233 else UNREACHABLE();
1234 }
1235
1236 out << "\n"
1237 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001238 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001239 }
1240
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001241 if (mUsesFragCoord)
1242 {
1243 out << "#define GL_USES_FRAG_COORD\n";
1244 }
1245
1246 if (mUsesPointCoord)
1247 {
1248 out << "#define GL_USES_POINT_COORD\n";
1249 }
1250
1251 if (mUsesFrontFacing)
1252 {
1253 out << "#define GL_USES_FRONT_FACING\n";
1254 }
1255
1256 if (mUsesPointSize)
1257 {
1258 out << "#define GL_USES_POINT_SIZE\n";
1259 }
1260
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001261 if (mUsesFragDepth)
1262 {
1263 out << "#define GL_USES_FRAG_DEPTH\n";
1264 }
1265
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001266 if (mUsesDepthRange)
1267 {
1268 out << "#define GL_USES_DEPTH_RANGE\n";
1269 }
1270
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001271 if (mUsesXor)
1272 {
1273 out << "bool xor(bool p, bool q)\n"
1274 "{\n"
1275 " return (p || q) && !(p && q);\n"
1276 "}\n"
1277 "\n";
1278 }
1279
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001280 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001281}
1282
1283void OutputHLSL::visitSymbol(TIntermSymbol *node)
1284{
Jamie Madill32aab012015-01-27 14:12:26 -05001285 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001286
Jamie Madill570e04d2013-06-21 09:15:33 -04001287 // Handle accessing std140 structs by value
1288 if (mFlaggedStructMappedNames.count(node) > 0)
1289 {
1290 out << mFlaggedStructMappedNames[node];
1291 return;
1292 }
1293
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001294 TString name = node->getSymbol();
1295
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001296 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001297 {
1298 mUsesDepthRange = true;
1299 out << name;
1300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001301 else
1302 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001303 TQualifier qualifier = node->getQualifier();
1304
1305 if (qualifier == EvqUniform)
1306 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001307 const TType& nodeType = node->getType();
1308 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1309
1310 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001311 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001312 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001313 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001314 else
1315 {
1316 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001317 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001318
Jamie Madill033dae62014-06-18 12:56:28 -04001319 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001320 }
Jamie Madill19571812013-08-12 15:26:34 -07001321 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001322 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001323 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001324 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001325 }
Jamie Madill033dae62014-06-18 12:56:28 -04001326 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001327 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001328 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001329 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001330 }
Jamie Madill19571812013-08-12 15:26:34 -07001331 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001332 {
1333 mReferencedOutputVariables[name] = node;
1334 out << "out_" << name;
1335 }
1336 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001337 {
1338 out << "gl_Color[0]";
1339 mUsesFragColor = true;
1340 }
1341 else if (qualifier == EvqFragData)
1342 {
1343 out << "gl_Color";
1344 mUsesFragData = true;
1345 }
1346 else if (qualifier == EvqFragCoord)
1347 {
1348 mUsesFragCoord = true;
1349 out << name;
1350 }
1351 else if (qualifier == EvqPointCoord)
1352 {
1353 mUsesPointCoord = true;
1354 out << name;
1355 }
1356 else if (qualifier == EvqFrontFacing)
1357 {
1358 mUsesFrontFacing = true;
1359 out << name;
1360 }
1361 else if (qualifier == EvqPointSize)
1362 {
1363 mUsesPointSize = true;
1364 out << name;
1365 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001366 else if (qualifier == EvqInstanceID)
1367 {
1368 mUsesInstanceID = true;
1369 out << name;
1370 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001371 else if (name == "gl_FragDepthEXT")
1372 {
1373 mUsesFragDepth = true;
1374 out << "gl_Depth";
1375 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001376 else if (qualifier == EvqInternal)
1377 {
1378 out << name;
1379 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001380 else
1381 {
Jamie Madill033dae62014-06-18 12:56:28 -04001382 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001383 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001384 }
1385}
1386
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001387void OutputHLSL::visitRaw(TIntermRaw *node)
1388{
Jamie Madill32aab012015-01-27 14:12:26 -05001389 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001390}
1391
Olli Etuaho7fb49552015-03-18 17:27:44 +02001392void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1393{
1394 if (type.isScalar() && !type.isArray())
1395 {
1396 if (op == EOpEqual)
1397 {
1398 outputTriplet(visit, "(", " == ", ")", out);
1399 }
1400 else
1401 {
1402 outputTriplet(visit, "(", " != ", ")", out);
1403 }
1404 }
1405 else
1406 {
1407 if (visit == PreVisit && op == EOpNotEqual)
1408 {
1409 out << "!";
1410 }
1411
1412 if (type.isArray())
1413 {
1414 const TString &functionName = addArrayEqualityFunction(type);
1415 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1416 }
1417 else if (type.getBasicType() == EbtStruct)
1418 {
1419 const TStructure &structure = *type.getStruct();
1420 const TString &functionName = addStructEqualityFunction(structure);
1421 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1422 }
1423 else
1424 {
1425 ASSERT(type.isMatrix() || type.isVector());
1426 outputTriplet(visit, "all(", " == ", ")", out);
1427 }
1428 }
1429}
1430
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001431bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1432{
Jamie Madill32aab012015-01-27 14:12:26 -05001433 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001434
Jamie Madill570e04d2013-06-21 09:15:33 -04001435 // Handle accessing std140 structs by value
1436 if (mFlaggedStructMappedNames.count(node) > 0)
1437 {
1438 out << mFlaggedStructMappedNames[node];
1439 return false;
1440 }
1441
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001442 switch (node->getOp())
1443 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001444 case EOpAssign:
1445 if (node->getLeft()->isArray())
1446 {
1447 UNIMPLEMENTED();
1448 }
1449 else
1450 {
1451 outputTriplet(visit, "(", " = ", ")");
1452 }
1453 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001454 case EOpInitialize:
1455 if (visit == PreVisit)
1456 {
1457 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1458 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1459 // new variable is created before the assignment is evaluated), so we need to convert
1460 // this to "float t = x, x = t;".
1461
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001462 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001463 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001464 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001465
Jamie Madill37997142015-01-28 10:06:34 -05001466 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1467 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001468 {
Jamie Madill37997142015-01-28 10:06:34 -05001469 // For variables which are not constant, defer their real initialization until
1470 // after we initialize other globals: uniforms, attributes and varyings.
1471 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1472 const TString &initString = initializer(node->getType());
1473 node->setRight(new TIntermRaw(node->getType(), initString));
1474 }
1475 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1476 {
1477 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001478 return false;
1479 }
1480 }
1481 else if (visit == InVisit)
1482 {
1483 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001484 }
1485 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001486 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1487 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1488 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1489 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1490 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1491 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001492 if (visit == PreVisit)
1493 {
1494 out << "(";
1495 }
1496 else if (visit == InVisit)
1497 {
1498 out << " = mul(";
1499 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001500 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001501 }
1502 else
1503 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001504 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001505 }
1506 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001507 case EOpMatrixTimesMatrixAssign:
1508 if (visit == PreVisit)
1509 {
1510 out << "(";
1511 }
1512 else if (visit == InVisit)
1513 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001514 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001515 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001516 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001517 }
1518 else
1519 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001520 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001521 }
1522 break;
1523 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001524 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001525 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1526 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1527 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1528 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1529 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001530 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001531 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001532 const TType& leftType = node->getLeft()->getType();
1533 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001534 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001535 if (visit == PreVisit)
1536 {
1537 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1538 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001539 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001540 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001541 return false;
1542 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001543 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001544 else
1545 {
1546 outputTriplet(visit, "", "[", "]");
1547 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001548 }
1549 break;
1550 case EOpIndexIndirect:
1551 // We do not currently support indirect references to interface blocks
1552 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1553 outputTriplet(visit, "", "[", "]");
1554 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001555 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001556 if (visit == InVisit)
1557 {
1558 const TStructure* structure = node->getLeft()->getType().getStruct();
1559 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1560 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001561 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001562
1563 return false;
1564 }
1565 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001566 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001567 if (visit == InVisit)
1568 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001569 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1570 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1571 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001572 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001573
1574 return false;
1575 }
1576 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577 case EOpVectorSwizzle:
1578 if (visit == InVisit)
1579 {
1580 out << ".";
1581
1582 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1583
1584 if (swizzle)
1585 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001586 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001588 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001589 {
1590 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1591
1592 if (element)
1593 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001594 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001595
1596 switch (i)
1597 {
1598 case 0: out << "x"; break;
1599 case 1: out << "y"; break;
1600 case 2: out << "z"; break;
1601 case 3: out << "w"; break;
1602 default: UNREACHABLE();
1603 }
1604 }
1605 else UNREACHABLE();
1606 }
1607 }
1608 else UNREACHABLE();
1609
1610 return false; // Fully processed
1611 }
1612 break;
1613 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1614 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1615 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1616 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001617 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001618 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1619 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1620 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1621 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1622 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001623 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001624 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001625 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001626 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001627 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1628 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1629 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1630 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1631 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001632 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001633 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1634 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001635 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001636 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001637 if (node->getRight()->hasSideEffects())
1638 {
1639 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1640 return false;
1641 }
1642 else
1643 {
1644 outputTriplet(visit, "(", " || ", ")");
1645 return true;
1646 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001647 case EOpLogicalXor:
1648 mUsesXor = true;
1649 outputTriplet(visit, "xor(", ", ", ")");
1650 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001651 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001652 if (node->getRight()->hasSideEffects())
1653 {
1654 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1655 return false;
1656 }
1657 else
1658 {
1659 outputTriplet(visit, "(", " && ", ")");
1660 return true;
1661 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001662 default: UNREACHABLE();
1663 }
1664
1665 return true;
1666}
1667
1668bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1669{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670 switch (node->getOp())
1671 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001672 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001673 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001674 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1675 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001676 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001677 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1678 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1679 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1680 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001681 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1682 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1683 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1684 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1685 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1686 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1687 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1688 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001689 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1690 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1691 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1692 case EOpAsinh:
1693 ASSERT(node->getUseEmulatedFunction());
1694 writeEmulatedFunctionTriplet(visit, "asinh(");
1695 break;
1696 case EOpAcosh:
1697 ASSERT(node->getUseEmulatedFunction());
1698 writeEmulatedFunctionTriplet(visit, "acosh(");
1699 break;
1700 case EOpAtanh:
1701 ASSERT(node->getUseEmulatedFunction());
1702 writeEmulatedFunctionTriplet(visit, "atanh(");
1703 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001704 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1705 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1706 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1707 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1708 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1709 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1710 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1711 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1712 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001713 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1714 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1715 case EOpRoundEven:
1716 ASSERT(node->getUseEmulatedFunction());
1717 writeEmulatedFunctionTriplet(visit, "roundEven(");
1718 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001719 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1720 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301721 case EOpIsNan:
1722 outputTriplet(visit, "isnan(", "", ")");
1723 mRequiresIEEEStrictCompiling = true;
1724 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301725 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001726 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1727 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1728 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1729 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001730 case EOpPackSnorm2x16:
1731 ASSERT(node->getUseEmulatedFunction());
1732 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1733 break;
1734 case EOpPackUnorm2x16:
1735 ASSERT(node->getUseEmulatedFunction());
1736 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1737 break;
1738 case EOpPackHalf2x16:
1739 ASSERT(node->getUseEmulatedFunction());
1740 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1741 break;
1742 case EOpUnpackSnorm2x16:
1743 ASSERT(node->getUseEmulatedFunction());
1744 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1745 break;
1746 case EOpUnpackUnorm2x16:
1747 ASSERT(node->getUseEmulatedFunction());
1748 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1749 break;
1750 case EOpUnpackHalf2x16:
1751 ASSERT(node->getUseEmulatedFunction());
1752 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1753 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001754 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1755 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001756 case EOpDFdx:
1757 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1758 {
1759 outputTriplet(visit, "(", "", ", 0.0)");
1760 }
1761 else
1762 {
1763 outputTriplet(visit, "ddx(", "", ")");
1764 }
1765 break;
1766 case EOpDFdy:
1767 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1768 {
1769 outputTriplet(visit, "(", "", ", 0.0)");
1770 }
1771 else
1772 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001773 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001774 }
1775 break;
1776 case EOpFwidth:
1777 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1778 {
1779 outputTriplet(visit, "(", "", ", 0.0)");
1780 }
1781 else
1782 {
1783 outputTriplet(visit, "fwidth(", "", ")");
1784 }
1785 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001786 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1787 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001788 case EOpInverse:
1789 ASSERT(node->getUseEmulatedFunction());
1790 writeEmulatedFunctionTriplet(visit, "inverse(");
1791 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001792
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001793 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1794 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795 default: UNREACHABLE();
1796 }
1797
1798 return true;
1799}
1800
1801bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1802{
Jamie Madill32aab012015-01-27 14:12:26 -05001803 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805 switch (node->getOp())
1806 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001807 case EOpSequence:
1808 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001809 if (mInsideFunction)
1810 {
Jamie Madill075edd82013-07-08 13:30:19 -04001811 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001812 out << "{\n";
1813 }
1814
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001815 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001816 {
Jamie Madill075edd82013-07-08 13:30:19 -04001817 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001818
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001819 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001820
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001821 // Don't output ; after case labels, they're terminated by :
1822 // This is needed especially since outputting a ; after a case statement would turn empty
1823 // case statements into non-empty case statements, disallowing fall-through from them.
1824 if ((*sit)->getAsCaseNode() == nullptr)
1825 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001826 }
1827
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001828 if (mInsideFunction)
1829 {
Jamie Madill075edd82013-07-08 13:30:19 -04001830 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001831 out << "}\n";
1832 }
1833
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001834 return false;
1835 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836 case EOpDeclaration:
1837 if (visit == PreVisit)
1838 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001839 TIntermSequence *sequence = node->getSequence();
1840 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001842 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001844 TStructure *structure = variable->getType().getStruct();
1845
1846 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001847 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001848 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001849 }
1850
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001851 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001852 {
Jamie Madill37997142015-01-28 10:06:34 -05001853 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001854 {
Jamie Madill37997142015-01-28 10:06:34 -05001855 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001856 {
Jamie Madill37997142015-01-28 10:06:34 -05001857 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001858 }
1859
Nicolas Capensd974db42014-10-07 10:50:19 -04001860 if (!mInsideFunction)
1861 {
1862 out << "static ";
1863 }
1864
1865 out << TypeString(variable->getType()) + " ";
1866
Jamie Madill37997142015-01-28 10:06:34 -05001867 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001868
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001869 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001871 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001872 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001873 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001874 }
1875 else
1876 {
Jamie Madill37997142015-01-28 10:06:34 -05001877 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001878 }
1879
Jamie Madill37997142015-01-28 10:06:34 -05001880 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001881 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001882 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883 }
1884 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001886 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1887 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001888 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001889 }
1890 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891 }
Jamie Madill033dae62014-06-18 12:56:28 -04001892 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001893 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001894 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001895 {
1896 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1897
1898 if (symbol)
1899 {
1900 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1901 mReferencedVaryings[symbol->getSymbol()] = symbol;
1902 }
1903 else
1904 {
1905 (*sit)->traverse(this);
1906 }
1907 }
1908 }
1909
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001910 return false;
1911 }
1912 else if (visit == InVisit)
1913 {
1914 out << ", ";
1915 }
1916 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001917 case EOpInvariantDeclaration:
1918 // Do not do any translation
1919 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001920 case EOpPrototype:
1921 if (visit == PreVisit)
1922 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001923 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001924
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001925 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001926
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001927 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001928 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001929 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001930
1931 if (symbol)
1932 {
1933 out << argumentString(symbol);
1934
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001935 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001936 {
1937 out << ", ";
1938 }
1939 }
1940 else UNREACHABLE();
1941 }
1942
1943 out << ");\n";
1944
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001945 // Also prototype the Lod0 variant if needed
1946 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1947 {
1948 mOutputLod0Function = true;
1949 node->traverse(this);
1950 mOutputLod0Function = false;
1951 }
1952
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001953 return false;
1954 }
1955 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001956 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957 case EOpFunction:
1958 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001959 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960
Jamie Madill033dae62014-06-18 12:56:28 -04001961 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001962
1963 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001964 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001965 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001966 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001967 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968 {
Jamie Madill033dae62014-06-18 12:56:28 -04001969 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001970 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001971
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001972 TIntermSequence *sequence = node->getSequence();
1973 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001974
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001975 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001976 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001977 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001978
1979 if (symbol)
1980 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001981 TStructure *structure = symbol->getType().getStruct();
1982
1983 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001984 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001985 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001986 }
1987
1988 out << argumentString(symbol);
1989
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001990 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001991 {
1992 out << ", ";
1993 }
1994 }
1995 else UNREACHABLE();
1996 }
1997
1998 out << ")\n"
1999 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002000
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002001 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002002 {
2003 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002004 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002005 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002007
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002008 out << "}\n";
2009
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002010 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2011 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002012 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002013 {
2014 mOutputLod0Function = true;
2015 node->traverse(this);
2016 mOutputLod0Function = false;
2017 }
2018 }
2019
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002020 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002021 }
2022 break;
2023 case EOpFunctionCall:
2024 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002025 TString name = TFunction::unmangleName(node->getName());
2026 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002027 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002028
2029 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002030 {
Jamie Madill033dae62014-06-18 12:56:28 -04002031 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032 }
2033 else
2034 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002035 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002036
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002037 TextureFunction textureFunction;
2038 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002039 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002040 textureFunction.method = TextureFunction::IMPLICIT;
2041 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002042 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002043
2044 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002045 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002046 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002047 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002048 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002049 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002050 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002051 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002052 }
Nicolas Capens46485082014-04-15 13:12:50 -04002053 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2054 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002055 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002056 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002057 }
Nicolas Capens46485082014-04-15 13:12:50 -04002058 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002059 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002060 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002061 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002062 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002063 else if (name == "textureSize")
2064 {
2065 textureFunction.method = TextureFunction::SIZE;
2066 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002067 else if (name == "textureOffset")
2068 {
2069 textureFunction.method = TextureFunction::IMPLICIT;
2070 textureFunction.offset = true;
2071 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002072 else if (name == "textureProjOffset")
2073 {
2074 textureFunction.method = TextureFunction::IMPLICIT;
2075 textureFunction.offset = true;
2076 textureFunction.proj = true;
2077 }
2078 else if (name == "textureLodOffset")
2079 {
2080 textureFunction.method = TextureFunction::LOD;
2081 textureFunction.offset = true;
2082 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002083 else if (name == "textureProjLodOffset")
2084 {
2085 textureFunction.method = TextureFunction::LOD;
2086 textureFunction.proj = true;
2087 textureFunction.offset = true;
2088 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002089 else if (name == "texelFetch")
2090 {
2091 textureFunction.method = TextureFunction::FETCH;
2092 }
2093 else if (name == "texelFetchOffset")
2094 {
2095 textureFunction.method = TextureFunction::FETCH;
2096 textureFunction.offset = true;
2097 }
Nicolas Capens46485082014-04-15 13:12:50 -04002098 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002099 {
2100 textureFunction.method = TextureFunction::GRAD;
2101 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002102 else if (name == "textureGradOffset")
2103 {
2104 textureFunction.method = TextureFunction::GRAD;
2105 textureFunction.offset = true;
2106 }
Nicolas Capens46485082014-04-15 13:12:50 -04002107 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002108 {
2109 textureFunction.method = TextureFunction::GRAD;
2110 textureFunction.proj = true;
2111 }
2112 else if (name == "textureProjGradOffset")
2113 {
2114 textureFunction.method = TextureFunction::GRAD;
2115 textureFunction.proj = true;
2116 textureFunction.offset = true;
2117 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002118 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002119
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002120 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002121 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002122 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2123
2124 if (textureFunction.offset)
2125 {
2126 mandatoryArgumentCount++;
2127 }
2128
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002129 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002130
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002131 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002132 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002133 if (bias)
2134 {
2135 textureFunction.method = TextureFunction::LOD0BIAS;
2136 }
2137 else
2138 {
2139 textureFunction.method = TextureFunction::LOD0;
2140 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002141 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002142 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002143 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002144 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002145 }
2146 }
2147
2148 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002149
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002150 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002152
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002153 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002154 {
2155 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2156 {
2157 out << "texture_";
2158 (*arg)->traverse(this);
2159 out << ", sampler_";
2160 }
2161
2162 (*arg)->traverse(this);
2163
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002164 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002165 {
2166 out << ", ";
2167 }
2168 }
2169
2170 out << ")";
2171
2172 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173 }
2174 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002175 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002176 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2177 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2178 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2179 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2180 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2181 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2182 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2183 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2184 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2185 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2186 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2187 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2188 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2189 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2190 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2191 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2192 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2193 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2194 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002195 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002196 {
Jamie Madill033dae62014-06-18 12:56:28 -04002197 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002198 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002199 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002200 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002201 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002202 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2203 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2204 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2205 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2206 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2207 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002208 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002209 ASSERT(node->getUseEmulatedFunction());
2210 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002211 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002212 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002213 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002215 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002216 ASSERT(node->getUseEmulatedFunction());
2217 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002218 break;
2219 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2220 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2221 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2222 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2223 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2224 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2225 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2226 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2227 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002228 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002229 ASSERT(node->getUseEmulatedFunction());
2230 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002231 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002232 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2233 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002234 case EOpOuterProduct:
2235 ASSERT(node->getUseEmulatedFunction());
2236 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2237 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002239 default: UNREACHABLE();
2240 }
2241
2242 return true;
2243}
2244
2245bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2246{
Jamie Madill32aab012015-01-27 14:12:26 -05002247 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002249 if (node->usesTernaryOperator())
2250 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002251 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002252 }
2253 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002255 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002256
Corentin Wallez80bacde2014-11-10 12:07:37 -08002257 // D3D errors when there is a gradient operation in a loop in an unflattened if
2258 // however flattening all the ifs in branch heavy shaders made D3D error too.
2259 // As a temporary workaround we flatten the ifs only if there is at least a loop
2260 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002261 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002262 {
2263 out << "FLATTEN ";
2264 }
2265
2266 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002267
2268 node->getCondition()->traverse(this);
2269
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002270 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002271
Jamie Madill075edd82013-07-08 13:30:19 -04002272 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002273 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002275 bool discard = false;
2276
daniel@transgaming.combb885322010-04-15 20:45:24 +00002277 if (node->getTrueBlock())
2278 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002279 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002280
2281 // Detect true discard
2282 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002283 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002284
Jamie Madill075edd82013-07-08 13:30:19 -04002285 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002286 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002287
2288 if (node->getFalseBlock())
2289 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002290 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002291
Jamie Madill075edd82013-07-08 13:30:19 -04002292 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002293 out << "{\n";
2294
Jamie Madill075edd82013-07-08 13:30:19 -04002295 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002296 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002297
Jamie Madill075edd82013-07-08 13:30:19 -04002298 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002299 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002300
2301 // Detect false discard
2302 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2303 }
2304
2305 // ANGLE issue 486: Detect problematic conditional discard
2306 if (discard && FindSideEffectRewriting::search(node))
2307 {
2308 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002309 }
2310 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311
2312 return false;
2313}
2314
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002315bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002316{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002317 if (node->getStatementList())
2318 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002319 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002320 outputTriplet(visit, "switch (", ") ", "");
2321 // The curly braces get written when visiting the statementList aggregate
2322 }
2323 else
2324 {
2325 // No statementList, so it won't output curly braces
2326 outputTriplet(visit, "switch (", ") {", "}\n");
2327 }
2328 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002329}
2330
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002331bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002332{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002333 if (node->hasCondition())
2334 {
2335 outputTriplet(visit, "case (", "", "):\n");
2336 return true;
2337 }
2338 else
2339 {
2340 TInfoSinkBase &out = getInfoSink();
2341 out << "default:\n";
2342 return false;
2343 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002344}
2345
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002346void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2347{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002348 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349}
2350
2351bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2352{
Nicolas Capens655fe362014-04-11 13:12:34 -04002353 mNestedLoopDepth++;
2354
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002355 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2356
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002357 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002358 {
2359 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2360 }
2361
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002362 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002363 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002364 if (handleExcessiveLoop(node))
2365 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002366 mInsideDiscontinuousLoop = wasDiscontinuous;
2367 mNestedLoopDepth--;
2368
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002369 return false;
2370 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002371 }
2372
Jamie Madill32aab012015-01-27 14:12:26 -05002373 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374
alokp@chromium.org52813552010-11-16 18:36:09 +00002375 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002377 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002378
Jamie Madill075edd82013-07-08 13:30:19 -04002379 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002380 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 }
2382 else
2383 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002384 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002385
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386 if (node->getInit())
2387 {
2388 node->getInit()->traverse(this);
2389 }
2390
2391 out << "; ";
2392
alokp@chromium.org52813552010-11-16 18:36:09 +00002393 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002395 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396 }
2397
2398 out << "; ";
2399
alokp@chromium.org52813552010-11-16 18:36:09 +00002400 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002401 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002402 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 }
2404
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002405 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002406
Jamie Madill075edd82013-07-08 13:30:19 -04002407 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002408 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409 }
2410
2411 if (node->getBody())
2412 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002413 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 }
2415
Jamie Madill075edd82013-07-08 13:30:19 -04002416 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002417 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418
alokp@chromium.org52813552010-11-16 18:36:09 +00002419 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420 {
Jamie Madill075edd82013-07-08 13:30:19 -04002421 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 out << "while(\n";
2423
alokp@chromium.org52813552010-11-16 18:36:09 +00002424 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425
daniel@transgaming.com73536982012-03-21 20:45:49 +00002426 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 }
2428
daniel@transgaming.com73536982012-03-21 20:45:49 +00002429 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002431 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002432 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002433
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 return false;
2435}
2436
2437bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2438{
Jamie Madill32aab012015-01-27 14:12:26 -05002439 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002440
2441 switch (node->getFlowOp())
2442 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002443 case EOpKill:
2444 outputTriplet(visit, "discard;\n", "", "");
2445 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002446 case EOpBreak:
2447 if (visit == PreVisit)
2448 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002449 if (mNestedLoopDepth > 1)
2450 {
2451 mUsesNestedBreak = true;
2452 }
2453
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002454 if (mExcessiveLoopIndex)
2455 {
2456 out << "{Break";
2457 mExcessiveLoopIndex->traverse(this);
2458 out << " = true; break;}\n";
2459 }
2460 else
2461 {
2462 out << "break;\n";
2463 }
2464 }
2465 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002466 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 case EOpReturn:
2468 if (visit == PreVisit)
2469 {
2470 if (node->getExpression())
2471 {
2472 out << "return ";
2473 }
2474 else
2475 {
2476 out << "return;\n";
2477 }
2478 }
2479 else if (visit == PostVisit)
2480 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002481 if (node->getExpression())
2482 {
2483 out << ";\n";
2484 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485 }
2486 break;
2487 default: UNREACHABLE();
2488 }
2489
2490 return true;
2491}
2492
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002493void OutputHLSL::traverseStatements(TIntermNode *node)
2494{
2495 if (isSingleStatement(node))
2496 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002497 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002498 }
2499
2500 node->traverse(this);
2501}
2502
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002503bool OutputHLSL::isSingleStatement(TIntermNode *node)
2504{
2505 TIntermAggregate *aggregate = node->getAsAggregate();
2506
2507 if (aggregate)
2508 {
2509 if (aggregate->getOp() == EOpSequence)
2510 {
2511 return false;
2512 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002513 else if (aggregate->getOp() == EOpDeclaration)
2514 {
2515 // Declaring multiple comma-separated variables must be considered multiple statements
2516 // because each individual declaration has side effects which are visible in the next.
2517 return false;
2518 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002519 else
2520 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002521 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002522 {
2523 if (!isSingleStatement(*sit))
2524 {
2525 return false;
2526 }
2527 }
2528
2529 return true;
2530 }
2531 }
2532
2533 return true;
2534}
2535
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002536// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2537// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002538bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2539{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002540 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002541 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002542
2543 // Parse loops of the form:
2544 // for(int index = initial; index [comparator] limit; index += increment)
2545 TIntermSymbol *index = NULL;
2546 TOperator comparator = EOpNull;
2547 int initial = 0;
2548 int limit = 0;
2549 int increment = 0;
2550
2551 // Parse index name and intial value
2552 if (node->getInit())
2553 {
2554 TIntermAggregate *init = node->getInit()->getAsAggregate();
2555
2556 if (init)
2557 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002558 TIntermSequence *sequence = init->getSequence();
2559 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002560
2561 if (variable && variable->getQualifier() == EvqTemporary)
2562 {
2563 TIntermBinary *assign = variable->getAsBinaryNode();
2564
2565 if (assign->getOp() == EOpInitialize)
2566 {
2567 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2568 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2569
2570 if (symbol && constant)
2571 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002572 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002573 {
2574 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002575 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002576 }
2577 }
2578 }
2579 }
2580 }
2581 }
2582
2583 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002584 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002585 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002586 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002587
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002588 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2589 {
2590 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2591
2592 if (constant)
2593 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002594 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002595 {
2596 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002597 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002598 }
2599 }
2600 }
2601 }
2602
2603 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002604 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002605 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002606 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2607 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002608
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002609 if (binaryTerminal)
2610 {
2611 TOperator op = binaryTerminal->getOp();
2612 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2613
2614 if (constant)
2615 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002616 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002617 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002618 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002619
2620 switch (op)
2621 {
2622 case EOpAddAssign: increment = value; break;
2623 case EOpSubAssign: increment = -value; break;
2624 default: UNIMPLEMENTED();
2625 }
2626 }
2627 }
2628 }
2629 else if (unaryTerminal)
2630 {
2631 TOperator op = unaryTerminal->getOp();
2632
2633 switch (op)
2634 {
2635 case EOpPostIncrement: increment = 1; break;
2636 case EOpPostDecrement: increment = -1; break;
2637 case EOpPreIncrement: increment = 1; break;
2638 case EOpPreDecrement: increment = -1; break;
2639 default: UNIMPLEMENTED();
2640 }
2641 }
2642 }
2643
2644 if (index != NULL && comparator != EOpNull && increment != 0)
2645 {
2646 if (comparator == EOpLessThanEqual)
2647 {
2648 comparator = EOpLessThan;
2649 limit += 1;
2650 }
2651
2652 if (comparator == EOpLessThan)
2653 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002654 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002655
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002656 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002657 {
2658 return false; // Not an excessive loop
2659 }
2660
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002661 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2662 mExcessiveLoopIndex = index;
2663
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002664 out << "{int ";
2665 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002666 out << ";\n"
2667 "bool Break";
2668 index->traverse(this);
2669 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002670
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002671 bool firstLoopFragment = true;
2672
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002673 while (iterations > 0)
2674 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002675 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002676
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002677 if (!firstLoopFragment)
2678 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002679 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002680 index->traverse(this);
2681 out << ") {\n";
2682 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002683
2684 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2685 {
2686 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2687 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002688
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002689 // for(int index = initial; index < clampedLimit; index += increment)
2690
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002691 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002692 index->traverse(this);
2693 out << " = ";
2694 out << initial;
2695
2696 out << "; ";
2697 index->traverse(this);
2698 out << " < ";
2699 out << clampedLimit;
2700
2701 out << "; ";
2702 index->traverse(this);
2703 out << " += ";
2704 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002705 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002706
Jamie Madill075edd82013-07-08 13:30:19 -04002707 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002708 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002709
2710 if (node->getBody())
2711 {
2712 node->getBody()->traverse(this);
2713 }
2714
Jamie Madill075edd82013-07-08 13:30:19 -04002715 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002716 out << ";}\n";
2717
2718 if (!firstLoopFragment)
2719 {
2720 out << "}\n";
2721 }
2722
2723 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002724
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002725 initial += MAX_LOOP_ITERATIONS * increment;
2726 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002727 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002728
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002729 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002730
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002731 mExcessiveLoopIndex = restoreIndex;
2732
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002733 return true;
2734 }
2735 else UNIMPLEMENTED();
2736 }
2737
2738 return false; // Not handled as an excessive loop
2739}
2740
Olli Etuaho7fb49552015-03-18 17:27:44 +02002741void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002743 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002744 {
2745 out << preString;
2746 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002747 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748 {
2749 out << inString;
2750 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002751 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002752 {
2753 out << postString;
2754 }
2755}
2756
Olli Etuaho7fb49552015-03-18 17:27:44 +02002757void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2758{
2759 outputTriplet(visit, preString, inString, postString, getInfoSink());
2760}
2761
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002762void OutputHLSL::outputLineDirective(int line)
2763{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002764 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002765 {
Jamie Madill32aab012015-01-27 14:12:26 -05002766 TInfoSinkBase &out = getInfoSink();
2767
2768 out << "\n";
2769 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002770
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002771 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002772 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002773 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002774 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002775
Jamie Madill32aab012015-01-27 14:12:26 -05002776 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002777 }
2778}
2779
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002780TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2781{
2782 TQualifier qualifier = symbol->getQualifier();
2783 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002784 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002785
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002786 if (name.empty()) // HLSL demands named arguments, also for prototypes
2787 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002788 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002789 }
2790 else
2791 {
Jamie Madill033dae62014-06-18 12:56:28 -04002792 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002793 }
2794
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002795 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2796 {
Jamie Madill033dae62014-06-18 12:56:28 -04002797 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002798 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002799 }
2800
Jamie Madill033dae62014-06-18 12:56:28 -04002801 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002802}
2803
2804TString OutputHLSL::initializer(const TType &type)
2805{
2806 TString string;
2807
Jamie Madill94bf7f22013-07-08 13:31:15 -04002808 size_t size = type.getObjectSize();
2809 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002811 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002812
Jamie Madill94bf7f22013-07-08 13:31:15 -04002813 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002814 {
2815 string += ", ";
2816 }
2817 }
2818
daniel@transgaming.comead23042010-04-29 03:35:36 +00002819 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002820}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002821
Daniel Bratell29190082015-02-20 16:42:54 +01002822void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002823{
Jamie Madill32aab012015-01-27 14:12:26 -05002824 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002825
2826 if (visit == PreVisit)
2827 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002828 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002829
Daniel Bratell29190082015-02-20 16:42:54 +01002830 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002831 }
2832 else if (visit == InVisit)
2833 {
2834 out << ", ";
2835 }
2836 else if (visit == PostVisit)
2837 {
2838 out << ")";
2839 }
2840}
2841
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002842const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2843{
Jamie Madill32aab012015-01-27 14:12:26 -05002844 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002845
Jamie Madill98493dd2013-07-08 14:39:03 -04002846 const TStructure* structure = type.getStruct();
2847 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002848 {
Jamie Madill033dae62014-06-18 12:56:28 -04002849 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002850
Jamie Madill98493dd2013-07-08 14:39:03 -04002851 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002852
Jamie Madill98493dd2013-07-08 14:39:03 -04002853 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002854 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002855 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002856 constUnion = writeConstantUnion(*fieldType, constUnion);
2857
Jamie Madill98493dd2013-07-08 14:39:03 -04002858 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002859 {
2860 out << ", ";
2861 }
2862 }
2863
2864 out << ")";
2865 }
2866 else
2867 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002868 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002869 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002870
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002871 if (writeType)
2872 {
Jamie Madill033dae62014-06-18 12:56:28 -04002873 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002874 }
2875
Jamie Madill94bf7f22013-07-08 13:31:15 -04002876 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002877 {
2878 switch (constUnion->getType())
2879 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002880 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002881 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002882 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002883 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002884 default: UNREACHABLE();
2885 }
2886
2887 if (i != size - 1)
2888 {
2889 out << ", ";
2890 }
2891 }
2892
2893 if (writeType)
2894 {
2895 out << ")";
2896 }
2897 }
2898
2899 return constUnion;
2900}
2901
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002902void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2903{
2904 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2905 outputTriplet(visit, preString.c_str(), ", ", ")");
2906}
2907
Jamie Madill37997142015-01-28 10:06:34 -05002908bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2909{
2910 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2911 expression->traverse(&searchSymbol);
2912
2913 if (searchSymbol.foundMatch())
2914 {
2915 // Type already printed
2916 out << "t" + str(mUniqueIndex) + " = ";
2917 expression->traverse(this);
2918 out << ", ";
2919 symbolNode->traverse(this);
2920 out << " = t" + str(mUniqueIndex);
2921
2922 mUniqueIndex++;
2923 return true;
2924 }
2925
2926 return false;
2927}
2928
2929void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2930{
2931 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2932 << "\n"
2933 << "void initializeDeferredGlobals()\n"
2934 << "{\n";
2935
2936 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2937 {
2938 TIntermSymbol *symbol = deferredGlobal.first;
2939 TIntermTyped *expression = deferredGlobal.second;
2940 ASSERT(symbol);
2941 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2942
2943 out << " " << Decorate(symbol->getSymbol()) << " = ";
2944
2945 if (!writeSameSymbolInitializer(out, symbol, expression))
2946 {
2947 ASSERT(mInfoSinkStack.top() == &out);
2948 expression->traverse(this);
2949 }
2950
2951 out << ";\n";
2952 }
2953
2954 out << "}\n"
2955 << "\n";
2956}
2957
Jamie Madill55e79e02015-02-09 15:35:00 -05002958TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2959{
2960 const TFieldList &fields = structure.fields();
2961
2962 for (const auto &eqFunction : mStructEqualityFunctions)
2963 {
2964 if (eqFunction.structure == &structure)
2965 {
2966 return eqFunction.functionName;
2967 }
2968 }
2969
2970 const TString &structNameString = StructNameString(structure);
2971
2972 StructEqualityFunction function;
2973 function.structure = &structure;
2974 function.functionName = "angle_eq_" + structNameString;
2975
2976 TString &func = function.functionDefinition;
2977
2978 func = "bool " + function.functionName + "(" + structNameString + " a, " + structNameString + " b)\n" +
2979 "{\n"
2980 " return ";
2981
2982 for (size_t i = 0; i < fields.size(); i++)
2983 {
2984 const TField *field = fields[i];
2985 const TType *fieldType = field->type();
2986
2987 const TString &fieldNameA = "a." + Decorate(field->name());
2988 const TString &fieldNameB = "b." + Decorate(field->name());
2989
Olli Etuaho7fb49552015-03-18 17:27:44 +02002990 // TODO (oetuaho): Use outputEqual() here instead
2991
Jamie Madill55e79e02015-02-09 15:35:00 -05002992 if (i > 0)
2993 {
2994 func += " && ";
2995 }
2996
Olli Etuaho7fb49552015-03-18 17:27:44 +02002997 if (fieldType->isArray())
2998 {
2999 // TODO (oetuaho): This requires sorting array and struct equality functions together.
3000 UNIMPLEMENTED();
3001 }
3002 else if (fieldType->getBasicType() == EbtStruct)
Jamie Madill55e79e02015-02-09 15:35:00 -05003003 {
3004 const TStructure &fieldStruct = *fieldType->getStruct();
3005 const TString &functionName = addStructEqualityFunction(fieldStruct);
3006 func += functionName + "(" + fieldNameA + ", " + fieldNameB + ")";
3007 }
3008 else if (fieldType->isScalar())
3009 {
3010 func += "(" + fieldNameA + " == " + fieldNameB + ")";
3011 }
3012 else
3013 {
3014 ASSERT(fieldType->isMatrix() || fieldType->isVector());
3015 func += "all(" + fieldNameA + " == " + fieldNameB + ")";
3016 }
3017 }
3018
3019 func = func + ";\n" + "}\n";
3020
3021 mStructEqualityFunctions.push_back(function);
3022
3023 return function.functionName;
3024}
3025
Olli Etuaho7fb49552015-03-18 17:27:44 +02003026TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3027{
3028 for (const auto &eqFunction : mArrayEqualityFunctions)
3029 {
3030 if (eqFunction.type == type)
3031 {
3032 return eqFunction.functionName;
3033 }
3034 }
3035
3036 const TString &typeName = TypeString(type);
3037
3038 ArrayEqualityFunction function;
3039 function.type = type;
3040
3041 TInfoSinkBase fnNameOut;
3042 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
3043 function.functionName = fnNameOut.c_str();
3044
3045 TType nonArrayType = type;
3046 nonArrayType.clearArrayness();
3047
3048 TInfoSinkBase fnOut;
3049
3050 fnOut << "bool " << function.functionName << "("
3051 << typeName << "[" << type.getArraySize() << "] a, "
3052 << typeName << "[" << type.getArraySize() << "] b)\n"
3053 << "{\n"
3054 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3055 " {\n"
3056 " if (";
3057
3058 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3059 fnOut << "a[i]";
3060 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3061 fnOut << "b[i]";
3062 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3063
3064 fnOut << ") { return false; }\n"
3065 " }\n"
3066 " return true;\n"
3067 "}\n";
3068
3069 function.functionDefinition = fnOut.c_str();
3070
3071 mArrayEqualityFunctions.push_back(function);
3072
3073 return function.functionName;
3074}
3075
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003076}