blob: 03aab78987fb8548193dc9dc832d893e611042a4 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
14#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020015#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050016#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/FlagStd140Structs.h"
18#include "compiler/translator/InfoSink.h"
19#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020020#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050021#include "compiler/translator/RewriteElseBlocks.h"
22#include "compiler/translator/SearchSymbol.h"
23#include "compiler/translator/StructureHLSL.h"
24#include "compiler/translator/TranslatorHLSL.h"
25#include "compiler/translator/UnfoldShortCircuit.h"
26#include "compiler/translator/UniformHLSL.h"
27#include "compiler/translator/UtilsHLSL.h"
28#include "compiler/translator/blocklayout.h"
Jamie Madilla6564902015-04-27 14:30:32 +000029#include "compiler/translator/compilerdebug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050030#include "compiler/translator/util.h"
31
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032namespace sh
33{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000034
Nicolas Capense0ba27a2013-06-24 16:10:52 -040035TString OutputHLSL::TextureFunction::name() const
36{
37 TString name = "gl_texture";
38
Nicolas Capens6d232bb2013-07-08 15:56:38 -040039 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040040 {
41 name += "2D";
42 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040043 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040044 {
45 name += "3D";
46 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040047 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040048 {
49 name += "Cube";
50 }
51 else UNREACHABLE();
52
53 if (proj)
54 {
55 name += "Proj";
56 }
57
Nicolas Capensb1f45b72013-12-19 17:37:19 -050058 if (offset)
59 {
60 name += "Offset";
61 }
62
Nicolas Capens75fb4752013-07-10 15:14:47 -040063 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040064 {
Nicolas Capensfc014542014-02-18 14:47:13 -050065 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040066 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050067 case LOD: name += "Lod"; break;
68 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040069 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050070 case SIZE: name += "Size"; break;
71 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050072 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040073 default: UNREACHABLE();
74 }
75
76 return name + "(";
77}
78
79bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
80{
81 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040082 if (sampler > rhs.sampler) return false;
83
Nicolas Capense0ba27a2013-06-24 16:10:52 -040084 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040085 if (coords > rhs.coords) return false;
86
Nicolas Capense0ba27a2013-06-24 16:10:52 -040087 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040088 if (proj && !rhs.proj) return false;
89
90 if (!offset && rhs.offset) return true;
91 if (offset && !rhs.offset) return false;
92
Nicolas Capens75fb4752013-07-10 15:14:47 -040093 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040094 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040095
96 return false;
97}
98
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020099OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
100 const TExtensionBehavior &extensionBehavior,
101 const char *sourcePath, ShShaderOutput outputType,
102 int numRenderTargets, const std::vector<Uniform> &uniforms,
103 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400104 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200105 mShaderType(shaderType),
106 mShaderVersion(shaderVersion),
107 mExtensionBehavior(extensionBehavior),
108 mSourcePath(sourcePath),
109 mOutputType(outputType),
110 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700111 mCompileOptions(compileOptions),
112 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200114 mUnfoldShortCircuit = new UnfoldShortCircuit(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000115 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000116
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000117 mUsesFragColor = false;
118 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000119 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000120 mUsesFragCoord = false;
121 mUsesPointCoord = false;
122 mUsesFrontFacing = false;
123 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000124 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400125 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000126 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500127 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400128 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530129 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000130
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000131 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000132
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000133 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000134 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400135 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000136
137 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000138
Jamie Madill8daaba12014-06-13 10:04:33 -0400139 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200140 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400141
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000142 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000143 {
Arun Patole63419392015-03-13 11:51:07 +0530144 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
145 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
146 // In both cases total 3 uniform registers need to be reserved.
147 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000148 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000149
Jamie Madillf91ce812014-06-13 10:04:34 -0400150 // Reserve registers for the default uniform block and driver constants
151 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152}
153
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000154OutputHLSL::~OutputHLSL()
155{
Jamie Madill8daaba12014-06-13 10:04:33 -0400156 SafeDelete(mUnfoldShortCircuit);
157 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400158 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200159 for (auto &eqFunction : mStructEqualityFunctions)
160 {
161 SafeDelete(eqFunction);
162 }
163 for (auto &eqFunction : mArrayEqualityFunctions)
164 {
165 SafeDelete(eqFunction);
166 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000167}
168
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200169void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000170{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200171 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400172 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000173
Jamie Madille53c98b2014-02-03 11:57:13 -0500174 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
175 // use a vertex attribute as a condition, and some related computation in the else block.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200176 if (mOutputType == SH_HLSL9_OUTPUT && mShaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500177 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200178 RewriteElseBlocks(treeRoot);
Jamie Madille53c98b2014-02-03 11:57:13 -0500179 }
180
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200181 BuiltInFunctionEmulator builtInFunctionEmulator;
182 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200183 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500184
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700185 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700186 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
187 ASSERT(success == CallDAG::INITDAG_SUCCESS);
188 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700189
Jamie Madill37997142015-01-28 10:06:34 -0500190 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500191 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200192 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500193 mInfoSinkStack.pop();
194
Jamie Madill37997142015-01-28 10:06:34 -0500195 mInfoSinkStack.push(&mFooter);
196 if (!mDeferredGlobalInitializers.empty())
197 {
198 writeDeferredGlobalInitializers(mFooter);
199 }
200 mInfoSinkStack.pop();
201
Jamie Madill32aab012015-01-27 14:12:26 -0500202 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200203 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500204 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000205
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200206 objSink << mHeader.c_str();
207 objSink << mBody.c_str();
208 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200209
210 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000211}
212
Jamie Madill570e04d2013-06-21 09:15:33 -0400213void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
214{
215 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
216 {
217 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
218
Jamie Madill32aab012015-01-27 14:12:26 -0500219 TInfoSinkBase structInfoSink;
220 mInfoSinkStack.push(&structInfoSink);
221
Jamie Madill570e04d2013-06-21 09:15:33 -0400222 // This will mark the necessary block elements as referenced
223 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500224
225 TString structName(structInfoSink.c_str());
226 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400227
228 mFlaggedStructOriginalNames[flaggedNode] = structName;
229
230 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
231 {
232 structName.erase(pos, 1);
233 }
234
235 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
236 }
237}
238
Jamie Madill4e1fd412014-07-10 17:50:10 -0400239const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
240{
241 return mUniformHLSL->getInterfaceBlockRegisterMap();
242}
243
Jamie Madill9fe25e92014-07-18 10:33:08 -0400244const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
245{
246 return mUniformHLSL->getUniformRegisterMap();
247}
248
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000249int OutputHLSL::vectorSize(const TType &type) const
250{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000251 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000252 int arraySize = type.isArray() ? type.getArraySize() : 1;
253
254 return elementSize * arraySize;
255}
256
Jamie Madill98493dd2013-07-08 14:39:03 -0400257TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400258{
259 TString init;
260
261 TString preIndentString;
262 TString fullIndentString;
263
264 for (int spaces = 0; spaces < (indent * 4); spaces++)
265 {
266 preIndentString += ' ';
267 }
268
269 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
270 {
271 fullIndentString += ' ';
272 }
273
274 init += preIndentString + "{\n";
275
Jamie Madill98493dd2013-07-08 14:39:03 -0400276 const TFieldList &fields = structure.fields();
277 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400278 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400279 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400280 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400281 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400282
Jamie Madill98493dd2013-07-08 14:39:03 -0400283 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400284 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400285 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 }
287 else
288 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400289 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400290 }
291 }
292
293 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
294
295 return init;
296}
297
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200298void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299{
Jamie Madill32aab012015-01-27 14:12:26 -0500300 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000302 TString varyings;
303 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400304 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000305
Jamie Madill829f59e2013-11-13 19:40:54 -0500306 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400307 {
308 TIntermTyped *structNode = flaggedStructIt->first;
309 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400310 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400311 const TString &originalName = mFlaggedStructOriginalNames[structNode];
312
Jamie Madill033dae62014-06-18 12:56:28 -0400313 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400314 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400315 flaggedStructs += "\n";
316 }
317
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000318 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
319 {
320 const TType &type = varying->second->getType();
321 const TString &name = varying->second->getSymbol();
322
323 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400324 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
325 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000326 }
327
328 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
329 {
330 const TType &type = attribute->second->getType();
331 const TString &name = attribute->second->getSymbol();
332
Jamie Madill033dae62014-06-18 12:56:28 -0400333 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000334 }
335
Jamie Madill8daaba12014-06-13 10:04:33 -0400336 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400337
Jamie Madillf91ce812014-06-13 10:04:34 -0400338 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
339 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
340
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200341 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500342 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200343 out << "\n// Equality functions\n\n";
344 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500345 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200346 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200347 }
348 }
Olli Etuaho12690762015-03-31 12:55:28 +0300349 if (!mArrayAssignmentFunctions.empty())
350 {
351 out << "\n// Assignment functions\n\n";
352 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
353 {
354 out << assignmentFunction.functionDefinition << "\n";
355 }
356 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300357 if (!mArrayConstructIntoFunctions.empty())
358 {
359 out << "\n// Array constructor functions\n\n";
360 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
361 {
362 out << constructIntoFunction.functionDefinition << "\n";
363 }
364 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200365
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500366 if (mUsesDiscardRewriting)
367 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400368 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500369 }
370
Nicolas Capens655fe362014-04-11 13:12:34 -0400371 if (mUsesNestedBreak)
372 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400373 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400374 }
375
Arun Patole44efa0b2015-03-04 17:11:05 +0530376 if (mRequiresIEEEStrictCompiling)
377 {
378 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
379 }
380
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400381 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
382 "#define LOOP [loop]\n"
383 "#define FLATTEN [flatten]\n"
384 "#else\n"
385 "#define LOOP\n"
386 "#define FLATTEN\n"
387 "#endif\n";
388
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200389 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200391 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
392 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000393
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000394 out << "// Varyings\n";
395 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400396 out << "\n";
397
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200398 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000399 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500400 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000401 {
Jamie Madill46131a32013-06-20 11:55:50 -0400402 const TString &variableName = outputVariableIt->first;
403 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400404
Jamie Madill033dae62014-06-18 12:56:28 -0400405 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400406 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000407 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000408 }
Jamie Madill46131a32013-06-20 11:55:50 -0400409 else
410 {
411 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
412
413 out << "static float4 gl_Color[" << numColorValues << "] =\n"
414 "{\n";
415 for (unsigned int i = 0; i < numColorValues; i++)
416 {
417 out << " float4(0, 0, 0, 0)";
418 if (i + 1 != numColorValues)
419 {
420 out << ",";
421 }
422 out << "\n";
423 }
424
425 out << "};\n";
426 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000427
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400428 if (mUsesFragDepth)
429 {
430 out << "static float gl_Depth = 0.0;\n";
431 }
432
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000433 if (mUsesFragCoord)
434 {
435 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
436 }
437
438 if (mUsesPointCoord)
439 {
440 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
441 }
442
443 if (mUsesFrontFacing)
444 {
445 out << "static bool gl_FrontFacing = false;\n";
446 }
447
448 out << "\n";
449
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000450 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000451 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000452 out << "struct gl_DepthRangeParameters\n"
453 "{\n"
454 " float near;\n"
455 " float far;\n"
456 " float diff;\n"
457 "};\n"
458 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000459 }
460
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000461 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000462 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000463 out << "cbuffer DriverConstants : register(b1)\n"
464 "{\n";
465
466 if (mUsesDepthRange)
467 {
468 out << " float3 dx_DepthRange : packoffset(c0);\n";
469 }
470
471 if (mUsesFragCoord)
472 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000473 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000474 }
475
476 if (mUsesFragCoord || mUsesFrontFacing)
477 {
478 out << " float3 dx_DepthFront : packoffset(c2);\n";
479 }
480
481 out << "};\n";
482 }
483 else
484 {
485 if (mUsesDepthRange)
486 {
487 out << "uniform float3 dx_DepthRange : register(c0);";
488 }
489
490 if (mUsesFragCoord)
491 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000492 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000493 }
494
495 if (mUsesFragCoord || mUsesFrontFacing)
496 {
497 out << "uniform float3 dx_DepthFront : register(c2);\n";
498 }
499 }
500
501 out << "\n";
502
503 if (mUsesDepthRange)
504 {
505 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
506 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000507 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000508
Jamie Madillf91ce812014-06-13 10:04:34 -0400509 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000510 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400511 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000512 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400513 out << flaggedStructs;
514 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000515 }
516
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000517 if (usingMRTExtension && mNumRenderTargets > 1)
518 {
519 out << "#define GL_USES_MRT\n";
520 }
521
522 if (mUsesFragColor)
523 {
524 out << "#define GL_USES_FRAG_COLOR\n";
525 }
526
527 if (mUsesFragData)
528 {
529 out << "#define GL_USES_FRAG_DATA\n";
530 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000532 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000534 out << "// Attributes\n";
535 out << attributes;
536 out << "\n"
537 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400538
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000539 if (mUsesPointSize)
540 {
541 out << "static float gl_PointSize = float(1);\n";
542 }
543
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000544 if (mUsesInstanceID)
545 {
546 out << "static int gl_InstanceID;";
547 }
548
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000549 out << "\n"
550 "// Varyings\n";
551 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000552 out << "\n";
553
554 if (mUsesDepthRange)
555 {
556 out << "struct gl_DepthRangeParameters\n"
557 "{\n"
558 " float near;\n"
559 " float far;\n"
560 " float diff;\n"
561 "};\n"
562 "\n";
563 }
564
565 if (mOutputType == SH_HLSL11_OUTPUT)
566 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800567 out << "cbuffer DriverConstants : register(b1)\n"
568 "{\n";
569
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000570 if (mUsesDepthRange)
571 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800572 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000573 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800574
Cooper Partine6664f02015-01-09 16:22:24 -0800575 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800576 // However, we declare it for all shaders (including Feature Level 10+).
577 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
578 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800579 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800580
581 out << "};\n"
582 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000583 }
584 else
585 {
586 if (mUsesDepthRange)
587 {
588 out << "uniform float3 dx_DepthRange : register(c0);\n";
589 }
590
Cooper Partine6664f02015-01-09 16:22:24 -0800591 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
592 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000593 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000594 }
595
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000596 if (mUsesDepthRange)
597 {
598 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
599 "\n";
600 }
601
Jamie Madillf91ce812014-06-13 10:04:34 -0400602 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000603 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400604 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000605 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400606 out << flaggedStructs;
607 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000608 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400609 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000610
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400611 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
612 {
613 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400614 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000615 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400616 switch(textureFunction->sampler)
617 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400618 case EbtSampler2D: out << "int2 "; break;
619 case EbtSampler3D: out << "int3 "; break;
620 case EbtSamplerCube: out << "int2 "; break;
621 case EbtSampler2DArray: out << "int3 "; break;
622 case EbtISampler2D: out << "int2 "; break;
623 case EbtISampler3D: out << "int3 "; break;
624 case EbtISamplerCube: out << "int2 "; break;
625 case EbtISampler2DArray: out << "int3 "; break;
626 case EbtUSampler2D: out << "int2 "; break;
627 case EbtUSampler3D: out << "int3 "; break;
628 case EbtUSamplerCube: out << "int2 "; break;
629 case EbtUSampler2DArray: out << "int3 "; break;
630 case EbtSampler2DShadow: out << "int2 "; break;
631 case EbtSamplerCubeShadow: out << "int2 "; break;
632 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400633 default: UNREACHABLE();
634 }
635 }
636 else // Sampling function
637 {
638 switch(textureFunction->sampler)
639 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400640 case EbtSampler2D: out << "float4 "; break;
641 case EbtSampler3D: out << "float4 "; break;
642 case EbtSamplerCube: out << "float4 "; break;
643 case EbtSampler2DArray: out << "float4 "; break;
644 case EbtISampler2D: out << "int4 "; break;
645 case EbtISampler3D: out << "int4 "; break;
646 case EbtISamplerCube: out << "int4 "; break;
647 case EbtISampler2DArray: out << "int4 "; break;
648 case EbtUSampler2D: out << "uint4 "; break;
649 case EbtUSampler3D: out << "uint4 "; break;
650 case EbtUSamplerCube: out << "uint4 "; break;
651 case EbtUSampler2DArray: out << "uint4 "; break;
652 case EbtSampler2DShadow: out << "float "; break;
653 case EbtSamplerCubeShadow: out << "float "; break;
654 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400655 default: UNREACHABLE();
656 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000657 }
658
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400659 // Function name
660 out << textureFunction->name();
661
662 // Argument list
663 int hlslCoords = 4;
664
665 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000666 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400667 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000668 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400669 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
670 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
671 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000672 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400673
Nicolas Capens75fb4752013-07-10 15:14:47 -0400674 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000675 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400676 case TextureFunction::IMPLICIT: break;
677 case TextureFunction::BIAS: hlslCoords = 4; break;
678 case TextureFunction::LOD: hlslCoords = 4; break;
679 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400680 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400681 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000682 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400683 }
684 else if (mOutputType == SH_HLSL11_OUTPUT)
685 {
686 switch(textureFunction->sampler)
687 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400688 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
689 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
690 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
691 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
692 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
693 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500694 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400695 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
696 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
697 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500698 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400699 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
700 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
701 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
702 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400703 default: UNREACHABLE();
704 }
705 }
706 else UNREACHABLE();
707
Nicolas Capensfc014542014-02-18 14:47:13 -0500708 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400709 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500710 switch(textureFunction->coords)
711 {
712 case 2: out << ", int2 t"; break;
713 case 3: out << ", int3 t"; break;
714 default: UNREACHABLE();
715 }
716 }
717 else // Floating-point coordinates (except textureSize)
718 {
719 switch(textureFunction->coords)
720 {
721 case 1: out << ", int lod"; break; // textureSize()
722 case 2: out << ", float2 t"; break;
723 case 3: out << ", float3 t"; break;
724 case 4: out << ", float4 t"; break;
725 default: UNREACHABLE();
726 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000727 }
728
Nicolas Capensd11d5492014-02-19 17:06:10 -0500729 if (textureFunction->method == TextureFunction::GRAD)
730 {
731 switch(textureFunction->sampler)
732 {
733 case EbtSampler2D:
734 case EbtISampler2D:
735 case EbtUSampler2D:
736 case EbtSampler2DArray:
737 case EbtISampler2DArray:
738 case EbtUSampler2DArray:
739 case EbtSampler2DShadow:
740 case EbtSampler2DArrayShadow:
741 out << ", float2 ddx, float2 ddy";
742 break;
743 case EbtSampler3D:
744 case EbtISampler3D:
745 case EbtUSampler3D:
746 case EbtSamplerCube:
747 case EbtISamplerCube:
748 case EbtUSamplerCube:
749 case EbtSamplerCubeShadow:
750 out << ", float3 ddx, float3 ddy";
751 break;
752 default: UNREACHABLE();
753 }
754 }
755
Nicolas Capens75fb4752013-07-10 15:14:47 -0400756 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000757 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400758 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400759 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400760 case TextureFunction::LOD: out << ", float lod"; break;
761 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400762 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400763 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500764 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500765 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400766 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000767 }
768
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500769 if (textureFunction->offset)
770 {
771 switch(textureFunction->sampler)
772 {
773 case EbtSampler2D: out << ", int2 offset"; break;
774 case EbtSampler3D: out << ", int3 offset"; break;
775 case EbtSampler2DArray: out << ", int2 offset"; break;
776 case EbtISampler2D: out << ", int2 offset"; break;
777 case EbtISampler3D: out << ", int3 offset"; break;
778 case EbtISampler2DArray: out << ", int2 offset"; break;
779 case EbtUSampler2D: out << ", int2 offset"; break;
780 case EbtUSampler3D: out << ", int3 offset"; break;
781 case EbtUSampler2DArray: out << ", int2 offset"; break;
782 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500783 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500784 default: UNREACHABLE();
785 }
786 }
787
Nicolas Capens84cfa122014-04-14 13:48:45 -0400788 if (textureFunction->method == TextureFunction::BIAS ||
789 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500790 {
791 out << ", float bias";
792 }
793
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400794 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400795 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400796
Nicolas Capens75fb4752013-07-10 15:14:47 -0400797 if (textureFunction->method == TextureFunction::SIZE)
798 {
799 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
800 {
801 if (IsSamplerArray(textureFunction->sampler))
802 {
803 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
804 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
805 }
806 else
807 {
808 out << " uint width; uint height; uint numberOfLevels;\n"
809 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
810 }
811 }
812 else if (IsSampler3D(textureFunction->sampler))
813 {
814 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
815 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
816 }
817 else UNREACHABLE();
818
819 switch(textureFunction->sampler)
820 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400821 case EbtSampler2D: out << " return int2(width, height);"; break;
822 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
823 case EbtSamplerCube: out << " return int2(width, height);"; break;
824 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
825 case EbtISampler2D: out << " return int2(width, height);"; break;
826 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
827 case EbtISamplerCube: out << " return int2(width, height);"; break;
828 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
829 case EbtUSampler2D: out << " return int2(width, height);"; break;
830 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
831 case EbtUSamplerCube: out << " return int2(width, height);"; break;
832 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
833 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
834 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
835 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400836 default: UNREACHABLE();
837 }
838 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400839 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400840 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500841 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
842 {
843 out << " float width; float height; float layers; float levels;\n";
844
845 out << " uint mip = 0;\n";
846
847 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
848
849 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
850 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
851 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
852 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
853
854 // FACE_POSITIVE_X = 000b
855 // FACE_NEGATIVE_X = 001b
856 // FACE_POSITIVE_Y = 010b
857 // FACE_NEGATIVE_Y = 011b
858 // FACE_POSITIVE_Z = 100b
859 // FACE_NEGATIVE_Z = 101b
860 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
861
862 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
863 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
864 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
865
866 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
867 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
868 }
869 else if (IsIntegerSampler(textureFunction->sampler) &&
870 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400871 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400872 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400873 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400874 if (IsSamplerArray(textureFunction->sampler))
875 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400876 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400877
Nicolas Capens9edebd62013-08-06 10:59:10 -0400878 if (textureFunction->method == TextureFunction::LOD0)
879 {
880 out << " uint mip = 0;\n";
881 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400882 else if (textureFunction->method == TextureFunction::LOD0BIAS)
883 {
884 out << " uint mip = bias;\n";
885 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400886 else
887 {
888 if (textureFunction->method == TextureFunction::IMPLICIT ||
889 textureFunction->method == TextureFunction::BIAS)
890 {
891 out << " x.GetDimensions(0, width, height, layers, levels);\n"
892 " float2 tSized = float2(t.x * width, t.y * height);\n"
893 " float dx = length(ddx(tSized));\n"
894 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500895 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400896
897 if (textureFunction->method == TextureFunction::BIAS)
898 {
899 out << " lod += bias;\n";
900 }
901 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500902 else if (textureFunction->method == TextureFunction::GRAD)
903 {
904 out << " x.GetDimensions(0, width, height, layers, levels);\n"
905 " float lod = log2(max(length(ddx), length(ddy)));\n";
906 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400907
908 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
909 }
910
911 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400912 }
913 else
914 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400915 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400916
Nicolas Capens9edebd62013-08-06 10:59:10 -0400917 if (textureFunction->method == TextureFunction::LOD0)
918 {
919 out << " uint mip = 0;\n";
920 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400921 else if (textureFunction->method == TextureFunction::LOD0BIAS)
922 {
923 out << " uint mip = bias;\n";
924 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400925 else
926 {
927 if (textureFunction->method == TextureFunction::IMPLICIT ||
928 textureFunction->method == TextureFunction::BIAS)
929 {
930 out << " x.GetDimensions(0, width, height, levels);\n"
931 " float2 tSized = float2(t.x * width, t.y * height);\n"
932 " float dx = length(ddx(tSized));\n"
933 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500934 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400935
936 if (textureFunction->method == TextureFunction::BIAS)
937 {
938 out << " lod += bias;\n";
939 }
940 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500941 else if (textureFunction->method == TextureFunction::LOD)
942 {
943 out << " x.GetDimensions(0, width, height, levels);\n";
944 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500945 else if (textureFunction->method == TextureFunction::GRAD)
946 {
947 out << " x.GetDimensions(0, width, height, levels);\n"
948 " float lod = log2(max(length(ddx), length(ddy)));\n";
949 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400950
951 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
952 }
953
954 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400955 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400956 }
957 else if (IsSampler3D(textureFunction->sampler))
958 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400959 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400960
Nicolas Capens9edebd62013-08-06 10:59:10 -0400961 if (textureFunction->method == TextureFunction::LOD0)
962 {
963 out << " uint mip = 0;\n";
964 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400965 else if (textureFunction->method == TextureFunction::LOD0BIAS)
966 {
967 out << " uint mip = bias;\n";
968 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400969 else
970 {
971 if (textureFunction->method == TextureFunction::IMPLICIT ||
972 textureFunction->method == TextureFunction::BIAS)
973 {
974 out << " x.GetDimensions(0, width, height, depth, levels);\n"
975 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
976 " float dx = length(ddx(tSized));\n"
977 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500978 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400979
980 if (textureFunction->method == TextureFunction::BIAS)
981 {
982 out << " lod += bias;\n";
983 }
984 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500985 else if (textureFunction->method == TextureFunction::GRAD)
986 {
987 out << " x.GetDimensions(0, width, height, depth, levels);\n"
988 " float lod = log2(max(length(ddx), length(ddy)));\n";
989 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400990
991 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
992 }
993
994 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400995 }
996 else UNREACHABLE();
997 }
998
999 out << " return ";
1000
1001 // HLSL intrinsic
1002 if (mOutputType == SH_HLSL9_OUTPUT)
1003 {
1004 switch(textureFunction->sampler)
1005 {
1006 case EbtSampler2D: out << "tex2D"; break;
1007 case EbtSamplerCube: out << "texCUBE"; break;
1008 default: UNREACHABLE();
1009 }
1010
Nicolas Capens75fb4752013-07-10 15:14:47 -04001011 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001012 {
1013 case TextureFunction::IMPLICIT: out << "(s, "; break;
1014 case TextureFunction::BIAS: out << "bias(s, "; break;
1015 case TextureFunction::LOD: out << "lod(s, "; break;
1016 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001017 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001018 default: UNREACHABLE();
1019 }
1020 }
1021 else if (mOutputType == SH_HLSL11_OUTPUT)
1022 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001023 if (textureFunction->method == TextureFunction::GRAD)
1024 {
1025 if (IsIntegerSampler(textureFunction->sampler))
1026 {
1027 out << "x.Load(";
1028 }
1029 else if (IsShadowSampler(textureFunction->sampler))
1030 {
1031 out << "x.SampleCmpLevelZero(s, ";
1032 }
1033 else
1034 {
1035 out << "x.SampleGrad(s, ";
1036 }
1037 }
1038 else if (IsIntegerSampler(textureFunction->sampler) ||
1039 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001040 {
1041 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001042 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001043 else if (IsShadowSampler(textureFunction->sampler))
1044 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001045 switch(textureFunction->method)
1046 {
1047 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1048 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1049 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1050 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1051 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1052 default: UNREACHABLE();
1053 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001054 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001055 else
1056 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001057 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001058 {
1059 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1060 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1061 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1062 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001063 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001064 default: UNREACHABLE();
1065 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001066 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001067 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001068 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001069
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001070 // Integer sampling requires integer addresses
1071 TString addressx = "";
1072 TString addressy = "";
1073 TString addressz = "";
1074 TString close = "";
1075
Nicolas Capensfc014542014-02-18 14:47:13 -05001076 if (IsIntegerSampler(textureFunction->sampler) ||
1077 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001078 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001079 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001080 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001081 case 2: out << "int3("; break;
1082 case 3: out << "int4("; break;
1083 default: UNREACHABLE();
1084 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001085
Nicolas Capensfc014542014-02-18 14:47:13 -05001086 // Convert from normalized floating-point to integer
1087 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001088 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001089 addressx = "int(floor(width * frac((";
1090 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001091
Nicolas Capensfc014542014-02-18 14:47:13 -05001092 if (IsSamplerArray(textureFunction->sampler))
1093 {
1094 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1095 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001096 else if (IsSamplerCube(textureFunction->sampler))
1097 {
1098 addressz = "((((";
1099 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001100 else
1101 {
1102 addressz = "int(floor(depth * frac((";
1103 }
1104
1105 close = "))))";
1106 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001107 }
1108 else
1109 {
1110 switch(hlslCoords)
1111 {
1112 case 2: out << "float2("; break;
1113 case 3: out << "float3("; break;
1114 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001115 default: UNREACHABLE();
1116 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001117 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001118
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001119 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001120
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001121 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001122 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001123 switch(textureFunction->coords)
1124 {
1125 case 3: proj = " / t.z"; break;
1126 case 4: proj = " / t.w"; break;
1127 default: UNREACHABLE();
1128 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001129 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001130
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001131 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001132
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001133 if (mOutputType == SH_HLSL9_OUTPUT)
1134 {
1135 if (hlslCoords >= 3)
1136 {
1137 if (textureFunction->coords < 3)
1138 {
1139 out << ", 0";
1140 }
1141 else
1142 {
1143 out << ", t.z" + proj;
1144 }
1145 }
1146
1147 if (hlslCoords == 4)
1148 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001149 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001150 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001151 case TextureFunction::BIAS: out << ", bias"; break;
1152 case TextureFunction::LOD: out << ", lod"; break;
1153 case TextureFunction::LOD0: out << ", 0"; break;
1154 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001155 default: UNREACHABLE();
1156 }
1157 }
1158
1159 out << "));\n";
1160 }
1161 else if (mOutputType == SH_HLSL11_OUTPUT)
1162 {
1163 if (hlslCoords >= 3)
1164 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001165 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1166 {
1167 out << ", face";
1168 }
1169 else
1170 {
1171 out << ", " + addressz + ("t.z" + proj) + close;
1172 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001173 }
1174
Nicolas Capensd11d5492014-02-19 17:06:10 -05001175 if (textureFunction->method == TextureFunction::GRAD)
1176 {
1177 if (IsIntegerSampler(textureFunction->sampler))
1178 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001179 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001180 }
1181 else if (IsShadowSampler(textureFunction->sampler))
1182 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001183 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001184 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001185 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001186 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1187 // The resulting third component of P' in the shadow forms is used as Dref
1188 out << "), t.z" << proj;
1189 }
1190 else
1191 {
1192 switch(textureFunction->coords)
1193 {
1194 case 3: out << "), t.z"; break;
1195 case 4: out << "), t.w"; break;
1196 default: UNREACHABLE();
1197 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001198 }
1199 }
1200 else
1201 {
1202 out << "), ddx, ddy";
1203 }
1204 }
1205 else if (IsIntegerSampler(textureFunction->sampler) ||
1206 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001207 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001208 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001209 }
1210 else if (IsShadowSampler(textureFunction->sampler))
1211 {
1212 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001213 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001214 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001215 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1216 // The resulting third component of P' in the shadow forms is used as Dref
1217 out << "), t.z" << proj;
1218 }
1219 else
1220 {
1221 switch(textureFunction->coords)
1222 {
1223 case 3: out << "), t.z"; break;
1224 case 4: out << "), t.w"; break;
1225 default: UNREACHABLE();
1226 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001227 }
1228 }
1229 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001230 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001231 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001232 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001233 case TextureFunction::IMPLICIT: out << ")"; break;
1234 case TextureFunction::BIAS: out << "), bias"; break;
1235 case TextureFunction::LOD: out << "), lod"; break;
1236 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001237 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001238 default: UNREACHABLE();
1239 }
1240 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001241
1242 if (textureFunction->offset)
1243 {
1244 out << ", offset";
1245 }
1246
1247 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001248 }
1249 else UNREACHABLE();
1250 }
1251
1252 out << "\n"
1253 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001254 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001255 }
1256
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001257 if (mUsesFragCoord)
1258 {
1259 out << "#define GL_USES_FRAG_COORD\n";
1260 }
1261
1262 if (mUsesPointCoord)
1263 {
1264 out << "#define GL_USES_POINT_COORD\n";
1265 }
1266
1267 if (mUsesFrontFacing)
1268 {
1269 out << "#define GL_USES_FRONT_FACING\n";
1270 }
1271
1272 if (mUsesPointSize)
1273 {
1274 out << "#define GL_USES_POINT_SIZE\n";
1275 }
1276
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001277 if (mUsesFragDepth)
1278 {
1279 out << "#define GL_USES_FRAG_DEPTH\n";
1280 }
1281
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001282 if (mUsesDepthRange)
1283 {
1284 out << "#define GL_USES_DEPTH_RANGE\n";
1285 }
1286
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001287 if (mUsesXor)
1288 {
1289 out << "bool xor(bool p, bool q)\n"
1290 "{\n"
1291 " return (p || q) && !(p && q);\n"
1292 "}\n"
1293 "\n";
1294 }
1295
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001296 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001297}
1298
1299void OutputHLSL::visitSymbol(TIntermSymbol *node)
1300{
Jamie Madill32aab012015-01-27 14:12:26 -05001301 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001302
Jamie Madill570e04d2013-06-21 09:15:33 -04001303 // Handle accessing std140 structs by value
1304 if (mFlaggedStructMappedNames.count(node) > 0)
1305 {
1306 out << mFlaggedStructMappedNames[node];
1307 return;
1308 }
1309
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001310 TString name = node->getSymbol();
1311
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001312 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001313 {
1314 mUsesDepthRange = true;
1315 out << name;
1316 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001317 else
1318 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001319 TQualifier qualifier = node->getQualifier();
1320
1321 if (qualifier == EvqUniform)
1322 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001323 const TType& nodeType = node->getType();
1324 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1325
1326 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001327 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001328 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001329 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001330 else
1331 {
1332 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001333 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001334
Jamie Madill033dae62014-06-18 12:56:28 -04001335 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001336 }
Jamie Madill19571812013-08-12 15:26:34 -07001337 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001338 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001339 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001340 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001341 }
Jamie Madill033dae62014-06-18 12:56:28 -04001342 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001343 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001344 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001345 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001346 }
Jamie Madill19571812013-08-12 15:26:34 -07001347 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001348 {
1349 mReferencedOutputVariables[name] = node;
1350 out << "out_" << name;
1351 }
1352 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001353 {
1354 out << "gl_Color[0]";
1355 mUsesFragColor = true;
1356 }
1357 else if (qualifier == EvqFragData)
1358 {
1359 out << "gl_Color";
1360 mUsesFragData = true;
1361 }
1362 else if (qualifier == EvqFragCoord)
1363 {
1364 mUsesFragCoord = true;
1365 out << name;
1366 }
1367 else if (qualifier == EvqPointCoord)
1368 {
1369 mUsesPointCoord = true;
1370 out << name;
1371 }
1372 else if (qualifier == EvqFrontFacing)
1373 {
1374 mUsesFrontFacing = true;
1375 out << name;
1376 }
1377 else if (qualifier == EvqPointSize)
1378 {
1379 mUsesPointSize = true;
1380 out << name;
1381 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001382 else if (qualifier == EvqInstanceID)
1383 {
1384 mUsesInstanceID = true;
1385 out << name;
1386 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001387 else if (name == "gl_FragDepthEXT")
1388 {
1389 mUsesFragDepth = true;
1390 out << "gl_Depth";
1391 }
Olli Etuaho78174db2015-04-21 16:14:00 +03001392 else if (node->isInternal())
Jamie Madille53c98b2014-02-03 11:57:13 -05001393 {
1394 out << name;
1395 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001396 else
1397 {
Jamie Madill033dae62014-06-18 12:56:28 -04001398 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001399 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001400 }
1401}
1402
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001403void OutputHLSL::visitRaw(TIntermRaw *node)
1404{
Jamie Madill32aab012015-01-27 14:12:26 -05001405 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001406}
1407
Olli Etuaho7fb49552015-03-18 17:27:44 +02001408void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1409{
1410 if (type.isScalar() && !type.isArray())
1411 {
1412 if (op == EOpEqual)
1413 {
1414 outputTriplet(visit, "(", " == ", ")", out);
1415 }
1416 else
1417 {
1418 outputTriplet(visit, "(", " != ", ")", out);
1419 }
1420 }
1421 else
1422 {
1423 if (visit == PreVisit && op == EOpNotEqual)
1424 {
1425 out << "!";
1426 }
1427
1428 if (type.isArray())
1429 {
1430 const TString &functionName = addArrayEqualityFunction(type);
1431 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1432 }
1433 else if (type.getBasicType() == EbtStruct)
1434 {
1435 const TStructure &structure = *type.getStruct();
1436 const TString &functionName = addStructEqualityFunction(structure);
1437 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1438 }
1439 else
1440 {
1441 ASSERT(type.isMatrix() || type.isVector());
1442 outputTriplet(visit, "all(", " == ", ")", out);
1443 }
1444 }
1445}
1446
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001447bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1448{
Jamie Madill32aab012015-01-27 14:12:26 -05001449 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001450
Jamie Madill570e04d2013-06-21 09:15:33 -04001451 // Handle accessing std140 structs by value
1452 if (mFlaggedStructMappedNames.count(node) > 0)
1453 {
1454 out << mFlaggedStructMappedNames[node];
1455 return false;
1456 }
1457
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001458 switch (node->getOp())
1459 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001460 case EOpAssign:
1461 if (node->getLeft()->isArray())
1462 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001463 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1464 if (rightAgg != nullptr && rightAgg->isConstructor())
1465 {
1466 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1467 out << functionName << "(";
1468 node->getLeft()->traverse(this);
1469 TIntermSequence *seq = rightAgg->getSequence();
1470 for (auto &arrayElement : *seq)
1471 {
1472 out << ", ";
1473 arrayElement->traverse(this);
1474 }
1475 out << ")";
1476 return false;
1477 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001478 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1479 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1480
1481 const TString &functionName = addArrayAssignmentFunction(node->getType());
1482 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001483 }
1484 else
1485 {
1486 outputTriplet(visit, "(", " = ", ")");
1487 }
1488 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001489 case EOpInitialize:
1490 if (visit == PreVisit)
1491 {
1492 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1493 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1494 // new variable is created before the assignment is evaluated), so we need to convert
1495 // this to "float t = x, x = t;".
1496
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001497 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001498 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001499 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001500
Jamie Madill37997142015-01-28 10:06:34 -05001501 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1502 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001503 {
Jamie Madill37997142015-01-28 10:06:34 -05001504 // For variables which are not constant, defer their real initialization until
1505 // after we initialize other globals: uniforms, attributes and varyings.
1506 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1507 const TString &initString = initializer(node->getType());
1508 node->setRight(new TIntermRaw(node->getType(), initString));
1509 }
1510 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1511 {
1512 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001513 return false;
1514 }
1515 }
1516 else if (visit == InVisit)
1517 {
1518 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001519 }
1520 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001521 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1522 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1523 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1524 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1525 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1526 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001527 if (visit == PreVisit)
1528 {
1529 out << "(";
1530 }
1531 else if (visit == InVisit)
1532 {
1533 out << " = mul(";
1534 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001535 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001536 }
1537 else
1538 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001539 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001540 }
1541 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001542 case EOpMatrixTimesMatrixAssign:
1543 if (visit == PreVisit)
1544 {
1545 out << "(";
1546 }
1547 else if (visit == InVisit)
1548 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001549 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001550 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001551 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001552 }
1553 else
1554 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001555 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001556 }
1557 break;
1558 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001559 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001560 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1561 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1562 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1563 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1564 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001565 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001566 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001567 const TType& leftType = node->getLeft()->getType();
1568 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001569 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001570 if (visit == PreVisit)
1571 {
1572 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1573 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001574 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001575 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001576 return false;
1577 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001578 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001579 else
1580 {
1581 outputTriplet(visit, "", "[", "]");
1582 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001583 }
1584 break;
1585 case EOpIndexIndirect:
1586 // We do not currently support indirect references to interface blocks
1587 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1588 outputTriplet(visit, "", "[", "]");
1589 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001590 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001591 if (visit == InVisit)
1592 {
1593 const TStructure* structure = node->getLeft()->getType().getStruct();
1594 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1595 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001596 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001597
1598 return false;
1599 }
1600 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001601 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001602 if (visit == InVisit)
1603 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001604 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1605 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1606 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001607 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001608
1609 return false;
1610 }
1611 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001612 case EOpVectorSwizzle:
1613 if (visit == InVisit)
1614 {
1615 out << ".";
1616
1617 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1618
1619 if (swizzle)
1620 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001621 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001622
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001623 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001624 {
1625 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1626
1627 if (element)
1628 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001629 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001630
1631 switch (i)
1632 {
1633 case 0: out << "x"; break;
1634 case 1: out << "y"; break;
1635 case 2: out << "z"; break;
1636 case 3: out << "w"; break;
1637 default: UNREACHABLE();
1638 }
1639 }
1640 else UNREACHABLE();
1641 }
1642 }
1643 else UNREACHABLE();
1644
1645 return false; // Fully processed
1646 }
1647 break;
1648 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1649 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1650 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1651 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001652 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001653 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1654 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1655 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1656 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1657 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001658 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001659 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001660 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001661 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001662 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1663 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1664 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1665 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1666 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001667 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001668 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1669 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001670 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001671 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001672 if (node->getRight()->hasSideEffects())
1673 {
1674 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1675 return false;
1676 }
1677 else
1678 {
1679 outputTriplet(visit, "(", " || ", ")");
1680 return true;
1681 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001682 case EOpLogicalXor:
1683 mUsesXor = true;
1684 outputTriplet(visit, "xor(", ", ", ")");
1685 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001686 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001687 if (node->getRight()->hasSideEffects())
1688 {
1689 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1690 return false;
1691 }
1692 else
1693 {
1694 outputTriplet(visit, "(", " && ", ")");
1695 return true;
1696 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 default: UNREACHABLE();
1698 }
1699
1700 return true;
1701}
1702
1703bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1704{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705 switch (node->getOp())
1706 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001707 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001708 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001709 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1710 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001711 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001712 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1713 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1714 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1715 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001716 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1717 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1718 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1719 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1720 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1721 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1722 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1723 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001724 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1725 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1726 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1727 case EOpAsinh:
1728 ASSERT(node->getUseEmulatedFunction());
1729 writeEmulatedFunctionTriplet(visit, "asinh(");
1730 break;
1731 case EOpAcosh:
1732 ASSERT(node->getUseEmulatedFunction());
1733 writeEmulatedFunctionTriplet(visit, "acosh(");
1734 break;
1735 case EOpAtanh:
1736 ASSERT(node->getUseEmulatedFunction());
1737 writeEmulatedFunctionTriplet(visit, "atanh(");
1738 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001739 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1740 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1741 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1742 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1743 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1744 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1745 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1746 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1747 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001748 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1749 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1750 case EOpRoundEven:
1751 ASSERT(node->getUseEmulatedFunction());
1752 writeEmulatedFunctionTriplet(visit, "roundEven(");
1753 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001754 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1755 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301756 case EOpIsNan:
1757 outputTriplet(visit, "isnan(", "", ")");
1758 mRequiresIEEEStrictCompiling = true;
1759 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301760 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001761 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1762 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1763 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1764 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001765 case EOpPackSnorm2x16:
1766 ASSERT(node->getUseEmulatedFunction());
1767 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1768 break;
1769 case EOpPackUnorm2x16:
1770 ASSERT(node->getUseEmulatedFunction());
1771 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1772 break;
1773 case EOpPackHalf2x16:
1774 ASSERT(node->getUseEmulatedFunction());
1775 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1776 break;
1777 case EOpUnpackSnorm2x16:
1778 ASSERT(node->getUseEmulatedFunction());
1779 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1780 break;
1781 case EOpUnpackUnorm2x16:
1782 ASSERT(node->getUseEmulatedFunction());
1783 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1784 break;
1785 case EOpUnpackHalf2x16:
1786 ASSERT(node->getUseEmulatedFunction());
1787 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1788 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001789 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1790 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001791 case EOpDFdx:
1792 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1793 {
1794 outputTriplet(visit, "(", "", ", 0.0)");
1795 }
1796 else
1797 {
1798 outputTriplet(visit, "ddx(", "", ")");
1799 }
1800 break;
1801 case EOpDFdy:
1802 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1803 {
1804 outputTriplet(visit, "(", "", ", 0.0)");
1805 }
1806 else
1807 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001808 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001809 }
1810 break;
1811 case EOpFwidth:
1812 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1813 {
1814 outputTriplet(visit, "(", "", ", 0.0)");
1815 }
1816 else
1817 {
1818 outputTriplet(visit, "fwidth(", "", ")");
1819 }
1820 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001821 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1822 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001823 case EOpInverse:
1824 ASSERT(node->getUseEmulatedFunction());
1825 writeEmulatedFunctionTriplet(visit, "inverse(");
1826 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001827
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001828 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1829 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 default: UNREACHABLE();
1831 }
1832
1833 return true;
1834}
1835
1836bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1837{
Jamie Madill32aab012015-01-27 14:12:26 -05001838 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840 switch (node->getOp())
1841 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001842 case EOpSequence:
1843 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001844 if (mInsideFunction)
1845 {
Jamie Madill075edd82013-07-08 13:30:19 -04001846 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001847 out << "{\n";
1848 }
1849
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001850 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001851 {
Jamie Madill075edd82013-07-08 13:30:19 -04001852 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001853
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001854 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001855
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001856 // Don't output ; after case labels, they're terminated by :
1857 // This is needed especially since outputting a ; after a case statement would turn empty
1858 // case statements into non-empty case statements, disallowing fall-through from them.
1859 if ((*sit)->getAsCaseNode() == nullptr)
1860 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001861 }
1862
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001863 if (mInsideFunction)
1864 {
Jamie Madill075edd82013-07-08 13:30:19 -04001865 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001866 out << "}\n";
1867 }
1868
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001869 return false;
1870 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871 case EOpDeclaration:
1872 if (visit == PreVisit)
1873 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001874 TIntermSequence *sequence = node->getSequence();
1875 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001876
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001877 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001879 TStructure *structure = variable->getType().getStruct();
1880
1881 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001882 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001883 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001884 }
1885
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001886 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887 {
Jamie Madill37997142015-01-28 10:06:34 -05001888 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889 {
Jamie Madill37997142015-01-28 10:06:34 -05001890 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001891 {
Jamie Madill37997142015-01-28 10:06:34 -05001892 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001893 }
1894
Nicolas Capensd974db42014-10-07 10:50:19 -04001895 if (!mInsideFunction)
1896 {
1897 out << "static ";
1898 }
1899
1900 out << TypeString(variable->getType()) + " ";
1901
Jamie Madill37997142015-01-28 10:06:34 -05001902 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001904 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001906 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001907 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001908 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001909 }
1910 else
1911 {
Jamie Madill37997142015-01-28 10:06:34 -05001912 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001913 }
1914
Jamie Madill37997142015-01-28 10:06:34 -05001915 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001916 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001917 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 }
1919 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001921 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1922 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001923 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001924 }
1925 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 }
Jamie Madill033dae62014-06-18 12:56:28 -04001927 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001928 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001929 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001930 {
1931 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1932
1933 if (symbol)
1934 {
1935 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1936 mReferencedVaryings[symbol->getSymbol()] = symbol;
1937 }
1938 else
1939 {
1940 (*sit)->traverse(this);
1941 }
1942 }
1943 }
1944
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945 return false;
1946 }
1947 else if (visit == InVisit)
1948 {
1949 out << ", ";
1950 }
1951 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001952 case EOpInvariantDeclaration:
1953 // Do not do any translation
1954 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001955 case EOpPrototype:
1956 if (visit == PreVisit)
1957 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001958 size_t index = mCallDag.findIndex(node);
1959 // Skip the prototype if it is not implemented (and thus not used)
1960 if (index == CallDAG::InvalidIndex)
1961 {
1962 return false;
1963 }
1964
Olli Etuaho76acee82014-11-04 13:44:03 +02001965 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001966
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001967 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001968
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001969 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001970 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001971 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001972
1973 if (symbol)
1974 {
1975 out << argumentString(symbol);
1976
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001977 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001978 {
1979 out << ", ";
1980 }
1981 }
1982 else UNREACHABLE();
1983 }
1984
1985 out << ");\n";
1986
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001987 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001988 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1989 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001990 {
1991 mOutputLod0Function = true;
1992 node->traverse(this);
1993 mOutputLod0Function = false;
1994 }
1995
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001996 return false;
1997 }
1998 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001999 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000 case EOpFunction:
2001 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002002 ASSERT(mCurrentFunctionMetadata == nullptr);
alokp@chromium.org43884872010-03-30 00:08:52 +00002003 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002004
Corentin Wallez1239ee92015-03-19 14:38:02 -07002005 size_t index = mCallDag.findIndex(node);
2006 ASSERT(index != CallDAG::InvalidIndex);
2007 mCurrentFunctionMetadata = &mASTMetadataList[index];
2008
Jamie Madill033dae62014-06-18 12:56:28 -04002009 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002010
2011 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002012 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002013 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002014 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002015 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016 {
Jamie Madill033dae62014-06-18 12:56:28 -04002017 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002018 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002019
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002020 TIntermSequence *sequence = node->getSequence();
2021 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002022
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002023 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002024 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002025 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002026
2027 if (symbol)
2028 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002029 TStructure *structure = symbol->getType().getStruct();
2030
2031 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002032 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002033 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002034 }
2035
2036 out << argumentString(symbol);
2037
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002038 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002039 {
2040 out << ", ";
2041 }
2042 }
2043 else UNREACHABLE();
2044 }
2045
2046 out << ")\n"
2047 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002048
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002049 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002050 {
2051 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002052 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002053 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002054 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002055
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002056 out << "}\n";
2057
Corentin Wallez1239ee92015-03-19 14:38:02 -07002058 mCurrentFunctionMetadata = nullptr;
2059
2060 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2061 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002062 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002063 ASSERT(name != "main");
2064 mOutputLod0Function = true;
2065 node->traverse(this);
2066 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002067 }
2068
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002069 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 }
2071 break;
2072 case EOpFunctionCall:
2073 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002074 TString name = TFunction::unmangleName(node->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002075 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002076
Corentin Wallez1239ee92015-03-19 14:38:02 -07002077 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002078 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002080 if (node->isArray())
2081 {
2082 UNIMPLEMENTED();
2083 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002084 size_t index = mCallDag.findIndex(node);
2085 ASSERT(index != CallDAG::InvalidIndex);
2086 lod0 &= mASTMetadataList[index].mNeedsLod0;
2087
Jamie Madill033dae62014-06-18 12:56:28 -04002088 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089 }
2090 else
2091 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002092 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002093
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002094 TextureFunction textureFunction;
2095 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002096 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002097 textureFunction.method = TextureFunction::IMPLICIT;
2098 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002099 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002100
2101 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002102 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002103 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002104 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002105 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002106 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002107 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002108 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002109 }
Nicolas Capens46485082014-04-15 13:12:50 -04002110 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2111 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002112 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002113 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002114 }
Nicolas Capens46485082014-04-15 13:12:50 -04002115 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002116 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002117 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002118 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002119 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002120 else if (name == "textureSize")
2121 {
2122 textureFunction.method = TextureFunction::SIZE;
2123 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002124 else if (name == "textureOffset")
2125 {
2126 textureFunction.method = TextureFunction::IMPLICIT;
2127 textureFunction.offset = true;
2128 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002129 else if (name == "textureProjOffset")
2130 {
2131 textureFunction.method = TextureFunction::IMPLICIT;
2132 textureFunction.offset = true;
2133 textureFunction.proj = true;
2134 }
2135 else if (name == "textureLodOffset")
2136 {
2137 textureFunction.method = TextureFunction::LOD;
2138 textureFunction.offset = true;
2139 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002140 else if (name == "textureProjLodOffset")
2141 {
2142 textureFunction.method = TextureFunction::LOD;
2143 textureFunction.proj = true;
2144 textureFunction.offset = true;
2145 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002146 else if (name == "texelFetch")
2147 {
2148 textureFunction.method = TextureFunction::FETCH;
2149 }
2150 else if (name == "texelFetchOffset")
2151 {
2152 textureFunction.method = TextureFunction::FETCH;
2153 textureFunction.offset = true;
2154 }
Nicolas Capens46485082014-04-15 13:12:50 -04002155 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002156 {
2157 textureFunction.method = TextureFunction::GRAD;
2158 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002159 else if (name == "textureGradOffset")
2160 {
2161 textureFunction.method = TextureFunction::GRAD;
2162 textureFunction.offset = true;
2163 }
Nicolas Capens46485082014-04-15 13:12:50 -04002164 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002165 {
2166 textureFunction.method = TextureFunction::GRAD;
2167 textureFunction.proj = true;
2168 }
2169 else if (name == "textureProjGradOffset")
2170 {
2171 textureFunction.method = TextureFunction::GRAD;
2172 textureFunction.proj = true;
2173 textureFunction.offset = true;
2174 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002175 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002176
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002177 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002178 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002179 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2180
2181 if (textureFunction.offset)
2182 {
2183 mandatoryArgumentCount++;
2184 }
2185
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002186 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002187
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002188 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002189 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002190 if (bias)
2191 {
2192 textureFunction.method = TextureFunction::LOD0BIAS;
2193 }
2194 else
2195 {
2196 textureFunction.method = TextureFunction::LOD0;
2197 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002198 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002199 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002200 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002201 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002202 }
2203 }
2204
2205 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002206
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002207 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002208 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002209
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002210 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002211 {
2212 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2213 {
2214 out << "texture_";
2215 (*arg)->traverse(this);
2216 out << ", sampler_";
2217 }
2218
2219 (*arg)->traverse(this);
2220
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002221 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002222 {
2223 out << ", ";
2224 }
2225 }
2226
2227 out << ")";
2228
2229 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002230 }
2231 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002232 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002233 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2234 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2235 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2236 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2237 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2238 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2239 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2240 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2241 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2242 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2243 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2244 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2245 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2246 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2247 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2248 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2249 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2250 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2251 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002252 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002253 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002254 if (node->getType().isArray())
2255 {
2256 UNIMPLEMENTED();
2257 }
Jamie Madill033dae62014-06-18 12:56:28 -04002258 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002259 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002260 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002261 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002262 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002263 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2264 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2265 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2266 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2267 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2268 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002269 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002270 ASSERT(node->getUseEmulatedFunction());
2271 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002272 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002273 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002274 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002275 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002276 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002277 ASSERT(node->getUseEmulatedFunction());
2278 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 break;
2280 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2281 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2282 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2283 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2284 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2285 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2286 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2287 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2288 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002289 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002290 ASSERT(node->getUseEmulatedFunction());
2291 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002292 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2294 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002295 case EOpOuterProduct:
2296 ASSERT(node->getUseEmulatedFunction());
2297 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2298 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 default: UNREACHABLE();
2301 }
2302
2303 return true;
2304}
2305
2306bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2307{
Jamie Madill32aab012015-01-27 14:12:26 -05002308 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002309
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002310 if (node->usesTernaryOperator())
2311 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002312 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002313 }
2314 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002316 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002317
Corentin Wallez1239ee92015-03-19 14:38:02 -07002318 // D3D errors when there is a gradient operation in a loop in an unflattened if.
2319 if (mShaderType == GL_FRAGMENT_SHADER
2320 && mCurrentFunctionMetadata->hasDiscontinuousLoop(node)
2321 && mCurrentFunctionMetadata->hasGradientInCallGraph(node))
Corentin Wallez80bacde2014-11-10 12:07:37 -08002322 {
2323 out << "FLATTEN ";
2324 }
2325
2326 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002327
2328 node->getCondition()->traverse(this);
2329
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002330 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002331
Jamie Madill075edd82013-07-08 13:30:19 -04002332 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002333 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002334
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002335 bool discard = false;
2336
daniel@transgaming.combb885322010-04-15 20:45:24 +00002337 if (node->getTrueBlock())
2338 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002339 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002340
2341 // Detect true discard
2342 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344
Jamie Madill075edd82013-07-08 13:30:19 -04002345 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002346 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002347
2348 if (node->getFalseBlock())
2349 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002350 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002351
Jamie Madill075edd82013-07-08 13:30:19 -04002352 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002353 out << "{\n";
2354
Jamie Madill075edd82013-07-08 13:30:19 -04002355 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002356 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002357
Jamie Madill075edd82013-07-08 13:30:19 -04002358 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002359 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002360
2361 // Detect false discard
2362 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2363 }
2364
2365 // ANGLE issue 486: Detect problematic conditional discard
2366 if (discard && FindSideEffectRewriting::search(node))
2367 {
2368 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002369 }
2370 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002371
2372 return false;
2373}
2374
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002375bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002376{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002377 if (node->getStatementList())
2378 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002379 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002380 outputTriplet(visit, "switch (", ") ", "");
2381 // The curly braces get written when visiting the statementList aggregate
2382 }
2383 else
2384 {
2385 // No statementList, so it won't output curly braces
2386 outputTriplet(visit, "switch (", ") {", "}\n");
2387 }
2388 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002389}
2390
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002391bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002392{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002393 if (node->hasCondition())
2394 {
2395 outputTriplet(visit, "case (", "", "):\n");
2396 return true;
2397 }
2398 else
2399 {
2400 TInfoSinkBase &out = getInfoSink();
2401 out << "default:\n";
2402 return false;
2403 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002404}
2405
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2407{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002408 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409}
2410
2411bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2412{
Nicolas Capens655fe362014-04-11 13:12:34 -04002413 mNestedLoopDepth++;
2414
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002415 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002416 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
2417 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) >= 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002418
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002419 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002420 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002421 if (handleExcessiveLoop(node))
2422 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002423 mInsideDiscontinuousLoop = wasDiscontinuous;
2424 mNestedLoopDepth--;
2425
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002426 return false;
2427 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002428 }
2429
Jamie Madill32aab012015-01-27 14:12:26 -05002430 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431
Corentin Wallez1239ee92015-03-19 14:38:02 -07002432 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002433 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002435 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002436
Jamie Madill075edd82013-07-08 13:30:19 -04002437 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002438 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439 }
2440 else
2441 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002442 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002443
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 if (node->getInit())
2445 {
2446 node->getInit()->traverse(this);
2447 }
2448
2449 out << "; ";
2450
alokp@chromium.org52813552010-11-16 18:36:09 +00002451 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002453 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 }
2455
2456 out << "; ";
2457
alokp@chromium.org52813552010-11-16 18:36:09 +00002458 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002459 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002460 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461 }
2462
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002463 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002464
Jamie Madill075edd82013-07-08 13:30:19 -04002465 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002466 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 }
2468
2469 if (node->getBody())
2470 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002471 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002472 }
2473
Jamie Madill075edd82013-07-08 13:30:19 -04002474 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002475 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476
alokp@chromium.org52813552010-11-16 18:36:09 +00002477 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478 {
Jamie Madill075edd82013-07-08 13:30:19 -04002479 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 out << "while(\n";
2481
alokp@chromium.org52813552010-11-16 18:36:09 +00002482 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483
daniel@transgaming.com73536982012-03-21 20:45:49 +00002484 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485 }
2486
daniel@transgaming.com73536982012-03-21 20:45:49 +00002487 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002489 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002490 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002491
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492 return false;
2493}
2494
2495bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2496{
Jamie Madill32aab012015-01-27 14:12:26 -05002497 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002498
2499 switch (node->getFlowOp())
2500 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002501 case EOpKill:
2502 outputTriplet(visit, "discard;\n", "", "");
2503 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002504 case EOpBreak:
2505 if (visit == PreVisit)
2506 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002507 if (mNestedLoopDepth > 1)
2508 {
2509 mUsesNestedBreak = true;
2510 }
2511
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002512 if (mExcessiveLoopIndex)
2513 {
2514 out << "{Break";
2515 mExcessiveLoopIndex->traverse(this);
2516 out << " = true; break;}\n";
2517 }
2518 else
2519 {
2520 out << "break;\n";
2521 }
2522 }
2523 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002524 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002525 case EOpReturn:
2526 if (visit == PreVisit)
2527 {
2528 if (node->getExpression())
2529 {
2530 out << "return ";
2531 }
2532 else
2533 {
2534 out << "return;\n";
2535 }
2536 }
2537 else if (visit == PostVisit)
2538 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002539 if (node->getExpression())
2540 {
2541 out << ";\n";
2542 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543 }
2544 break;
2545 default: UNREACHABLE();
2546 }
2547
2548 return true;
2549}
2550
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002551void OutputHLSL::traverseStatements(TIntermNode *node)
2552{
2553 if (isSingleStatement(node))
2554 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002555 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002556 }
2557
2558 node->traverse(this);
2559}
2560
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002561bool OutputHLSL::isSingleStatement(TIntermNode *node)
2562{
2563 TIntermAggregate *aggregate = node->getAsAggregate();
2564
2565 if (aggregate)
2566 {
2567 if (aggregate->getOp() == EOpSequence)
2568 {
2569 return false;
2570 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002571 else if (aggregate->getOp() == EOpDeclaration)
2572 {
2573 // Declaring multiple comma-separated variables must be considered multiple statements
2574 // because each individual declaration has side effects which are visible in the next.
2575 return false;
2576 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002577 else
2578 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002579 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002580 {
2581 if (!isSingleStatement(*sit))
2582 {
2583 return false;
2584 }
2585 }
2586
2587 return true;
2588 }
2589 }
2590
2591 return true;
2592}
2593
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002594// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2595// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002596bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2597{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002598 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002599 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002600
2601 // Parse loops of the form:
2602 // for(int index = initial; index [comparator] limit; index += increment)
2603 TIntermSymbol *index = NULL;
2604 TOperator comparator = EOpNull;
2605 int initial = 0;
2606 int limit = 0;
2607 int increment = 0;
2608
2609 // Parse index name and intial value
2610 if (node->getInit())
2611 {
2612 TIntermAggregate *init = node->getInit()->getAsAggregate();
2613
2614 if (init)
2615 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002616 TIntermSequence *sequence = init->getSequence();
2617 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002618
2619 if (variable && variable->getQualifier() == EvqTemporary)
2620 {
2621 TIntermBinary *assign = variable->getAsBinaryNode();
2622
2623 if (assign->getOp() == EOpInitialize)
2624 {
2625 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2626 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2627
2628 if (symbol && constant)
2629 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002630 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002631 {
2632 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002633 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002634 }
2635 }
2636 }
2637 }
2638 }
2639 }
2640
2641 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002642 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002643 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002644 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002645
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002646 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2647 {
2648 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2649
2650 if (constant)
2651 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002652 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002653 {
2654 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002655 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002656 }
2657 }
2658 }
2659 }
2660
2661 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002662 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002663 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002664 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2665 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002666
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002667 if (binaryTerminal)
2668 {
2669 TOperator op = binaryTerminal->getOp();
2670 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2671
2672 if (constant)
2673 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002674 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002675 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002676 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002677
2678 switch (op)
2679 {
2680 case EOpAddAssign: increment = value; break;
2681 case EOpSubAssign: increment = -value; break;
2682 default: UNIMPLEMENTED();
2683 }
2684 }
2685 }
2686 }
2687 else if (unaryTerminal)
2688 {
2689 TOperator op = unaryTerminal->getOp();
2690
2691 switch (op)
2692 {
2693 case EOpPostIncrement: increment = 1; break;
2694 case EOpPostDecrement: increment = -1; break;
2695 case EOpPreIncrement: increment = 1; break;
2696 case EOpPreDecrement: increment = -1; break;
2697 default: UNIMPLEMENTED();
2698 }
2699 }
2700 }
2701
2702 if (index != NULL && comparator != EOpNull && increment != 0)
2703 {
2704 if (comparator == EOpLessThanEqual)
2705 {
2706 comparator = EOpLessThan;
2707 limit += 1;
2708 }
2709
2710 if (comparator == EOpLessThan)
2711 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002712 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002713
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002714 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002715 {
2716 return false; // Not an excessive loop
2717 }
2718
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002719 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2720 mExcessiveLoopIndex = index;
2721
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002722 out << "{int ";
2723 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002724 out << ";\n"
2725 "bool Break";
2726 index->traverse(this);
2727 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002728
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002729 bool firstLoopFragment = true;
2730
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002731 while (iterations > 0)
2732 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002733 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002734
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002735 if (!firstLoopFragment)
2736 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002737 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002738 index->traverse(this);
2739 out << ") {\n";
2740 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002741
2742 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2743 {
2744 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2745 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002746
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002747 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002748 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002749
Corentin Wallez1239ee92015-03-19 14:38:02 -07002750 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002751 index->traverse(this);
2752 out << " = ";
2753 out << initial;
2754
2755 out << "; ";
2756 index->traverse(this);
2757 out << " < ";
2758 out << clampedLimit;
2759
2760 out << "; ";
2761 index->traverse(this);
2762 out << " += ";
2763 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002764 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002765
Jamie Madill075edd82013-07-08 13:30:19 -04002766 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002767 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002768
2769 if (node->getBody())
2770 {
2771 node->getBody()->traverse(this);
2772 }
2773
Jamie Madill075edd82013-07-08 13:30:19 -04002774 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002775 out << ";}\n";
2776
2777 if (!firstLoopFragment)
2778 {
2779 out << "}\n";
2780 }
2781
2782 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002783
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002784 initial += MAX_LOOP_ITERATIONS * increment;
2785 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002786 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002787
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002788 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002789
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002790 mExcessiveLoopIndex = restoreIndex;
2791
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002792 return true;
2793 }
2794 else UNIMPLEMENTED();
2795 }
2796
2797 return false; // Not handled as an excessive loop
2798}
2799
Olli Etuaho7fb49552015-03-18 17:27:44 +02002800void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002801{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002802 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002803 {
2804 out << preString;
2805 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002806 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807 {
2808 out << inString;
2809 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002810 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002811 {
2812 out << postString;
2813 }
2814}
2815
Olli Etuaho7fb49552015-03-18 17:27:44 +02002816void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2817{
2818 outputTriplet(visit, preString, inString, postString, getInfoSink());
2819}
2820
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002821void OutputHLSL::outputLineDirective(int line)
2822{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002823 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002824 {
Jamie Madill32aab012015-01-27 14:12:26 -05002825 TInfoSinkBase &out = getInfoSink();
2826
2827 out << "\n";
2828 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002829
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002830 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002831 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002832 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002833 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002834
Jamie Madill32aab012015-01-27 14:12:26 -05002835 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002836 }
2837}
2838
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002839TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2840{
2841 TQualifier qualifier = symbol->getQualifier();
2842 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002843 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002844
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002845 if (name.empty()) // HLSL demands named arguments, also for prototypes
2846 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002847 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002848 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002849 else if (!symbol->isInternal())
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002850 {
Jamie Madill033dae62014-06-18 12:56:28 -04002851 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002852 }
2853
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002854 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2855 {
Jamie Madill033dae62014-06-18 12:56:28 -04002856 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002857 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002858 }
2859
Jamie Madill033dae62014-06-18 12:56:28 -04002860 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002861}
2862
2863TString OutputHLSL::initializer(const TType &type)
2864{
2865 TString string;
2866
Jamie Madill94bf7f22013-07-08 13:31:15 -04002867 size_t size = type.getObjectSize();
2868 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002870 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871
Jamie Madill94bf7f22013-07-08 13:31:15 -04002872 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873 {
2874 string += ", ";
2875 }
2876 }
2877
daniel@transgaming.comead23042010-04-29 03:35:36 +00002878 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002879}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002880
Daniel Bratell29190082015-02-20 16:42:54 +01002881void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002882{
Olli Etuahof40319e2015-03-10 14:33:00 +02002883 if (type.isArray())
2884 {
2885 UNIMPLEMENTED();
2886 }
Jamie Madill32aab012015-01-27 14:12:26 -05002887 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002888
2889 if (visit == PreVisit)
2890 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002891 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002892
Daniel Bratell29190082015-02-20 16:42:54 +01002893 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002894 }
2895 else if (visit == InVisit)
2896 {
2897 out << ", ";
2898 }
2899 else if (visit == PostVisit)
2900 {
2901 out << ")";
2902 }
2903}
2904
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002905const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2906{
Jamie Madill32aab012015-01-27 14:12:26 -05002907 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002908
Jamie Madill98493dd2013-07-08 14:39:03 -04002909 const TStructure* structure = type.getStruct();
2910 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002911 {
Jamie Madill033dae62014-06-18 12:56:28 -04002912 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002913
Jamie Madill98493dd2013-07-08 14:39:03 -04002914 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002915
Jamie Madill98493dd2013-07-08 14:39:03 -04002916 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002917 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002918 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002919 constUnion = writeConstantUnion(*fieldType, constUnion);
2920
Jamie Madill98493dd2013-07-08 14:39:03 -04002921 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002922 {
2923 out << ", ";
2924 }
2925 }
2926
2927 out << ")";
2928 }
2929 else
2930 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002931 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002932 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002933
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002934 if (writeType)
2935 {
Jamie Madill033dae62014-06-18 12:56:28 -04002936 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002937 }
2938
Jamie Madill94bf7f22013-07-08 13:31:15 -04002939 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002940 {
2941 switch (constUnion->getType())
2942 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002943 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002944 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002945 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002946 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002947 default: UNREACHABLE();
2948 }
2949
2950 if (i != size - 1)
2951 {
2952 out << ", ";
2953 }
2954 }
2955
2956 if (writeType)
2957 {
2958 out << ")";
2959 }
2960 }
2961
2962 return constUnion;
2963}
2964
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002965void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2966{
2967 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2968 outputTriplet(visit, preString.c_str(), ", ", ")");
2969}
2970
Jamie Madill37997142015-01-28 10:06:34 -05002971bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2972{
2973 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2974 expression->traverse(&searchSymbol);
2975
2976 if (searchSymbol.foundMatch())
2977 {
2978 // Type already printed
2979 out << "t" + str(mUniqueIndex) + " = ";
2980 expression->traverse(this);
2981 out << ", ";
2982 symbolNode->traverse(this);
2983 out << " = t" + str(mUniqueIndex);
2984
2985 mUniqueIndex++;
2986 return true;
2987 }
2988
2989 return false;
2990}
2991
2992void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2993{
2994 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2995 << "\n"
2996 << "void initializeDeferredGlobals()\n"
2997 << "{\n";
2998
2999 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3000 {
3001 TIntermSymbol *symbol = deferredGlobal.first;
3002 TIntermTyped *expression = deferredGlobal.second;
3003 ASSERT(symbol);
3004 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
3005
3006 out << " " << Decorate(symbol->getSymbol()) << " = ";
3007
3008 if (!writeSameSymbolInitializer(out, symbol, expression))
3009 {
3010 ASSERT(mInfoSinkStack.top() == &out);
3011 expression->traverse(this);
3012 }
3013
3014 out << ";\n";
3015 }
3016
3017 out << "}\n"
3018 << "\n";
3019}
3020
Jamie Madill55e79e02015-02-09 15:35:00 -05003021TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3022{
3023 const TFieldList &fields = structure.fields();
3024
3025 for (const auto &eqFunction : mStructEqualityFunctions)
3026 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003027 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003028 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003029 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003030 }
3031 }
3032
3033 const TString &structNameString = StructNameString(structure);
3034
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003035 StructEqualityFunction *function = new StructEqualityFunction();
3036 function->structure = &structure;
3037 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003038
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003039 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003040
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003041 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3042 << "{\n"
3043 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003044
3045 for (size_t i = 0; i < fields.size(); i++)
3046 {
3047 const TField *field = fields[i];
3048 const TType *fieldType = field->type();
3049
3050 const TString &fieldNameA = "a." + Decorate(field->name());
3051 const TString &fieldNameB = "b." + Decorate(field->name());
3052
3053 if (i > 0)
3054 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003055 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003056 }
3057
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003058 fnOut << "(";
3059 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3060 fnOut << fieldNameA;
3061 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3062 fnOut << fieldNameB;
3063 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3064 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003065 }
3066
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003067 fnOut << ";\n" << "}\n";
3068
3069 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003070
3071 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003072 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003073
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003074 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003075}
3076
Olli Etuaho7fb49552015-03-18 17:27:44 +02003077TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3078{
3079 for (const auto &eqFunction : mArrayEqualityFunctions)
3080 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003081 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003082 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003083 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003084 }
3085 }
3086
3087 const TString &typeName = TypeString(type);
3088
Olli Etuaho12690762015-03-31 12:55:28 +03003089 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003090 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003091
3092 TInfoSinkBase fnNameOut;
3093 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003094 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003095
3096 TType nonArrayType = type;
3097 nonArrayType.clearArrayness();
3098
3099 TInfoSinkBase fnOut;
3100
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003101 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003102 << typeName << " a[" << type.getArraySize() << "], "
3103 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003104 << "{\n"
3105 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3106 " {\n"
3107 " if (";
3108
3109 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3110 fnOut << "a[i]";
3111 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3112 fnOut << "b[i]";
3113 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3114
3115 fnOut << ") { return false; }\n"
3116 " }\n"
3117 " return true;\n"
3118 "}\n";
3119
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003120 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003121
3122 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003123 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003124
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003125 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003126}
3127
Olli Etuaho12690762015-03-31 12:55:28 +03003128TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3129{
3130 for (const auto &assignFunction : mArrayAssignmentFunctions)
3131 {
3132 if (assignFunction.type == type)
3133 {
3134 return assignFunction.functionName;
3135 }
3136 }
3137
3138 const TString &typeName = TypeString(type);
3139
3140 ArrayHelperFunction function;
3141 function.type = type;
3142
3143 TInfoSinkBase fnNameOut;
3144 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3145 function.functionName = fnNameOut.c_str();
3146
3147 TInfoSinkBase fnOut;
3148
3149 fnOut << "void " << function.functionName << "(out "
3150 << typeName << " a[" << type.getArraySize() << "], "
3151 << typeName << " b[" << type.getArraySize() << "])\n"
3152 << "{\n"
3153 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3154 " {\n"
3155 " a[i] = b[i];\n"
3156 " }\n"
3157 "}\n";
3158
3159 function.functionDefinition = fnOut.c_str();
3160
3161 mArrayAssignmentFunctions.push_back(function);
3162
3163 return function.functionName;
3164}
3165
Olli Etuaho9638c352015-04-01 14:34:52 +03003166TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3167{
3168 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3169 {
3170 if (constructIntoFunction.type == type)
3171 {
3172 return constructIntoFunction.functionName;
3173 }
3174 }
3175
3176 const TString &typeName = TypeString(type);
3177
3178 ArrayHelperFunction function;
3179 function.type = type;
3180
3181 TInfoSinkBase fnNameOut;
3182 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3183 function.functionName = fnNameOut.c_str();
3184
3185 TInfoSinkBase fnOut;
3186
3187 fnOut << "void " << function.functionName << "(out "
3188 << typeName << " a[" << type.getArraySize() << "]";
3189 for (int i = 0; i < type.getArraySize(); ++i)
3190 {
3191 fnOut << ", " << typeName << " b" << i;
3192 }
3193 fnOut << ")\n"
3194 "{\n";
3195
3196 for (int i = 0; i < type.getArraySize(); ++i)
3197 {
3198 fnOut << " a[" << i << "] = b" << i << ";\n";
3199 }
3200 fnOut << "}\n";
3201
3202 function.functionDefinition = fnOut.c_str();
3203
3204 mArrayConstructIntoFunctions.push_back(function);
3205
3206 return function.functionName;
3207}
3208
3209
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003210}