blob: b626f1fa68b4f2a220a916630a7318318e40ce71 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
14#include "common/utilities.h"
15#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
16#include "compiler/translator/DetectDiscontinuity.h"
17#include "compiler/translator/FlagStd140Structs.h"
18#include "compiler/translator/InfoSink.h"
19#include "compiler/translator/NodeSearch.h"
20#include "compiler/translator/RewriteElseBlocks.h"
21#include "compiler/translator/SearchSymbol.h"
22#include "compiler/translator/StructureHLSL.h"
23#include "compiler/translator/TranslatorHLSL.h"
24#include "compiler/translator/UnfoldShortCircuit.h"
25#include "compiler/translator/UniformHLSL.h"
26#include "compiler/translator/UtilsHLSL.h"
27#include "compiler/translator/blocklayout.h"
28#include "compiler/translator/compilerdebug.h"
29#include "compiler/translator/util.h"
30
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031namespace sh
32{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000033
Nicolas Capense0ba27a2013-06-24 16:10:52 -040034TString OutputHLSL::TextureFunction::name() const
35{
36 TString name = "gl_texture";
37
Nicolas Capens6d232bb2013-07-08 15:56:38 -040038 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040039 {
40 name += "2D";
41 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040042 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040043 {
44 name += "3D";
45 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040046 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040047 {
48 name += "Cube";
49 }
50 else UNREACHABLE();
51
52 if (proj)
53 {
54 name += "Proj";
55 }
56
Nicolas Capensb1f45b72013-12-19 17:37:19 -050057 if (offset)
58 {
59 name += "Offset";
60 }
61
Nicolas Capens75fb4752013-07-10 15:14:47 -040062 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040063 {
Nicolas Capensfc014542014-02-18 14:47:13 -050064 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040065 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050066 case LOD: name += "Lod"; break;
67 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040068 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050069 case SIZE: name += "Size"; break;
70 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050071 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040072 default: UNREACHABLE();
73 }
74
75 return name + "(";
76}
77
78bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
79{
80 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040081 if (sampler > rhs.sampler) return false;
82
Nicolas Capense0ba27a2013-06-24 16:10:52 -040083 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040084 if (coords > rhs.coords) return false;
85
Nicolas Capense0ba27a2013-06-24 16:10:52 -040086 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040087 if (proj && !rhs.proj) return false;
88
89 if (!offset && rhs.offset) return true;
90 if (offset && !rhs.offset) return false;
91
Nicolas Capens75fb4752013-07-10 15:14:47 -040092 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040093 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040094
95 return false;
96}
97
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020098OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
99 const TExtensionBehavior &extensionBehavior,
100 const char *sourcePath, ShShaderOutput outputType,
101 int numRenderTargets, const std::vector<Uniform> &uniforms,
102 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400103 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200104 mShaderType(shaderType),
105 mShaderVersion(shaderVersion),
106 mExtensionBehavior(extensionBehavior),
107 mSourcePath(sourcePath),
108 mOutputType(outputType),
109 mNumRenderTargets(numRenderTargets),
110 mCompileOptions(compileOptions)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200112 mUnfoldShortCircuit = new UnfoldShortCircuit(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000113 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000114
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000115 mUsesFragColor = false;
116 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000117 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000118 mUsesFragCoord = false;
119 mUsesPointCoord = false;
120 mUsesFrontFacing = false;
121 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000122 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400123 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000124 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500125 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400126 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000127
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000128 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000129
130 mContainsLoopDiscontinuity = false;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800131 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000132 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000133 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400134 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000135
136 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000137
Jamie Madill8daaba12014-06-13 10:04:33 -0400138 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200139 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400140
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000141 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000142 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200143 if (mShaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000144 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400145 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
146 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000147 }
148 else
149 {
Cooper Partine6664f02015-01-09 16:22:24 -0800150 // Reserve registers for dx_DepthRange, dx_ViewAdjust and dx_ViewCoords
151 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000152 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000153 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000154
Jamie Madillf91ce812014-06-13 10:04:34 -0400155 // Reserve registers for the default uniform block and driver constants
156 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157}
158
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000159OutputHLSL::~OutputHLSL()
160{
Jamie Madill8daaba12014-06-13 10:04:33 -0400161 SafeDelete(mUnfoldShortCircuit);
162 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400163 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000164}
165
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200166void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000167{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200168 mContainsLoopDiscontinuity = mShaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(treeRoot);
169 mContainsAnyLoop = containsAnyLoop(treeRoot);
170 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400171 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000172
Jamie Madille53c98b2014-02-03 11:57:13 -0500173 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
174 // use a vertex attribute as a condition, and some related computation in the else block.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200175 if (mOutputType == SH_HLSL9_OUTPUT && mShaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500176 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200177 RewriteElseBlocks(treeRoot);
Jamie Madille53c98b2014-02-03 11:57:13 -0500178 }
179
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200180 BuiltInFunctionEmulatorHLSL builtInFunctionEmulator;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200181 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500182
Jamie Madill37997142015-01-28 10:06:34 -0500183 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500184 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200185 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500186 mInfoSinkStack.pop();
187
Jamie Madill37997142015-01-28 10:06:34 -0500188 mInfoSinkStack.push(&mFooter);
189 if (!mDeferredGlobalInitializers.empty())
190 {
191 writeDeferredGlobalInitializers(mFooter);
192 }
193 mInfoSinkStack.pop();
194
Jamie Madill32aab012015-01-27 14:12:26 -0500195 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200196 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500197 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000198
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200199 objSink << mHeader.c_str();
200 objSink << mBody.c_str();
201 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200202
203 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000204}
205
Jamie Madill570e04d2013-06-21 09:15:33 -0400206void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
207{
208 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
209 {
210 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
211
Jamie Madill32aab012015-01-27 14:12:26 -0500212 TInfoSinkBase structInfoSink;
213 mInfoSinkStack.push(&structInfoSink);
214
Jamie Madill570e04d2013-06-21 09:15:33 -0400215 // This will mark the necessary block elements as referenced
216 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500217
218 TString structName(structInfoSink.c_str());
219 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400220
221 mFlaggedStructOriginalNames[flaggedNode] = structName;
222
223 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
224 {
225 structName.erase(pos, 1);
226 }
227
228 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
229 }
230}
231
Jamie Madill4e1fd412014-07-10 17:50:10 -0400232const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
233{
234 return mUniformHLSL->getInterfaceBlockRegisterMap();
235}
236
Jamie Madill9fe25e92014-07-18 10:33:08 -0400237const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
238{
239 return mUniformHLSL->getUniformRegisterMap();
240}
241
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000242int OutputHLSL::vectorSize(const TType &type) const
243{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000244 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000245 int arraySize = type.isArray() ? type.getArraySize() : 1;
246
247 return elementSize * arraySize;
248}
249
Jamie Madill98493dd2013-07-08 14:39:03 -0400250TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400251{
252 TString init;
253
254 TString preIndentString;
255 TString fullIndentString;
256
257 for (int spaces = 0; spaces < (indent * 4); spaces++)
258 {
259 preIndentString += ' ';
260 }
261
262 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
263 {
264 fullIndentString += ' ';
265 }
266
267 init += preIndentString + "{\n";
268
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 const TFieldList &fields = structure.fields();
270 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400271 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400272 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400273 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400274 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400275
Jamie Madill98493dd2013-07-08 14:39:03 -0400276 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400277 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400278 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400279 }
280 else
281 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400282 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400283 }
284 }
285
286 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
287
288 return init;
289}
290
Olli Etuahoe17e3192015-01-02 12:47:59 +0200291void OutputHLSL::header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292{
Jamie Madill32aab012015-01-27 14:12:26 -0500293 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000295 TString varyings;
296 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400297 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000298
Jamie Madill829f59e2013-11-13 19:40:54 -0500299 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400300 {
301 TIntermTyped *structNode = flaggedStructIt->first;
302 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400303 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400304 const TString &originalName = mFlaggedStructOriginalNames[structNode];
305
Jamie Madill033dae62014-06-18 12:56:28 -0400306 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400307 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400308 flaggedStructs += "\n";
309 }
310
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000311 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
312 {
313 const TType &type = varying->second->getType();
314 const TString &name = varying->second->getSymbol();
315
316 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400317 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
318 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000319 }
320
321 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
322 {
323 const TType &type = attribute->second->getType();
324 const TString &name = attribute->second->getSymbol();
325
Jamie Madill033dae62014-06-18 12:56:28 -0400326 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000327 }
328
Jamie Madill8daaba12014-06-13 10:04:33 -0400329 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400330
Jamie Madillf91ce812014-06-13 10:04:34 -0400331 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
332 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
333
Jamie Madill55e79e02015-02-09 15:35:00 -0500334 if (!mStructEqualityFunctions.empty())
335 {
336 out << "\n// Structure equality functions\n\n";
337 for (const auto &eqFunction : mStructEqualityFunctions)
338 {
339 out << eqFunction.functionDefinition << "\n";
340 }
341 }
342
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500343 if (mUsesDiscardRewriting)
344 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400345 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500346 }
347
Nicolas Capens655fe362014-04-11 13:12:34 -0400348 if (mUsesNestedBreak)
349 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400350 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400351 }
352
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400353 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
354 "#define LOOP [loop]\n"
355 "#define FLATTEN [flatten]\n"
356 "#else\n"
357 "#define LOOP\n"
358 "#define FLATTEN\n"
359 "#endif\n";
360
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200361 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200363 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
364 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000365
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000366 out << "// Varyings\n";
367 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400368 out << "\n";
369
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200370 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000371 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500372 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000373 {
Jamie Madill46131a32013-06-20 11:55:50 -0400374 const TString &variableName = outputVariableIt->first;
375 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400376
Jamie Madill033dae62014-06-18 12:56:28 -0400377 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400378 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000379 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000380 }
Jamie Madill46131a32013-06-20 11:55:50 -0400381 else
382 {
383 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
384
385 out << "static float4 gl_Color[" << numColorValues << "] =\n"
386 "{\n";
387 for (unsigned int i = 0; i < numColorValues; i++)
388 {
389 out << " float4(0, 0, 0, 0)";
390 if (i + 1 != numColorValues)
391 {
392 out << ",";
393 }
394 out << "\n";
395 }
396
397 out << "};\n";
398 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000399
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400400 if (mUsesFragDepth)
401 {
402 out << "static float gl_Depth = 0.0;\n";
403 }
404
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000405 if (mUsesFragCoord)
406 {
407 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
408 }
409
410 if (mUsesPointCoord)
411 {
412 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
413 }
414
415 if (mUsesFrontFacing)
416 {
417 out << "static bool gl_FrontFacing = false;\n";
418 }
419
420 out << "\n";
421
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000422 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000423 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000424 out << "struct gl_DepthRangeParameters\n"
425 "{\n"
426 " float near;\n"
427 " float far;\n"
428 " float diff;\n"
429 "};\n"
430 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000431 }
432
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000433 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000434 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000435 out << "cbuffer DriverConstants : register(b1)\n"
436 "{\n";
437
438 if (mUsesDepthRange)
439 {
440 out << " float3 dx_DepthRange : packoffset(c0);\n";
441 }
442
443 if (mUsesFragCoord)
444 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000445 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000446 }
447
448 if (mUsesFragCoord || mUsesFrontFacing)
449 {
450 out << " float3 dx_DepthFront : packoffset(c2);\n";
451 }
452
453 out << "};\n";
454 }
455 else
456 {
457 if (mUsesDepthRange)
458 {
459 out << "uniform float3 dx_DepthRange : register(c0);";
460 }
461
462 if (mUsesFragCoord)
463 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000464 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000465 }
466
467 if (mUsesFragCoord || mUsesFrontFacing)
468 {
469 out << "uniform float3 dx_DepthFront : register(c2);\n";
470 }
471 }
472
473 out << "\n";
474
475 if (mUsesDepthRange)
476 {
477 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
478 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000479 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000480
Jamie Madillf91ce812014-06-13 10:04:34 -0400481 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000482 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400483 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000484 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400485 out << flaggedStructs;
486 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000487 }
488
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000489 if (usingMRTExtension && mNumRenderTargets > 1)
490 {
491 out << "#define GL_USES_MRT\n";
492 }
493
494 if (mUsesFragColor)
495 {
496 out << "#define GL_USES_FRAG_COLOR\n";
497 }
498
499 if (mUsesFragData)
500 {
501 out << "#define GL_USES_FRAG_DATA\n";
502 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000504 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000505 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000506 out << "// Attributes\n";
507 out << attributes;
508 out << "\n"
509 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400510
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000511 if (mUsesPointSize)
512 {
513 out << "static float gl_PointSize = float(1);\n";
514 }
515
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000516 if (mUsesInstanceID)
517 {
518 out << "static int gl_InstanceID;";
519 }
520
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000521 out << "\n"
522 "// Varyings\n";
523 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000524 out << "\n";
525
526 if (mUsesDepthRange)
527 {
528 out << "struct gl_DepthRangeParameters\n"
529 "{\n"
530 " float near;\n"
531 " float far;\n"
532 " float diff;\n"
533 "};\n"
534 "\n";
535 }
536
537 if (mOutputType == SH_HLSL11_OUTPUT)
538 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800539 out << "cbuffer DriverConstants : register(b1)\n"
540 "{\n";
541
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000542 if (mUsesDepthRange)
543 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800544 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000545 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800546
Cooper Partine6664f02015-01-09 16:22:24 -0800547 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800548 // However, we declare it for all shaders (including Feature Level 10+).
549 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
550 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800551 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800552
553 out << "};\n"
554 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000555 }
556 else
557 {
558 if (mUsesDepthRange)
559 {
560 out << "uniform float3 dx_DepthRange : register(c0);\n";
561 }
562
Cooper Partine6664f02015-01-09 16:22:24 -0800563 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
564 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000565 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000566 }
567
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000568 if (mUsesDepthRange)
569 {
570 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
571 "\n";
572 }
573
Jamie Madillf91ce812014-06-13 10:04:34 -0400574 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000575 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400576 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000577 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400578 out << flaggedStructs;
579 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000580 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400581 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000582
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400583 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
584 {
585 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400586 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000587 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400588 switch(textureFunction->sampler)
589 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400590 case EbtSampler2D: out << "int2 "; break;
591 case EbtSampler3D: out << "int3 "; break;
592 case EbtSamplerCube: out << "int2 "; break;
593 case EbtSampler2DArray: out << "int3 "; break;
594 case EbtISampler2D: out << "int2 "; break;
595 case EbtISampler3D: out << "int3 "; break;
596 case EbtISamplerCube: out << "int2 "; break;
597 case EbtISampler2DArray: out << "int3 "; break;
598 case EbtUSampler2D: out << "int2 "; break;
599 case EbtUSampler3D: out << "int3 "; break;
600 case EbtUSamplerCube: out << "int2 "; break;
601 case EbtUSampler2DArray: out << "int3 "; break;
602 case EbtSampler2DShadow: out << "int2 "; break;
603 case EbtSamplerCubeShadow: out << "int2 "; break;
604 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400605 default: UNREACHABLE();
606 }
607 }
608 else // Sampling function
609 {
610 switch(textureFunction->sampler)
611 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400612 case EbtSampler2D: out << "float4 "; break;
613 case EbtSampler3D: out << "float4 "; break;
614 case EbtSamplerCube: out << "float4 "; break;
615 case EbtSampler2DArray: out << "float4 "; break;
616 case EbtISampler2D: out << "int4 "; break;
617 case EbtISampler3D: out << "int4 "; break;
618 case EbtISamplerCube: out << "int4 "; break;
619 case EbtISampler2DArray: out << "int4 "; break;
620 case EbtUSampler2D: out << "uint4 "; break;
621 case EbtUSampler3D: out << "uint4 "; break;
622 case EbtUSamplerCube: out << "uint4 "; break;
623 case EbtUSampler2DArray: out << "uint4 "; break;
624 case EbtSampler2DShadow: out << "float "; break;
625 case EbtSamplerCubeShadow: out << "float "; break;
626 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400627 default: UNREACHABLE();
628 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000629 }
630
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400631 // Function name
632 out << textureFunction->name();
633
634 // Argument list
635 int hlslCoords = 4;
636
637 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000638 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400639 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000640 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400641 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
642 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
643 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000644 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400645
Nicolas Capens75fb4752013-07-10 15:14:47 -0400646 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000647 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400648 case TextureFunction::IMPLICIT: break;
649 case TextureFunction::BIAS: hlslCoords = 4; break;
650 case TextureFunction::LOD: hlslCoords = 4; break;
651 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400652 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400653 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000654 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400655 }
656 else if (mOutputType == SH_HLSL11_OUTPUT)
657 {
658 switch(textureFunction->sampler)
659 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400660 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
661 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
662 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
663 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
664 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
665 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500666 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400667 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
668 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
669 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500670 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400671 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
672 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
673 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
674 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400675 default: UNREACHABLE();
676 }
677 }
678 else UNREACHABLE();
679
Nicolas Capensfc014542014-02-18 14:47:13 -0500680 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400681 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500682 switch(textureFunction->coords)
683 {
684 case 2: out << ", int2 t"; break;
685 case 3: out << ", int3 t"; break;
686 default: UNREACHABLE();
687 }
688 }
689 else // Floating-point coordinates (except textureSize)
690 {
691 switch(textureFunction->coords)
692 {
693 case 1: out << ", int lod"; break; // textureSize()
694 case 2: out << ", float2 t"; break;
695 case 3: out << ", float3 t"; break;
696 case 4: out << ", float4 t"; break;
697 default: UNREACHABLE();
698 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000699 }
700
Nicolas Capensd11d5492014-02-19 17:06:10 -0500701 if (textureFunction->method == TextureFunction::GRAD)
702 {
703 switch(textureFunction->sampler)
704 {
705 case EbtSampler2D:
706 case EbtISampler2D:
707 case EbtUSampler2D:
708 case EbtSampler2DArray:
709 case EbtISampler2DArray:
710 case EbtUSampler2DArray:
711 case EbtSampler2DShadow:
712 case EbtSampler2DArrayShadow:
713 out << ", float2 ddx, float2 ddy";
714 break;
715 case EbtSampler3D:
716 case EbtISampler3D:
717 case EbtUSampler3D:
718 case EbtSamplerCube:
719 case EbtISamplerCube:
720 case EbtUSamplerCube:
721 case EbtSamplerCubeShadow:
722 out << ", float3 ddx, float3 ddy";
723 break;
724 default: UNREACHABLE();
725 }
726 }
727
Nicolas Capens75fb4752013-07-10 15:14:47 -0400728 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000729 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400730 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400731 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400732 case TextureFunction::LOD: out << ", float lod"; break;
733 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400734 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400735 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500736 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500737 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400738 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000739 }
740
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500741 if (textureFunction->offset)
742 {
743 switch(textureFunction->sampler)
744 {
745 case EbtSampler2D: out << ", int2 offset"; break;
746 case EbtSampler3D: out << ", int3 offset"; break;
747 case EbtSampler2DArray: out << ", int2 offset"; break;
748 case EbtISampler2D: out << ", int2 offset"; break;
749 case EbtISampler3D: out << ", int3 offset"; break;
750 case EbtISampler2DArray: out << ", int2 offset"; break;
751 case EbtUSampler2D: out << ", int2 offset"; break;
752 case EbtUSampler3D: out << ", int3 offset"; break;
753 case EbtUSampler2DArray: out << ", int2 offset"; break;
754 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500755 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500756 default: UNREACHABLE();
757 }
758 }
759
Nicolas Capens84cfa122014-04-14 13:48:45 -0400760 if (textureFunction->method == TextureFunction::BIAS ||
761 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500762 {
763 out << ", float bias";
764 }
765
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400766 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400767 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400768
Nicolas Capens75fb4752013-07-10 15:14:47 -0400769 if (textureFunction->method == TextureFunction::SIZE)
770 {
771 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
772 {
773 if (IsSamplerArray(textureFunction->sampler))
774 {
775 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
776 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
777 }
778 else
779 {
780 out << " uint width; uint height; uint numberOfLevels;\n"
781 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
782 }
783 }
784 else if (IsSampler3D(textureFunction->sampler))
785 {
786 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
787 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
788 }
789 else UNREACHABLE();
790
791 switch(textureFunction->sampler)
792 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400793 case EbtSampler2D: out << " return int2(width, height);"; break;
794 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
795 case EbtSamplerCube: out << " return int2(width, height);"; break;
796 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
797 case EbtISampler2D: out << " return int2(width, height);"; break;
798 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
799 case EbtISamplerCube: out << " return int2(width, height);"; break;
800 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
801 case EbtUSampler2D: out << " return int2(width, height);"; break;
802 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
803 case EbtUSamplerCube: out << " return int2(width, height);"; break;
804 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
805 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
806 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
807 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400808 default: UNREACHABLE();
809 }
810 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400811 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400812 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500813 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
814 {
815 out << " float width; float height; float layers; float levels;\n";
816
817 out << " uint mip = 0;\n";
818
819 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
820
821 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
822 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
823 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
824 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
825
826 // FACE_POSITIVE_X = 000b
827 // FACE_NEGATIVE_X = 001b
828 // FACE_POSITIVE_Y = 010b
829 // FACE_NEGATIVE_Y = 011b
830 // FACE_POSITIVE_Z = 100b
831 // FACE_NEGATIVE_Z = 101b
832 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
833
834 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
835 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
836 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
837
838 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
839 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
840 }
841 else if (IsIntegerSampler(textureFunction->sampler) &&
842 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400843 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400844 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400845 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400846 if (IsSamplerArray(textureFunction->sampler))
847 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400848 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400849
Nicolas Capens9edebd62013-08-06 10:59:10 -0400850 if (textureFunction->method == TextureFunction::LOD0)
851 {
852 out << " uint mip = 0;\n";
853 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400854 else if (textureFunction->method == TextureFunction::LOD0BIAS)
855 {
856 out << " uint mip = bias;\n";
857 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400858 else
859 {
860 if (textureFunction->method == TextureFunction::IMPLICIT ||
861 textureFunction->method == TextureFunction::BIAS)
862 {
863 out << " x.GetDimensions(0, width, height, layers, levels);\n"
864 " float2 tSized = float2(t.x * width, t.y * height);\n"
865 " float dx = length(ddx(tSized));\n"
866 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500867 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400868
869 if (textureFunction->method == TextureFunction::BIAS)
870 {
871 out << " lod += bias;\n";
872 }
873 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500874 else if (textureFunction->method == TextureFunction::GRAD)
875 {
876 out << " x.GetDimensions(0, width, height, layers, levels);\n"
877 " float lod = log2(max(length(ddx), length(ddy)));\n";
878 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400879
880 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
881 }
882
883 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400884 }
885 else
886 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400887 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400888
Nicolas Capens9edebd62013-08-06 10:59:10 -0400889 if (textureFunction->method == TextureFunction::LOD0)
890 {
891 out << " uint mip = 0;\n";
892 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400893 else if (textureFunction->method == TextureFunction::LOD0BIAS)
894 {
895 out << " uint mip = bias;\n";
896 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400897 else
898 {
899 if (textureFunction->method == TextureFunction::IMPLICIT ||
900 textureFunction->method == TextureFunction::BIAS)
901 {
902 out << " x.GetDimensions(0, width, height, levels);\n"
903 " float2 tSized = float2(t.x * width, t.y * height);\n"
904 " float dx = length(ddx(tSized));\n"
905 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500906 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400907
908 if (textureFunction->method == TextureFunction::BIAS)
909 {
910 out << " lod += bias;\n";
911 }
912 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500913 else if (textureFunction->method == TextureFunction::LOD)
914 {
915 out << " x.GetDimensions(0, width, height, levels);\n";
916 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500917 else if (textureFunction->method == TextureFunction::GRAD)
918 {
919 out << " x.GetDimensions(0, width, height, levels);\n"
920 " float lod = log2(max(length(ddx), length(ddy)));\n";
921 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400922
923 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
924 }
925
926 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400927 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400928 }
929 else if (IsSampler3D(textureFunction->sampler))
930 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400931 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400932
Nicolas Capens9edebd62013-08-06 10:59:10 -0400933 if (textureFunction->method == TextureFunction::LOD0)
934 {
935 out << " uint mip = 0;\n";
936 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400937 else if (textureFunction->method == TextureFunction::LOD0BIAS)
938 {
939 out << " uint mip = bias;\n";
940 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400941 else
942 {
943 if (textureFunction->method == TextureFunction::IMPLICIT ||
944 textureFunction->method == TextureFunction::BIAS)
945 {
946 out << " x.GetDimensions(0, width, height, depth, levels);\n"
947 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
948 " float dx = length(ddx(tSized));\n"
949 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500950 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400951
952 if (textureFunction->method == TextureFunction::BIAS)
953 {
954 out << " lod += bias;\n";
955 }
956 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500957 else if (textureFunction->method == TextureFunction::GRAD)
958 {
959 out << " x.GetDimensions(0, width, height, depth, levels);\n"
960 " float lod = log2(max(length(ddx), length(ddy)));\n";
961 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400962
963 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
964 }
965
966 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400967 }
968 else UNREACHABLE();
969 }
970
971 out << " return ";
972
973 // HLSL intrinsic
974 if (mOutputType == SH_HLSL9_OUTPUT)
975 {
976 switch(textureFunction->sampler)
977 {
978 case EbtSampler2D: out << "tex2D"; break;
979 case EbtSamplerCube: out << "texCUBE"; break;
980 default: UNREACHABLE();
981 }
982
Nicolas Capens75fb4752013-07-10 15:14:47 -0400983 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400984 {
985 case TextureFunction::IMPLICIT: out << "(s, "; break;
986 case TextureFunction::BIAS: out << "bias(s, "; break;
987 case TextureFunction::LOD: out << "lod(s, "; break;
988 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400989 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400990 default: UNREACHABLE();
991 }
992 }
993 else if (mOutputType == SH_HLSL11_OUTPUT)
994 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500995 if (textureFunction->method == TextureFunction::GRAD)
996 {
997 if (IsIntegerSampler(textureFunction->sampler))
998 {
999 out << "x.Load(";
1000 }
1001 else if (IsShadowSampler(textureFunction->sampler))
1002 {
1003 out << "x.SampleCmpLevelZero(s, ";
1004 }
1005 else
1006 {
1007 out << "x.SampleGrad(s, ";
1008 }
1009 }
1010 else if (IsIntegerSampler(textureFunction->sampler) ||
1011 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001012 {
1013 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001014 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001015 else if (IsShadowSampler(textureFunction->sampler))
1016 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001017 switch(textureFunction->method)
1018 {
1019 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1020 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1021 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1022 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1023 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1024 default: UNREACHABLE();
1025 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001026 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001027 else
1028 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001029 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001030 {
1031 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1032 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1033 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1034 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001035 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001036 default: UNREACHABLE();
1037 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001038 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001039 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001040 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001041
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001042 // Integer sampling requires integer addresses
1043 TString addressx = "";
1044 TString addressy = "";
1045 TString addressz = "";
1046 TString close = "";
1047
Nicolas Capensfc014542014-02-18 14:47:13 -05001048 if (IsIntegerSampler(textureFunction->sampler) ||
1049 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001050 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001051 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001052 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001053 case 2: out << "int3("; break;
1054 case 3: out << "int4("; break;
1055 default: UNREACHABLE();
1056 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001057
Nicolas Capensfc014542014-02-18 14:47:13 -05001058 // Convert from normalized floating-point to integer
1059 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001060 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001061 addressx = "int(floor(width * frac((";
1062 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001063
Nicolas Capensfc014542014-02-18 14:47:13 -05001064 if (IsSamplerArray(textureFunction->sampler))
1065 {
1066 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1067 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001068 else if (IsSamplerCube(textureFunction->sampler))
1069 {
1070 addressz = "((((";
1071 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001072 else
1073 {
1074 addressz = "int(floor(depth * frac((";
1075 }
1076
1077 close = "))))";
1078 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001079 }
1080 else
1081 {
1082 switch(hlslCoords)
1083 {
1084 case 2: out << "float2("; break;
1085 case 3: out << "float3("; break;
1086 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001087 default: UNREACHABLE();
1088 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001089 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001090
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001091 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001092
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001093 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001094 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001095 switch(textureFunction->coords)
1096 {
1097 case 3: proj = " / t.z"; break;
1098 case 4: proj = " / t.w"; break;
1099 default: UNREACHABLE();
1100 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001101 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001102
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001103 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001104
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001105 if (mOutputType == SH_HLSL9_OUTPUT)
1106 {
1107 if (hlslCoords >= 3)
1108 {
1109 if (textureFunction->coords < 3)
1110 {
1111 out << ", 0";
1112 }
1113 else
1114 {
1115 out << ", t.z" + proj;
1116 }
1117 }
1118
1119 if (hlslCoords == 4)
1120 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001121 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001122 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001123 case TextureFunction::BIAS: out << ", bias"; break;
1124 case TextureFunction::LOD: out << ", lod"; break;
1125 case TextureFunction::LOD0: out << ", 0"; break;
1126 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001127 default: UNREACHABLE();
1128 }
1129 }
1130
1131 out << "));\n";
1132 }
1133 else if (mOutputType == SH_HLSL11_OUTPUT)
1134 {
1135 if (hlslCoords >= 3)
1136 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001137 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1138 {
1139 out << ", face";
1140 }
1141 else
1142 {
1143 out << ", " + addressz + ("t.z" + proj) + close;
1144 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001145 }
1146
Nicolas Capensd11d5492014-02-19 17:06:10 -05001147 if (textureFunction->method == TextureFunction::GRAD)
1148 {
1149 if (IsIntegerSampler(textureFunction->sampler))
1150 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001151 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001152 }
1153 else if (IsShadowSampler(textureFunction->sampler))
1154 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001155 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001156 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001157 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001158 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1159 // The resulting third component of P' in the shadow forms is used as Dref
1160 out << "), t.z" << proj;
1161 }
1162 else
1163 {
1164 switch(textureFunction->coords)
1165 {
1166 case 3: out << "), t.z"; break;
1167 case 4: out << "), t.w"; break;
1168 default: UNREACHABLE();
1169 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001170 }
1171 }
1172 else
1173 {
1174 out << "), ddx, ddy";
1175 }
1176 }
1177 else if (IsIntegerSampler(textureFunction->sampler) ||
1178 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001179 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001180 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001181 }
1182 else if (IsShadowSampler(textureFunction->sampler))
1183 {
1184 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001185 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001186 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001187 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1188 // The resulting third component of P' in the shadow forms is used as Dref
1189 out << "), t.z" << proj;
1190 }
1191 else
1192 {
1193 switch(textureFunction->coords)
1194 {
1195 case 3: out << "), t.z"; break;
1196 case 4: out << "), t.w"; break;
1197 default: UNREACHABLE();
1198 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001199 }
1200 }
1201 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001202 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001203 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001204 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001205 case TextureFunction::IMPLICIT: out << ")"; break;
1206 case TextureFunction::BIAS: out << "), bias"; break;
1207 case TextureFunction::LOD: out << "), lod"; break;
1208 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001209 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001210 default: UNREACHABLE();
1211 }
1212 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001213
1214 if (textureFunction->offset)
1215 {
1216 out << ", offset";
1217 }
1218
1219 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001220 }
1221 else UNREACHABLE();
1222 }
1223
1224 out << "\n"
1225 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001226 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001227 }
1228
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001229 if (mUsesFragCoord)
1230 {
1231 out << "#define GL_USES_FRAG_COORD\n";
1232 }
1233
1234 if (mUsesPointCoord)
1235 {
1236 out << "#define GL_USES_POINT_COORD\n";
1237 }
1238
1239 if (mUsesFrontFacing)
1240 {
1241 out << "#define GL_USES_FRONT_FACING\n";
1242 }
1243
1244 if (mUsesPointSize)
1245 {
1246 out << "#define GL_USES_POINT_SIZE\n";
1247 }
1248
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001249 if (mUsesFragDepth)
1250 {
1251 out << "#define GL_USES_FRAG_DEPTH\n";
1252 }
1253
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001254 if (mUsesDepthRange)
1255 {
1256 out << "#define GL_USES_DEPTH_RANGE\n";
1257 }
1258
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001259 if (mUsesXor)
1260 {
1261 out << "bool xor(bool p, bool q)\n"
1262 "{\n"
1263 " return (p || q) && !(p && q);\n"
1264 "}\n"
1265 "\n";
1266 }
1267
Olli Etuaho80a5a6c2015-01-12 15:35:27 +02001268 builtInFunctionEmulator->OutputEmulatedFunctionDefinition(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001269}
1270
1271void OutputHLSL::visitSymbol(TIntermSymbol *node)
1272{
Jamie Madill32aab012015-01-27 14:12:26 -05001273 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274
Jamie Madill570e04d2013-06-21 09:15:33 -04001275 // Handle accessing std140 structs by value
1276 if (mFlaggedStructMappedNames.count(node) > 0)
1277 {
1278 out << mFlaggedStructMappedNames[node];
1279 return;
1280 }
1281
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001282 TString name = node->getSymbol();
1283
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001284 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001285 {
1286 mUsesDepthRange = true;
1287 out << name;
1288 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001289 else
1290 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001291 TQualifier qualifier = node->getQualifier();
1292
1293 if (qualifier == EvqUniform)
1294 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001295 const TType& nodeType = node->getType();
1296 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1297
1298 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001299 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001300 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001301 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001302 else
1303 {
1304 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001305 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001306
Jamie Madill033dae62014-06-18 12:56:28 -04001307 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001308 }
Jamie Madill19571812013-08-12 15:26:34 -07001309 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001310 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001311 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001312 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001313 }
Jamie Madill033dae62014-06-18 12:56:28 -04001314 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001315 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001316 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001317 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001318 }
Jamie Madill19571812013-08-12 15:26:34 -07001319 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001320 {
1321 mReferencedOutputVariables[name] = node;
1322 out << "out_" << name;
1323 }
1324 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001325 {
1326 out << "gl_Color[0]";
1327 mUsesFragColor = true;
1328 }
1329 else if (qualifier == EvqFragData)
1330 {
1331 out << "gl_Color";
1332 mUsesFragData = true;
1333 }
1334 else if (qualifier == EvqFragCoord)
1335 {
1336 mUsesFragCoord = true;
1337 out << name;
1338 }
1339 else if (qualifier == EvqPointCoord)
1340 {
1341 mUsesPointCoord = true;
1342 out << name;
1343 }
1344 else if (qualifier == EvqFrontFacing)
1345 {
1346 mUsesFrontFacing = true;
1347 out << name;
1348 }
1349 else if (qualifier == EvqPointSize)
1350 {
1351 mUsesPointSize = true;
1352 out << name;
1353 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001354 else if (qualifier == EvqInstanceID)
1355 {
1356 mUsesInstanceID = true;
1357 out << name;
1358 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001359 else if (name == "gl_FragDepthEXT")
1360 {
1361 mUsesFragDepth = true;
1362 out << "gl_Depth";
1363 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001364 else if (qualifier == EvqInternal)
1365 {
1366 out << name;
1367 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001368 else
1369 {
Jamie Madill033dae62014-06-18 12:56:28 -04001370 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001371 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001372 }
1373}
1374
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001375void OutputHLSL::visitRaw(TIntermRaw *node)
1376{
Jamie Madill32aab012015-01-27 14:12:26 -05001377 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001378}
1379
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001380bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1381{
Jamie Madill32aab012015-01-27 14:12:26 -05001382 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001383
Jamie Madill570e04d2013-06-21 09:15:33 -04001384 // Handle accessing std140 structs by value
1385 if (mFlaggedStructMappedNames.count(node) > 0)
1386 {
1387 out << mFlaggedStructMappedNames[node];
1388 return false;
1389 }
1390
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001391 switch (node->getOp())
1392 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001393 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001394 case EOpInitialize:
1395 if (visit == PreVisit)
1396 {
1397 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1398 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1399 // new variable is created before the assignment is evaluated), so we need to convert
1400 // this to "float t = x, x = t;".
1401
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001402 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001403 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001404 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001405
Jamie Madill37997142015-01-28 10:06:34 -05001406 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1407 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001408 {
Jamie Madill37997142015-01-28 10:06:34 -05001409 // For variables which are not constant, defer their real initialization until
1410 // after we initialize other globals: uniforms, attributes and varyings.
1411 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1412 const TString &initString = initializer(node->getType());
1413 node->setRight(new TIntermRaw(node->getType(), initString));
1414 }
1415 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1416 {
1417 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001418 return false;
1419 }
1420 }
1421 else if (visit == InVisit)
1422 {
1423 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001424 }
1425 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001426 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1427 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1428 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1429 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1430 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1431 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001432 if (visit == PreVisit)
1433 {
1434 out << "(";
1435 }
1436 else if (visit == InVisit)
1437 {
1438 out << " = mul(";
1439 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001440 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001441 }
1442 else
1443 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001444 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001445 }
1446 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001447 case EOpMatrixTimesMatrixAssign:
1448 if (visit == PreVisit)
1449 {
1450 out << "(";
1451 }
1452 else if (visit == InVisit)
1453 {
1454 out << " = mul(";
1455 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001456 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001457 }
1458 else
1459 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001460 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001461 }
1462 break;
1463 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001464 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001465 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1466 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1467 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1468 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1469 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001470 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001471 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001472 const TType& leftType = node->getLeft()->getType();
1473 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001474 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001475 if (visit == PreVisit)
1476 {
1477 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1478 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001479 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001480 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001481 return false;
1482 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001483 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001484 else
1485 {
1486 outputTriplet(visit, "", "[", "]");
1487 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001488 }
1489 break;
1490 case EOpIndexIndirect:
1491 // We do not currently support indirect references to interface blocks
1492 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1493 outputTriplet(visit, "", "[", "]");
1494 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001495 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001496 if (visit == InVisit)
1497 {
1498 const TStructure* structure = node->getLeft()->getType().getStruct();
1499 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1500 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001501 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001502
1503 return false;
1504 }
1505 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001506 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001507 if (visit == InVisit)
1508 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001509 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1510 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1511 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001512 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001513
1514 return false;
1515 }
1516 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001517 case EOpVectorSwizzle:
1518 if (visit == InVisit)
1519 {
1520 out << ".";
1521
1522 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1523
1524 if (swizzle)
1525 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001526 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001527
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001528 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001529 {
1530 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1531
1532 if (element)
1533 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001534 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001535
1536 switch (i)
1537 {
1538 case 0: out << "x"; break;
1539 case 1: out << "y"; break;
1540 case 2: out << "z"; break;
1541 case 3: out << "w"; break;
1542 default: UNREACHABLE();
1543 }
1544 }
1545 else UNREACHABLE();
1546 }
1547 }
1548 else UNREACHABLE();
1549
1550 return false; // Fully processed
1551 }
1552 break;
1553 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1554 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1555 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1556 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001557 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001558 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1559 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1560 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1561 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1562 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001563 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001564 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001565 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001566 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001567 if (node->getOp() == EOpEqual)
1568 {
1569 outputTriplet(visit, "(", " == ", ")");
1570 }
1571 else
1572 {
1573 outputTriplet(visit, "(", " != ", ")");
1574 }
1575 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001576 else
1577 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001578 if (visit == PreVisit && node->getOp() == EOpNotEqual)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001579 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001580 out << "!";
1581 }
1582
1583 if (node->getLeft()->getBasicType() == EbtStruct)
1584 {
1585 const TStructure &structure = *node->getLeft()->getType().getStruct();
1586 const TString &functionName = addStructEqualityFunction(structure);
Daniel Bratell29190082015-02-20 16:42:54 +01001587 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001588 }
1589 else
1590 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001591 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
1592 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001593 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001594 }
1595 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001596 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1597 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1598 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1599 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1600 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001601 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001602 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1603 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001604 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001605 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001606 if (node->getRight()->hasSideEffects())
1607 {
1608 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1609 return false;
1610 }
1611 else
1612 {
1613 outputTriplet(visit, "(", " || ", ")");
1614 return true;
1615 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001616 case EOpLogicalXor:
1617 mUsesXor = true;
1618 outputTriplet(visit, "xor(", ", ", ")");
1619 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001620 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001621 if (node->getRight()->hasSideEffects())
1622 {
1623 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1624 return false;
1625 }
1626 else
1627 {
1628 outputTriplet(visit, "(", " && ", ")");
1629 return true;
1630 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001631 default: UNREACHABLE();
1632 }
1633
1634 return true;
1635}
1636
1637bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1638{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639 switch (node->getOp())
1640 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001641 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001642 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001643 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1644 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001645 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001646 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1647 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1648 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1649 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001650 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1651 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1652 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1653 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1654 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1655 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1656 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1657 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001658 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1659 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1660 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1661 case EOpAsinh:
1662 ASSERT(node->getUseEmulatedFunction());
1663 writeEmulatedFunctionTriplet(visit, "asinh(");
1664 break;
1665 case EOpAcosh:
1666 ASSERT(node->getUseEmulatedFunction());
1667 writeEmulatedFunctionTriplet(visit, "acosh(");
1668 break;
1669 case EOpAtanh:
1670 ASSERT(node->getUseEmulatedFunction());
1671 writeEmulatedFunctionTriplet(visit, "atanh(");
1672 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001673 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1674 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1675 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1676 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1677 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1678 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1679 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1680 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1681 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1682 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1683 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301684 case EOpIsNan: outputTriplet(visit, "isnan(", "", ")"); break;
1685 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001686 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1687 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1688 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1689 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001690 case EOpPackSnorm2x16:
1691 ASSERT(node->getUseEmulatedFunction());
1692 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1693 break;
1694 case EOpPackUnorm2x16:
1695 ASSERT(node->getUseEmulatedFunction());
1696 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1697 break;
1698 case EOpPackHalf2x16:
1699 ASSERT(node->getUseEmulatedFunction());
1700 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1701 break;
1702 case EOpUnpackSnorm2x16:
1703 ASSERT(node->getUseEmulatedFunction());
1704 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1705 break;
1706 case EOpUnpackUnorm2x16:
1707 ASSERT(node->getUseEmulatedFunction());
1708 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1709 break;
1710 case EOpUnpackHalf2x16:
1711 ASSERT(node->getUseEmulatedFunction());
1712 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1713 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001714 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1715 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001716 case EOpDFdx:
1717 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1718 {
1719 outputTriplet(visit, "(", "", ", 0.0)");
1720 }
1721 else
1722 {
1723 outputTriplet(visit, "ddx(", "", ")");
1724 }
1725 break;
1726 case EOpDFdy:
1727 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1728 {
1729 outputTriplet(visit, "(", "", ", 0.0)");
1730 }
1731 else
1732 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001733 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001734 }
1735 break;
1736 case EOpFwidth:
1737 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1738 {
1739 outputTriplet(visit, "(", "", ", 0.0)");
1740 }
1741 else
1742 {
1743 outputTriplet(visit, "fwidth(", "", ")");
1744 }
1745 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001746 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1747 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001748 case EOpInverse:
1749 ASSERT(node->getUseEmulatedFunction());
1750 writeEmulatedFunctionTriplet(visit, "inverse(");
1751 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001752
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001753 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1754 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755 default: UNREACHABLE();
1756 }
1757
1758 return true;
1759}
1760
1761bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1762{
Jamie Madill32aab012015-01-27 14:12:26 -05001763 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001765 switch (node->getOp())
1766 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001767 case EOpSequence:
1768 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001769 if (mInsideFunction)
1770 {
Jamie Madill075edd82013-07-08 13:30:19 -04001771 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001772 out << "{\n";
1773 }
1774
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001775 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001776 {
Jamie Madill075edd82013-07-08 13:30:19 -04001777 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001778
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001779 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001780
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001781 // Don't output ; after case labels, they're terminated by :
1782 // This is needed especially since outputting a ; after a case statement would turn empty
1783 // case statements into non-empty case statements, disallowing fall-through from them.
1784 if ((*sit)->getAsCaseNode() == nullptr)
1785 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001786 }
1787
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001788 if (mInsideFunction)
1789 {
Jamie Madill075edd82013-07-08 13:30:19 -04001790 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001791 out << "}\n";
1792 }
1793
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001794 return false;
1795 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001796 case EOpDeclaration:
1797 if (visit == PreVisit)
1798 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001799 TIntermSequence *sequence = node->getSequence();
1800 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001801
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001802 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001804 TStructure *structure = variable->getType().getStruct();
1805
1806 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001807 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001808 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001809 }
1810
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001811 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812 {
Jamie Madill37997142015-01-28 10:06:34 -05001813 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814 {
Jamie Madill37997142015-01-28 10:06:34 -05001815 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001816 {
Jamie Madill37997142015-01-28 10:06:34 -05001817 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001818 }
1819
Nicolas Capensd974db42014-10-07 10:50:19 -04001820 if (!mInsideFunction)
1821 {
1822 out << "static ";
1823 }
1824
1825 out << TypeString(variable->getType()) + " ";
1826
Jamie Madill37997142015-01-28 10:06:34 -05001827 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001829 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001831 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001832 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001833 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001834 }
1835 else
1836 {
Jamie Madill37997142015-01-28 10:06:34 -05001837 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001838 }
1839
Jamie Madill37997142015-01-28 10:06:34 -05001840 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001841 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001842 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843 }
1844 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001845 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001846 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1847 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001848 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001849 }
1850 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001851 }
Jamie Madill033dae62014-06-18 12:56:28 -04001852 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001853 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001854 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001855 {
1856 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1857
1858 if (symbol)
1859 {
1860 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1861 mReferencedVaryings[symbol->getSymbol()] = symbol;
1862 }
1863 else
1864 {
1865 (*sit)->traverse(this);
1866 }
1867 }
1868 }
1869
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 return false;
1871 }
1872 else if (visit == InVisit)
1873 {
1874 out << ", ";
1875 }
1876 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001877 case EOpInvariantDeclaration:
1878 // Do not do any translation
1879 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001880 case EOpPrototype:
1881 if (visit == PreVisit)
1882 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001883 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001884
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001885 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001886
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001887 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001888 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001889 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001890
1891 if (symbol)
1892 {
1893 out << argumentString(symbol);
1894
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001895 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001896 {
1897 out << ", ";
1898 }
1899 }
1900 else UNREACHABLE();
1901 }
1902
1903 out << ");\n";
1904
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001905 // Also prototype the Lod0 variant if needed
1906 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1907 {
1908 mOutputLod0Function = true;
1909 node->traverse(this);
1910 mOutputLod0Function = false;
1911 }
1912
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001913 return false;
1914 }
1915 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001916 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917 case EOpFunction:
1918 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001919 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920
Jamie Madill033dae62014-06-18 12:56:28 -04001921 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001922
1923 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001925 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001927 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001928 {
Jamie Madill033dae62014-06-18 12:56:28 -04001929 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001930 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001931
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001932 TIntermSequence *sequence = node->getSequence();
1933 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001934
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001935 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001936 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001937 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001938
1939 if (symbol)
1940 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001941 TStructure *structure = symbol->getType().getStruct();
1942
1943 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001944 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001945 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001946 }
1947
1948 out << argumentString(symbol);
1949
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001950 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001951 {
1952 out << ", ";
1953 }
1954 }
1955 else UNREACHABLE();
1956 }
1957
1958 out << ")\n"
1959 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001960
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001961 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001962 {
1963 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001964 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001965 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001966 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001967
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001968 out << "}\n";
1969
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001970 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1971 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001972 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001973 {
1974 mOutputLod0Function = true;
1975 node->traverse(this);
1976 mOutputLod0Function = false;
1977 }
1978 }
1979
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001980 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981 }
1982 break;
1983 case EOpFunctionCall:
1984 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001985 TString name = TFunction::unmangleName(node->getName());
1986 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001987 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001988
1989 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990 {
Jamie Madill033dae62014-06-18 12:56:28 -04001991 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992 }
1993 else
1994 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001995 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001996
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001997 TextureFunction textureFunction;
1998 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001999 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002000 textureFunction.method = TextureFunction::IMPLICIT;
2001 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002002 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002003
2004 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002005 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002006 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002007 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002008 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002009 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002010 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002011 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002012 }
Nicolas Capens46485082014-04-15 13:12:50 -04002013 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2014 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002015 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002016 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002017 }
Nicolas Capens46485082014-04-15 13:12:50 -04002018 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002019 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002020 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002021 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002022 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002023 else if (name == "textureSize")
2024 {
2025 textureFunction.method = TextureFunction::SIZE;
2026 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002027 else if (name == "textureOffset")
2028 {
2029 textureFunction.method = TextureFunction::IMPLICIT;
2030 textureFunction.offset = true;
2031 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002032 else if (name == "textureProjOffset")
2033 {
2034 textureFunction.method = TextureFunction::IMPLICIT;
2035 textureFunction.offset = true;
2036 textureFunction.proj = true;
2037 }
2038 else if (name == "textureLodOffset")
2039 {
2040 textureFunction.method = TextureFunction::LOD;
2041 textureFunction.offset = true;
2042 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002043 else if (name == "textureProjLodOffset")
2044 {
2045 textureFunction.method = TextureFunction::LOD;
2046 textureFunction.proj = true;
2047 textureFunction.offset = true;
2048 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002049 else if (name == "texelFetch")
2050 {
2051 textureFunction.method = TextureFunction::FETCH;
2052 }
2053 else if (name == "texelFetchOffset")
2054 {
2055 textureFunction.method = TextureFunction::FETCH;
2056 textureFunction.offset = true;
2057 }
Nicolas Capens46485082014-04-15 13:12:50 -04002058 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002059 {
2060 textureFunction.method = TextureFunction::GRAD;
2061 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002062 else if (name == "textureGradOffset")
2063 {
2064 textureFunction.method = TextureFunction::GRAD;
2065 textureFunction.offset = true;
2066 }
Nicolas Capens46485082014-04-15 13:12:50 -04002067 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002068 {
2069 textureFunction.method = TextureFunction::GRAD;
2070 textureFunction.proj = true;
2071 }
2072 else if (name == "textureProjGradOffset")
2073 {
2074 textureFunction.method = TextureFunction::GRAD;
2075 textureFunction.proj = true;
2076 textureFunction.offset = true;
2077 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002078 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002079
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002080 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002081 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002082 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2083
2084 if (textureFunction.offset)
2085 {
2086 mandatoryArgumentCount++;
2087 }
2088
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002089 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002090
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002091 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002092 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002093 if (bias)
2094 {
2095 textureFunction.method = TextureFunction::LOD0BIAS;
2096 }
2097 else
2098 {
2099 textureFunction.method = TextureFunction::LOD0;
2100 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002101 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002102 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002103 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002104 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002105 }
2106 }
2107
2108 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002109
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002110 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002112
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002113 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002114 {
2115 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2116 {
2117 out << "texture_";
2118 (*arg)->traverse(this);
2119 out << ", sampler_";
2120 }
2121
2122 (*arg)->traverse(this);
2123
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002124 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002125 {
2126 out << ", ";
2127 }
2128 }
2129
2130 out << ")";
2131
2132 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133 }
2134 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002135 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002136 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2137 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2138 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2139 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2140 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2141 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2142 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2143 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2144 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2145 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2146 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2147 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2148 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2149 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2150 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2151 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2152 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2153 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2154 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002155 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002156 {
Jamie Madill033dae62014-06-18 12:56:28 -04002157 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002158 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002159 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002160 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002161 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002162 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2163 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2164 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2165 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2166 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2167 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002168 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002169 ASSERT(node->getUseEmulatedFunction());
2170 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002171 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002172 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002173 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002175 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002176 ASSERT(node->getUseEmulatedFunction());
2177 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002178 break;
2179 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2180 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2181 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2182 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2183 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2184 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2185 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2186 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2187 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002188 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002189 ASSERT(node->getUseEmulatedFunction());
2190 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002191 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2193 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002194 case EOpOuterProduct:
2195 ASSERT(node->getUseEmulatedFunction());
2196 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2197 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002198 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199 default: UNREACHABLE();
2200 }
2201
2202 return true;
2203}
2204
2205bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2206{
Jamie Madill32aab012015-01-27 14:12:26 -05002207 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002208
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002209 if (node->usesTernaryOperator())
2210 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002211 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002212 }
2213 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002215 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002216
Corentin Wallez80bacde2014-11-10 12:07:37 -08002217 // D3D errors when there is a gradient operation in a loop in an unflattened if
2218 // however flattening all the ifs in branch heavy shaders made D3D error too.
2219 // As a temporary workaround we flatten the ifs only if there is at least a loop
2220 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002221 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002222 {
2223 out << "FLATTEN ";
2224 }
2225
2226 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002227
2228 node->getCondition()->traverse(this);
2229
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002230 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002231
Jamie Madill075edd82013-07-08 13:30:19 -04002232 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002233 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002234
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002235 bool discard = false;
2236
daniel@transgaming.combb885322010-04-15 20:45:24 +00002237 if (node->getTrueBlock())
2238 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002239 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002240
2241 // Detect true discard
2242 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002243 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244
Jamie Madill075edd82013-07-08 13:30:19 -04002245 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002246 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002247
2248 if (node->getFalseBlock())
2249 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002250 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002251
Jamie Madill075edd82013-07-08 13:30:19 -04002252 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002253 out << "{\n";
2254
Jamie Madill075edd82013-07-08 13:30:19 -04002255 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002256 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002257
Jamie Madill075edd82013-07-08 13:30:19 -04002258 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002259 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002260
2261 // Detect false discard
2262 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2263 }
2264
2265 // ANGLE issue 486: Detect problematic conditional discard
2266 if (discard && FindSideEffectRewriting::search(node))
2267 {
2268 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002269 }
2270 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271
2272 return false;
2273}
2274
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002275bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002276{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002277 if (node->getStatementList())
2278 {
2279 outputTriplet(visit, "switch (", ") ", "");
2280 // The curly braces get written when visiting the statementList aggregate
2281 }
2282 else
2283 {
2284 // No statementList, so it won't output curly braces
2285 outputTriplet(visit, "switch (", ") {", "}\n");
2286 }
2287 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002288}
2289
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002290bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002291{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002292 if (node->hasCondition())
2293 {
2294 outputTriplet(visit, "case (", "", "):\n");
2295 return true;
2296 }
2297 else
2298 {
2299 TInfoSinkBase &out = getInfoSink();
2300 out << "default:\n";
2301 return false;
2302 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002303}
2304
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2306{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002307 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002308}
2309
2310bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2311{
Nicolas Capens655fe362014-04-11 13:12:34 -04002312 mNestedLoopDepth++;
2313
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002314 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2315
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002316 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002317 {
2318 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2319 }
2320
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002321 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002322 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002323 if (handleExcessiveLoop(node))
2324 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002325 mInsideDiscontinuousLoop = wasDiscontinuous;
2326 mNestedLoopDepth--;
2327
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002328 return false;
2329 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002330 }
2331
Jamie Madill32aab012015-01-27 14:12:26 -05002332 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333
alokp@chromium.org52813552010-11-16 18:36:09 +00002334 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002336 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002337
Jamie Madill075edd82013-07-08 13:30:19 -04002338 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002339 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002340 }
2341 else
2342 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002343 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002344
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345 if (node->getInit())
2346 {
2347 node->getInit()->traverse(this);
2348 }
2349
2350 out << "; ";
2351
alokp@chromium.org52813552010-11-16 18:36:09 +00002352 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002354 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 }
2356
2357 out << "; ";
2358
alokp@chromium.org52813552010-11-16 18:36:09 +00002359 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002360 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002361 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362 }
2363
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002364 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002365
Jamie Madill075edd82013-07-08 13:30:19 -04002366 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002367 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368 }
2369
2370 if (node->getBody())
2371 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002372 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373 }
2374
Jamie Madill075edd82013-07-08 13:30:19 -04002375 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002376 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002377
alokp@chromium.org52813552010-11-16 18:36:09 +00002378 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379 {
Jamie Madill075edd82013-07-08 13:30:19 -04002380 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 out << "while(\n";
2382
alokp@chromium.org52813552010-11-16 18:36:09 +00002383 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002384
daniel@transgaming.com73536982012-03-21 20:45:49 +00002385 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386 }
2387
daniel@transgaming.com73536982012-03-21 20:45:49 +00002388 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002390 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002391 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002392
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393 return false;
2394}
2395
2396bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2397{
Jamie Madill32aab012015-01-27 14:12:26 -05002398 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399
2400 switch (node->getFlowOp())
2401 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002402 case EOpKill:
2403 outputTriplet(visit, "discard;\n", "", "");
2404 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002405 case EOpBreak:
2406 if (visit == PreVisit)
2407 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002408 if (mNestedLoopDepth > 1)
2409 {
2410 mUsesNestedBreak = true;
2411 }
2412
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002413 if (mExcessiveLoopIndex)
2414 {
2415 out << "{Break";
2416 mExcessiveLoopIndex->traverse(this);
2417 out << " = true; break;}\n";
2418 }
2419 else
2420 {
2421 out << "break;\n";
2422 }
2423 }
2424 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002425 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 case EOpReturn:
2427 if (visit == PreVisit)
2428 {
2429 if (node->getExpression())
2430 {
2431 out << "return ";
2432 }
2433 else
2434 {
2435 out << "return;\n";
2436 }
2437 }
2438 else if (visit == PostVisit)
2439 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002440 if (node->getExpression())
2441 {
2442 out << ";\n";
2443 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 }
2445 break;
2446 default: UNREACHABLE();
2447 }
2448
2449 return true;
2450}
2451
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002452void OutputHLSL::traverseStatements(TIntermNode *node)
2453{
2454 if (isSingleStatement(node))
2455 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002456 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002457 }
2458
2459 node->traverse(this);
2460}
2461
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002462bool OutputHLSL::isSingleStatement(TIntermNode *node)
2463{
2464 TIntermAggregate *aggregate = node->getAsAggregate();
2465
2466 if (aggregate)
2467 {
2468 if (aggregate->getOp() == EOpSequence)
2469 {
2470 return false;
2471 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002472 else if (aggregate->getOp() == EOpDeclaration)
2473 {
2474 // Declaring multiple comma-separated variables must be considered multiple statements
2475 // because each individual declaration has side effects which are visible in the next.
2476 return false;
2477 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002478 else
2479 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002480 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002481 {
2482 if (!isSingleStatement(*sit))
2483 {
2484 return false;
2485 }
2486 }
2487
2488 return true;
2489 }
2490 }
2491
2492 return true;
2493}
2494
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002495// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2496// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002497bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2498{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002499 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002500 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002501
2502 // Parse loops of the form:
2503 // for(int index = initial; index [comparator] limit; index += increment)
2504 TIntermSymbol *index = NULL;
2505 TOperator comparator = EOpNull;
2506 int initial = 0;
2507 int limit = 0;
2508 int increment = 0;
2509
2510 // Parse index name and intial value
2511 if (node->getInit())
2512 {
2513 TIntermAggregate *init = node->getInit()->getAsAggregate();
2514
2515 if (init)
2516 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002517 TIntermSequence *sequence = init->getSequence();
2518 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002519
2520 if (variable && variable->getQualifier() == EvqTemporary)
2521 {
2522 TIntermBinary *assign = variable->getAsBinaryNode();
2523
2524 if (assign->getOp() == EOpInitialize)
2525 {
2526 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2527 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2528
2529 if (symbol && constant)
2530 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002531 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002532 {
2533 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002534 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002535 }
2536 }
2537 }
2538 }
2539 }
2540 }
2541
2542 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002543 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002544 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002545 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002546
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002547 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2548 {
2549 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2550
2551 if (constant)
2552 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002553 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002554 {
2555 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002556 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002557 }
2558 }
2559 }
2560 }
2561
2562 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002563 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002564 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002565 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2566 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002567
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002568 if (binaryTerminal)
2569 {
2570 TOperator op = binaryTerminal->getOp();
2571 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2572
2573 if (constant)
2574 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002575 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002576 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002577 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002578
2579 switch (op)
2580 {
2581 case EOpAddAssign: increment = value; break;
2582 case EOpSubAssign: increment = -value; break;
2583 default: UNIMPLEMENTED();
2584 }
2585 }
2586 }
2587 }
2588 else if (unaryTerminal)
2589 {
2590 TOperator op = unaryTerminal->getOp();
2591
2592 switch (op)
2593 {
2594 case EOpPostIncrement: increment = 1; break;
2595 case EOpPostDecrement: increment = -1; break;
2596 case EOpPreIncrement: increment = 1; break;
2597 case EOpPreDecrement: increment = -1; break;
2598 default: UNIMPLEMENTED();
2599 }
2600 }
2601 }
2602
2603 if (index != NULL && comparator != EOpNull && increment != 0)
2604 {
2605 if (comparator == EOpLessThanEqual)
2606 {
2607 comparator = EOpLessThan;
2608 limit += 1;
2609 }
2610
2611 if (comparator == EOpLessThan)
2612 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002613 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002614
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002615 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002616 {
2617 return false; // Not an excessive loop
2618 }
2619
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002620 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2621 mExcessiveLoopIndex = index;
2622
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002623 out << "{int ";
2624 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002625 out << ";\n"
2626 "bool Break";
2627 index->traverse(this);
2628 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002629
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002630 bool firstLoopFragment = true;
2631
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002632 while (iterations > 0)
2633 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002634 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002635
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002636 if (!firstLoopFragment)
2637 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002638 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002639 index->traverse(this);
2640 out << ") {\n";
2641 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002642
2643 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2644 {
2645 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2646 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002647
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002648 // for(int index = initial; index < clampedLimit; index += increment)
2649
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002650 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002651 index->traverse(this);
2652 out << " = ";
2653 out << initial;
2654
2655 out << "; ";
2656 index->traverse(this);
2657 out << " < ";
2658 out << clampedLimit;
2659
2660 out << "; ";
2661 index->traverse(this);
2662 out << " += ";
2663 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002664 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002665
Jamie Madill075edd82013-07-08 13:30:19 -04002666 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002667 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002668
2669 if (node->getBody())
2670 {
2671 node->getBody()->traverse(this);
2672 }
2673
Jamie Madill075edd82013-07-08 13:30:19 -04002674 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002675 out << ";}\n";
2676
2677 if (!firstLoopFragment)
2678 {
2679 out << "}\n";
2680 }
2681
2682 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002683
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002684 initial += MAX_LOOP_ITERATIONS * increment;
2685 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002686 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002687
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002688 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002689
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002690 mExcessiveLoopIndex = restoreIndex;
2691
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002692 return true;
2693 }
2694 else UNIMPLEMENTED();
2695 }
2696
2697 return false; // Not handled as an excessive loop
2698}
2699
Daniel Bratell29190082015-02-20 16:42:54 +01002700void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002701{
Jamie Madill32aab012015-01-27 14:12:26 -05002702 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002704 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705 {
2706 out << preString;
2707 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002708 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002709 {
2710 out << inString;
2711 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002712 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713 {
2714 out << postString;
2715 }
2716}
2717
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002718void OutputHLSL::outputLineDirective(int line)
2719{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002720 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002721 {
Jamie Madill32aab012015-01-27 14:12:26 -05002722 TInfoSinkBase &out = getInfoSink();
2723
2724 out << "\n";
2725 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002726
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002727 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002728 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002729 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002730 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002731
Jamie Madill32aab012015-01-27 14:12:26 -05002732 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002733 }
2734}
2735
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002736TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2737{
2738 TQualifier qualifier = symbol->getQualifier();
2739 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002740 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002741
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002742 if (name.empty()) // HLSL demands named arguments, also for prototypes
2743 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002744 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002745 }
2746 else
2747 {
Jamie Madill033dae62014-06-18 12:56:28 -04002748 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002749 }
2750
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002751 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2752 {
Jamie Madill033dae62014-06-18 12:56:28 -04002753 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002754 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002755 }
2756
Jamie Madill033dae62014-06-18 12:56:28 -04002757 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758}
2759
2760TString OutputHLSL::initializer(const TType &type)
2761{
2762 TString string;
2763
Jamie Madill94bf7f22013-07-08 13:31:15 -04002764 size_t size = type.getObjectSize();
2765 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002767 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768
Jamie Madill94bf7f22013-07-08 13:31:15 -04002769 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002770 {
2771 string += ", ";
2772 }
2773 }
2774
daniel@transgaming.comead23042010-04-29 03:35:36 +00002775 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002777
Daniel Bratell29190082015-02-20 16:42:54 +01002778void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002779{
Jamie Madill32aab012015-01-27 14:12:26 -05002780 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002781
2782 if (visit == PreVisit)
2783 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002784 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002785
Daniel Bratell29190082015-02-20 16:42:54 +01002786 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002787 }
2788 else if (visit == InVisit)
2789 {
2790 out << ", ";
2791 }
2792 else if (visit == PostVisit)
2793 {
2794 out << ")";
2795 }
2796}
2797
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002798const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2799{
Jamie Madill32aab012015-01-27 14:12:26 -05002800 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002801
Jamie Madill98493dd2013-07-08 14:39:03 -04002802 const TStructure* structure = type.getStruct();
2803 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002804 {
Jamie Madill033dae62014-06-18 12:56:28 -04002805 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002806
Jamie Madill98493dd2013-07-08 14:39:03 -04002807 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002808
Jamie Madill98493dd2013-07-08 14:39:03 -04002809 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002810 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002811 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002812 constUnion = writeConstantUnion(*fieldType, constUnion);
2813
Jamie Madill98493dd2013-07-08 14:39:03 -04002814 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002815 {
2816 out << ", ";
2817 }
2818 }
2819
2820 out << ")";
2821 }
2822 else
2823 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002824 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002825 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002826
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002827 if (writeType)
2828 {
Jamie Madill033dae62014-06-18 12:56:28 -04002829 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002830 }
2831
Jamie Madill94bf7f22013-07-08 13:31:15 -04002832 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002833 {
2834 switch (constUnion->getType())
2835 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002836 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002837 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002838 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002839 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002840 default: UNREACHABLE();
2841 }
2842
2843 if (i != size - 1)
2844 {
2845 out << ", ";
2846 }
2847 }
2848
2849 if (writeType)
2850 {
2851 out << ")";
2852 }
2853 }
2854
2855 return constUnion;
2856}
2857
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002858void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2859{
2860 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2861 outputTriplet(visit, preString.c_str(), ", ", ")");
2862}
2863
Jamie Madill37997142015-01-28 10:06:34 -05002864bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2865{
2866 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2867 expression->traverse(&searchSymbol);
2868
2869 if (searchSymbol.foundMatch())
2870 {
2871 // Type already printed
2872 out << "t" + str(mUniqueIndex) + " = ";
2873 expression->traverse(this);
2874 out << ", ";
2875 symbolNode->traverse(this);
2876 out << " = t" + str(mUniqueIndex);
2877
2878 mUniqueIndex++;
2879 return true;
2880 }
2881
2882 return false;
2883}
2884
2885void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2886{
2887 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2888 << "\n"
2889 << "void initializeDeferredGlobals()\n"
2890 << "{\n";
2891
2892 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2893 {
2894 TIntermSymbol *symbol = deferredGlobal.first;
2895 TIntermTyped *expression = deferredGlobal.second;
2896 ASSERT(symbol);
2897 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2898
2899 out << " " << Decorate(symbol->getSymbol()) << " = ";
2900
2901 if (!writeSameSymbolInitializer(out, symbol, expression))
2902 {
2903 ASSERT(mInfoSinkStack.top() == &out);
2904 expression->traverse(this);
2905 }
2906
2907 out << ";\n";
2908 }
2909
2910 out << "}\n"
2911 << "\n";
2912}
2913
Jamie Madill55e79e02015-02-09 15:35:00 -05002914TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2915{
2916 const TFieldList &fields = structure.fields();
2917
2918 for (const auto &eqFunction : mStructEqualityFunctions)
2919 {
2920 if (eqFunction.structure == &structure)
2921 {
2922 return eqFunction.functionName;
2923 }
2924 }
2925
2926 const TString &structNameString = StructNameString(structure);
2927
2928 StructEqualityFunction function;
2929 function.structure = &structure;
2930 function.functionName = "angle_eq_" + structNameString;
2931
2932 TString &func = function.functionDefinition;
2933
2934 func = "bool " + function.functionName + "(" + structNameString + " a, " + structNameString + " b)\n" +
2935 "{\n"
2936 " return ";
2937
2938 for (size_t i = 0; i < fields.size(); i++)
2939 {
2940 const TField *field = fields[i];
2941 const TType *fieldType = field->type();
2942
2943 const TString &fieldNameA = "a." + Decorate(field->name());
2944 const TString &fieldNameB = "b." + Decorate(field->name());
2945
2946 if (i > 0)
2947 {
2948 func += " && ";
2949 }
2950
2951 if (fieldType->getBasicType() == EbtStruct)
2952 {
2953 const TStructure &fieldStruct = *fieldType->getStruct();
2954 const TString &functionName = addStructEqualityFunction(fieldStruct);
2955 func += functionName + "(" + fieldNameA + ", " + fieldNameB + ")";
2956 }
2957 else if (fieldType->isScalar())
2958 {
2959 func += "(" + fieldNameA + " == " + fieldNameB + ")";
2960 }
2961 else
2962 {
2963 ASSERT(fieldType->isMatrix() || fieldType->isVector());
2964 func += "all(" + fieldNameA + " == " + fieldNameB + ")";
2965 }
2966 }
2967
2968 func = func + ";\n" + "}\n";
2969
2970 mStructEqualityFunctions.push_back(function);
2971
2972 return function.functionName;
2973}
2974
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002975}