blob: 5bf7d7b54f6d67ea8a68a63a10425dc16a78025b [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;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000129
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000130 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000131
132 mContainsLoopDiscontinuity = false;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800133 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000134 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000135 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400136 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000137
138 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000139
Jamie Madill8daaba12014-06-13 10:04:33 -0400140 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200141 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400142
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000143 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000144 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200145 if (mShaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000146 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400147 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
148 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000149 }
150 else
151 {
Cooper Partine6664f02015-01-09 16:22:24 -0800152 // Reserve registers for dx_DepthRange, dx_ViewAdjust and dx_ViewCoords
153 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000154 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000155 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000156
Jamie Madillf91ce812014-06-13 10:04:34 -0400157 // Reserve registers for the default uniform block and driver constants
158 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159}
160
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000161OutputHLSL::~OutputHLSL()
162{
Jamie Madill8daaba12014-06-13 10:04:33 -0400163 SafeDelete(mUnfoldShortCircuit);
164 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400165 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000166}
167
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200168void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000169{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200170 mContainsLoopDiscontinuity = mShaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(treeRoot);
171 mContainsAnyLoop = containsAnyLoop(treeRoot);
172 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400173 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000174
Jamie Madille53c98b2014-02-03 11:57:13 -0500175 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
176 // use a vertex attribute as a condition, and some related computation in the else block.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200177 if (mOutputType == SH_HLSL9_OUTPUT && mShaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500178 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200179 RewriteElseBlocks(treeRoot);
Jamie Madille53c98b2014-02-03 11:57:13 -0500180 }
181
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200182 BuiltInFunctionEmulator builtInFunctionEmulator;
183 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200184 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500185
Jamie Madill37997142015-01-28 10:06:34 -0500186 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500187 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200188 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500189 mInfoSinkStack.pop();
190
Jamie Madill37997142015-01-28 10:06:34 -0500191 mInfoSinkStack.push(&mFooter);
192 if (!mDeferredGlobalInitializers.empty())
193 {
194 writeDeferredGlobalInitializers(mFooter);
195 }
196 mInfoSinkStack.pop();
197
Jamie Madill32aab012015-01-27 14:12:26 -0500198 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200199 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500200 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000201
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200202 objSink << mHeader.c_str();
203 objSink << mBody.c_str();
204 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200205
206 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000207}
208
Jamie Madill570e04d2013-06-21 09:15:33 -0400209void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
210{
211 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
212 {
213 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
214
Jamie Madill32aab012015-01-27 14:12:26 -0500215 TInfoSinkBase structInfoSink;
216 mInfoSinkStack.push(&structInfoSink);
217
Jamie Madill570e04d2013-06-21 09:15:33 -0400218 // This will mark the necessary block elements as referenced
219 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500220
221 TString structName(structInfoSink.c_str());
222 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400223
224 mFlaggedStructOriginalNames[flaggedNode] = structName;
225
226 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
227 {
228 structName.erase(pos, 1);
229 }
230
231 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
232 }
233}
234
Jamie Madill4e1fd412014-07-10 17:50:10 -0400235const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
236{
237 return mUniformHLSL->getInterfaceBlockRegisterMap();
238}
239
Jamie Madill9fe25e92014-07-18 10:33:08 -0400240const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
241{
242 return mUniformHLSL->getUniformRegisterMap();
243}
244
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000245int OutputHLSL::vectorSize(const TType &type) const
246{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000247 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000248 int arraySize = type.isArray() ? type.getArraySize() : 1;
249
250 return elementSize * arraySize;
251}
252
Jamie Madill98493dd2013-07-08 14:39:03 -0400253TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400254{
255 TString init;
256
257 TString preIndentString;
258 TString fullIndentString;
259
260 for (int spaces = 0; spaces < (indent * 4); spaces++)
261 {
262 preIndentString += ' ';
263 }
264
265 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
266 {
267 fullIndentString += ' ';
268 }
269
270 init += preIndentString + "{\n";
271
Jamie Madill98493dd2013-07-08 14:39:03 -0400272 const TFieldList &fields = structure.fields();
273 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400274 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400276 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400278
Jamie Madill98493dd2013-07-08 14:39:03 -0400279 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400280 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400281 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400282 }
283 else
284 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400285 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 }
287 }
288
289 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
290
291 return init;
292}
293
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200294void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295{
Jamie Madill32aab012015-01-27 14:12:26 -0500296 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000298 TString varyings;
299 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400300 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000301
Jamie Madill829f59e2013-11-13 19:40:54 -0500302 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400303 {
304 TIntermTyped *structNode = flaggedStructIt->first;
305 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400306 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400307 const TString &originalName = mFlaggedStructOriginalNames[structNode];
308
Jamie Madill033dae62014-06-18 12:56:28 -0400309 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400310 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400311 flaggedStructs += "\n";
312 }
313
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000314 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
315 {
316 const TType &type = varying->second->getType();
317 const TString &name = varying->second->getSymbol();
318
319 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400320 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
321 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000322 }
323
324 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
325 {
326 const TType &type = attribute->second->getType();
327 const TString &name = attribute->second->getSymbol();
328
Jamie Madill033dae62014-06-18 12:56:28 -0400329 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000330 }
331
Jamie Madill8daaba12014-06-13 10:04:33 -0400332 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400333
Jamie Madillf91ce812014-06-13 10:04:34 -0400334 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
335 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
336
Jamie Madill55e79e02015-02-09 15:35:00 -0500337 if (!mStructEqualityFunctions.empty())
338 {
339 out << "\n// Structure equality functions\n\n";
340 for (const auto &eqFunction : mStructEqualityFunctions)
341 {
342 out << eqFunction.functionDefinition << "\n";
343 }
344 }
345
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500346 if (mUsesDiscardRewriting)
347 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400348 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500349 }
350
Nicolas Capens655fe362014-04-11 13:12:34 -0400351 if (mUsesNestedBreak)
352 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400353 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400354 }
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 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001396 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001397 case EOpInitialize:
1398 if (visit == PreVisit)
1399 {
1400 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1401 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1402 // new variable is created before the assignment is evaluated), so we need to convert
1403 // this to "float t = x, x = t;".
1404
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001405 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001406 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001407 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001408
Jamie Madill37997142015-01-28 10:06:34 -05001409 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1410 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001411 {
Jamie Madill37997142015-01-28 10:06:34 -05001412 // For variables which are not constant, defer their real initialization until
1413 // after we initialize other globals: uniforms, attributes and varyings.
1414 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1415 const TString &initString = initializer(node->getType());
1416 node->setRight(new TIntermRaw(node->getType(), initString));
1417 }
1418 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1419 {
1420 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001421 return false;
1422 }
1423 }
1424 else if (visit == InVisit)
1425 {
1426 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001427 }
1428 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001429 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1430 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1431 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1432 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1433 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1434 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001435 if (visit == PreVisit)
1436 {
1437 out << "(";
1438 }
1439 else if (visit == InVisit)
1440 {
1441 out << " = mul(";
1442 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001443 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001444 }
1445 else
1446 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001447 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001448 }
1449 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001450 case EOpMatrixTimesMatrixAssign:
1451 if (visit == PreVisit)
1452 {
1453 out << "(";
1454 }
1455 else if (visit == InVisit)
1456 {
1457 out << " = mul(";
1458 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001459 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001460 }
1461 else
1462 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001463 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001464 }
1465 break;
1466 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001467 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001468 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1469 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1470 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1471 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1472 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001473 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001474 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001475 const TType& leftType = node->getLeft()->getType();
1476 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001477 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001478 if (visit == PreVisit)
1479 {
1480 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1481 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001482 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001483 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001484 return false;
1485 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001486 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001487 else
1488 {
1489 outputTriplet(visit, "", "[", "]");
1490 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001491 }
1492 break;
1493 case EOpIndexIndirect:
1494 // We do not currently support indirect references to interface blocks
1495 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1496 outputTriplet(visit, "", "[", "]");
1497 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001498 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001499 if (visit == InVisit)
1500 {
1501 const TStructure* structure = node->getLeft()->getType().getStruct();
1502 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1503 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001504 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001505
1506 return false;
1507 }
1508 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001509 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001510 if (visit == InVisit)
1511 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001512 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1513 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1514 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001515 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001516
1517 return false;
1518 }
1519 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001520 case EOpVectorSwizzle:
1521 if (visit == InVisit)
1522 {
1523 out << ".";
1524
1525 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1526
1527 if (swizzle)
1528 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001529 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001531 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001532 {
1533 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1534
1535 if (element)
1536 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001537 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001538
1539 switch (i)
1540 {
1541 case 0: out << "x"; break;
1542 case 1: out << "y"; break;
1543 case 2: out << "z"; break;
1544 case 3: out << "w"; break;
1545 default: UNREACHABLE();
1546 }
1547 }
1548 else UNREACHABLE();
1549 }
1550 }
1551 else UNREACHABLE();
1552
1553 return false; // Fully processed
1554 }
1555 break;
1556 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1557 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1558 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1559 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001560 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001561 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1562 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1563 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1564 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1565 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001566 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001567 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001568 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001569 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001570 if (node->getOp() == EOpEqual)
1571 {
1572 outputTriplet(visit, "(", " == ", ")");
1573 }
1574 else
1575 {
1576 outputTriplet(visit, "(", " != ", ")");
1577 }
1578 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001579 else
1580 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001581 if (visit == PreVisit && node->getOp() == EOpNotEqual)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001582 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001583 out << "!";
1584 }
1585
1586 if (node->getLeft()->getBasicType() == EbtStruct)
1587 {
1588 const TStructure &structure = *node->getLeft()->getType().getStruct();
1589 const TString &functionName = addStructEqualityFunction(structure);
Daniel Bratell29190082015-02-20 16:42:54 +01001590 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001591 }
1592 else
1593 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001594 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
1595 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001596 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001597 }
1598 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001599 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1600 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1601 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1602 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1603 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001604 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001605 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1606 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001607 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001608 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001609 if (node->getRight()->hasSideEffects())
1610 {
1611 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1612 return false;
1613 }
1614 else
1615 {
1616 outputTriplet(visit, "(", " || ", ")");
1617 return true;
1618 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001619 case EOpLogicalXor:
1620 mUsesXor = true;
1621 outputTriplet(visit, "xor(", ", ", ")");
1622 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001623 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001624 if (node->getRight()->hasSideEffects())
1625 {
1626 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1627 return false;
1628 }
1629 else
1630 {
1631 outputTriplet(visit, "(", " && ", ")");
1632 return true;
1633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634 default: UNREACHABLE();
1635 }
1636
1637 return true;
1638}
1639
1640bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1641{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001642 switch (node->getOp())
1643 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001644 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001645 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001646 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1647 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001648 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001649 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1650 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1651 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1652 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001653 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1654 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1655 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1656 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1657 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1658 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1659 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1660 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001661 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1662 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1663 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1664 case EOpAsinh:
1665 ASSERT(node->getUseEmulatedFunction());
1666 writeEmulatedFunctionTriplet(visit, "asinh(");
1667 break;
1668 case EOpAcosh:
1669 ASSERT(node->getUseEmulatedFunction());
1670 writeEmulatedFunctionTriplet(visit, "acosh(");
1671 break;
1672 case EOpAtanh:
1673 ASSERT(node->getUseEmulatedFunction());
1674 writeEmulatedFunctionTriplet(visit, "atanh(");
1675 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001676 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1677 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1678 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1679 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1680 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1681 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1682 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1683 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1684 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001685 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1686 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1687 case EOpRoundEven:
1688 ASSERT(node->getUseEmulatedFunction());
1689 writeEmulatedFunctionTriplet(visit, "roundEven(");
1690 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001691 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1692 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301693 case EOpIsNan: outputTriplet(visit, "isnan(", "", ")"); break;
1694 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001695 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1696 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1697 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1698 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001699 case EOpPackSnorm2x16:
1700 ASSERT(node->getUseEmulatedFunction());
1701 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1702 break;
1703 case EOpPackUnorm2x16:
1704 ASSERT(node->getUseEmulatedFunction());
1705 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1706 break;
1707 case EOpPackHalf2x16:
1708 ASSERT(node->getUseEmulatedFunction());
1709 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1710 break;
1711 case EOpUnpackSnorm2x16:
1712 ASSERT(node->getUseEmulatedFunction());
1713 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1714 break;
1715 case EOpUnpackUnorm2x16:
1716 ASSERT(node->getUseEmulatedFunction());
1717 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1718 break;
1719 case EOpUnpackHalf2x16:
1720 ASSERT(node->getUseEmulatedFunction());
1721 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1722 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001723 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1724 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001725 case EOpDFdx:
1726 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1727 {
1728 outputTriplet(visit, "(", "", ", 0.0)");
1729 }
1730 else
1731 {
1732 outputTriplet(visit, "ddx(", "", ")");
1733 }
1734 break;
1735 case EOpDFdy:
1736 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1737 {
1738 outputTriplet(visit, "(", "", ", 0.0)");
1739 }
1740 else
1741 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001742 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001743 }
1744 break;
1745 case EOpFwidth:
1746 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1747 {
1748 outputTriplet(visit, "(", "", ", 0.0)");
1749 }
1750 else
1751 {
1752 outputTriplet(visit, "fwidth(", "", ")");
1753 }
1754 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001755 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1756 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001757 case EOpInverse:
1758 ASSERT(node->getUseEmulatedFunction());
1759 writeEmulatedFunctionTriplet(visit, "inverse(");
1760 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001761
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001762 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1763 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 default: UNREACHABLE();
1765 }
1766
1767 return true;
1768}
1769
1770bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1771{
Jamie Madill32aab012015-01-27 14:12:26 -05001772 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774 switch (node->getOp())
1775 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001776 case EOpSequence:
1777 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001778 if (mInsideFunction)
1779 {
Jamie Madill075edd82013-07-08 13:30:19 -04001780 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001781 out << "{\n";
1782 }
1783
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001784 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001785 {
Jamie Madill075edd82013-07-08 13:30:19 -04001786 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001787
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001788 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001789
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001790 // Don't output ; after case labels, they're terminated by :
1791 // This is needed especially since outputting a ; after a case statement would turn empty
1792 // case statements into non-empty case statements, disallowing fall-through from them.
1793 if ((*sit)->getAsCaseNode() == nullptr)
1794 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001795 }
1796
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001797 if (mInsideFunction)
1798 {
Jamie Madill075edd82013-07-08 13:30:19 -04001799 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001800 out << "}\n";
1801 }
1802
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001803 return false;
1804 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805 case EOpDeclaration:
1806 if (visit == PreVisit)
1807 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001808 TIntermSequence *sequence = node->getSequence();
1809 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001811 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001813 TStructure *structure = variable->getType().getStruct();
1814
1815 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001816 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001817 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001818 }
1819
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001820 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001821 {
Jamie Madill37997142015-01-28 10:06:34 -05001822 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001823 {
Jamie Madill37997142015-01-28 10:06:34 -05001824 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001825 {
Jamie Madill37997142015-01-28 10:06:34 -05001826 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001827 }
1828
Nicolas Capensd974db42014-10-07 10:50:19 -04001829 if (!mInsideFunction)
1830 {
1831 out << "static ";
1832 }
1833
1834 out << TypeString(variable->getType()) + " ";
1835
Jamie Madill37997142015-01-28 10:06:34 -05001836 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001838 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001840 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001841 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001842 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001843 }
1844 else
1845 {
Jamie Madill37997142015-01-28 10:06:34 -05001846 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001847 }
1848
Jamie Madill37997142015-01-28 10:06:34 -05001849 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001850 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001851 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001852 }
1853 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001854 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001855 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1856 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001857 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001858 }
1859 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860 }
Jamie Madill033dae62014-06-18 12:56:28 -04001861 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001862 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001863 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001864 {
1865 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1866
1867 if (symbol)
1868 {
1869 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1870 mReferencedVaryings[symbol->getSymbol()] = symbol;
1871 }
1872 else
1873 {
1874 (*sit)->traverse(this);
1875 }
1876 }
1877 }
1878
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879 return false;
1880 }
1881 else if (visit == InVisit)
1882 {
1883 out << ", ";
1884 }
1885 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001886 case EOpInvariantDeclaration:
1887 // Do not do any translation
1888 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001889 case EOpPrototype:
1890 if (visit == PreVisit)
1891 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001892 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001893
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001894 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001895
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001896 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001897 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001898 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001899
1900 if (symbol)
1901 {
1902 out << argumentString(symbol);
1903
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001904 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001905 {
1906 out << ", ";
1907 }
1908 }
1909 else UNREACHABLE();
1910 }
1911
1912 out << ");\n";
1913
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001914 // Also prototype the Lod0 variant if needed
1915 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1916 {
1917 mOutputLod0Function = true;
1918 node->traverse(this);
1919 mOutputLod0Function = false;
1920 }
1921
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001922 return false;
1923 }
1924 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001925 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 case EOpFunction:
1927 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001928 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001929
Jamie Madill033dae62014-06-18 12:56:28 -04001930 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001931
1932 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001934 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001936 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937 {
Jamie Madill033dae62014-06-18 12:56:28 -04001938 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001939 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001940
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001941 TIntermSequence *sequence = node->getSequence();
1942 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001943
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001944 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001945 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001946 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001947
1948 if (symbol)
1949 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001950 TStructure *structure = symbol->getType().getStruct();
1951
1952 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001953 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001954 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001955 }
1956
1957 out << argumentString(symbol);
1958
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001959 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001960 {
1961 out << ", ";
1962 }
1963 }
1964 else UNREACHABLE();
1965 }
1966
1967 out << ")\n"
1968 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001969
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001970 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001971 {
1972 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001973 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001974 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001975 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001976
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001977 out << "}\n";
1978
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001979 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1980 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001981 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001982 {
1983 mOutputLod0Function = true;
1984 node->traverse(this);
1985 mOutputLod0Function = false;
1986 }
1987 }
1988
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001989 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990 }
1991 break;
1992 case EOpFunctionCall:
1993 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001994 TString name = TFunction::unmangleName(node->getName());
1995 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001996 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001997
1998 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 {
Jamie Madill033dae62014-06-18 12:56:28 -04002000 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002001 }
2002 else
2003 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002004 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002005
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002006 TextureFunction textureFunction;
2007 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002008 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002009 textureFunction.method = TextureFunction::IMPLICIT;
2010 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002011 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002012
2013 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002014 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002015 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002016 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002017 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002018 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002019 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002020 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002021 }
Nicolas Capens46485082014-04-15 13:12:50 -04002022 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2023 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002024 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002025 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002026 }
Nicolas Capens46485082014-04-15 13:12:50 -04002027 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002028 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002029 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002030 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002031 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002032 else if (name == "textureSize")
2033 {
2034 textureFunction.method = TextureFunction::SIZE;
2035 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002036 else if (name == "textureOffset")
2037 {
2038 textureFunction.method = TextureFunction::IMPLICIT;
2039 textureFunction.offset = true;
2040 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002041 else if (name == "textureProjOffset")
2042 {
2043 textureFunction.method = TextureFunction::IMPLICIT;
2044 textureFunction.offset = true;
2045 textureFunction.proj = true;
2046 }
2047 else if (name == "textureLodOffset")
2048 {
2049 textureFunction.method = TextureFunction::LOD;
2050 textureFunction.offset = true;
2051 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002052 else if (name == "textureProjLodOffset")
2053 {
2054 textureFunction.method = TextureFunction::LOD;
2055 textureFunction.proj = true;
2056 textureFunction.offset = true;
2057 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002058 else if (name == "texelFetch")
2059 {
2060 textureFunction.method = TextureFunction::FETCH;
2061 }
2062 else if (name == "texelFetchOffset")
2063 {
2064 textureFunction.method = TextureFunction::FETCH;
2065 textureFunction.offset = true;
2066 }
Nicolas Capens46485082014-04-15 13:12:50 -04002067 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002068 {
2069 textureFunction.method = TextureFunction::GRAD;
2070 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002071 else if (name == "textureGradOffset")
2072 {
2073 textureFunction.method = TextureFunction::GRAD;
2074 textureFunction.offset = true;
2075 }
Nicolas Capens46485082014-04-15 13:12:50 -04002076 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002077 {
2078 textureFunction.method = TextureFunction::GRAD;
2079 textureFunction.proj = true;
2080 }
2081 else if (name == "textureProjGradOffset")
2082 {
2083 textureFunction.method = TextureFunction::GRAD;
2084 textureFunction.proj = true;
2085 textureFunction.offset = true;
2086 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002087 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002088
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002089 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002090 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002091 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2092
2093 if (textureFunction.offset)
2094 {
2095 mandatoryArgumentCount++;
2096 }
2097
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002098 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002099
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002100 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002101 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002102 if (bias)
2103 {
2104 textureFunction.method = TextureFunction::LOD0BIAS;
2105 }
2106 else
2107 {
2108 textureFunction.method = TextureFunction::LOD0;
2109 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002110 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002111 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002112 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002113 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002114 }
2115 }
2116
2117 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002118
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002119 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002121
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002122 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002123 {
2124 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2125 {
2126 out << "texture_";
2127 (*arg)->traverse(this);
2128 out << ", sampler_";
2129 }
2130
2131 (*arg)->traverse(this);
2132
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002133 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002134 {
2135 out << ", ";
2136 }
2137 }
2138
2139 out << ")";
2140
2141 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142 }
2143 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002144 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002145 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2146 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2147 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2148 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2149 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2150 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2151 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2152 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2153 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2154 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2155 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2156 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2157 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2158 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2159 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2160 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2161 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2162 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2163 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002164 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002165 {
Jamie Madill033dae62014-06-18 12:56:28 -04002166 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002167 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002168 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002169 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002170 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002171 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2172 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2173 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2174 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2175 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2176 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002177 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002178 ASSERT(node->getUseEmulatedFunction());
2179 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002180 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002181 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002182 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002183 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002184 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002185 ASSERT(node->getUseEmulatedFunction());
2186 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002187 break;
2188 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2189 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2190 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2191 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2192 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2193 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2194 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2195 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2196 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002197 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002198 ASSERT(node->getUseEmulatedFunction());
2199 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002200 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2202 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002203 case EOpOuterProduct:
2204 ASSERT(node->getUseEmulatedFunction());
2205 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2206 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002207 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002208 default: UNREACHABLE();
2209 }
2210
2211 return true;
2212}
2213
2214bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2215{
Jamie Madill32aab012015-01-27 14:12:26 -05002216 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002217
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002218 if (node->usesTernaryOperator())
2219 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002220 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002221 }
2222 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002223 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002224 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002225
Corentin Wallez80bacde2014-11-10 12:07:37 -08002226 // D3D errors when there is a gradient operation in a loop in an unflattened if
2227 // however flattening all the ifs in branch heavy shaders made D3D error too.
2228 // As a temporary workaround we flatten the ifs only if there is at least a loop
2229 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002230 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002231 {
2232 out << "FLATTEN ";
2233 }
2234
2235 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002236
2237 node->getCondition()->traverse(this);
2238
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002239 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002240
Jamie Madill075edd82013-07-08 13:30:19 -04002241 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002242 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002243
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002244 bool discard = false;
2245
daniel@transgaming.combb885322010-04-15 20:45:24 +00002246 if (node->getTrueBlock())
2247 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002248 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002249
2250 // Detect true discard
2251 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002252 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253
Jamie Madill075edd82013-07-08 13:30:19 -04002254 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002255 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002256
2257 if (node->getFalseBlock())
2258 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002259 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002260
Jamie Madill075edd82013-07-08 13:30:19 -04002261 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002262 out << "{\n";
2263
Jamie Madill075edd82013-07-08 13:30:19 -04002264 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002265 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002266
Jamie Madill075edd82013-07-08 13:30:19 -04002267 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002268 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002269
2270 // Detect false discard
2271 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2272 }
2273
2274 // ANGLE issue 486: Detect problematic conditional discard
2275 if (discard && FindSideEffectRewriting::search(node))
2276 {
2277 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002278 }
2279 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280
2281 return false;
2282}
2283
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002284bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002285{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002286 if (node->getStatementList())
2287 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002288 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002289 outputTriplet(visit, "switch (", ") ", "");
2290 // The curly braces get written when visiting the statementList aggregate
2291 }
2292 else
2293 {
2294 // No statementList, so it won't output curly braces
2295 outputTriplet(visit, "switch (", ") {", "}\n");
2296 }
2297 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002298}
2299
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002300bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002301{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002302 if (node->hasCondition())
2303 {
2304 outputTriplet(visit, "case (", "", "):\n");
2305 return true;
2306 }
2307 else
2308 {
2309 TInfoSinkBase &out = getInfoSink();
2310 out << "default:\n";
2311 return false;
2312 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002313}
2314
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2316{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002317 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318}
2319
2320bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2321{
Nicolas Capens655fe362014-04-11 13:12:34 -04002322 mNestedLoopDepth++;
2323
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002324 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2325
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002326 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002327 {
2328 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2329 }
2330
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002331 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002332 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002333 if (handleExcessiveLoop(node))
2334 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002335 mInsideDiscontinuousLoop = wasDiscontinuous;
2336 mNestedLoopDepth--;
2337
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002338 return false;
2339 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002340 }
2341
Jamie Madill32aab012015-01-27 14:12:26 -05002342 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002343
alokp@chromium.org52813552010-11-16 18:36:09 +00002344 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002346 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002347
Jamie Madill075edd82013-07-08 13:30:19 -04002348 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002349 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350 }
2351 else
2352 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002353 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002354
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 if (node->getInit())
2356 {
2357 node->getInit()->traverse(this);
2358 }
2359
2360 out << "; ";
2361
alokp@chromium.org52813552010-11-16 18:36:09 +00002362 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002364 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002365 }
2366
2367 out << "; ";
2368
alokp@chromium.org52813552010-11-16 18:36:09 +00002369 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002371 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372 }
2373
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002374 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002375
Jamie Madill075edd82013-07-08 13:30:19 -04002376 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002377 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378 }
2379
2380 if (node->getBody())
2381 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002382 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383 }
2384
Jamie Madill075edd82013-07-08 13:30:19 -04002385 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002386 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387
alokp@chromium.org52813552010-11-16 18:36:09 +00002388 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 {
Jamie Madill075edd82013-07-08 13:30:19 -04002390 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391 out << "while(\n";
2392
alokp@chromium.org52813552010-11-16 18:36:09 +00002393 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394
daniel@transgaming.com73536982012-03-21 20:45:49 +00002395 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396 }
2397
daniel@transgaming.com73536982012-03-21 20:45:49 +00002398 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002400 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002401 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002402
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 return false;
2404}
2405
2406bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2407{
Jamie Madill32aab012015-01-27 14:12:26 -05002408 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409
2410 switch (node->getFlowOp())
2411 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002412 case EOpKill:
2413 outputTriplet(visit, "discard;\n", "", "");
2414 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002415 case EOpBreak:
2416 if (visit == PreVisit)
2417 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002418 if (mNestedLoopDepth > 1)
2419 {
2420 mUsesNestedBreak = true;
2421 }
2422
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002423 if (mExcessiveLoopIndex)
2424 {
2425 out << "{Break";
2426 mExcessiveLoopIndex->traverse(this);
2427 out << " = true; break;}\n";
2428 }
2429 else
2430 {
2431 out << "break;\n";
2432 }
2433 }
2434 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002435 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436 case EOpReturn:
2437 if (visit == PreVisit)
2438 {
2439 if (node->getExpression())
2440 {
2441 out << "return ";
2442 }
2443 else
2444 {
2445 out << "return;\n";
2446 }
2447 }
2448 else if (visit == PostVisit)
2449 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002450 if (node->getExpression())
2451 {
2452 out << ";\n";
2453 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 }
2455 break;
2456 default: UNREACHABLE();
2457 }
2458
2459 return true;
2460}
2461
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002462void OutputHLSL::traverseStatements(TIntermNode *node)
2463{
2464 if (isSingleStatement(node))
2465 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002466 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002467 }
2468
2469 node->traverse(this);
2470}
2471
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002472bool OutputHLSL::isSingleStatement(TIntermNode *node)
2473{
2474 TIntermAggregate *aggregate = node->getAsAggregate();
2475
2476 if (aggregate)
2477 {
2478 if (aggregate->getOp() == EOpSequence)
2479 {
2480 return false;
2481 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002482 else if (aggregate->getOp() == EOpDeclaration)
2483 {
2484 // Declaring multiple comma-separated variables must be considered multiple statements
2485 // because each individual declaration has side effects which are visible in the next.
2486 return false;
2487 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002488 else
2489 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002490 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002491 {
2492 if (!isSingleStatement(*sit))
2493 {
2494 return false;
2495 }
2496 }
2497
2498 return true;
2499 }
2500 }
2501
2502 return true;
2503}
2504
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002505// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2506// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002507bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2508{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002509 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002510 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002511
2512 // Parse loops of the form:
2513 // for(int index = initial; index [comparator] limit; index += increment)
2514 TIntermSymbol *index = NULL;
2515 TOperator comparator = EOpNull;
2516 int initial = 0;
2517 int limit = 0;
2518 int increment = 0;
2519
2520 // Parse index name and intial value
2521 if (node->getInit())
2522 {
2523 TIntermAggregate *init = node->getInit()->getAsAggregate();
2524
2525 if (init)
2526 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002527 TIntermSequence *sequence = init->getSequence();
2528 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002529
2530 if (variable && variable->getQualifier() == EvqTemporary)
2531 {
2532 TIntermBinary *assign = variable->getAsBinaryNode();
2533
2534 if (assign->getOp() == EOpInitialize)
2535 {
2536 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2537 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2538
2539 if (symbol && constant)
2540 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002541 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002542 {
2543 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002544 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002545 }
2546 }
2547 }
2548 }
2549 }
2550 }
2551
2552 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002553 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002554 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002555 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002556
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002557 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2558 {
2559 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2560
2561 if (constant)
2562 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002563 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002564 {
2565 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002566 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002567 }
2568 }
2569 }
2570 }
2571
2572 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002573 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002574 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002575 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2576 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002577
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002578 if (binaryTerminal)
2579 {
2580 TOperator op = binaryTerminal->getOp();
2581 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2582
2583 if (constant)
2584 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002585 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002586 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002587 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002588
2589 switch (op)
2590 {
2591 case EOpAddAssign: increment = value; break;
2592 case EOpSubAssign: increment = -value; break;
2593 default: UNIMPLEMENTED();
2594 }
2595 }
2596 }
2597 }
2598 else if (unaryTerminal)
2599 {
2600 TOperator op = unaryTerminal->getOp();
2601
2602 switch (op)
2603 {
2604 case EOpPostIncrement: increment = 1; break;
2605 case EOpPostDecrement: increment = -1; break;
2606 case EOpPreIncrement: increment = 1; break;
2607 case EOpPreDecrement: increment = -1; break;
2608 default: UNIMPLEMENTED();
2609 }
2610 }
2611 }
2612
2613 if (index != NULL && comparator != EOpNull && increment != 0)
2614 {
2615 if (comparator == EOpLessThanEqual)
2616 {
2617 comparator = EOpLessThan;
2618 limit += 1;
2619 }
2620
2621 if (comparator == EOpLessThan)
2622 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002623 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002624
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002625 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002626 {
2627 return false; // Not an excessive loop
2628 }
2629
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002630 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2631 mExcessiveLoopIndex = index;
2632
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002633 out << "{int ";
2634 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002635 out << ";\n"
2636 "bool Break";
2637 index->traverse(this);
2638 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002639
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002640 bool firstLoopFragment = true;
2641
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002642 while (iterations > 0)
2643 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002644 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002645
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002646 if (!firstLoopFragment)
2647 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002648 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002649 index->traverse(this);
2650 out << ") {\n";
2651 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002652
2653 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2654 {
2655 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2656 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002657
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002658 // for(int index = initial; index < clampedLimit; index += increment)
2659
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002660 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002661 index->traverse(this);
2662 out << " = ";
2663 out << initial;
2664
2665 out << "; ";
2666 index->traverse(this);
2667 out << " < ";
2668 out << clampedLimit;
2669
2670 out << "; ";
2671 index->traverse(this);
2672 out << " += ";
2673 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002674 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002675
Jamie Madill075edd82013-07-08 13:30:19 -04002676 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002677 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002678
2679 if (node->getBody())
2680 {
2681 node->getBody()->traverse(this);
2682 }
2683
Jamie Madill075edd82013-07-08 13:30:19 -04002684 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002685 out << ";}\n";
2686
2687 if (!firstLoopFragment)
2688 {
2689 out << "}\n";
2690 }
2691
2692 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002693
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002694 initial += MAX_LOOP_ITERATIONS * increment;
2695 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002696 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002697
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002698 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002699
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002700 mExcessiveLoopIndex = restoreIndex;
2701
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002702 return true;
2703 }
2704 else UNIMPLEMENTED();
2705 }
2706
2707 return false; // Not handled as an excessive loop
2708}
2709
Daniel Bratell29190082015-02-20 16:42:54 +01002710void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711{
Jamie Madill32aab012015-01-27 14:12:26 -05002712 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002714 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715 {
2716 out << preString;
2717 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002718 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719 {
2720 out << inString;
2721 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002722 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002723 {
2724 out << postString;
2725 }
2726}
2727
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002728void OutputHLSL::outputLineDirective(int line)
2729{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002730 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002731 {
Jamie Madill32aab012015-01-27 14:12:26 -05002732 TInfoSinkBase &out = getInfoSink();
2733
2734 out << "\n";
2735 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002736
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002737 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002738 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002739 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002740 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002741
Jamie Madill32aab012015-01-27 14:12:26 -05002742 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002743 }
2744}
2745
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002746TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2747{
2748 TQualifier qualifier = symbol->getQualifier();
2749 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002750 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002751
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002752 if (name.empty()) // HLSL demands named arguments, also for prototypes
2753 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002754 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002755 }
2756 else
2757 {
Jamie Madill033dae62014-06-18 12:56:28 -04002758 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002759 }
2760
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002761 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2762 {
Jamie Madill033dae62014-06-18 12:56:28 -04002763 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002764 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002765 }
2766
Jamie Madill033dae62014-06-18 12:56:28 -04002767 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768}
2769
2770TString OutputHLSL::initializer(const TType &type)
2771{
2772 TString string;
2773
Jamie Madill94bf7f22013-07-08 13:31:15 -04002774 size_t size = type.getObjectSize();
2775 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002777 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778
Jamie Madill94bf7f22013-07-08 13:31:15 -04002779 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002780 {
2781 string += ", ";
2782 }
2783 }
2784
daniel@transgaming.comead23042010-04-29 03:35:36 +00002785 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002787
Daniel Bratell29190082015-02-20 16:42:54 +01002788void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002789{
Jamie Madill32aab012015-01-27 14:12:26 -05002790 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002791
2792 if (visit == PreVisit)
2793 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002794 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002795
Daniel Bratell29190082015-02-20 16:42:54 +01002796 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002797 }
2798 else if (visit == InVisit)
2799 {
2800 out << ", ";
2801 }
2802 else if (visit == PostVisit)
2803 {
2804 out << ")";
2805 }
2806}
2807
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002808const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2809{
Jamie Madill32aab012015-01-27 14:12:26 -05002810 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002811
Jamie Madill98493dd2013-07-08 14:39:03 -04002812 const TStructure* structure = type.getStruct();
2813 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002814 {
Jamie Madill033dae62014-06-18 12:56:28 -04002815 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002816
Jamie Madill98493dd2013-07-08 14:39:03 -04002817 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002818
Jamie Madill98493dd2013-07-08 14:39:03 -04002819 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002820 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002821 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002822 constUnion = writeConstantUnion(*fieldType, constUnion);
2823
Jamie Madill98493dd2013-07-08 14:39:03 -04002824 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002825 {
2826 out << ", ";
2827 }
2828 }
2829
2830 out << ")";
2831 }
2832 else
2833 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002834 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002835 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002836
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002837 if (writeType)
2838 {
Jamie Madill033dae62014-06-18 12:56:28 -04002839 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002840 }
2841
Jamie Madill94bf7f22013-07-08 13:31:15 -04002842 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002843 {
2844 switch (constUnion->getType())
2845 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002846 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002847 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002848 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002849 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002850 default: UNREACHABLE();
2851 }
2852
2853 if (i != size - 1)
2854 {
2855 out << ", ";
2856 }
2857 }
2858
2859 if (writeType)
2860 {
2861 out << ")";
2862 }
2863 }
2864
2865 return constUnion;
2866}
2867
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002868void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2869{
2870 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2871 outputTriplet(visit, preString.c_str(), ", ", ")");
2872}
2873
Jamie Madill37997142015-01-28 10:06:34 -05002874bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2875{
2876 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2877 expression->traverse(&searchSymbol);
2878
2879 if (searchSymbol.foundMatch())
2880 {
2881 // Type already printed
2882 out << "t" + str(mUniqueIndex) + " = ";
2883 expression->traverse(this);
2884 out << ", ";
2885 symbolNode->traverse(this);
2886 out << " = t" + str(mUniqueIndex);
2887
2888 mUniqueIndex++;
2889 return true;
2890 }
2891
2892 return false;
2893}
2894
2895void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2896{
2897 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2898 << "\n"
2899 << "void initializeDeferredGlobals()\n"
2900 << "{\n";
2901
2902 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2903 {
2904 TIntermSymbol *symbol = deferredGlobal.first;
2905 TIntermTyped *expression = deferredGlobal.second;
2906 ASSERT(symbol);
2907 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2908
2909 out << " " << Decorate(symbol->getSymbol()) << " = ";
2910
2911 if (!writeSameSymbolInitializer(out, symbol, expression))
2912 {
2913 ASSERT(mInfoSinkStack.top() == &out);
2914 expression->traverse(this);
2915 }
2916
2917 out << ";\n";
2918 }
2919
2920 out << "}\n"
2921 << "\n";
2922}
2923
Jamie Madill55e79e02015-02-09 15:35:00 -05002924TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2925{
2926 const TFieldList &fields = structure.fields();
2927
2928 for (const auto &eqFunction : mStructEqualityFunctions)
2929 {
2930 if (eqFunction.structure == &structure)
2931 {
2932 return eqFunction.functionName;
2933 }
2934 }
2935
2936 const TString &structNameString = StructNameString(structure);
2937
2938 StructEqualityFunction function;
2939 function.structure = &structure;
2940 function.functionName = "angle_eq_" + structNameString;
2941
2942 TString &func = function.functionDefinition;
2943
2944 func = "bool " + function.functionName + "(" + structNameString + " a, " + structNameString + " b)\n" +
2945 "{\n"
2946 " return ";
2947
2948 for (size_t i = 0; i < fields.size(); i++)
2949 {
2950 const TField *field = fields[i];
2951 const TType *fieldType = field->type();
2952
2953 const TString &fieldNameA = "a." + Decorate(field->name());
2954 const TString &fieldNameB = "b." + Decorate(field->name());
2955
2956 if (i > 0)
2957 {
2958 func += " && ";
2959 }
2960
2961 if (fieldType->getBasicType() == EbtStruct)
2962 {
2963 const TStructure &fieldStruct = *fieldType->getStruct();
2964 const TString &functionName = addStructEqualityFunction(fieldStruct);
2965 func += functionName + "(" + fieldNameA + ", " + fieldNameB + ")";
2966 }
2967 else if (fieldType->isScalar())
2968 {
2969 func += "(" + fieldNameA + " == " + fieldNameB + ")";
2970 }
2971 else
2972 {
2973 ASSERT(fieldType->isMatrix() || fieldType->isVector());
2974 func += "all(" + fieldNameA + " == " + fieldNameB + ")";
2975 }
2976 }
2977
2978 func = func + ";\n" + "}\n";
2979
2980 mStructEqualityFunctions.push_back(function);
2981
2982 return function.functionName;
2983}
2984
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002985}