blob: 045ea4c0833789e2b803a6e4e0865969bb1d8141 [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);
1561 outputTriplet(visit, functionName + "(", ", ", ")");
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;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001658 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1659 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1660 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1661 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001662 case EOpPackSnorm2x16:
1663 ASSERT(node->getUseEmulatedFunction());
1664 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1665 break;
1666 case EOpPackUnorm2x16:
1667 ASSERT(node->getUseEmulatedFunction());
1668 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1669 break;
1670 case EOpPackHalf2x16:
1671 ASSERT(node->getUseEmulatedFunction());
1672 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1673 break;
1674 case EOpUnpackSnorm2x16:
1675 ASSERT(node->getUseEmulatedFunction());
1676 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1677 break;
1678 case EOpUnpackUnorm2x16:
1679 ASSERT(node->getUseEmulatedFunction());
1680 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1681 break;
1682 case EOpUnpackHalf2x16:
1683 ASSERT(node->getUseEmulatedFunction());
1684 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1685 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001686 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1687 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001688 case EOpDFdx:
1689 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1690 {
1691 outputTriplet(visit, "(", "", ", 0.0)");
1692 }
1693 else
1694 {
1695 outputTriplet(visit, "ddx(", "", ")");
1696 }
1697 break;
1698 case EOpDFdy:
1699 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1700 {
1701 outputTriplet(visit, "(", "", ", 0.0)");
1702 }
1703 else
1704 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001705 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001706 }
1707 break;
1708 case EOpFwidth:
1709 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1710 {
1711 outputTriplet(visit, "(", "", ", 0.0)");
1712 }
1713 else
1714 {
1715 outputTriplet(visit, "fwidth(", "", ")");
1716 }
1717 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001718 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1719 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001720 case EOpInverse:
1721 ASSERT(node->getUseEmulatedFunction());
1722 writeEmulatedFunctionTriplet(visit, "inverse(");
1723 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001724
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001725 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1726 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 default: UNREACHABLE();
1728 }
1729
1730 return true;
1731}
1732
1733bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1734{
Jamie Madill32aab012015-01-27 14:12:26 -05001735 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737 switch (node->getOp())
1738 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001739 case EOpSequence:
1740 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001741 if (mInsideFunction)
1742 {
Jamie Madill075edd82013-07-08 13:30:19 -04001743 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001744 out << "{\n";
1745 }
1746
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001747 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001748 {
Jamie Madill075edd82013-07-08 13:30:19 -04001749 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001750
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001751 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001752
1753 out << ";\n";
1754 }
1755
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001756 if (mInsideFunction)
1757 {
Jamie Madill075edd82013-07-08 13:30:19 -04001758 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001759 out << "}\n";
1760 }
1761
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001762 return false;
1763 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 case EOpDeclaration:
1765 if (visit == PreVisit)
1766 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001767 TIntermSequence *sequence = node->getSequence();
1768 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001770 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001772 TStructure *structure = variable->getType().getStruct();
1773
1774 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001775 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001776 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001777 }
1778
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001779 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001780 {
Jamie Madill37997142015-01-28 10:06:34 -05001781 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001782 {
Jamie Madill37997142015-01-28 10:06:34 -05001783 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001784 {
Jamie Madill37997142015-01-28 10:06:34 -05001785 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001786 }
1787
Nicolas Capensd974db42014-10-07 10:50:19 -04001788 if (!mInsideFunction)
1789 {
1790 out << "static ";
1791 }
1792
1793 out << TypeString(variable->getType()) + " ";
1794
Jamie Madill37997142015-01-28 10:06:34 -05001795 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001796
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001797 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001799 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001800 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001801 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001802 }
1803 else
1804 {
Jamie Madill37997142015-01-28 10:06:34 -05001805 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001806 }
1807
Jamie Madill37997142015-01-28 10:06:34 -05001808 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001809 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001810 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811 }
1812 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001814 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1815 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001816 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001817 }
1818 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819 }
Jamie Madill033dae62014-06-18 12:56:28 -04001820 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001821 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001822 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001823 {
1824 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1825
1826 if (symbol)
1827 {
1828 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1829 mReferencedVaryings[symbol->getSymbol()] = symbol;
1830 }
1831 else
1832 {
1833 (*sit)->traverse(this);
1834 }
1835 }
1836 }
1837
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838 return false;
1839 }
1840 else if (visit == InVisit)
1841 {
1842 out << ", ";
1843 }
1844 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001845 case EOpInvariantDeclaration:
1846 // Do not do any translation
1847 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001848 case EOpPrototype:
1849 if (visit == PreVisit)
1850 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001851 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001852
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001853 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001854
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001855 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001856 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001857 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001858
1859 if (symbol)
1860 {
1861 out << argumentString(symbol);
1862
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001863 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001864 {
1865 out << ", ";
1866 }
1867 }
1868 else UNREACHABLE();
1869 }
1870
1871 out << ");\n";
1872
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001873 // Also prototype the Lod0 variant if needed
1874 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1875 {
1876 mOutputLod0Function = true;
1877 node->traverse(this);
1878 mOutputLod0Function = false;
1879 }
1880
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001881 return false;
1882 }
1883 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001884 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885 case EOpFunction:
1886 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001887 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888
Jamie Madill033dae62014-06-18 12:56:28 -04001889 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001890
1891 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001893 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001895 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896 {
Jamie Madill033dae62014-06-18 12:56:28 -04001897 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001898 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001899
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001900 TIntermSequence *sequence = node->getSequence();
1901 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001902
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001903 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001904 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001905 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001906
1907 if (symbol)
1908 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001909 TStructure *structure = symbol->getType().getStruct();
1910
1911 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001912 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001913 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001914 }
1915
1916 out << argumentString(symbol);
1917
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001918 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001919 {
1920 out << ", ";
1921 }
1922 }
1923 else UNREACHABLE();
1924 }
1925
1926 out << ")\n"
1927 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001928
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001929 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001930 {
1931 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001932 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001933 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001934 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001935
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001936 out << "}\n";
1937
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001938 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1939 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001940 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001941 {
1942 mOutputLod0Function = true;
1943 node->traverse(this);
1944 mOutputLod0Function = false;
1945 }
1946 }
1947
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001948 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949 }
1950 break;
1951 case EOpFunctionCall:
1952 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001953 TString name = TFunction::unmangleName(node->getName());
1954 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001955 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001956
1957 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001958 {
Jamie Madill033dae62014-06-18 12:56:28 -04001959 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960 }
1961 else
1962 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001963 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001964
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001965 TextureFunction textureFunction;
1966 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001967 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04001968 textureFunction.method = TextureFunction::IMPLICIT;
1969 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001970 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001971
1972 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001973 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001974 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001975 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001976 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001977 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001978 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001979 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001980 }
Nicolas Capens46485082014-04-15 13:12:50 -04001981 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
1982 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001983 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001984 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001985 }
Nicolas Capens46485082014-04-15 13:12:50 -04001986 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001987 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001988 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001989 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001990 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04001991 else if (name == "textureSize")
1992 {
1993 textureFunction.method = TextureFunction::SIZE;
1994 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001995 else if (name == "textureOffset")
1996 {
1997 textureFunction.method = TextureFunction::IMPLICIT;
1998 textureFunction.offset = true;
1999 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002000 else if (name == "textureProjOffset")
2001 {
2002 textureFunction.method = TextureFunction::IMPLICIT;
2003 textureFunction.offset = true;
2004 textureFunction.proj = true;
2005 }
2006 else if (name == "textureLodOffset")
2007 {
2008 textureFunction.method = TextureFunction::LOD;
2009 textureFunction.offset = true;
2010 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002011 else if (name == "textureProjLodOffset")
2012 {
2013 textureFunction.method = TextureFunction::LOD;
2014 textureFunction.proj = true;
2015 textureFunction.offset = true;
2016 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002017 else if (name == "texelFetch")
2018 {
2019 textureFunction.method = TextureFunction::FETCH;
2020 }
2021 else if (name == "texelFetchOffset")
2022 {
2023 textureFunction.method = TextureFunction::FETCH;
2024 textureFunction.offset = true;
2025 }
Nicolas Capens46485082014-04-15 13:12:50 -04002026 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002027 {
2028 textureFunction.method = TextureFunction::GRAD;
2029 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002030 else if (name == "textureGradOffset")
2031 {
2032 textureFunction.method = TextureFunction::GRAD;
2033 textureFunction.offset = true;
2034 }
Nicolas Capens46485082014-04-15 13:12:50 -04002035 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002036 {
2037 textureFunction.method = TextureFunction::GRAD;
2038 textureFunction.proj = true;
2039 }
2040 else if (name == "textureProjGradOffset")
2041 {
2042 textureFunction.method = TextureFunction::GRAD;
2043 textureFunction.proj = true;
2044 textureFunction.offset = true;
2045 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002046 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002047
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002048 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002049 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002050 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2051
2052 if (textureFunction.offset)
2053 {
2054 mandatoryArgumentCount++;
2055 }
2056
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002057 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002058
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002059 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002060 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002061 if (bias)
2062 {
2063 textureFunction.method = TextureFunction::LOD0BIAS;
2064 }
2065 else
2066 {
2067 textureFunction.method = TextureFunction::LOD0;
2068 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002069 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002070 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002071 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002072 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002073 }
2074 }
2075
2076 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002077
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002078 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002080
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002081 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002082 {
2083 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2084 {
2085 out << "texture_";
2086 (*arg)->traverse(this);
2087 out << ", sampler_";
2088 }
2089
2090 (*arg)->traverse(this);
2091
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002092 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002093 {
2094 out << ", ";
2095 }
2096 }
2097
2098 out << ")";
2099
2100 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101 }
2102 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002103 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002104 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2105 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2106 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2107 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2108 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2109 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2110 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2111 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2112 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2113 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2114 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2115 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2116 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2117 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2118 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2119 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2120 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2121 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2122 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002123 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002124 {
Jamie Madill033dae62014-06-18 12:56:28 -04002125 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002126 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002127 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2128 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002129 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002130 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2131 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2132 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2133 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2134 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2135 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002136 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002137 ASSERT(node->getUseEmulatedFunction());
2138 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002139 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002140 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002142 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002143 ASSERT(node->getUseEmulatedFunction());
2144 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145 break;
2146 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2147 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2148 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2149 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2150 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2151 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2152 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2153 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2154 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002155 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002156 ASSERT(node->getUseEmulatedFunction());
2157 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002158 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002159 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2160 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002161 case EOpOuterProduct:
2162 ASSERT(node->getUseEmulatedFunction());
2163 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2164 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002165 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 default: UNREACHABLE();
2167 }
2168
2169 return true;
2170}
2171
2172bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2173{
Jamie Madill32aab012015-01-27 14:12:26 -05002174 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002176 if (node->usesTernaryOperator())
2177 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002178 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002179 }
2180 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002182 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002183
Corentin Wallez80bacde2014-11-10 12:07:37 -08002184 // D3D errors when there is a gradient operation in a loop in an unflattened if
2185 // however flattening all the ifs in branch heavy shaders made D3D error too.
2186 // As a temporary workaround we flatten the ifs only if there is at least a loop
2187 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002188 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002189 {
2190 out << "FLATTEN ";
2191 }
2192
2193 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002194
2195 node->getCondition()->traverse(this);
2196
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002197 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002198
Jamie Madill075edd82013-07-08 13:30:19 -04002199 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002200 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002202 bool discard = false;
2203
daniel@transgaming.combb885322010-04-15 20:45:24 +00002204 if (node->getTrueBlock())
2205 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002206 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002207
2208 // Detect true discard
2209 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002210 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211
Jamie Madill075edd82013-07-08 13:30:19 -04002212 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002213 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002214
2215 if (node->getFalseBlock())
2216 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002217 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002218
Jamie Madill075edd82013-07-08 13:30:19 -04002219 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002220 out << "{\n";
2221
Jamie Madill075edd82013-07-08 13:30:19 -04002222 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002223 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002224
Jamie Madill075edd82013-07-08 13:30:19 -04002225 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002226 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002227
2228 // Detect false discard
2229 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2230 }
2231
2232 // ANGLE issue 486: Detect problematic conditional discard
2233 if (discard && FindSideEffectRewriting::search(node))
2234 {
2235 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002236 }
2237 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238
2239 return false;
2240}
2241
2242void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2243{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002244 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245}
2246
2247bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2248{
Nicolas Capens655fe362014-04-11 13:12:34 -04002249 mNestedLoopDepth++;
2250
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002251 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2252
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002253 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002254 {
2255 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2256 }
2257
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002258 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002259 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002260 if (handleExcessiveLoop(node))
2261 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002262 mInsideDiscontinuousLoop = wasDiscontinuous;
2263 mNestedLoopDepth--;
2264
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002265 return false;
2266 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002267 }
2268
Jamie Madill32aab012015-01-27 14:12:26 -05002269 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002270
alokp@chromium.org52813552010-11-16 18:36:09 +00002271 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002273 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002274
Jamie Madill075edd82013-07-08 13:30:19 -04002275 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002276 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002277 }
2278 else
2279 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002280 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002281
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282 if (node->getInit())
2283 {
2284 node->getInit()->traverse(this);
2285 }
2286
2287 out << "; ";
2288
alokp@chromium.org52813552010-11-16 18:36:09 +00002289 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002291 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 }
2293
2294 out << "; ";
2295
alokp@chromium.org52813552010-11-16 18:36:09 +00002296 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002298 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 }
2300
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002301 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002302
Jamie Madill075edd82013-07-08 13:30:19 -04002303 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002304 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 }
2306
2307 if (node->getBody())
2308 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002309 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 }
2311
Jamie Madill075edd82013-07-08 13:30:19 -04002312 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002313 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002314
alokp@chromium.org52813552010-11-16 18:36:09 +00002315 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316 {
Jamie Madill075edd82013-07-08 13:30:19 -04002317 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318 out << "while(\n";
2319
alokp@chromium.org52813552010-11-16 18:36:09 +00002320 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321
daniel@transgaming.com73536982012-03-21 20:45:49 +00002322 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323 }
2324
daniel@transgaming.com73536982012-03-21 20:45:49 +00002325 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002327 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002328 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002329
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330 return false;
2331}
2332
2333bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2334{
Jamie Madill32aab012015-01-27 14:12:26 -05002335 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336
2337 switch (node->getFlowOp())
2338 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002339 case EOpKill:
2340 outputTriplet(visit, "discard;\n", "", "");
2341 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002342 case EOpBreak:
2343 if (visit == PreVisit)
2344 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002345 if (mNestedLoopDepth > 1)
2346 {
2347 mUsesNestedBreak = true;
2348 }
2349
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002350 if (mExcessiveLoopIndex)
2351 {
2352 out << "{Break";
2353 mExcessiveLoopIndex->traverse(this);
2354 out << " = true; break;}\n";
2355 }
2356 else
2357 {
2358 out << "break;\n";
2359 }
2360 }
2361 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002362 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 case EOpReturn:
2364 if (visit == PreVisit)
2365 {
2366 if (node->getExpression())
2367 {
2368 out << "return ";
2369 }
2370 else
2371 {
2372 out << "return;\n";
2373 }
2374 }
2375 else if (visit == PostVisit)
2376 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002377 if (node->getExpression())
2378 {
2379 out << ";\n";
2380 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 }
2382 break;
2383 default: UNREACHABLE();
2384 }
2385
2386 return true;
2387}
2388
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002389void OutputHLSL::traverseStatements(TIntermNode *node)
2390{
2391 if (isSingleStatement(node))
2392 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002393 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002394 }
2395
2396 node->traverse(this);
2397}
2398
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002399bool OutputHLSL::isSingleStatement(TIntermNode *node)
2400{
2401 TIntermAggregate *aggregate = node->getAsAggregate();
2402
2403 if (aggregate)
2404 {
2405 if (aggregate->getOp() == EOpSequence)
2406 {
2407 return false;
2408 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002409 else if (aggregate->getOp() == EOpDeclaration)
2410 {
2411 // Declaring multiple comma-separated variables must be considered multiple statements
2412 // because each individual declaration has side effects which are visible in the next.
2413 return false;
2414 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002415 else
2416 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002417 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002418 {
2419 if (!isSingleStatement(*sit))
2420 {
2421 return false;
2422 }
2423 }
2424
2425 return true;
2426 }
2427 }
2428
2429 return true;
2430}
2431
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002432// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2433// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002434bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2435{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002436 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002437 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002438
2439 // Parse loops of the form:
2440 // for(int index = initial; index [comparator] limit; index += increment)
2441 TIntermSymbol *index = NULL;
2442 TOperator comparator = EOpNull;
2443 int initial = 0;
2444 int limit = 0;
2445 int increment = 0;
2446
2447 // Parse index name and intial value
2448 if (node->getInit())
2449 {
2450 TIntermAggregate *init = node->getInit()->getAsAggregate();
2451
2452 if (init)
2453 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002454 TIntermSequence *sequence = init->getSequence();
2455 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002456
2457 if (variable && variable->getQualifier() == EvqTemporary)
2458 {
2459 TIntermBinary *assign = variable->getAsBinaryNode();
2460
2461 if (assign->getOp() == EOpInitialize)
2462 {
2463 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2464 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2465
2466 if (symbol && constant)
2467 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002468 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002469 {
2470 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002471 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002472 }
2473 }
2474 }
2475 }
2476 }
2477 }
2478
2479 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002480 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002481 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002482 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002483
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002484 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2485 {
2486 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2487
2488 if (constant)
2489 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002490 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002491 {
2492 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002493 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002494 }
2495 }
2496 }
2497 }
2498
2499 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002500 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002501 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002502 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2503 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002504
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002505 if (binaryTerminal)
2506 {
2507 TOperator op = binaryTerminal->getOp();
2508 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2509
2510 if (constant)
2511 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002512 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002513 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002514 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002515
2516 switch (op)
2517 {
2518 case EOpAddAssign: increment = value; break;
2519 case EOpSubAssign: increment = -value; break;
2520 default: UNIMPLEMENTED();
2521 }
2522 }
2523 }
2524 }
2525 else if (unaryTerminal)
2526 {
2527 TOperator op = unaryTerminal->getOp();
2528
2529 switch (op)
2530 {
2531 case EOpPostIncrement: increment = 1; break;
2532 case EOpPostDecrement: increment = -1; break;
2533 case EOpPreIncrement: increment = 1; break;
2534 case EOpPreDecrement: increment = -1; break;
2535 default: UNIMPLEMENTED();
2536 }
2537 }
2538 }
2539
2540 if (index != NULL && comparator != EOpNull && increment != 0)
2541 {
2542 if (comparator == EOpLessThanEqual)
2543 {
2544 comparator = EOpLessThan;
2545 limit += 1;
2546 }
2547
2548 if (comparator == EOpLessThan)
2549 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002550 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002551
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002552 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002553 {
2554 return false; // Not an excessive loop
2555 }
2556
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002557 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2558 mExcessiveLoopIndex = index;
2559
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002560 out << "{int ";
2561 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002562 out << ";\n"
2563 "bool Break";
2564 index->traverse(this);
2565 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002566
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002567 bool firstLoopFragment = true;
2568
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002569 while (iterations > 0)
2570 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002571 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002572
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002573 if (!firstLoopFragment)
2574 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002575 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002576 index->traverse(this);
2577 out << ") {\n";
2578 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002579
2580 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2581 {
2582 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2583 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002584
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002585 // for(int index = initial; index < clampedLimit; index += increment)
2586
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002587 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002588 index->traverse(this);
2589 out << " = ";
2590 out << initial;
2591
2592 out << "; ";
2593 index->traverse(this);
2594 out << " < ";
2595 out << clampedLimit;
2596
2597 out << "; ";
2598 index->traverse(this);
2599 out << " += ";
2600 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002601 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002602
Jamie Madill075edd82013-07-08 13:30:19 -04002603 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002604 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002605
2606 if (node->getBody())
2607 {
2608 node->getBody()->traverse(this);
2609 }
2610
Jamie Madill075edd82013-07-08 13:30:19 -04002611 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002612 out << ";}\n";
2613
2614 if (!firstLoopFragment)
2615 {
2616 out << "}\n";
2617 }
2618
2619 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002620
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002621 initial += MAX_LOOP_ITERATIONS * increment;
2622 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002623 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002624
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002625 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002626
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002627 mExcessiveLoopIndex = restoreIndex;
2628
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002629 return true;
2630 }
2631 else UNIMPLEMENTED();
2632 }
2633
2634 return false; // Not handled as an excessive loop
2635}
2636
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002637void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002638{
Jamie Madill32aab012015-01-27 14:12:26 -05002639 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002640
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002641 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002642 {
2643 out << preString;
2644 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002645 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646 {
2647 out << inString;
2648 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002649 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002650 {
2651 out << postString;
2652 }
2653}
2654
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002655void OutputHLSL::outputLineDirective(int line)
2656{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002657 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002658 {
Jamie Madill32aab012015-01-27 14:12:26 -05002659 TInfoSinkBase &out = getInfoSink();
2660
2661 out << "\n";
2662 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002663
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002664 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002665 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002666 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002667 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002668
Jamie Madill32aab012015-01-27 14:12:26 -05002669 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002670 }
2671}
2672
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002673TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2674{
2675 TQualifier qualifier = symbol->getQualifier();
2676 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002677 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002678
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002679 if (name.empty()) // HLSL demands named arguments, also for prototypes
2680 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002681 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002682 }
2683 else
2684 {
Jamie Madill033dae62014-06-18 12:56:28 -04002685 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002686 }
2687
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002688 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2689 {
Jamie Madill033dae62014-06-18 12:56:28 -04002690 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002691 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002692 }
2693
Jamie Madill033dae62014-06-18 12:56:28 -04002694 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002695}
2696
2697TString OutputHLSL::initializer(const TType &type)
2698{
2699 TString string;
2700
Jamie Madill94bf7f22013-07-08 13:31:15 -04002701 size_t size = type.getObjectSize();
2702 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002704 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705
Jamie Madill94bf7f22013-07-08 13:31:15 -04002706 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707 {
2708 string += ", ";
2709 }
2710 }
2711
daniel@transgaming.comead23042010-04-29 03:35:36 +00002712 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002714
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002715void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2716{
Jamie Madill32aab012015-01-27 14:12:26 -05002717 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002718
2719 if (visit == PreVisit)
2720 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002721 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002722
2723 out << name + "(";
2724 }
2725 else if (visit == InVisit)
2726 {
2727 out << ", ";
2728 }
2729 else if (visit == PostVisit)
2730 {
2731 out << ")";
2732 }
2733}
2734
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002735const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2736{
Jamie Madill32aab012015-01-27 14:12:26 -05002737 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002738
Jamie Madill98493dd2013-07-08 14:39:03 -04002739 const TStructure* structure = type.getStruct();
2740 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002741 {
Jamie Madill033dae62014-06-18 12:56:28 -04002742 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002743
Jamie Madill98493dd2013-07-08 14:39:03 -04002744 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002745
Jamie Madill98493dd2013-07-08 14:39:03 -04002746 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002747 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002748 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002749 constUnion = writeConstantUnion(*fieldType, constUnion);
2750
Jamie Madill98493dd2013-07-08 14:39:03 -04002751 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002752 {
2753 out << ", ";
2754 }
2755 }
2756
2757 out << ")";
2758 }
2759 else
2760 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002761 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002762 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002763
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002764 if (writeType)
2765 {
Jamie Madill033dae62014-06-18 12:56:28 -04002766 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002767 }
2768
Jamie Madill94bf7f22013-07-08 13:31:15 -04002769 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002770 {
2771 switch (constUnion->getType())
2772 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002773 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002774 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002775 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002776 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002777 default: UNREACHABLE();
2778 }
2779
2780 if (i != size - 1)
2781 {
2782 out << ", ";
2783 }
2784 }
2785
2786 if (writeType)
2787 {
2788 out << ")";
2789 }
2790 }
2791
2792 return constUnion;
2793}
2794
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002795void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2796{
2797 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2798 outputTriplet(visit, preString.c_str(), ", ", ")");
2799}
2800
Jamie Madill37997142015-01-28 10:06:34 -05002801bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2802{
2803 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2804 expression->traverse(&searchSymbol);
2805
2806 if (searchSymbol.foundMatch())
2807 {
2808 // Type already printed
2809 out << "t" + str(mUniqueIndex) + " = ";
2810 expression->traverse(this);
2811 out << ", ";
2812 symbolNode->traverse(this);
2813 out << " = t" + str(mUniqueIndex);
2814
2815 mUniqueIndex++;
2816 return true;
2817 }
2818
2819 return false;
2820}
2821
2822void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2823{
2824 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2825 << "\n"
2826 << "void initializeDeferredGlobals()\n"
2827 << "{\n";
2828
2829 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2830 {
2831 TIntermSymbol *symbol = deferredGlobal.first;
2832 TIntermTyped *expression = deferredGlobal.second;
2833 ASSERT(symbol);
2834 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2835
2836 out << " " << Decorate(symbol->getSymbol()) << " = ";
2837
2838 if (!writeSameSymbolInitializer(out, symbol, expression))
2839 {
2840 ASSERT(mInfoSinkStack.top() == &out);
2841 expression->traverse(this);
2842 }
2843
2844 out << ";\n";
2845 }
2846
2847 out << "}\n"
2848 << "\n";
2849}
2850
Jamie Madill55e79e02015-02-09 15:35:00 -05002851TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2852{
2853 const TFieldList &fields = structure.fields();
2854
2855 for (const auto &eqFunction : mStructEqualityFunctions)
2856 {
2857 if (eqFunction.structure == &structure)
2858 {
2859 return eqFunction.functionName;
2860 }
2861 }
2862
2863 const TString &structNameString = StructNameString(structure);
2864
2865 StructEqualityFunction function;
2866 function.structure = &structure;
2867 function.functionName = "angle_eq_" + structNameString;
2868
2869 TString &func = function.functionDefinition;
2870
2871 func = "bool " + function.functionName + "(" + structNameString + " a, " + structNameString + " b)\n" +
2872 "{\n"
2873 " return ";
2874
2875 for (size_t i = 0; i < fields.size(); i++)
2876 {
2877 const TField *field = fields[i];
2878 const TType *fieldType = field->type();
2879
2880 const TString &fieldNameA = "a." + Decorate(field->name());
2881 const TString &fieldNameB = "b." + Decorate(field->name());
2882
2883 if (i > 0)
2884 {
2885 func += " && ";
2886 }
2887
2888 if (fieldType->getBasicType() == EbtStruct)
2889 {
2890 const TStructure &fieldStruct = *fieldType->getStruct();
2891 const TString &functionName = addStructEqualityFunction(fieldStruct);
2892 func += functionName + "(" + fieldNameA + ", " + fieldNameB + ")";
2893 }
2894 else if (fieldType->isScalar())
2895 {
2896 func += "(" + fieldNameA + " == " + fieldNameB + ")";
2897 }
2898 else
2899 {
2900 ASSERT(fieldType->isMatrix() || fieldType->isVector());
2901 func += "all(" + fieldNameA + " == " + fieldNameB + ")";
2902 }
2903 }
2904
2905 func = func + ";\n" + "}\n";
2906
2907 mStructEqualityFunctions.push_back(function);
2908
2909 return function.functionName;
2910}
2911
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002912}