blob: 7390b1acb623efd93ac68c0be5bc9e2c35e92da1 [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
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500341 if (mUsesDiscardRewriting)
342 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400343 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500344 }
345
Nicolas Capens655fe362014-04-11 13:12:34 -0400346 if (mUsesNestedBreak)
347 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400348 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400349 }
350
Arun Patole44efa0b2015-03-04 17:11:05 +0530351 if (mRequiresIEEEStrictCompiling)
352 {
353 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
354 }
355
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400356 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
357 "#define LOOP [loop]\n"
358 "#define FLATTEN [flatten]\n"
359 "#else\n"
360 "#define LOOP\n"
361 "#define FLATTEN\n"
362 "#endif\n";
363
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200364 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200366 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
367 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000368
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000369 out << "// Varyings\n";
370 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400371 out << "\n";
372
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200373 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000374 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500375 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000376 {
Jamie Madill46131a32013-06-20 11:55:50 -0400377 const TString &variableName = outputVariableIt->first;
378 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400379
Jamie Madill033dae62014-06-18 12:56:28 -0400380 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400381 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000382 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000383 }
Jamie Madill46131a32013-06-20 11:55:50 -0400384 else
385 {
386 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
387
388 out << "static float4 gl_Color[" << numColorValues << "] =\n"
389 "{\n";
390 for (unsigned int i = 0; i < numColorValues; i++)
391 {
392 out << " float4(0, 0, 0, 0)";
393 if (i + 1 != numColorValues)
394 {
395 out << ",";
396 }
397 out << "\n";
398 }
399
400 out << "};\n";
401 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000402
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400403 if (mUsesFragDepth)
404 {
405 out << "static float gl_Depth = 0.0;\n";
406 }
407
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000408 if (mUsesFragCoord)
409 {
410 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
411 }
412
413 if (mUsesPointCoord)
414 {
415 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
416 }
417
418 if (mUsesFrontFacing)
419 {
420 out << "static bool gl_FrontFacing = false;\n";
421 }
422
423 out << "\n";
424
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000425 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000426 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000427 out << "struct gl_DepthRangeParameters\n"
428 "{\n"
429 " float near;\n"
430 " float far;\n"
431 " float diff;\n"
432 "};\n"
433 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000434 }
435
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000436 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000437 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000438 out << "cbuffer DriverConstants : register(b1)\n"
439 "{\n";
440
441 if (mUsesDepthRange)
442 {
443 out << " float3 dx_DepthRange : packoffset(c0);\n";
444 }
445
446 if (mUsesFragCoord)
447 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000448 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000449 }
450
451 if (mUsesFragCoord || mUsesFrontFacing)
452 {
453 out << " float3 dx_DepthFront : packoffset(c2);\n";
454 }
455
456 out << "};\n";
457 }
458 else
459 {
460 if (mUsesDepthRange)
461 {
462 out << "uniform float3 dx_DepthRange : register(c0);";
463 }
464
465 if (mUsesFragCoord)
466 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000467 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000468 }
469
470 if (mUsesFragCoord || mUsesFrontFacing)
471 {
472 out << "uniform float3 dx_DepthFront : register(c2);\n";
473 }
474 }
475
476 out << "\n";
477
478 if (mUsesDepthRange)
479 {
480 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
481 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000482 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000483
Jamie Madillf91ce812014-06-13 10:04:34 -0400484 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000485 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400486 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000487 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400488 out << flaggedStructs;
489 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000490 }
491
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000492 if (usingMRTExtension && mNumRenderTargets > 1)
493 {
494 out << "#define GL_USES_MRT\n";
495 }
496
497 if (mUsesFragColor)
498 {
499 out << "#define GL_USES_FRAG_COLOR\n";
500 }
501
502 if (mUsesFragData)
503 {
504 out << "#define GL_USES_FRAG_DATA\n";
505 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000507 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000508 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000509 out << "// Attributes\n";
510 out << attributes;
511 out << "\n"
512 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400513
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000514 if (mUsesPointSize)
515 {
516 out << "static float gl_PointSize = float(1);\n";
517 }
518
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000519 if (mUsesInstanceID)
520 {
521 out << "static int gl_InstanceID;";
522 }
523
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000524 out << "\n"
525 "// Varyings\n";
526 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000527 out << "\n";
528
529 if (mUsesDepthRange)
530 {
531 out << "struct gl_DepthRangeParameters\n"
532 "{\n"
533 " float near;\n"
534 " float far;\n"
535 " float diff;\n"
536 "};\n"
537 "\n";
538 }
539
540 if (mOutputType == SH_HLSL11_OUTPUT)
541 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800542 out << "cbuffer DriverConstants : register(b1)\n"
543 "{\n";
544
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000545 if (mUsesDepthRange)
546 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800547 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000548 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800549
Cooper Partine6664f02015-01-09 16:22:24 -0800550 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800551 // However, we declare it for all shaders (including Feature Level 10+).
552 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
553 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800554 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800555
556 out << "};\n"
557 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000558 }
559 else
560 {
561 if (mUsesDepthRange)
562 {
563 out << "uniform float3 dx_DepthRange : register(c0);\n";
564 }
565
Cooper Partine6664f02015-01-09 16:22:24 -0800566 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
567 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000568 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000569 }
570
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000571 if (mUsesDepthRange)
572 {
573 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
574 "\n";
575 }
576
Jamie Madillf91ce812014-06-13 10:04:34 -0400577 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000578 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400579 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000580 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400581 out << flaggedStructs;
582 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000583 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400584 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000585
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400586 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
587 {
588 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400589 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000590 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400591 switch(textureFunction->sampler)
592 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400593 case EbtSampler2D: out << "int2 "; break;
594 case EbtSampler3D: out << "int3 "; break;
595 case EbtSamplerCube: out << "int2 "; break;
596 case EbtSampler2DArray: out << "int3 "; break;
597 case EbtISampler2D: out << "int2 "; break;
598 case EbtISampler3D: out << "int3 "; break;
599 case EbtISamplerCube: out << "int2 "; break;
600 case EbtISampler2DArray: out << "int3 "; break;
601 case EbtUSampler2D: out << "int2 "; break;
602 case EbtUSampler3D: out << "int3 "; break;
603 case EbtUSamplerCube: out << "int2 "; break;
604 case EbtUSampler2DArray: out << "int3 "; break;
605 case EbtSampler2DShadow: out << "int2 "; break;
606 case EbtSamplerCubeShadow: out << "int2 "; break;
607 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400608 default: UNREACHABLE();
609 }
610 }
611 else // Sampling function
612 {
613 switch(textureFunction->sampler)
614 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400615 case EbtSampler2D: out << "float4 "; break;
616 case EbtSampler3D: out << "float4 "; break;
617 case EbtSamplerCube: out << "float4 "; break;
618 case EbtSampler2DArray: out << "float4 "; break;
619 case EbtISampler2D: out << "int4 "; break;
620 case EbtISampler3D: out << "int4 "; break;
621 case EbtISamplerCube: out << "int4 "; break;
622 case EbtISampler2DArray: out << "int4 "; break;
623 case EbtUSampler2D: out << "uint4 "; break;
624 case EbtUSampler3D: out << "uint4 "; break;
625 case EbtUSamplerCube: out << "uint4 "; break;
626 case EbtUSampler2DArray: out << "uint4 "; break;
627 case EbtSampler2DShadow: out << "float "; break;
628 case EbtSamplerCubeShadow: out << "float "; break;
629 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400630 default: UNREACHABLE();
631 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000632 }
633
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400634 // Function name
635 out << textureFunction->name();
636
637 // Argument list
638 int hlslCoords = 4;
639
640 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000641 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400642 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000643 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400644 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
645 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
646 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000647 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400648
Nicolas Capens75fb4752013-07-10 15:14:47 -0400649 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000650 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400651 case TextureFunction::IMPLICIT: break;
652 case TextureFunction::BIAS: hlslCoords = 4; break;
653 case TextureFunction::LOD: hlslCoords = 4; break;
654 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400655 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400656 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000657 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400658 }
659 else if (mOutputType == SH_HLSL11_OUTPUT)
660 {
661 switch(textureFunction->sampler)
662 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400663 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
664 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
665 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
666 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
667 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
668 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500669 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400670 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
671 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
672 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500673 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400674 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
675 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
676 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
677 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400678 default: UNREACHABLE();
679 }
680 }
681 else UNREACHABLE();
682
Nicolas Capensfc014542014-02-18 14:47:13 -0500683 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400684 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500685 switch(textureFunction->coords)
686 {
687 case 2: out << ", int2 t"; break;
688 case 3: out << ", int3 t"; break;
689 default: UNREACHABLE();
690 }
691 }
692 else // Floating-point coordinates (except textureSize)
693 {
694 switch(textureFunction->coords)
695 {
696 case 1: out << ", int lod"; break; // textureSize()
697 case 2: out << ", float2 t"; break;
698 case 3: out << ", float3 t"; break;
699 case 4: out << ", float4 t"; break;
700 default: UNREACHABLE();
701 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000702 }
703
Nicolas Capensd11d5492014-02-19 17:06:10 -0500704 if (textureFunction->method == TextureFunction::GRAD)
705 {
706 switch(textureFunction->sampler)
707 {
708 case EbtSampler2D:
709 case EbtISampler2D:
710 case EbtUSampler2D:
711 case EbtSampler2DArray:
712 case EbtISampler2DArray:
713 case EbtUSampler2DArray:
714 case EbtSampler2DShadow:
715 case EbtSampler2DArrayShadow:
716 out << ", float2 ddx, float2 ddy";
717 break;
718 case EbtSampler3D:
719 case EbtISampler3D:
720 case EbtUSampler3D:
721 case EbtSamplerCube:
722 case EbtISamplerCube:
723 case EbtUSamplerCube:
724 case EbtSamplerCubeShadow:
725 out << ", float3 ddx, float3 ddy";
726 break;
727 default: UNREACHABLE();
728 }
729 }
730
Nicolas Capens75fb4752013-07-10 15:14:47 -0400731 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000732 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400733 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400734 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400735 case TextureFunction::LOD: out << ", float lod"; break;
736 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400737 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400738 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500739 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500740 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400741 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000742 }
743
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500744 if (textureFunction->offset)
745 {
746 switch(textureFunction->sampler)
747 {
748 case EbtSampler2D: out << ", int2 offset"; break;
749 case EbtSampler3D: out << ", int3 offset"; break;
750 case EbtSampler2DArray: out << ", int2 offset"; break;
751 case EbtISampler2D: out << ", int2 offset"; break;
752 case EbtISampler3D: out << ", int3 offset"; break;
753 case EbtISampler2DArray: out << ", int2 offset"; break;
754 case EbtUSampler2D: out << ", int2 offset"; break;
755 case EbtUSampler3D: out << ", int3 offset"; break;
756 case EbtUSampler2DArray: out << ", int2 offset"; break;
757 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500758 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500759 default: UNREACHABLE();
760 }
761 }
762
Nicolas Capens84cfa122014-04-14 13:48:45 -0400763 if (textureFunction->method == TextureFunction::BIAS ||
764 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500765 {
766 out << ", float bias";
767 }
768
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400769 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400770 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400771
Nicolas Capens75fb4752013-07-10 15:14:47 -0400772 if (textureFunction->method == TextureFunction::SIZE)
773 {
774 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
775 {
776 if (IsSamplerArray(textureFunction->sampler))
777 {
778 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
779 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
780 }
781 else
782 {
783 out << " uint width; uint height; uint numberOfLevels;\n"
784 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
785 }
786 }
787 else if (IsSampler3D(textureFunction->sampler))
788 {
789 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
790 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
791 }
792 else UNREACHABLE();
793
794 switch(textureFunction->sampler)
795 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400796 case EbtSampler2D: out << " return int2(width, height);"; break;
797 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
798 case EbtSamplerCube: out << " return int2(width, height);"; break;
799 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
800 case EbtISampler2D: out << " return int2(width, height);"; break;
801 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
802 case EbtISamplerCube: out << " return int2(width, height);"; break;
803 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
804 case EbtUSampler2D: out << " return int2(width, height);"; break;
805 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
806 case EbtUSamplerCube: out << " return int2(width, height);"; break;
807 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
808 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
809 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
810 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400811 default: UNREACHABLE();
812 }
813 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400814 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400815 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500816 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
817 {
818 out << " float width; float height; float layers; float levels;\n";
819
820 out << " uint mip = 0;\n";
821
822 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
823
824 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
825 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
826 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
827 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
828
829 // FACE_POSITIVE_X = 000b
830 // FACE_NEGATIVE_X = 001b
831 // FACE_POSITIVE_Y = 010b
832 // FACE_NEGATIVE_Y = 011b
833 // FACE_POSITIVE_Z = 100b
834 // FACE_NEGATIVE_Z = 101b
835 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
836
837 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
838 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
839 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
840
841 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
842 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
843 }
844 else if (IsIntegerSampler(textureFunction->sampler) &&
845 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400846 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400847 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400848 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400849 if (IsSamplerArray(textureFunction->sampler))
850 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400851 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400852
Nicolas Capens9edebd62013-08-06 10:59:10 -0400853 if (textureFunction->method == TextureFunction::LOD0)
854 {
855 out << " uint mip = 0;\n";
856 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400857 else if (textureFunction->method == TextureFunction::LOD0BIAS)
858 {
859 out << " uint mip = bias;\n";
860 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400861 else
862 {
863 if (textureFunction->method == TextureFunction::IMPLICIT ||
864 textureFunction->method == TextureFunction::BIAS)
865 {
866 out << " x.GetDimensions(0, width, height, layers, levels);\n"
867 " float2 tSized = float2(t.x * width, t.y * height);\n"
868 " float dx = length(ddx(tSized));\n"
869 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500870 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400871
872 if (textureFunction->method == TextureFunction::BIAS)
873 {
874 out << " lod += bias;\n";
875 }
876 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500877 else if (textureFunction->method == TextureFunction::GRAD)
878 {
879 out << " x.GetDimensions(0, width, height, layers, levels);\n"
880 " float lod = log2(max(length(ddx), length(ddy)));\n";
881 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400882
883 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
884 }
885
886 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400887 }
888 else
889 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400890 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400891
Nicolas Capens9edebd62013-08-06 10:59:10 -0400892 if (textureFunction->method == TextureFunction::LOD0)
893 {
894 out << " uint mip = 0;\n";
895 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400896 else if (textureFunction->method == TextureFunction::LOD0BIAS)
897 {
898 out << " uint mip = bias;\n";
899 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400900 else
901 {
902 if (textureFunction->method == TextureFunction::IMPLICIT ||
903 textureFunction->method == TextureFunction::BIAS)
904 {
905 out << " x.GetDimensions(0, width, height, levels);\n"
906 " float2 tSized = float2(t.x * width, t.y * height);\n"
907 " float dx = length(ddx(tSized));\n"
908 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500909 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400910
911 if (textureFunction->method == TextureFunction::BIAS)
912 {
913 out << " lod += bias;\n";
914 }
915 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500916 else if (textureFunction->method == TextureFunction::LOD)
917 {
918 out << " x.GetDimensions(0, width, height, levels);\n";
919 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500920 else if (textureFunction->method == TextureFunction::GRAD)
921 {
922 out << " x.GetDimensions(0, width, height, levels);\n"
923 " float lod = log2(max(length(ddx), length(ddy)));\n";
924 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400925
926 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
927 }
928
929 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400930 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400931 }
932 else if (IsSampler3D(textureFunction->sampler))
933 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400934 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400935
Nicolas Capens9edebd62013-08-06 10:59:10 -0400936 if (textureFunction->method == TextureFunction::LOD0)
937 {
938 out << " uint mip = 0;\n";
939 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400940 else if (textureFunction->method == TextureFunction::LOD0BIAS)
941 {
942 out << " uint mip = bias;\n";
943 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400944 else
945 {
946 if (textureFunction->method == TextureFunction::IMPLICIT ||
947 textureFunction->method == TextureFunction::BIAS)
948 {
949 out << " x.GetDimensions(0, width, height, depth, levels);\n"
950 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
951 " float dx = length(ddx(tSized));\n"
952 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500953 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400954
955 if (textureFunction->method == TextureFunction::BIAS)
956 {
957 out << " lod += bias;\n";
958 }
959 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500960 else if (textureFunction->method == TextureFunction::GRAD)
961 {
962 out << " x.GetDimensions(0, width, height, depth, levels);\n"
963 " float lod = log2(max(length(ddx), length(ddy)));\n";
964 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400965
966 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
967 }
968
969 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400970 }
971 else UNREACHABLE();
972 }
973
974 out << " return ";
975
976 // HLSL intrinsic
977 if (mOutputType == SH_HLSL9_OUTPUT)
978 {
979 switch(textureFunction->sampler)
980 {
981 case EbtSampler2D: out << "tex2D"; break;
982 case EbtSamplerCube: out << "texCUBE"; break;
983 default: UNREACHABLE();
984 }
985
Nicolas Capens75fb4752013-07-10 15:14:47 -0400986 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400987 {
988 case TextureFunction::IMPLICIT: out << "(s, "; break;
989 case TextureFunction::BIAS: out << "bias(s, "; break;
990 case TextureFunction::LOD: out << "lod(s, "; break;
991 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400992 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400993 default: UNREACHABLE();
994 }
995 }
996 else if (mOutputType == SH_HLSL11_OUTPUT)
997 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500998 if (textureFunction->method == TextureFunction::GRAD)
999 {
1000 if (IsIntegerSampler(textureFunction->sampler))
1001 {
1002 out << "x.Load(";
1003 }
1004 else if (IsShadowSampler(textureFunction->sampler))
1005 {
1006 out << "x.SampleCmpLevelZero(s, ";
1007 }
1008 else
1009 {
1010 out << "x.SampleGrad(s, ";
1011 }
1012 }
1013 else if (IsIntegerSampler(textureFunction->sampler) ||
1014 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001015 {
1016 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001017 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001018 else if (IsShadowSampler(textureFunction->sampler))
1019 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001020 switch(textureFunction->method)
1021 {
1022 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1023 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1024 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1025 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1026 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1027 default: UNREACHABLE();
1028 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001029 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001030 else
1031 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001032 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001033 {
1034 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1035 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1036 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1037 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001038 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001039 default: UNREACHABLE();
1040 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001041 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001042 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001043 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001044
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001045 // Integer sampling requires integer addresses
1046 TString addressx = "";
1047 TString addressy = "";
1048 TString addressz = "";
1049 TString close = "";
1050
Nicolas Capensfc014542014-02-18 14:47:13 -05001051 if (IsIntegerSampler(textureFunction->sampler) ||
1052 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001053 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001054 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001055 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001056 case 2: out << "int3("; break;
1057 case 3: out << "int4("; break;
1058 default: UNREACHABLE();
1059 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001060
Nicolas Capensfc014542014-02-18 14:47:13 -05001061 // Convert from normalized floating-point to integer
1062 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001063 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001064 addressx = "int(floor(width * frac((";
1065 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001066
Nicolas Capensfc014542014-02-18 14:47:13 -05001067 if (IsSamplerArray(textureFunction->sampler))
1068 {
1069 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1070 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001071 else if (IsSamplerCube(textureFunction->sampler))
1072 {
1073 addressz = "((((";
1074 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001075 else
1076 {
1077 addressz = "int(floor(depth * frac((";
1078 }
1079
1080 close = "))))";
1081 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001082 }
1083 else
1084 {
1085 switch(hlslCoords)
1086 {
1087 case 2: out << "float2("; break;
1088 case 3: out << "float3("; break;
1089 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001090 default: UNREACHABLE();
1091 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001092 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001093
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001094 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001095
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001096 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001097 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001098 switch(textureFunction->coords)
1099 {
1100 case 3: proj = " / t.z"; break;
1101 case 4: proj = " / t.w"; break;
1102 default: UNREACHABLE();
1103 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001104 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001105
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001106 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001107
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001108 if (mOutputType == SH_HLSL9_OUTPUT)
1109 {
1110 if (hlslCoords >= 3)
1111 {
1112 if (textureFunction->coords < 3)
1113 {
1114 out << ", 0";
1115 }
1116 else
1117 {
1118 out << ", t.z" + proj;
1119 }
1120 }
1121
1122 if (hlslCoords == 4)
1123 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001124 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001125 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001126 case TextureFunction::BIAS: out << ", bias"; break;
1127 case TextureFunction::LOD: out << ", lod"; break;
1128 case TextureFunction::LOD0: out << ", 0"; break;
1129 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001130 default: UNREACHABLE();
1131 }
1132 }
1133
1134 out << "));\n";
1135 }
1136 else if (mOutputType == SH_HLSL11_OUTPUT)
1137 {
1138 if (hlslCoords >= 3)
1139 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001140 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1141 {
1142 out << ", face";
1143 }
1144 else
1145 {
1146 out << ", " + addressz + ("t.z" + proj) + close;
1147 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001148 }
1149
Nicolas Capensd11d5492014-02-19 17:06:10 -05001150 if (textureFunction->method == TextureFunction::GRAD)
1151 {
1152 if (IsIntegerSampler(textureFunction->sampler))
1153 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001154 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001155 }
1156 else if (IsShadowSampler(textureFunction->sampler))
1157 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001158 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001159 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001160 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001161 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1162 // The resulting third component of P' in the shadow forms is used as Dref
1163 out << "), t.z" << proj;
1164 }
1165 else
1166 {
1167 switch(textureFunction->coords)
1168 {
1169 case 3: out << "), t.z"; break;
1170 case 4: out << "), t.w"; break;
1171 default: UNREACHABLE();
1172 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001173 }
1174 }
1175 else
1176 {
1177 out << "), ddx, ddy";
1178 }
1179 }
1180 else if (IsIntegerSampler(textureFunction->sampler) ||
1181 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001182 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001183 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001184 }
1185 else if (IsShadowSampler(textureFunction->sampler))
1186 {
1187 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001188 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001189 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001190 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1191 // The resulting third component of P' in the shadow forms is used as Dref
1192 out << "), t.z" << proj;
1193 }
1194 else
1195 {
1196 switch(textureFunction->coords)
1197 {
1198 case 3: out << "), t.z"; break;
1199 case 4: out << "), t.w"; break;
1200 default: UNREACHABLE();
1201 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001202 }
1203 }
1204 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001205 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001206 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001207 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001208 case TextureFunction::IMPLICIT: out << ")"; break;
1209 case TextureFunction::BIAS: out << "), bias"; break;
1210 case TextureFunction::LOD: out << "), lod"; break;
1211 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001212 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001213 default: UNREACHABLE();
1214 }
1215 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001216
1217 if (textureFunction->offset)
1218 {
1219 out << ", offset";
1220 }
1221
1222 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001223 }
1224 else UNREACHABLE();
1225 }
1226
1227 out << "\n"
1228 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001229 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001230 }
1231
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001232 if (mUsesFragCoord)
1233 {
1234 out << "#define GL_USES_FRAG_COORD\n";
1235 }
1236
1237 if (mUsesPointCoord)
1238 {
1239 out << "#define GL_USES_POINT_COORD\n";
1240 }
1241
1242 if (mUsesFrontFacing)
1243 {
1244 out << "#define GL_USES_FRONT_FACING\n";
1245 }
1246
1247 if (mUsesPointSize)
1248 {
1249 out << "#define GL_USES_POINT_SIZE\n";
1250 }
1251
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001252 if (mUsesFragDepth)
1253 {
1254 out << "#define GL_USES_FRAG_DEPTH\n";
1255 }
1256
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001257 if (mUsesDepthRange)
1258 {
1259 out << "#define GL_USES_DEPTH_RANGE\n";
1260 }
1261
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001262 if (mUsesXor)
1263 {
1264 out << "bool xor(bool p, bool q)\n"
1265 "{\n"
1266 " return (p || q) && !(p && q);\n"
1267 "}\n"
1268 "\n";
1269 }
1270
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001271 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001272}
1273
1274void OutputHLSL::visitSymbol(TIntermSymbol *node)
1275{
Jamie Madill32aab012015-01-27 14:12:26 -05001276 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001277
Jamie Madill570e04d2013-06-21 09:15:33 -04001278 // Handle accessing std140 structs by value
1279 if (mFlaggedStructMappedNames.count(node) > 0)
1280 {
1281 out << mFlaggedStructMappedNames[node];
1282 return;
1283 }
1284
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001285 TString name = node->getSymbol();
1286
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001287 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001288 {
1289 mUsesDepthRange = true;
1290 out << name;
1291 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292 else
1293 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001294 TQualifier qualifier = node->getQualifier();
1295
1296 if (qualifier == EvqUniform)
1297 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001298 const TType& nodeType = node->getType();
1299 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1300
1301 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001302 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001303 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001304 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001305 else
1306 {
1307 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001308 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001309
Jamie Madill033dae62014-06-18 12:56:28 -04001310 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001311 }
Jamie Madill19571812013-08-12 15:26:34 -07001312 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001313 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001314 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001315 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001316 }
Jamie Madill033dae62014-06-18 12:56:28 -04001317 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001318 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001319 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001320 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001321 }
Jamie Madill19571812013-08-12 15:26:34 -07001322 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001323 {
1324 mReferencedOutputVariables[name] = node;
1325 out << "out_" << name;
1326 }
1327 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001328 {
1329 out << "gl_Color[0]";
1330 mUsesFragColor = true;
1331 }
1332 else if (qualifier == EvqFragData)
1333 {
1334 out << "gl_Color";
1335 mUsesFragData = true;
1336 }
1337 else if (qualifier == EvqFragCoord)
1338 {
1339 mUsesFragCoord = true;
1340 out << name;
1341 }
1342 else if (qualifier == EvqPointCoord)
1343 {
1344 mUsesPointCoord = true;
1345 out << name;
1346 }
1347 else if (qualifier == EvqFrontFacing)
1348 {
1349 mUsesFrontFacing = true;
1350 out << name;
1351 }
1352 else if (qualifier == EvqPointSize)
1353 {
1354 mUsesPointSize = true;
1355 out << name;
1356 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001357 else if (qualifier == EvqInstanceID)
1358 {
1359 mUsesInstanceID = true;
1360 out << name;
1361 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001362 else if (name == "gl_FragDepthEXT")
1363 {
1364 mUsesFragDepth = true;
1365 out << "gl_Depth";
1366 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001367 else if (qualifier == EvqInternal)
1368 {
1369 out << name;
1370 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001371 else
1372 {
Jamie Madill033dae62014-06-18 12:56:28 -04001373 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001374 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001375 }
1376}
1377
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001378void OutputHLSL::visitRaw(TIntermRaw *node)
1379{
Jamie Madill32aab012015-01-27 14:12:26 -05001380 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001381}
1382
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001383bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1384{
Jamie Madill32aab012015-01-27 14:12:26 -05001385 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001386
Jamie Madill570e04d2013-06-21 09:15:33 -04001387 // Handle accessing std140 structs by value
1388 if (mFlaggedStructMappedNames.count(node) > 0)
1389 {
1390 out << mFlaggedStructMappedNames[node];
1391 return false;
1392 }
1393
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001394 switch (node->getOp())
1395 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001396 case EOpAssign:
1397 if (node->getLeft()->isArray())
1398 {
1399 UNIMPLEMENTED();
1400 }
1401 else
1402 {
1403 outputTriplet(visit, "(", " = ", ")");
1404 }
1405 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001406 case EOpInitialize:
1407 if (visit == PreVisit)
1408 {
1409 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1410 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1411 // new variable is created before the assignment is evaluated), so we need to convert
1412 // this to "float t = x, x = t;".
1413
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001414 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001415 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001416 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001417
Jamie Madill37997142015-01-28 10:06:34 -05001418 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1419 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001420 {
Jamie Madill37997142015-01-28 10:06:34 -05001421 // For variables which are not constant, defer their real initialization until
1422 // after we initialize other globals: uniforms, attributes and varyings.
1423 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1424 const TString &initString = initializer(node->getType());
1425 node->setRight(new TIntermRaw(node->getType(), initString));
1426 }
1427 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1428 {
1429 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001430 return false;
1431 }
1432 }
1433 else if (visit == InVisit)
1434 {
1435 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001436 }
1437 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001438 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1439 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1440 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1441 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1442 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1443 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001444 if (visit == PreVisit)
1445 {
1446 out << "(";
1447 }
1448 else if (visit == InVisit)
1449 {
1450 out << " = mul(";
1451 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001452 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001453 }
1454 else
1455 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001456 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001457 }
1458 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001459 case EOpMatrixTimesMatrixAssign:
1460 if (visit == PreVisit)
1461 {
1462 out << "(";
1463 }
1464 else if (visit == InVisit)
1465 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001466 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001467 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001468 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001469 }
1470 else
1471 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001472 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001473 }
1474 break;
1475 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001476 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001477 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1478 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1479 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1480 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1481 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001482 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001483 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001484 const TType& leftType = node->getLeft()->getType();
1485 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001486 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001487 if (visit == PreVisit)
1488 {
1489 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1490 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001491 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001492 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001493 return false;
1494 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001495 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001496 else
1497 {
1498 outputTriplet(visit, "", "[", "]");
1499 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001500 }
1501 break;
1502 case EOpIndexIndirect:
1503 // We do not currently support indirect references to interface blocks
1504 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1505 outputTriplet(visit, "", "[", "]");
1506 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001507 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001508 if (visit == InVisit)
1509 {
1510 const TStructure* structure = node->getLeft()->getType().getStruct();
1511 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1512 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001513 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001514
1515 return false;
1516 }
1517 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001518 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001519 if (visit == InVisit)
1520 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001521 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1522 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1523 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001524 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001525
1526 return false;
1527 }
1528 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001529 case EOpVectorSwizzle:
1530 if (visit == InVisit)
1531 {
1532 out << ".";
1533
1534 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1535
1536 if (swizzle)
1537 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001538 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001539
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001540 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001541 {
1542 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1543
1544 if (element)
1545 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001546 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001547
1548 switch (i)
1549 {
1550 case 0: out << "x"; break;
1551 case 1: out << "y"; break;
1552 case 2: out << "z"; break;
1553 case 3: out << "w"; break;
1554 default: UNREACHABLE();
1555 }
1556 }
1557 else UNREACHABLE();
1558 }
1559 }
1560 else UNREACHABLE();
1561
1562 return false; // Fully processed
1563 }
1564 break;
1565 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1566 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1567 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1568 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001569 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001570 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1571 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1572 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1573 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1574 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001575 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001576 case EOpNotEqual:
Olli Etuahoe79904c2015-03-18 16:56:42 +02001577 if (node->getLeft()->isArray())
1578 {
1579 UNIMPLEMENTED();
1580 }
1581 else if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001582 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001583 if (node->getOp() == EOpEqual)
1584 {
1585 outputTriplet(visit, "(", " == ", ")");
1586 }
1587 else
1588 {
1589 outputTriplet(visit, "(", " != ", ")");
1590 }
1591 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001592 else
1593 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001594 if (visit == PreVisit && node->getOp() == EOpNotEqual)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001595 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001596 out << "!";
1597 }
1598
1599 if (node->getLeft()->getBasicType() == EbtStruct)
1600 {
1601 const TStructure &structure = *node->getLeft()->getType().getStruct();
1602 const TString &functionName = addStructEqualityFunction(structure);
Daniel Bratell29190082015-02-20 16:42:54 +01001603 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001604 }
1605 else
1606 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001607 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
1608 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001609 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001610 }
1611 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001612 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1613 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1614 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1615 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1616 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001617 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001618 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1619 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001620 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001621 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001622 if (node->getRight()->hasSideEffects())
1623 {
1624 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1625 return false;
1626 }
1627 else
1628 {
1629 outputTriplet(visit, "(", " || ", ")");
1630 return true;
1631 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001632 case EOpLogicalXor:
1633 mUsesXor = true;
1634 outputTriplet(visit, "xor(", ", ", ")");
1635 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001636 case EOpLogicalAnd:
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.com4f39fd92010-03-08 20:26:45 +00001647 default: UNREACHABLE();
1648 }
1649
1650 return true;
1651}
1652
1653bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1654{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001655 switch (node->getOp())
1656 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001657 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001658 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001659 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1660 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001661 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001662 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1663 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1664 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1665 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001666 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1667 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1668 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1669 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1670 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1671 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1672 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1673 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001674 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1675 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1676 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1677 case EOpAsinh:
1678 ASSERT(node->getUseEmulatedFunction());
1679 writeEmulatedFunctionTriplet(visit, "asinh(");
1680 break;
1681 case EOpAcosh:
1682 ASSERT(node->getUseEmulatedFunction());
1683 writeEmulatedFunctionTriplet(visit, "acosh(");
1684 break;
1685 case EOpAtanh:
1686 ASSERT(node->getUseEmulatedFunction());
1687 writeEmulatedFunctionTriplet(visit, "atanh(");
1688 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001689 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1690 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1691 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1692 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1693 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1694 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1695 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1696 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1697 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001698 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1699 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1700 case EOpRoundEven:
1701 ASSERT(node->getUseEmulatedFunction());
1702 writeEmulatedFunctionTriplet(visit, "roundEven(");
1703 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001704 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1705 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301706 case EOpIsNan:
1707 outputTriplet(visit, "isnan(", "", ")");
1708 mRequiresIEEEStrictCompiling = true;
1709 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301710 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001711 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1712 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1713 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1714 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001715 case EOpPackSnorm2x16:
1716 ASSERT(node->getUseEmulatedFunction());
1717 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1718 break;
1719 case EOpPackUnorm2x16:
1720 ASSERT(node->getUseEmulatedFunction());
1721 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1722 break;
1723 case EOpPackHalf2x16:
1724 ASSERT(node->getUseEmulatedFunction());
1725 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1726 break;
1727 case EOpUnpackSnorm2x16:
1728 ASSERT(node->getUseEmulatedFunction());
1729 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1730 break;
1731 case EOpUnpackUnorm2x16:
1732 ASSERT(node->getUseEmulatedFunction());
1733 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1734 break;
1735 case EOpUnpackHalf2x16:
1736 ASSERT(node->getUseEmulatedFunction());
1737 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1738 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001739 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1740 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001741 case EOpDFdx:
1742 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1743 {
1744 outputTriplet(visit, "(", "", ", 0.0)");
1745 }
1746 else
1747 {
1748 outputTriplet(visit, "ddx(", "", ")");
1749 }
1750 break;
1751 case EOpDFdy:
1752 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1753 {
1754 outputTriplet(visit, "(", "", ", 0.0)");
1755 }
1756 else
1757 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001758 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001759 }
1760 break;
1761 case EOpFwidth:
1762 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1763 {
1764 outputTriplet(visit, "(", "", ", 0.0)");
1765 }
1766 else
1767 {
1768 outputTriplet(visit, "fwidth(", "", ")");
1769 }
1770 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001771 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1772 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001773 case EOpInverse:
1774 ASSERT(node->getUseEmulatedFunction());
1775 writeEmulatedFunctionTriplet(visit, "inverse(");
1776 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001777
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001778 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1779 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001780 default: UNREACHABLE();
1781 }
1782
1783 return true;
1784}
1785
1786bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1787{
Jamie Madill32aab012015-01-27 14:12:26 -05001788 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001789
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001790 switch (node->getOp())
1791 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001792 case EOpSequence:
1793 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001794 if (mInsideFunction)
1795 {
Jamie Madill075edd82013-07-08 13:30:19 -04001796 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001797 out << "{\n";
1798 }
1799
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001800 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001801 {
Jamie Madill075edd82013-07-08 13:30:19 -04001802 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001803
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001804 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001805
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001806 // Don't output ; after case labels, they're terminated by :
1807 // This is needed especially since outputting a ; after a case statement would turn empty
1808 // case statements into non-empty case statements, disallowing fall-through from them.
1809 if ((*sit)->getAsCaseNode() == nullptr)
1810 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001811 }
1812
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001813 if (mInsideFunction)
1814 {
Jamie Madill075edd82013-07-08 13:30:19 -04001815 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001816 out << "}\n";
1817 }
1818
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001819 return false;
1820 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001821 case EOpDeclaration:
1822 if (visit == PreVisit)
1823 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001824 TIntermSequence *sequence = node->getSequence();
1825 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001826
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001827 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001829 TStructure *structure = variable->getType().getStruct();
1830
1831 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001832 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001833 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001834 }
1835
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001836 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837 {
Jamie Madill37997142015-01-28 10:06:34 -05001838 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839 {
Jamie Madill37997142015-01-28 10:06:34 -05001840 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001841 {
Jamie Madill37997142015-01-28 10:06:34 -05001842 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001843 }
1844
Nicolas Capensd974db42014-10-07 10:50:19 -04001845 if (!mInsideFunction)
1846 {
1847 out << "static ";
1848 }
1849
1850 out << TypeString(variable->getType()) + " ";
1851
Jamie Madill37997142015-01-28 10:06:34 -05001852 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001854 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001856 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001857 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001858 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001859 }
1860 else
1861 {
Jamie Madill37997142015-01-28 10:06:34 -05001862 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001863 }
1864
Jamie Madill37997142015-01-28 10:06:34 -05001865 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001866 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001867 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001868 }
1869 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001871 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1872 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001873 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001874 }
1875 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001876 }
Jamie Madill033dae62014-06-18 12:56:28 -04001877 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001878 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001879 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001880 {
1881 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1882
1883 if (symbol)
1884 {
1885 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1886 mReferencedVaryings[symbol->getSymbol()] = symbol;
1887 }
1888 else
1889 {
1890 (*sit)->traverse(this);
1891 }
1892 }
1893 }
1894
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895 return false;
1896 }
1897 else if (visit == InVisit)
1898 {
1899 out << ", ";
1900 }
1901 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001902 case EOpInvariantDeclaration:
1903 // Do not do any translation
1904 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001905 case EOpPrototype:
1906 if (visit == PreVisit)
1907 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001908 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001909
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001910 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001911
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001912 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001913 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001914 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001915
1916 if (symbol)
1917 {
1918 out << argumentString(symbol);
1919
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001920 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001921 {
1922 out << ", ";
1923 }
1924 }
1925 else UNREACHABLE();
1926 }
1927
1928 out << ");\n";
1929
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001930 // Also prototype the Lod0 variant if needed
1931 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1932 {
1933 mOutputLod0Function = true;
1934 node->traverse(this);
1935 mOutputLod0Function = false;
1936 }
1937
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001938 return false;
1939 }
1940 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001941 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942 case EOpFunction:
1943 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001944 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945
Jamie Madill033dae62014-06-18 12:56:28 -04001946 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001947
1948 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001950 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001952 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953 {
Jamie Madill033dae62014-06-18 12:56:28 -04001954 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001955 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001956
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001957 TIntermSequence *sequence = node->getSequence();
1958 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001959
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001960 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001961 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001962 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001963
1964 if (symbol)
1965 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001966 TStructure *structure = symbol->getType().getStruct();
1967
1968 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001969 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001970 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001971 }
1972
1973 out << argumentString(symbol);
1974
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001975 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001976 {
1977 out << ", ";
1978 }
1979 }
1980 else UNREACHABLE();
1981 }
1982
1983 out << ")\n"
1984 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001985
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001986 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001987 {
1988 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001989 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001990 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001992
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001993 out << "}\n";
1994
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001995 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1996 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001997 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001998 {
1999 mOutputLod0Function = true;
2000 node->traverse(this);
2001 mOutputLod0Function = false;
2002 }
2003 }
2004
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002005 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006 }
2007 break;
2008 case EOpFunctionCall:
2009 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002010 TString name = TFunction::unmangleName(node->getName());
2011 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002012 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002013
2014 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015 {
Jamie Madill033dae62014-06-18 12:56:28 -04002016 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002017 }
2018 else
2019 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002020 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002021
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002022 TextureFunction textureFunction;
2023 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002024 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002025 textureFunction.method = TextureFunction::IMPLICIT;
2026 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002027 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002028
2029 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002030 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002031 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002032 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002033 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002034 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002035 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002036 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002037 }
Nicolas Capens46485082014-04-15 13:12:50 -04002038 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2039 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002040 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002041 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002042 }
Nicolas Capens46485082014-04-15 13:12:50 -04002043 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002044 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002045 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002046 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002047 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002048 else if (name == "textureSize")
2049 {
2050 textureFunction.method = TextureFunction::SIZE;
2051 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002052 else if (name == "textureOffset")
2053 {
2054 textureFunction.method = TextureFunction::IMPLICIT;
2055 textureFunction.offset = true;
2056 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002057 else if (name == "textureProjOffset")
2058 {
2059 textureFunction.method = TextureFunction::IMPLICIT;
2060 textureFunction.offset = true;
2061 textureFunction.proj = true;
2062 }
2063 else if (name == "textureLodOffset")
2064 {
2065 textureFunction.method = TextureFunction::LOD;
2066 textureFunction.offset = true;
2067 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002068 else if (name == "textureProjLodOffset")
2069 {
2070 textureFunction.method = TextureFunction::LOD;
2071 textureFunction.proj = true;
2072 textureFunction.offset = true;
2073 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002074 else if (name == "texelFetch")
2075 {
2076 textureFunction.method = TextureFunction::FETCH;
2077 }
2078 else if (name == "texelFetchOffset")
2079 {
2080 textureFunction.method = TextureFunction::FETCH;
2081 textureFunction.offset = true;
2082 }
Nicolas Capens46485082014-04-15 13:12:50 -04002083 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002084 {
2085 textureFunction.method = TextureFunction::GRAD;
2086 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002087 else if (name == "textureGradOffset")
2088 {
2089 textureFunction.method = TextureFunction::GRAD;
2090 textureFunction.offset = true;
2091 }
Nicolas Capens46485082014-04-15 13:12:50 -04002092 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002093 {
2094 textureFunction.method = TextureFunction::GRAD;
2095 textureFunction.proj = true;
2096 }
2097 else if (name == "textureProjGradOffset")
2098 {
2099 textureFunction.method = TextureFunction::GRAD;
2100 textureFunction.proj = true;
2101 textureFunction.offset = true;
2102 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002103 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002104
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002105 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002106 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002107 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2108
2109 if (textureFunction.offset)
2110 {
2111 mandatoryArgumentCount++;
2112 }
2113
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002114 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002115
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002116 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002117 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002118 if (bias)
2119 {
2120 textureFunction.method = TextureFunction::LOD0BIAS;
2121 }
2122 else
2123 {
2124 textureFunction.method = TextureFunction::LOD0;
2125 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002126 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002127 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002128 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002129 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002130 }
2131 }
2132
2133 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002134
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002135 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002137
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002138 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002139 {
2140 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2141 {
2142 out << "texture_";
2143 (*arg)->traverse(this);
2144 out << ", sampler_";
2145 }
2146
2147 (*arg)->traverse(this);
2148
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002149 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002150 {
2151 out << ", ";
2152 }
2153 }
2154
2155 out << ")";
2156
2157 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 }
2159 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002160 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002161 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2162 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2163 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2164 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2165 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2166 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2167 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2168 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2169 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2170 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2171 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2172 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2173 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2174 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2175 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2176 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2177 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2178 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2179 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002180 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002181 {
Jamie Madill033dae62014-06-18 12:56:28 -04002182 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002183 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002184 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002185 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002186 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002187 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2188 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2189 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2190 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2191 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2192 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002193 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002194 ASSERT(node->getUseEmulatedFunction());
2195 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002196 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002197 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002198 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002200 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002201 ASSERT(node->getUseEmulatedFunction());
2202 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002203 break;
2204 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2205 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2206 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2207 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2208 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2209 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2210 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2211 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2212 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002213 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002214 ASSERT(node->getUseEmulatedFunction());
2215 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002216 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002217 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2218 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002219 case EOpOuterProduct:
2220 ASSERT(node->getUseEmulatedFunction());
2221 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2222 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002223 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002224 default: UNREACHABLE();
2225 }
2226
2227 return true;
2228}
2229
2230bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2231{
Jamie Madill32aab012015-01-27 14:12:26 -05002232 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002234 if (node->usesTernaryOperator())
2235 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002236 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002237 }
2238 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002239 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002240 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002241
Corentin Wallez80bacde2014-11-10 12:07:37 -08002242 // D3D errors when there is a gradient operation in a loop in an unflattened if
2243 // however flattening all the ifs in branch heavy shaders made D3D error too.
2244 // As a temporary workaround we flatten the ifs only if there is at least a loop
2245 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002246 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002247 {
2248 out << "FLATTEN ";
2249 }
2250
2251 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002252
2253 node->getCondition()->traverse(this);
2254
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002255 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002256
Jamie Madill075edd82013-07-08 13:30:19 -04002257 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002258 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002260 bool discard = false;
2261
daniel@transgaming.combb885322010-04-15 20:45:24 +00002262 if (node->getTrueBlock())
2263 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002264 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002265
2266 // Detect true discard
2267 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002268 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269
Jamie Madill075edd82013-07-08 13:30:19 -04002270 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002271 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002272
2273 if (node->getFalseBlock())
2274 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002275 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002276
Jamie Madill075edd82013-07-08 13:30:19 -04002277 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002278 out << "{\n";
2279
Jamie Madill075edd82013-07-08 13:30:19 -04002280 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002281 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002282
Jamie Madill075edd82013-07-08 13:30:19 -04002283 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002284 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002285
2286 // Detect false discard
2287 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2288 }
2289
2290 // ANGLE issue 486: Detect problematic conditional discard
2291 if (discard && FindSideEffectRewriting::search(node))
2292 {
2293 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002294 }
2295 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296
2297 return false;
2298}
2299
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002300bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002301{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002302 if (node->getStatementList())
2303 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002304 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002305 outputTriplet(visit, "switch (", ") ", "");
2306 // The curly braces get written when visiting the statementList aggregate
2307 }
2308 else
2309 {
2310 // No statementList, so it won't output curly braces
2311 outputTriplet(visit, "switch (", ") {", "}\n");
2312 }
2313 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002314}
2315
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002316bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002317{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002318 if (node->hasCondition())
2319 {
2320 outputTriplet(visit, "case (", "", "):\n");
2321 return true;
2322 }
2323 else
2324 {
2325 TInfoSinkBase &out = getInfoSink();
2326 out << "default:\n";
2327 return false;
2328 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002329}
2330
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2332{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002333 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002334}
2335
2336bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2337{
Nicolas Capens655fe362014-04-11 13:12:34 -04002338 mNestedLoopDepth++;
2339
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002340 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2341
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002342 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002343 {
2344 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2345 }
2346
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002347 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002348 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002349 if (handleExcessiveLoop(node))
2350 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002351 mInsideDiscontinuousLoop = wasDiscontinuous;
2352 mNestedLoopDepth--;
2353
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002354 return false;
2355 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002356 }
2357
Jamie Madill32aab012015-01-27 14:12:26 -05002358 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359
alokp@chromium.org52813552010-11-16 18:36:09 +00002360 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002362 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002363
Jamie Madill075edd82013-07-08 13:30:19 -04002364 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002365 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366 }
2367 else
2368 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002369 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002370
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002371 if (node->getInit())
2372 {
2373 node->getInit()->traverse(this);
2374 }
2375
2376 out << "; ";
2377
alokp@chromium.org52813552010-11-16 18:36:09 +00002378 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002380 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 }
2382
2383 out << "; ";
2384
alokp@chromium.org52813552010-11-16 18:36:09 +00002385 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002387 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388 }
2389
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002390 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002391
Jamie Madill075edd82013-07-08 13:30:19 -04002392 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002393 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394 }
2395
2396 if (node->getBody())
2397 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002398 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399 }
2400
Jamie Madill075edd82013-07-08 13:30:19 -04002401 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002402 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403
alokp@chromium.org52813552010-11-16 18:36:09 +00002404 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 {
Jamie Madill075edd82013-07-08 13:30:19 -04002406 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407 out << "while(\n";
2408
alokp@chromium.org52813552010-11-16 18:36:09 +00002409 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410
daniel@transgaming.com73536982012-03-21 20:45:49 +00002411 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002412 }
2413
daniel@transgaming.com73536982012-03-21 20:45:49 +00002414 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002416 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002417 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002418
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419 return false;
2420}
2421
2422bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2423{
Jamie Madill32aab012015-01-27 14:12:26 -05002424 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425
2426 switch (node->getFlowOp())
2427 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002428 case EOpKill:
2429 outputTriplet(visit, "discard;\n", "", "");
2430 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002431 case EOpBreak:
2432 if (visit == PreVisit)
2433 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002434 if (mNestedLoopDepth > 1)
2435 {
2436 mUsesNestedBreak = true;
2437 }
2438
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002439 if (mExcessiveLoopIndex)
2440 {
2441 out << "{Break";
2442 mExcessiveLoopIndex->traverse(this);
2443 out << " = true; break;}\n";
2444 }
2445 else
2446 {
2447 out << "break;\n";
2448 }
2449 }
2450 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002451 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 case EOpReturn:
2453 if (visit == PreVisit)
2454 {
2455 if (node->getExpression())
2456 {
2457 out << "return ";
2458 }
2459 else
2460 {
2461 out << "return;\n";
2462 }
2463 }
2464 else if (visit == PostVisit)
2465 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002466 if (node->getExpression())
2467 {
2468 out << ";\n";
2469 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002470 }
2471 break;
2472 default: UNREACHABLE();
2473 }
2474
2475 return true;
2476}
2477
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002478void OutputHLSL::traverseStatements(TIntermNode *node)
2479{
2480 if (isSingleStatement(node))
2481 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002482 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002483 }
2484
2485 node->traverse(this);
2486}
2487
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002488bool OutputHLSL::isSingleStatement(TIntermNode *node)
2489{
2490 TIntermAggregate *aggregate = node->getAsAggregate();
2491
2492 if (aggregate)
2493 {
2494 if (aggregate->getOp() == EOpSequence)
2495 {
2496 return false;
2497 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002498 else if (aggregate->getOp() == EOpDeclaration)
2499 {
2500 // Declaring multiple comma-separated variables must be considered multiple statements
2501 // because each individual declaration has side effects which are visible in the next.
2502 return false;
2503 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002504 else
2505 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002506 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002507 {
2508 if (!isSingleStatement(*sit))
2509 {
2510 return false;
2511 }
2512 }
2513
2514 return true;
2515 }
2516 }
2517
2518 return true;
2519}
2520
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002521// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2522// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002523bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2524{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002525 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002526 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002527
2528 // Parse loops of the form:
2529 // for(int index = initial; index [comparator] limit; index += increment)
2530 TIntermSymbol *index = NULL;
2531 TOperator comparator = EOpNull;
2532 int initial = 0;
2533 int limit = 0;
2534 int increment = 0;
2535
2536 // Parse index name and intial value
2537 if (node->getInit())
2538 {
2539 TIntermAggregate *init = node->getInit()->getAsAggregate();
2540
2541 if (init)
2542 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002543 TIntermSequence *sequence = init->getSequence();
2544 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002545
2546 if (variable && variable->getQualifier() == EvqTemporary)
2547 {
2548 TIntermBinary *assign = variable->getAsBinaryNode();
2549
2550 if (assign->getOp() == EOpInitialize)
2551 {
2552 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2553 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2554
2555 if (symbol && constant)
2556 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002557 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002558 {
2559 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002560 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002561 }
2562 }
2563 }
2564 }
2565 }
2566 }
2567
2568 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002569 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002570 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002571 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002572
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002573 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2574 {
2575 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2576
2577 if (constant)
2578 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002579 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002580 {
2581 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002582 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002583 }
2584 }
2585 }
2586 }
2587
2588 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002589 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002590 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002591 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2592 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002593
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002594 if (binaryTerminal)
2595 {
2596 TOperator op = binaryTerminal->getOp();
2597 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2598
2599 if (constant)
2600 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002601 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002602 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002603 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002604
2605 switch (op)
2606 {
2607 case EOpAddAssign: increment = value; break;
2608 case EOpSubAssign: increment = -value; break;
2609 default: UNIMPLEMENTED();
2610 }
2611 }
2612 }
2613 }
2614 else if (unaryTerminal)
2615 {
2616 TOperator op = unaryTerminal->getOp();
2617
2618 switch (op)
2619 {
2620 case EOpPostIncrement: increment = 1; break;
2621 case EOpPostDecrement: increment = -1; break;
2622 case EOpPreIncrement: increment = 1; break;
2623 case EOpPreDecrement: increment = -1; break;
2624 default: UNIMPLEMENTED();
2625 }
2626 }
2627 }
2628
2629 if (index != NULL && comparator != EOpNull && increment != 0)
2630 {
2631 if (comparator == EOpLessThanEqual)
2632 {
2633 comparator = EOpLessThan;
2634 limit += 1;
2635 }
2636
2637 if (comparator == EOpLessThan)
2638 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002639 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002640
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002641 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002642 {
2643 return false; // Not an excessive loop
2644 }
2645
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002646 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2647 mExcessiveLoopIndex = index;
2648
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002649 out << "{int ";
2650 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002651 out << ";\n"
2652 "bool Break";
2653 index->traverse(this);
2654 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002655
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002656 bool firstLoopFragment = true;
2657
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002658 while (iterations > 0)
2659 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002660 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002661
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002662 if (!firstLoopFragment)
2663 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002664 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002665 index->traverse(this);
2666 out << ") {\n";
2667 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002668
2669 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2670 {
2671 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2672 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002673
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002674 // for(int index = initial; index < clampedLimit; index += increment)
2675
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002676 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002677 index->traverse(this);
2678 out << " = ";
2679 out << initial;
2680
2681 out << "; ";
2682 index->traverse(this);
2683 out << " < ";
2684 out << clampedLimit;
2685
2686 out << "; ";
2687 index->traverse(this);
2688 out << " += ";
2689 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002690 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002691
Jamie Madill075edd82013-07-08 13:30:19 -04002692 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002693 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002694
2695 if (node->getBody())
2696 {
2697 node->getBody()->traverse(this);
2698 }
2699
Jamie Madill075edd82013-07-08 13:30:19 -04002700 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002701 out << ";}\n";
2702
2703 if (!firstLoopFragment)
2704 {
2705 out << "}\n";
2706 }
2707
2708 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002709
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002710 initial += MAX_LOOP_ITERATIONS * increment;
2711 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002712 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002713
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002714 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002715
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002716 mExcessiveLoopIndex = restoreIndex;
2717
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002718 return true;
2719 }
2720 else UNIMPLEMENTED();
2721 }
2722
2723 return false; // Not handled as an excessive loop
2724}
2725
Daniel Bratell29190082015-02-20 16:42:54 +01002726void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002727{
Jamie Madill32aab012015-01-27 14:12:26 -05002728 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002729
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002730 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731 {
2732 out << preString;
2733 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002734 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735 {
2736 out << inString;
2737 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002738 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002739 {
2740 out << postString;
2741 }
2742}
2743
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002744void OutputHLSL::outputLineDirective(int line)
2745{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002746 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002747 {
Jamie Madill32aab012015-01-27 14:12:26 -05002748 TInfoSinkBase &out = getInfoSink();
2749
2750 out << "\n";
2751 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002752
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002753 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002754 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002755 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002756 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002757
Jamie Madill32aab012015-01-27 14:12:26 -05002758 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002759 }
2760}
2761
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002762TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2763{
2764 TQualifier qualifier = symbol->getQualifier();
2765 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002766 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002767
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002768 if (name.empty()) // HLSL demands named arguments, also for prototypes
2769 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002770 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002771 }
2772 else
2773 {
Jamie Madill033dae62014-06-18 12:56:28 -04002774 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002775 }
2776
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002777 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2778 {
Jamie Madill033dae62014-06-18 12:56:28 -04002779 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002780 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002781 }
2782
Jamie Madill033dae62014-06-18 12:56:28 -04002783 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784}
2785
2786TString OutputHLSL::initializer(const TType &type)
2787{
2788 TString string;
2789
Jamie Madill94bf7f22013-07-08 13:31:15 -04002790 size_t size = type.getObjectSize();
2791 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002792 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002793 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002794
Jamie Madill94bf7f22013-07-08 13:31:15 -04002795 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796 {
2797 string += ", ";
2798 }
2799 }
2800
daniel@transgaming.comead23042010-04-29 03:35:36 +00002801 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002802}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002803
Daniel Bratell29190082015-02-20 16:42:54 +01002804void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002805{
Jamie Madill32aab012015-01-27 14:12:26 -05002806 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002807
2808 if (visit == PreVisit)
2809 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002810 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002811
Daniel Bratell29190082015-02-20 16:42:54 +01002812 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002813 }
2814 else if (visit == InVisit)
2815 {
2816 out << ", ";
2817 }
2818 else if (visit == PostVisit)
2819 {
2820 out << ")";
2821 }
2822}
2823
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002824const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2825{
Jamie Madill32aab012015-01-27 14:12:26 -05002826 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002827
Jamie Madill98493dd2013-07-08 14:39:03 -04002828 const TStructure* structure = type.getStruct();
2829 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002830 {
Jamie Madill033dae62014-06-18 12:56:28 -04002831 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002832
Jamie Madill98493dd2013-07-08 14:39:03 -04002833 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002834
Jamie Madill98493dd2013-07-08 14:39:03 -04002835 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002836 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002837 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002838 constUnion = writeConstantUnion(*fieldType, constUnion);
2839
Jamie Madill98493dd2013-07-08 14:39:03 -04002840 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002841 {
2842 out << ", ";
2843 }
2844 }
2845
2846 out << ")";
2847 }
2848 else
2849 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002850 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002851 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002852
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002853 if (writeType)
2854 {
Jamie Madill033dae62014-06-18 12:56:28 -04002855 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002856 }
2857
Jamie Madill94bf7f22013-07-08 13:31:15 -04002858 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002859 {
2860 switch (constUnion->getType())
2861 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002862 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002863 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002864 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002865 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002866 default: UNREACHABLE();
2867 }
2868
2869 if (i != size - 1)
2870 {
2871 out << ", ";
2872 }
2873 }
2874
2875 if (writeType)
2876 {
2877 out << ")";
2878 }
2879 }
2880
2881 return constUnion;
2882}
2883
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002884void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2885{
2886 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2887 outputTriplet(visit, preString.c_str(), ", ", ")");
2888}
2889
Jamie Madill37997142015-01-28 10:06:34 -05002890bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2891{
2892 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2893 expression->traverse(&searchSymbol);
2894
2895 if (searchSymbol.foundMatch())
2896 {
2897 // Type already printed
2898 out << "t" + str(mUniqueIndex) + " = ";
2899 expression->traverse(this);
2900 out << ", ";
2901 symbolNode->traverse(this);
2902 out << " = t" + str(mUniqueIndex);
2903
2904 mUniqueIndex++;
2905 return true;
2906 }
2907
2908 return false;
2909}
2910
2911void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2912{
2913 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2914 << "\n"
2915 << "void initializeDeferredGlobals()\n"
2916 << "{\n";
2917
2918 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2919 {
2920 TIntermSymbol *symbol = deferredGlobal.first;
2921 TIntermTyped *expression = deferredGlobal.second;
2922 ASSERT(symbol);
2923 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2924
2925 out << " " << Decorate(symbol->getSymbol()) << " = ";
2926
2927 if (!writeSameSymbolInitializer(out, symbol, expression))
2928 {
2929 ASSERT(mInfoSinkStack.top() == &out);
2930 expression->traverse(this);
2931 }
2932
2933 out << ";\n";
2934 }
2935
2936 out << "}\n"
2937 << "\n";
2938}
2939
Jamie Madill55e79e02015-02-09 15:35:00 -05002940TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2941{
2942 const TFieldList &fields = structure.fields();
2943
2944 for (const auto &eqFunction : mStructEqualityFunctions)
2945 {
2946 if (eqFunction.structure == &structure)
2947 {
2948 return eqFunction.functionName;
2949 }
2950 }
2951
2952 const TString &structNameString = StructNameString(structure);
2953
2954 StructEqualityFunction function;
2955 function.structure = &structure;
2956 function.functionName = "angle_eq_" + structNameString;
2957
2958 TString &func = function.functionDefinition;
2959
2960 func = "bool " + function.functionName + "(" + structNameString + " a, " + structNameString + " b)\n" +
2961 "{\n"
2962 " return ";
2963
2964 for (size_t i = 0; i < fields.size(); i++)
2965 {
2966 const TField *field = fields[i];
2967 const TType *fieldType = field->type();
2968
2969 const TString &fieldNameA = "a." + Decorate(field->name());
2970 const TString &fieldNameB = "b." + Decorate(field->name());
2971
2972 if (i > 0)
2973 {
2974 func += " && ";
2975 }
2976
2977 if (fieldType->getBasicType() == EbtStruct)
2978 {
2979 const TStructure &fieldStruct = *fieldType->getStruct();
2980 const TString &functionName = addStructEqualityFunction(fieldStruct);
2981 func += functionName + "(" + fieldNameA + ", " + fieldNameB + ")";
2982 }
2983 else if (fieldType->isScalar())
2984 {
2985 func += "(" + fieldNameA + " == " + fieldNameB + ")";
2986 }
2987 else
2988 {
2989 ASSERT(fieldType->isMatrix() || fieldType->isVector());
2990 func += "all(" + fieldNameA + " == " + fieldNameB + ")";
2991 }
2992 }
2993
2994 func = func + ";\n" + "}\n";
2995
2996 mStructEqualityFunctions.push_back(function);
2997
2998 return function.functionName;
2999}
3000
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003001}