blob: 876dbb7023c5f9bcfef6962cf3adc08d99ac71e4 [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 {
1017 out << "x.SampleCmp(s, ";
1018 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001019 else
1020 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001021 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001022 {
1023 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1024 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1025 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1026 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001027 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001028 default: UNREACHABLE();
1029 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001030 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001031 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001032 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001033
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001034 // Integer sampling requires integer addresses
1035 TString addressx = "";
1036 TString addressy = "";
1037 TString addressz = "";
1038 TString close = "";
1039
Nicolas Capensfc014542014-02-18 14:47:13 -05001040 if (IsIntegerSampler(textureFunction->sampler) ||
1041 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001042 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001043 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001044 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001045 case 2: out << "int3("; break;
1046 case 3: out << "int4("; break;
1047 default: UNREACHABLE();
1048 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001049
Nicolas Capensfc014542014-02-18 14:47:13 -05001050 // Convert from normalized floating-point to integer
1051 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001052 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001053 addressx = "int(floor(width * frac((";
1054 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001055
Nicolas Capensfc014542014-02-18 14:47:13 -05001056 if (IsSamplerArray(textureFunction->sampler))
1057 {
1058 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1059 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001060 else if (IsSamplerCube(textureFunction->sampler))
1061 {
1062 addressz = "((((";
1063 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001064 else
1065 {
1066 addressz = "int(floor(depth * frac((";
1067 }
1068
1069 close = "))))";
1070 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001071 }
1072 else
1073 {
1074 switch(hlslCoords)
1075 {
1076 case 2: out << "float2("; break;
1077 case 3: out << "float3("; break;
1078 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001079 default: UNREACHABLE();
1080 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001081 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001082
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001083 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001084
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001085 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001086 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001087 switch(textureFunction->coords)
1088 {
1089 case 3: proj = " / t.z"; break;
1090 case 4: proj = " / t.w"; break;
1091 default: UNREACHABLE();
1092 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001093 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001094
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001095 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001096
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001097 if (mOutputType == SH_HLSL9_OUTPUT)
1098 {
1099 if (hlslCoords >= 3)
1100 {
1101 if (textureFunction->coords < 3)
1102 {
1103 out << ", 0";
1104 }
1105 else
1106 {
1107 out << ", t.z" + proj;
1108 }
1109 }
1110
1111 if (hlslCoords == 4)
1112 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001113 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001114 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001115 case TextureFunction::BIAS: out << ", bias"; break;
1116 case TextureFunction::LOD: out << ", lod"; break;
1117 case TextureFunction::LOD0: out << ", 0"; break;
1118 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001119 default: UNREACHABLE();
1120 }
1121 }
1122
1123 out << "));\n";
1124 }
1125 else if (mOutputType == SH_HLSL11_OUTPUT)
1126 {
1127 if (hlslCoords >= 3)
1128 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001129 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1130 {
1131 out << ", face";
1132 }
1133 else
1134 {
1135 out << ", " + addressz + ("t.z" + proj) + close;
1136 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001137 }
1138
Nicolas Capensd11d5492014-02-19 17:06:10 -05001139 if (textureFunction->method == TextureFunction::GRAD)
1140 {
1141 if (IsIntegerSampler(textureFunction->sampler))
1142 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001143 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001144 }
1145 else if (IsShadowSampler(textureFunction->sampler))
1146 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001147 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001148 switch(textureFunction->coords)
1149 {
1150 case 3: out << "), t.z"; break;
1151 case 4: out << "), t.w"; break;
1152 default: UNREACHABLE();
1153 }
1154 }
1155 else
1156 {
1157 out << "), ddx, ddy";
1158 }
1159 }
1160 else if (IsIntegerSampler(textureFunction->sampler) ||
1161 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001162 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001163 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001164 }
1165 else if (IsShadowSampler(textureFunction->sampler))
1166 {
1167 // Compare value
1168 switch(textureFunction->coords)
1169 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001170 case 3: out << "), t.z"; break;
1171 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001172 default: UNREACHABLE();
1173 }
1174 }
1175 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001176 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001177 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001178 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001179 case TextureFunction::IMPLICIT: out << ")"; break;
1180 case TextureFunction::BIAS: out << "), bias"; break;
1181 case TextureFunction::LOD: out << "), lod"; break;
1182 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001183 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001184 default: UNREACHABLE();
1185 }
1186 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001187
1188 if (textureFunction->offset)
1189 {
1190 out << ", offset";
1191 }
1192
1193 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001194 }
1195 else UNREACHABLE();
1196 }
1197
1198 out << "\n"
1199 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001200 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001201 }
1202
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001203 if (mUsesFragCoord)
1204 {
1205 out << "#define GL_USES_FRAG_COORD\n";
1206 }
1207
1208 if (mUsesPointCoord)
1209 {
1210 out << "#define GL_USES_POINT_COORD\n";
1211 }
1212
1213 if (mUsesFrontFacing)
1214 {
1215 out << "#define GL_USES_FRONT_FACING\n";
1216 }
1217
1218 if (mUsesPointSize)
1219 {
1220 out << "#define GL_USES_POINT_SIZE\n";
1221 }
1222
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001223 if (mUsesFragDepth)
1224 {
1225 out << "#define GL_USES_FRAG_DEPTH\n";
1226 }
1227
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001228 if (mUsesDepthRange)
1229 {
1230 out << "#define GL_USES_DEPTH_RANGE\n";
1231 }
1232
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001233 if (mUsesXor)
1234 {
1235 out << "bool xor(bool p, bool q)\n"
1236 "{\n"
1237 " return (p || q) && !(p && q);\n"
1238 "}\n"
1239 "\n";
1240 }
1241
Olli Etuaho80a5a6c2015-01-12 15:35:27 +02001242 builtInFunctionEmulator->OutputEmulatedFunctionDefinition(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001243}
1244
1245void OutputHLSL::visitSymbol(TIntermSymbol *node)
1246{
Jamie Madill32aab012015-01-27 14:12:26 -05001247 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001248
Jamie Madill570e04d2013-06-21 09:15:33 -04001249 // Handle accessing std140 structs by value
1250 if (mFlaggedStructMappedNames.count(node) > 0)
1251 {
1252 out << mFlaggedStructMappedNames[node];
1253 return;
1254 }
1255
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001256 TString name = node->getSymbol();
1257
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001258 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001259 {
1260 mUsesDepthRange = true;
1261 out << name;
1262 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001263 else
1264 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001265 TQualifier qualifier = node->getQualifier();
1266
1267 if (qualifier == EvqUniform)
1268 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001269 const TType& nodeType = node->getType();
1270 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1271
1272 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001273 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001274 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001275 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001276 else
1277 {
1278 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001279 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001280
Jamie Madill033dae62014-06-18 12:56:28 -04001281 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001282 }
Jamie Madill19571812013-08-12 15:26:34 -07001283 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001284 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001285 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001286 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001287 }
Jamie Madill033dae62014-06-18 12:56:28 -04001288 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001289 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001290 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001291 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001292 }
Jamie Madill19571812013-08-12 15:26:34 -07001293 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001294 {
1295 mReferencedOutputVariables[name] = node;
1296 out << "out_" << name;
1297 }
1298 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001299 {
1300 out << "gl_Color[0]";
1301 mUsesFragColor = true;
1302 }
1303 else if (qualifier == EvqFragData)
1304 {
1305 out << "gl_Color";
1306 mUsesFragData = true;
1307 }
1308 else if (qualifier == EvqFragCoord)
1309 {
1310 mUsesFragCoord = true;
1311 out << name;
1312 }
1313 else if (qualifier == EvqPointCoord)
1314 {
1315 mUsesPointCoord = true;
1316 out << name;
1317 }
1318 else if (qualifier == EvqFrontFacing)
1319 {
1320 mUsesFrontFacing = true;
1321 out << name;
1322 }
1323 else if (qualifier == EvqPointSize)
1324 {
1325 mUsesPointSize = true;
1326 out << name;
1327 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001328 else if (qualifier == EvqInstanceID)
1329 {
1330 mUsesInstanceID = true;
1331 out << name;
1332 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001333 else if (name == "gl_FragDepthEXT")
1334 {
1335 mUsesFragDepth = true;
1336 out << "gl_Depth";
1337 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001338 else if (qualifier == EvqInternal)
1339 {
1340 out << name;
1341 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001342 else
1343 {
Jamie Madill033dae62014-06-18 12:56:28 -04001344 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001345 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001346 }
1347}
1348
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001349void OutputHLSL::visitRaw(TIntermRaw *node)
1350{
Jamie Madill32aab012015-01-27 14:12:26 -05001351 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001352}
1353
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001354bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1355{
Jamie Madill32aab012015-01-27 14:12:26 -05001356 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001357
Jamie Madill570e04d2013-06-21 09:15:33 -04001358 // Handle accessing std140 structs by value
1359 if (mFlaggedStructMappedNames.count(node) > 0)
1360 {
1361 out << mFlaggedStructMappedNames[node];
1362 return false;
1363 }
1364
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001365 switch (node->getOp())
1366 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001367 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001368 case EOpInitialize:
1369 if (visit == PreVisit)
1370 {
1371 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1372 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1373 // new variable is created before the assignment is evaluated), so we need to convert
1374 // this to "float t = x, x = t;".
1375
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001376 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001377 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001378 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001379
Jamie Madill37997142015-01-28 10:06:34 -05001380 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1381 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001382 {
Jamie Madill37997142015-01-28 10:06:34 -05001383 // For variables which are not constant, defer their real initialization until
1384 // after we initialize other globals: uniforms, attributes and varyings.
1385 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1386 const TString &initString = initializer(node->getType());
1387 node->setRight(new TIntermRaw(node->getType(), initString));
1388 }
1389 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1390 {
1391 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001392 return false;
1393 }
1394 }
1395 else if (visit == InVisit)
1396 {
1397 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001398 }
1399 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001400 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1401 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1402 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1403 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1404 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1405 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001406 if (visit == PreVisit)
1407 {
1408 out << "(";
1409 }
1410 else if (visit == InVisit)
1411 {
1412 out << " = mul(";
1413 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001414 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001415 }
1416 else
1417 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001418 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001419 }
1420 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001421 case EOpMatrixTimesMatrixAssign:
1422 if (visit == PreVisit)
1423 {
1424 out << "(";
1425 }
1426 else if (visit == InVisit)
1427 {
1428 out << " = mul(";
1429 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001430 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001431 }
1432 else
1433 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001434 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001435 }
1436 break;
1437 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001438 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001439 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1440 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1441 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1442 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1443 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001444 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001445 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001446 const TType& leftType = node->getLeft()->getType();
1447 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001448 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001449 if (visit == PreVisit)
1450 {
1451 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1452 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001453 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001454 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001455 return false;
1456 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001457 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001458 else
1459 {
1460 outputTriplet(visit, "", "[", "]");
1461 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001462 }
1463 break;
1464 case EOpIndexIndirect:
1465 // We do not currently support indirect references to interface blocks
1466 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1467 outputTriplet(visit, "", "[", "]");
1468 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001469 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001470 if (visit == InVisit)
1471 {
1472 const TStructure* structure = node->getLeft()->getType().getStruct();
1473 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1474 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001475 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001476
1477 return false;
1478 }
1479 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001480 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001481 if (visit == InVisit)
1482 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001483 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1484 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1485 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001486 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001487
1488 return false;
1489 }
1490 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001491 case EOpVectorSwizzle:
1492 if (visit == InVisit)
1493 {
1494 out << ".";
1495
1496 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1497
1498 if (swizzle)
1499 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001500 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001501
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001502 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001503 {
1504 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1505
1506 if (element)
1507 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001508 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001509
1510 switch (i)
1511 {
1512 case 0: out << "x"; break;
1513 case 1: out << "y"; break;
1514 case 2: out << "z"; break;
1515 case 3: out << "w"; break;
1516 default: UNREACHABLE();
1517 }
1518 }
1519 else UNREACHABLE();
1520 }
1521 }
1522 else UNREACHABLE();
1523
1524 return false; // Fully processed
1525 }
1526 break;
1527 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1528 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1529 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1530 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001531 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001532 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1533 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1534 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1535 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1536 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001537 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001538 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001539 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001540 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001541 if (node->getOp() == EOpEqual)
1542 {
1543 outputTriplet(visit, "(", " == ", ")");
1544 }
1545 else
1546 {
1547 outputTriplet(visit, "(", " != ", ")");
1548 }
1549 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001550 else
1551 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001552 if (visit == PreVisit && node->getOp() == EOpNotEqual)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001553 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001554 out << "!";
1555 }
1556
1557 if (node->getLeft()->getBasicType() == EbtStruct)
1558 {
1559 const TStructure &structure = *node->getLeft()->getType().getStruct();
1560 const TString &functionName = addStructEqualityFunction(structure);
Daniel Bratell29190082015-02-20 16:42:54 +01001561 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001562 }
1563 else
1564 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001565 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
1566 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001567 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001568 }
1569 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001570 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1571 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1572 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1573 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1574 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001575 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001576 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1577 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001578 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001579 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001580 if (node->getRight()->hasSideEffects())
1581 {
1582 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1583 return false;
1584 }
1585 else
1586 {
1587 outputTriplet(visit, "(", " || ", ")");
1588 return true;
1589 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001590 case EOpLogicalXor:
1591 mUsesXor = true;
1592 outputTriplet(visit, "xor(", ", ", ")");
1593 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001594 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001595 if (node->getRight()->hasSideEffects())
1596 {
1597 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1598 return false;
1599 }
1600 else
1601 {
1602 outputTriplet(visit, "(", " && ", ")");
1603 return true;
1604 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001605 default: UNREACHABLE();
1606 }
1607
1608 return true;
1609}
1610
1611bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1612{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001613 switch (node->getOp())
1614 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001615 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001616 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001617 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1618 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001619 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001620 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1621 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1622 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1623 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001624 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1625 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1626 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1627 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1628 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1629 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1630 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1631 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001632 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1633 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1634 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1635 case EOpAsinh:
1636 ASSERT(node->getUseEmulatedFunction());
1637 writeEmulatedFunctionTriplet(visit, "asinh(");
1638 break;
1639 case EOpAcosh:
1640 ASSERT(node->getUseEmulatedFunction());
1641 writeEmulatedFunctionTriplet(visit, "acosh(");
1642 break;
1643 case EOpAtanh:
1644 ASSERT(node->getUseEmulatedFunction());
1645 writeEmulatedFunctionTriplet(visit, "atanh(");
1646 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001647 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1648 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1649 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1650 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1651 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1652 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1653 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1654 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1655 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1656 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1657 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301658 case EOpIsNan: outputTriplet(visit, "isnan(", "", ")"); break;
1659 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001660 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1661 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1662 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1663 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001664 case EOpPackSnorm2x16:
1665 ASSERT(node->getUseEmulatedFunction());
1666 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1667 break;
1668 case EOpPackUnorm2x16:
1669 ASSERT(node->getUseEmulatedFunction());
1670 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1671 break;
1672 case EOpPackHalf2x16:
1673 ASSERT(node->getUseEmulatedFunction());
1674 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1675 break;
1676 case EOpUnpackSnorm2x16:
1677 ASSERT(node->getUseEmulatedFunction());
1678 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1679 break;
1680 case EOpUnpackUnorm2x16:
1681 ASSERT(node->getUseEmulatedFunction());
1682 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1683 break;
1684 case EOpUnpackHalf2x16:
1685 ASSERT(node->getUseEmulatedFunction());
1686 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1687 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001688 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1689 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001690 case EOpDFdx:
1691 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1692 {
1693 outputTriplet(visit, "(", "", ", 0.0)");
1694 }
1695 else
1696 {
1697 outputTriplet(visit, "ddx(", "", ")");
1698 }
1699 break;
1700 case EOpDFdy:
1701 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1702 {
1703 outputTriplet(visit, "(", "", ", 0.0)");
1704 }
1705 else
1706 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001707 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001708 }
1709 break;
1710 case EOpFwidth:
1711 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1712 {
1713 outputTriplet(visit, "(", "", ", 0.0)");
1714 }
1715 else
1716 {
1717 outputTriplet(visit, "fwidth(", "", ")");
1718 }
1719 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001720 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1721 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001722 case EOpInverse:
1723 ASSERT(node->getUseEmulatedFunction());
1724 writeEmulatedFunctionTriplet(visit, "inverse(");
1725 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001726
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001727 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1728 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729 default: UNREACHABLE();
1730 }
1731
1732 return true;
1733}
1734
1735bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1736{
Jamie Madill32aab012015-01-27 14:12:26 -05001737 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 switch (node->getOp())
1740 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001741 case EOpSequence:
1742 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001743 if (mInsideFunction)
1744 {
Jamie Madill075edd82013-07-08 13:30:19 -04001745 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001746 out << "{\n";
1747 }
1748
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001749 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001750 {
Jamie Madill075edd82013-07-08 13:30:19 -04001751 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001752
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001753 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001754
1755 out << ";\n";
1756 }
1757
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001758 if (mInsideFunction)
1759 {
Jamie Madill075edd82013-07-08 13:30:19 -04001760 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001761 out << "}\n";
1762 }
1763
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001764 return false;
1765 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 case EOpDeclaration:
1767 if (visit == PreVisit)
1768 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001769 TIntermSequence *sequence = node->getSequence();
1770 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001772 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001774 TStructure *structure = variable->getType().getStruct();
1775
1776 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001777 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001778 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001779 }
1780
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001781 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001782 {
Jamie Madill37997142015-01-28 10:06:34 -05001783 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784 {
Jamie Madill37997142015-01-28 10:06:34 -05001785 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001786 {
Jamie Madill37997142015-01-28 10:06:34 -05001787 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001788 }
1789
Nicolas Capensd974db42014-10-07 10:50:19 -04001790 if (!mInsideFunction)
1791 {
1792 out << "static ";
1793 }
1794
1795 out << TypeString(variable->getType()) + " ";
1796
Jamie Madill37997142015-01-28 10:06:34 -05001797 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001799 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001801 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001802 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001803 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001804 }
1805 else
1806 {
Jamie Madill37997142015-01-28 10:06:34 -05001807 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001808 }
1809
Jamie Madill37997142015-01-28 10:06:34 -05001810 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001811 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001812 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813 }
1814 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001815 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001816 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1817 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001818 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001819 }
1820 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001821 }
Jamie Madill033dae62014-06-18 12:56:28 -04001822 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001823 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001824 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001825 {
1826 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1827
1828 if (symbol)
1829 {
1830 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1831 mReferencedVaryings[symbol->getSymbol()] = symbol;
1832 }
1833 else
1834 {
1835 (*sit)->traverse(this);
1836 }
1837 }
1838 }
1839
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840 return false;
1841 }
1842 else if (visit == InVisit)
1843 {
1844 out << ", ";
1845 }
1846 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001847 case EOpInvariantDeclaration:
1848 // Do not do any translation
1849 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001850 case EOpPrototype:
1851 if (visit == PreVisit)
1852 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001853 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001854
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001855 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001856
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001857 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001858 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001859 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001860
1861 if (symbol)
1862 {
1863 out << argumentString(symbol);
1864
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001865 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001866 {
1867 out << ", ";
1868 }
1869 }
1870 else UNREACHABLE();
1871 }
1872
1873 out << ");\n";
1874
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001875 // Also prototype the Lod0 variant if needed
1876 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1877 {
1878 mOutputLod0Function = true;
1879 node->traverse(this);
1880 mOutputLod0Function = false;
1881 }
1882
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001883 return false;
1884 }
1885 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001886 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887 case EOpFunction:
1888 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001889 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001890
Jamie Madill033dae62014-06-18 12:56:28 -04001891 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001892
1893 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001895 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001897 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 {
Jamie Madill033dae62014-06-18 12:56:28 -04001899 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001900 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001901
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001902 TIntermSequence *sequence = node->getSequence();
1903 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001904
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001905 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001906 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001907 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001908
1909 if (symbol)
1910 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001911 TStructure *structure = symbol->getType().getStruct();
1912
1913 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001914 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001915 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001916 }
1917
1918 out << argumentString(symbol);
1919
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001920 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001921 {
1922 out << ", ";
1923 }
1924 }
1925 else UNREACHABLE();
1926 }
1927
1928 out << ")\n"
1929 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001930
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001931 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001932 {
1933 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001934 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001935 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001936 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001937
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001938 out << "}\n";
1939
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001940 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1941 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001942 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001943 {
1944 mOutputLod0Function = true;
1945 node->traverse(this);
1946 mOutputLod0Function = false;
1947 }
1948 }
1949
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001950 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951 }
1952 break;
1953 case EOpFunctionCall:
1954 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001955 TString name = TFunction::unmangleName(node->getName());
1956 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001957 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001958
1959 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960 {
Jamie Madill033dae62014-06-18 12:56:28 -04001961 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962 }
1963 else
1964 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001965 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001966
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001967 TextureFunction textureFunction;
1968 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001969 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04001970 textureFunction.method = TextureFunction::IMPLICIT;
1971 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001972 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001973
1974 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001975 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001976 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001977 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001978 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001979 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001980 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001981 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001982 }
Nicolas Capens46485082014-04-15 13:12:50 -04001983 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
1984 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001985 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001986 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001987 }
Nicolas Capens46485082014-04-15 13:12:50 -04001988 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001989 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001990 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001991 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001992 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04001993 else if (name == "textureSize")
1994 {
1995 textureFunction.method = TextureFunction::SIZE;
1996 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001997 else if (name == "textureOffset")
1998 {
1999 textureFunction.method = TextureFunction::IMPLICIT;
2000 textureFunction.offset = true;
2001 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002002 else if (name == "textureProjOffset")
2003 {
2004 textureFunction.method = TextureFunction::IMPLICIT;
2005 textureFunction.offset = true;
2006 textureFunction.proj = true;
2007 }
2008 else if (name == "textureLodOffset")
2009 {
2010 textureFunction.method = TextureFunction::LOD;
2011 textureFunction.offset = true;
2012 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002013 else if (name == "textureProjLodOffset")
2014 {
2015 textureFunction.method = TextureFunction::LOD;
2016 textureFunction.proj = true;
2017 textureFunction.offset = true;
2018 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002019 else if (name == "texelFetch")
2020 {
2021 textureFunction.method = TextureFunction::FETCH;
2022 }
2023 else if (name == "texelFetchOffset")
2024 {
2025 textureFunction.method = TextureFunction::FETCH;
2026 textureFunction.offset = true;
2027 }
Nicolas Capens46485082014-04-15 13:12:50 -04002028 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002029 {
2030 textureFunction.method = TextureFunction::GRAD;
2031 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002032 else if (name == "textureGradOffset")
2033 {
2034 textureFunction.method = TextureFunction::GRAD;
2035 textureFunction.offset = true;
2036 }
Nicolas Capens46485082014-04-15 13:12:50 -04002037 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002038 {
2039 textureFunction.method = TextureFunction::GRAD;
2040 textureFunction.proj = true;
2041 }
2042 else if (name == "textureProjGradOffset")
2043 {
2044 textureFunction.method = TextureFunction::GRAD;
2045 textureFunction.proj = true;
2046 textureFunction.offset = true;
2047 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002048 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002049
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002050 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002051 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002052 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2053
2054 if (textureFunction.offset)
2055 {
2056 mandatoryArgumentCount++;
2057 }
2058
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002059 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002060
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002061 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002062 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002063 if (bias)
2064 {
2065 textureFunction.method = TextureFunction::LOD0BIAS;
2066 }
2067 else
2068 {
2069 textureFunction.method = TextureFunction::LOD0;
2070 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002071 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002072 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002073 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002074 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002075 }
2076 }
2077
2078 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002079
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002080 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002082
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002083 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002084 {
2085 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2086 {
2087 out << "texture_";
2088 (*arg)->traverse(this);
2089 out << ", sampler_";
2090 }
2091
2092 (*arg)->traverse(this);
2093
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002094 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002095 {
2096 out << ", ";
2097 }
2098 }
2099
2100 out << ")";
2101
2102 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 }
2104 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002105 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002106 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2107 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2108 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2109 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2110 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2111 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2112 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2113 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2114 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2115 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2116 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2117 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2118 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2119 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2120 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2121 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2122 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2123 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2124 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002125 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002126 {
Jamie Madill033dae62014-06-18 12:56:28 -04002127 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002128 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002129 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002130 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002131 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002132 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2133 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2134 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2135 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2136 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2137 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002138 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002139 ASSERT(node->getUseEmulatedFunction());
2140 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002141 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002142 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002143 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002145 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002146 ASSERT(node->getUseEmulatedFunction());
2147 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 break;
2149 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2150 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2151 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2152 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2153 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2154 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2155 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2156 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2157 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002158 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002159 ASSERT(node->getUseEmulatedFunction());
2160 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002161 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2163 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002164 case EOpOuterProduct:
2165 ASSERT(node->getUseEmulatedFunction());
2166 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2167 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169 default: UNREACHABLE();
2170 }
2171
2172 return true;
2173}
2174
2175bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2176{
Jamie Madill32aab012015-01-27 14:12:26 -05002177 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002178
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002179 if (node->usesTernaryOperator())
2180 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002181 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002182 }
2183 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002184 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002185 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002186
Corentin Wallez80bacde2014-11-10 12:07:37 -08002187 // D3D errors when there is a gradient operation in a loop in an unflattened if
2188 // however flattening all the ifs in branch heavy shaders made D3D error too.
2189 // As a temporary workaround we flatten the ifs only if there is at least a loop
2190 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002191 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002192 {
2193 out << "FLATTEN ";
2194 }
2195
2196 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002197
2198 node->getCondition()->traverse(this);
2199
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002200 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002201
Jamie Madill075edd82013-07-08 13:30:19 -04002202 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002203 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002204
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002205 bool discard = false;
2206
daniel@transgaming.combb885322010-04-15 20:45:24 +00002207 if (node->getTrueBlock())
2208 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002209 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002210
2211 // Detect true discard
2212 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002213 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214
Jamie Madill075edd82013-07-08 13:30:19 -04002215 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002216 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002217
2218 if (node->getFalseBlock())
2219 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002220 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002221
Jamie Madill075edd82013-07-08 13:30:19 -04002222 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002223 out << "{\n";
2224
Jamie Madill075edd82013-07-08 13:30:19 -04002225 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002226 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002227
Jamie Madill075edd82013-07-08 13:30:19 -04002228 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002229 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002230
2231 // Detect false discard
2232 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2233 }
2234
2235 // ANGLE issue 486: Detect problematic conditional discard
2236 if (discard && FindSideEffectRewriting::search(node))
2237 {
2238 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002239 }
2240 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002241
2242 return false;
2243}
2244
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002245bool OutputHLSL::visitSwitch(Visit, TIntermSwitch *)
2246{
2247 UNIMPLEMENTED();
2248 return false;
2249}
2250
2251bool OutputHLSL::visitCase(Visit, TIntermCase *)
2252{
2253 UNIMPLEMENTED();
2254 return false;
2255}
2256
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2258{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002259 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002260}
2261
2262bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2263{
Nicolas Capens655fe362014-04-11 13:12:34 -04002264 mNestedLoopDepth++;
2265
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002266 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2267
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002268 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002269 {
2270 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2271 }
2272
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002273 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002274 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002275 if (handleExcessiveLoop(node))
2276 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002277 mInsideDiscontinuousLoop = wasDiscontinuous;
2278 mNestedLoopDepth--;
2279
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002280 return false;
2281 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002282 }
2283
Jamie Madill32aab012015-01-27 14:12:26 -05002284 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285
alokp@chromium.org52813552010-11-16 18:36:09 +00002286 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002288 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002289
Jamie Madill075edd82013-07-08 13:30:19 -04002290 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002291 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 }
2293 else
2294 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002295 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002296
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297 if (node->getInit())
2298 {
2299 node->getInit()->traverse(this);
2300 }
2301
2302 out << "; ";
2303
alokp@chromium.org52813552010-11-16 18:36:09 +00002304 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002306 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 }
2308
2309 out << "; ";
2310
alokp@chromium.org52813552010-11-16 18:36:09 +00002311 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002313 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002314 }
2315
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002316 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002317
Jamie Madill075edd82013-07-08 13:30:19 -04002318 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002319 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002320 }
2321
2322 if (node->getBody())
2323 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002324 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325 }
2326
Jamie Madill075edd82013-07-08 13:30:19 -04002327 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002328 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329
alokp@chromium.org52813552010-11-16 18:36:09 +00002330 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331 {
Jamie Madill075edd82013-07-08 13:30:19 -04002332 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333 out << "while(\n";
2334
alokp@chromium.org52813552010-11-16 18:36:09 +00002335 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336
daniel@transgaming.com73536982012-03-21 20:45:49 +00002337 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338 }
2339
daniel@transgaming.com73536982012-03-21 20:45:49 +00002340 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002342 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002343 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002344
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345 return false;
2346}
2347
2348bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2349{
Jamie Madill32aab012015-01-27 14:12:26 -05002350 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351
2352 switch (node->getFlowOp())
2353 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002354 case EOpKill:
2355 outputTriplet(visit, "discard;\n", "", "");
2356 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002357 case EOpBreak:
2358 if (visit == PreVisit)
2359 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002360 if (mNestedLoopDepth > 1)
2361 {
2362 mUsesNestedBreak = true;
2363 }
2364
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002365 if (mExcessiveLoopIndex)
2366 {
2367 out << "{Break";
2368 mExcessiveLoopIndex->traverse(this);
2369 out << " = true; break;}\n";
2370 }
2371 else
2372 {
2373 out << "break;\n";
2374 }
2375 }
2376 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002377 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378 case EOpReturn:
2379 if (visit == PreVisit)
2380 {
2381 if (node->getExpression())
2382 {
2383 out << "return ";
2384 }
2385 else
2386 {
2387 out << "return;\n";
2388 }
2389 }
2390 else if (visit == PostVisit)
2391 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002392 if (node->getExpression())
2393 {
2394 out << ";\n";
2395 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396 }
2397 break;
2398 default: UNREACHABLE();
2399 }
2400
2401 return true;
2402}
2403
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002404void OutputHLSL::traverseStatements(TIntermNode *node)
2405{
2406 if (isSingleStatement(node))
2407 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002408 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002409 }
2410
2411 node->traverse(this);
2412}
2413
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002414bool OutputHLSL::isSingleStatement(TIntermNode *node)
2415{
2416 TIntermAggregate *aggregate = node->getAsAggregate();
2417
2418 if (aggregate)
2419 {
2420 if (aggregate->getOp() == EOpSequence)
2421 {
2422 return false;
2423 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002424 else if (aggregate->getOp() == EOpDeclaration)
2425 {
2426 // Declaring multiple comma-separated variables must be considered multiple statements
2427 // because each individual declaration has side effects which are visible in the next.
2428 return false;
2429 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002430 else
2431 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002432 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002433 {
2434 if (!isSingleStatement(*sit))
2435 {
2436 return false;
2437 }
2438 }
2439
2440 return true;
2441 }
2442 }
2443
2444 return true;
2445}
2446
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002447// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2448// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002449bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2450{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002451 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002452 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002453
2454 // Parse loops of the form:
2455 // for(int index = initial; index [comparator] limit; index += increment)
2456 TIntermSymbol *index = NULL;
2457 TOperator comparator = EOpNull;
2458 int initial = 0;
2459 int limit = 0;
2460 int increment = 0;
2461
2462 // Parse index name and intial value
2463 if (node->getInit())
2464 {
2465 TIntermAggregate *init = node->getInit()->getAsAggregate();
2466
2467 if (init)
2468 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002469 TIntermSequence *sequence = init->getSequence();
2470 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002471
2472 if (variable && variable->getQualifier() == EvqTemporary)
2473 {
2474 TIntermBinary *assign = variable->getAsBinaryNode();
2475
2476 if (assign->getOp() == EOpInitialize)
2477 {
2478 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2479 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2480
2481 if (symbol && constant)
2482 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002483 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002484 {
2485 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002486 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002487 }
2488 }
2489 }
2490 }
2491 }
2492 }
2493
2494 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002495 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002496 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002497 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002498
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002499 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2500 {
2501 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2502
2503 if (constant)
2504 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002505 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002506 {
2507 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002508 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002509 }
2510 }
2511 }
2512 }
2513
2514 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002515 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002516 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002517 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2518 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002519
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002520 if (binaryTerminal)
2521 {
2522 TOperator op = binaryTerminal->getOp();
2523 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2524
2525 if (constant)
2526 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002527 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002528 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002529 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002530
2531 switch (op)
2532 {
2533 case EOpAddAssign: increment = value; break;
2534 case EOpSubAssign: increment = -value; break;
2535 default: UNIMPLEMENTED();
2536 }
2537 }
2538 }
2539 }
2540 else if (unaryTerminal)
2541 {
2542 TOperator op = unaryTerminal->getOp();
2543
2544 switch (op)
2545 {
2546 case EOpPostIncrement: increment = 1; break;
2547 case EOpPostDecrement: increment = -1; break;
2548 case EOpPreIncrement: increment = 1; break;
2549 case EOpPreDecrement: increment = -1; break;
2550 default: UNIMPLEMENTED();
2551 }
2552 }
2553 }
2554
2555 if (index != NULL && comparator != EOpNull && increment != 0)
2556 {
2557 if (comparator == EOpLessThanEqual)
2558 {
2559 comparator = EOpLessThan;
2560 limit += 1;
2561 }
2562
2563 if (comparator == EOpLessThan)
2564 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002565 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002566
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002567 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002568 {
2569 return false; // Not an excessive loop
2570 }
2571
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002572 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2573 mExcessiveLoopIndex = index;
2574
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002575 out << "{int ";
2576 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002577 out << ";\n"
2578 "bool Break";
2579 index->traverse(this);
2580 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002581
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002582 bool firstLoopFragment = true;
2583
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002584 while (iterations > 0)
2585 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002586 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002587
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002588 if (!firstLoopFragment)
2589 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002590 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002591 index->traverse(this);
2592 out << ") {\n";
2593 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002594
2595 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2596 {
2597 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2598 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002599
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002600 // for(int index = initial; index < clampedLimit; index += increment)
2601
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002602 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002603 index->traverse(this);
2604 out << " = ";
2605 out << initial;
2606
2607 out << "; ";
2608 index->traverse(this);
2609 out << " < ";
2610 out << clampedLimit;
2611
2612 out << "; ";
2613 index->traverse(this);
2614 out << " += ";
2615 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002616 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002617
Jamie Madill075edd82013-07-08 13:30:19 -04002618 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002619 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002620
2621 if (node->getBody())
2622 {
2623 node->getBody()->traverse(this);
2624 }
2625
Jamie Madill075edd82013-07-08 13:30:19 -04002626 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002627 out << ";}\n";
2628
2629 if (!firstLoopFragment)
2630 {
2631 out << "}\n";
2632 }
2633
2634 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002635
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002636 initial += MAX_LOOP_ITERATIONS * increment;
2637 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002638 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002639
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002640 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002641
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002642 mExcessiveLoopIndex = restoreIndex;
2643
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002644 return true;
2645 }
2646 else UNIMPLEMENTED();
2647 }
2648
2649 return false; // Not handled as an excessive loop
2650}
2651
Daniel Bratell29190082015-02-20 16:42:54 +01002652void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002653{
Jamie Madill32aab012015-01-27 14:12:26 -05002654 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002655
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002656 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002657 {
2658 out << preString;
2659 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002660 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661 {
2662 out << inString;
2663 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002664 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002665 {
2666 out << postString;
2667 }
2668}
2669
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002670void OutputHLSL::outputLineDirective(int line)
2671{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002672 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002673 {
Jamie Madill32aab012015-01-27 14:12:26 -05002674 TInfoSinkBase &out = getInfoSink();
2675
2676 out << "\n";
2677 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002678
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002679 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002680 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002681 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002682 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002683
Jamie Madill32aab012015-01-27 14:12:26 -05002684 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002685 }
2686}
2687
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002688TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2689{
2690 TQualifier qualifier = symbol->getQualifier();
2691 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002692 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002693
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002694 if (name.empty()) // HLSL demands named arguments, also for prototypes
2695 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002696 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002697 }
2698 else
2699 {
Jamie Madill033dae62014-06-18 12:56:28 -04002700 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002701 }
2702
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002703 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2704 {
Jamie Madill033dae62014-06-18 12:56:28 -04002705 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002706 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002707 }
2708
Jamie Madill033dae62014-06-18 12:56:28 -04002709 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002710}
2711
2712TString OutputHLSL::initializer(const TType &type)
2713{
2714 TString string;
2715
Jamie Madill94bf7f22013-07-08 13:31:15 -04002716 size_t size = type.getObjectSize();
2717 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002718 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002719 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720
Jamie Madill94bf7f22013-07-08 13:31:15 -04002721 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002722 {
2723 string += ", ";
2724 }
2725 }
2726
daniel@transgaming.comead23042010-04-29 03:35:36 +00002727 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002728}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002729
Daniel Bratell29190082015-02-20 16:42:54 +01002730void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002731{
Jamie Madill32aab012015-01-27 14:12:26 -05002732 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002733
2734 if (visit == PreVisit)
2735 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002736 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002737
Daniel Bratell29190082015-02-20 16:42:54 +01002738 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002739 }
2740 else if (visit == InVisit)
2741 {
2742 out << ", ";
2743 }
2744 else if (visit == PostVisit)
2745 {
2746 out << ")";
2747 }
2748}
2749
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002750const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2751{
Jamie Madill32aab012015-01-27 14:12:26 -05002752 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002753
Jamie Madill98493dd2013-07-08 14:39:03 -04002754 const TStructure* structure = type.getStruct();
2755 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002756 {
Jamie Madill033dae62014-06-18 12:56:28 -04002757 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002758
Jamie Madill98493dd2013-07-08 14:39:03 -04002759 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002760
Jamie Madill98493dd2013-07-08 14:39:03 -04002761 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002762 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002763 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002764 constUnion = writeConstantUnion(*fieldType, constUnion);
2765
Jamie Madill98493dd2013-07-08 14:39:03 -04002766 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002767 {
2768 out << ", ";
2769 }
2770 }
2771
2772 out << ")";
2773 }
2774 else
2775 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002776 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002777 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002778
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002779 if (writeType)
2780 {
Jamie Madill033dae62014-06-18 12:56:28 -04002781 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002782 }
2783
Jamie Madill94bf7f22013-07-08 13:31:15 -04002784 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002785 {
2786 switch (constUnion->getType())
2787 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002788 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002789 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002790 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002791 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002792 default: UNREACHABLE();
2793 }
2794
2795 if (i != size - 1)
2796 {
2797 out << ", ";
2798 }
2799 }
2800
2801 if (writeType)
2802 {
2803 out << ")";
2804 }
2805 }
2806
2807 return constUnion;
2808}
2809
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002810void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2811{
2812 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2813 outputTriplet(visit, preString.c_str(), ", ", ")");
2814}
2815
Jamie Madill37997142015-01-28 10:06:34 -05002816bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2817{
2818 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2819 expression->traverse(&searchSymbol);
2820
2821 if (searchSymbol.foundMatch())
2822 {
2823 // Type already printed
2824 out << "t" + str(mUniqueIndex) + " = ";
2825 expression->traverse(this);
2826 out << ", ";
2827 symbolNode->traverse(this);
2828 out << " = t" + str(mUniqueIndex);
2829
2830 mUniqueIndex++;
2831 return true;
2832 }
2833
2834 return false;
2835}
2836
2837void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2838{
2839 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2840 << "\n"
2841 << "void initializeDeferredGlobals()\n"
2842 << "{\n";
2843
2844 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2845 {
2846 TIntermSymbol *symbol = deferredGlobal.first;
2847 TIntermTyped *expression = deferredGlobal.second;
2848 ASSERT(symbol);
2849 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2850
2851 out << " " << Decorate(symbol->getSymbol()) << " = ";
2852
2853 if (!writeSameSymbolInitializer(out, symbol, expression))
2854 {
2855 ASSERT(mInfoSinkStack.top() == &out);
2856 expression->traverse(this);
2857 }
2858
2859 out << ";\n";
2860 }
2861
2862 out << "}\n"
2863 << "\n";
2864}
2865
Jamie Madill55e79e02015-02-09 15:35:00 -05002866TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2867{
2868 const TFieldList &fields = structure.fields();
2869
2870 for (const auto &eqFunction : mStructEqualityFunctions)
2871 {
2872 if (eqFunction.structure == &structure)
2873 {
2874 return eqFunction.functionName;
2875 }
2876 }
2877
2878 const TString &structNameString = StructNameString(structure);
2879
2880 StructEqualityFunction function;
2881 function.structure = &structure;
2882 function.functionName = "angle_eq_" + structNameString;
2883
2884 TString &func = function.functionDefinition;
2885
2886 func = "bool " + function.functionName + "(" + structNameString + " a, " + structNameString + " b)\n" +
2887 "{\n"
2888 " return ";
2889
2890 for (size_t i = 0; i < fields.size(); i++)
2891 {
2892 const TField *field = fields[i];
2893 const TType *fieldType = field->type();
2894
2895 const TString &fieldNameA = "a." + Decorate(field->name());
2896 const TString &fieldNameB = "b." + Decorate(field->name());
2897
2898 if (i > 0)
2899 {
2900 func += " && ";
2901 }
2902
2903 if (fieldType->getBasicType() == EbtStruct)
2904 {
2905 const TStructure &fieldStruct = *fieldType->getStruct();
2906 const TString &functionName = addStructEqualityFunction(fieldStruct);
2907 func += functionName + "(" + fieldNameA + ", " + fieldNameB + ")";
2908 }
2909 else if (fieldType->isScalar())
2910 {
2911 func += "(" + fieldNameA + " == " + fieldNameB + ")";
2912 }
2913 else
2914 {
2915 ASSERT(fieldType->isMatrix() || fieldType->isVector());
2916 func += "all(" + fieldNameA + " == " + fieldNameB + ")";
2917 }
2918 }
2919
2920 func = func + ";\n" + "}\n";
2921
2922 mStructEqualityFunctions.push_back(function);
2923
2924 return function.functionName;
2925}
2926
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002927}