blob: cb7931ae2efac9196080b2c94dd3593bc06057f2 [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"
Olli Etuahod57e0db2015-04-24 15:05:08 +030014#include "common/debug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020016#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/FlagStd140Structs.h"
19#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/RewriteElseBlocks.h"
23#include "compiler/translator/SearchSymbol.h"
24#include "compiler/translator/StructureHLSL.h"
25#include "compiler/translator/TranslatorHLSL.h"
26#include "compiler/translator/UnfoldShortCircuit.h"
27#include "compiler/translator/UniformHLSL.h"
28#include "compiler/translator/UtilsHLSL.h"
29#include "compiler/translator/blocklayout.h"
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);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300188 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700189 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700190
Jamie Madill37997142015-01-28 10:06:34 -0500191 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500192 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200193 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500194 mInfoSinkStack.pop();
195
Jamie Madill37997142015-01-28 10:06:34 -0500196 mInfoSinkStack.push(&mFooter);
197 if (!mDeferredGlobalInitializers.empty())
198 {
199 writeDeferredGlobalInitializers(mFooter);
200 }
201 mInfoSinkStack.pop();
202
Jamie Madill32aab012015-01-27 14:12:26 -0500203 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200204 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500205 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000206
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200207 objSink << mHeader.c_str();
208 objSink << mBody.c_str();
209 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200210
211 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000212}
213
Jamie Madill570e04d2013-06-21 09:15:33 -0400214void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
215{
216 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
217 {
218 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
219
Jamie Madill32aab012015-01-27 14:12:26 -0500220 TInfoSinkBase structInfoSink;
221 mInfoSinkStack.push(&structInfoSink);
222
Jamie Madill570e04d2013-06-21 09:15:33 -0400223 // This will mark the necessary block elements as referenced
224 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500225
226 TString structName(structInfoSink.c_str());
227 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400228
229 mFlaggedStructOriginalNames[flaggedNode] = structName;
230
231 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
232 {
233 structName.erase(pos, 1);
234 }
235
236 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
237 }
238}
239
Jamie Madill4e1fd412014-07-10 17:50:10 -0400240const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
241{
242 return mUniformHLSL->getInterfaceBlockRegisterMap();
243}
244
Jamie Madill9fe25e92014-07-18 10:33:08 -0400245const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
246{
247 return mUniformHLSL->getUniformRegisterMap();
248}
249
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000250int OutputHLSL::vectorSize(const TType &type) const
251{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000252 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000253 int arraySize = type.isArray() ? type.getArraySize() : 1;
254
255 return elementSize * arraySize;
256}
257
Jamie Madill98493dd2013-07-08 14:39:03 -0400258TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400259{
260 TString init;
261
262 TString preIndentString;
263 TString fullIndentString;
264
265 for (int spaces = 0; spaces < (indent * 4); spaces++)
266 {
267 preIndentString += ' ';
268 }
269
270 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
271 {
272 fullIndentString += ' ';
273 }
274
275 init += preIndentString + "{\n";
276
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 const TFieldList &fields = structure.fields();
278 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400279 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400280 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400281 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400282 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400283
Jamie Madill98493dd2013-07-08 14:39:03 -0400284 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400285 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400287 }
288 else
289 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400290 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400291 }
292 }
293
294 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
295
296 return init;
297}
298
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200299void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300{
Jamie Madill32aab012015-01-27 14:12:26 -0500301 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000303 TString varyings;
304 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400305 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000306
Jamie Madill829f59e2013-11-13 19:40:54 -0500307 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400308 {
309 TIntermTyped *structNode = flaggedStructIt->first;
310 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400311 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400312 const TString &originalName = mFlaggedStructOriginalNames[structNode];
313
Jamie Madill033dae62014-06-18 12:56:28 -0400314 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400315 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400316 flaggedStructs += "\n";
317 }
318
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000319 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
320 {
321 const TType &type = varying->second->getType();
322 const TString &name = varying->second->getSymbol();
323
324 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400325 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
326 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000327 }
328
329 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
330 {
331 const TType &type = attribute->second->getType();
332 const TString &name = attribute->second->getSymbol();
333
Jamie Madill033dae62014-06-18 12:56:28 -0400334 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000335 }
336
Jamie Madill8daaba12014-06-13 10:04:33 -0400337 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400338
Jamie Madillf91ce812014-06-13 10:04:34 -0400339 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
340 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
341
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200342 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500343 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200344 out << "\n// Equality functions\n\n";
345 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500346 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200347 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200348 }
349 }
Olli Etuaho12690762015-03-31 12:55:28 +0300350 if (!mArrayAssignmentFunctions.empty())
351 {
352 out << "\n// Assignment functions\n\n";
353 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
354 {
355 out << assignmentFunction.functionDefinition << "\n";
356 }
357 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300358 if (!mArrayConstructIntoFunctions.empty())
359 {
360 out << "\n// Array constructor functions\n\n";
361 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
362 {
363 out << constructIntoFunction.functionDefinition << "\n";
364 }
365 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200366
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500367 if (mUsesDiscardRewriting)
368 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400369 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500370 }
371
Nicolas Capens655fe362014-04-11 13:12:34 -0400372 if (mUsesNestedBreak)
373 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400374 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400375 }
376
Arun Patole44efa0b2015-03-04 17:11:05 +0530377 if (mRequiresIEEEStrictCompiling)
378 {
379 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
380 }
381
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400382 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
383 "#define LOOP [loop]\n"
384 "#define FLATTEN [flatten]\n"
385 "#else\n"
386 "#define LOOP\n"
387 "#define FLATTEN\n"
388 "#endif\n";
389
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200390 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200392 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
393 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000394
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000395 out << "// Varyings\n";
396 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400397 out << "\n";
398
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200399 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000400 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500401 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000402 {
Jamie Madill46131a32013-06-20 11:55:50 -0400403 const TString &variableName = outputVariableIt->first;
404 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400405
Jamie Madill033dae62014-06-18 12:56:28 -0400406 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400407 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000408 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000409 }
Jamie Madill46131a32013-06-20 11:55:50 -0400410 else
411 {
412 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
413
414 out << "static float4 gl_Color[" << numColorValues << "] =\n"
415 "{\n";
416 for (unsigned int i = 0; i < numColorValues; i++)
417 {
418 out << " float4(0, 0, 0, 0)";
419 if (i + 1 != numColorValues)
420 {
421 out << ",";
422 }
423 out << "\n";
424 }
425
426 out << "};\n";
427 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000428
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400429 if (mUsesFragDepth)
430 {
431 out << "static float gl_Depth = 0.0;\n";
432 }
433
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000434 if (mUsesFragCoord)
435 {
436 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
437 }
438
439 if (mUsesPointCoord)
440 {
441 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
442 }
443
444 if (mUsesFrontFacing)
445 {
446 out << "static bool gl_FrontFacing = false;\n";
447 }
448
449 out << "\n";
450
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000451 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000452 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000453 out << "struct gl_DepthRangeParameters\n"
454 "{\n"
455 " float near;\n"
456 " float far;\n"
457 " float diff;\n"
458 "};\n"
459 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000460 }
461
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000462 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000463 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000464 out << "cbuffer DriverConstants : register(b1)\n"
465 "{\n";
466
467 if (mUsesDepthRange)
468 {
469 out << " float3 dx_DepthRange : packoffset(c0);\n";
470 }
471
472 if (mUsesFragCoord)
473 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000474 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000475 }
476
477 if (mUsesFragCoord || mUsesFrontFacing)
478 {
479 out << " float3 dx_DepthFront : packoffset(c2);\n";
480 }
481
482 out << "};\n";
483 }
484 else
485 {
486 if (mUsesDepthRange)
487 {
488 out << "uniform float3 dx_DepthRange : register(c0);";
489 }
490
491 if (mUsesFragCoord)
492 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000493 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000494 }
495
496 if (mUsesFragCoord || mUsesFrontFacing)
497 {
498 out << "uniform float3 dx_DepthFront : register(c2);\n";
499 }
500 }
501
502 out << "\n";
503
504 if (mUsesDepthRange)
505 {
506 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
507 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000508 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000509
Jamie Madillf91ce812014-06-13 10:04:34 -0400510 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000511 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400512 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000513 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400514 out << flaggedStructs;
515 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000516 }
517
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000518 if (usingMRTExtension && mNumRenderTargets > 1)
519 {
520 out << "#define GL_USES_MRT\n";
521 }
522
523 if (mUsesFragColor)
524 {
525 out << "#define GL_USES_FRAG_COLOR\n";
526 }
527
528 if (mUsesFragData)
529 {
530 out << "#define GL_USES_FRAG_DATA\n";
531 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000533 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000535 out << "// Attributes\n";
536 out << attributes;
537 out << "\n"
538 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400539
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000540 if (mUsesPointSize)
541 {
542 out << "static float gl_PointSize = float(1);\n";
543 }
544
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000545 if (mUsesInstanceID)
546 {
547 out << "static int gl_InstanceID;";
548 }
549
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000550 out << "\n"
551 "// Varyings\n";
552 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000553 out << "\n";
554
555 if (mUsesDepthRange)
556 {
557 out << "struct gl_DepthRangeParameters\n"
558 "{\n"
559 " float near;\n"
560 " float far;\n"
561 " float diff;\n"
562 "};\n"
563 "\n";
564 }
565
566 if (mOutputType == SH_HLSL11_OUTPUT)
567 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800568 out << "cbuffer DriverConstants : register(b1)\n"
569 "{\n";
570
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000571 if (mUsesDepthRange)
572 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800573 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000574 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800575
Cooper Partine6664f02015-01-09 16:22:24 -0800576 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800577 // However, we declare it for all shaders (including Feature Level 10+).
578 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
579 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800580 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800581
582 out << "};\n"
583 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000584 }
585 else
586 {
587 if (mUsesDepthRange)
588 {
589 out << "uniform float3 dx_DepthRange : register(c0);\n";
590 }
591
Cooper Partine6664f02015-01-09 16:22:24 -0800592 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
593 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000594 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000595 }
596
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000597 if (mUsesDepthRange)
598 {
599 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
600 "\n";
601 }
602
Jamie Madillf91ce812014-06-13 10:04:34 -0400603 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000604 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400605 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000606 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400607 out << flaggedStructs;
608 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000609 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400610 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000611
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400612 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
613 {
614 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400615 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000616 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400617 switch(textureFunction->sampler)
618 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400619 case EbtSampler2D: out << "int2 "; break;
620 case EbtSampler3D: out << "int3 "; break;
621 case EbtSamplerCube: out << "int2 "; break;
622 case EbtSampler2DArray: out << "int3 "; break;
623 case EbtISampler2D: out << "int2 "; break;
624 case EbtISampler3D: out << "int3 "; break;
625 case EbtISamplerCube: out << "int2 "; break;
626 case EbtISampler2DArray: out << "int3 "; break;
627 case EbtUSampler2D: out << "int2 "; break;
628 case EbtUSampler3D: out << "int3 "; break;
629 case EbtUSamplerCube: out << "int2 "; break;
630 case EbtUSampler2DArray: out << "int3 "; break;
631 case EbtSampler2DShadow: out << "int2 "; break;
632 case EbtSamplerCubeShadow: out << "int2 "; break;
633 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400634 default: UNREACHABLE();
635 }
636 }
637 else // Sampling function
638 {
639 switch(textureFunction->sampler)
640 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400641 case EbtSampler2D: out << "float4 "; break;
642 case EbtSampler3D: out << "float4 "; break;
643 case EbtSamplerCube: out << "float4 "; break;
644 case EbtSampler2DArray: out << "float4 "; break;
645 case EbtISampler2D: out << "int4 "; break;
646 case EbtISampler3D: out << "int4 "; break;
647 case EbtISamplerCube: out << "int4 "; break;
648 case EbtISampler2DArray: out << "int4 "; break;
649 case EbtUSampler2D: out << "uint4 "; break;
650 case EbtUSampler3D: out << "uint4 "; break;
651 case EbtUSamplerCube: out << "uint4 "; break;
652 case EbtUSampler2DArray: out << "uint4 "; break;
653 case EbtSampler2DShadow: out << "float "; break;
654 case EbtSamplerCubeShadow: out << "float "; break;
655 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400656 default: UNREACHABLE();
657 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000658 }
659
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400660 // Function name
661 out << textureFunction->name();
662
663 // Argument list
664 int hlslCoords = 4;
665
666 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000667 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400668 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000669 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400670 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
671 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
672 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000673 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400674
Nicolas Capens75fb4752013-07-10 15:14:47 -0400675 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000676 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400677 case TextureFunction::IMPLICIT: break;
678 case TextureFunction::BIAS: hlslCoords = 4; break;
679 case TextureFunction::LOD: hlslCoords = 4; break;
680 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400681 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400682 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000683 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400684 }
685 else if (mOutputType == SH_HLSL11_OUTPUT)
686 {
687 switch(textureFunction->sampler)
688 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400689 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
690 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
691 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
692 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
693 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
694 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500695 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400696 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
697 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
698 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500699 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400700 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
701 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
702 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
703 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400704 default: UNREACHABLE();
705 }
706 }
707 else UNREACHABLE();
708
Nicolas Capensfc014542014-02-18 14:47:13 -0500709 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400710 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500711 switch(textureFunction->coords)
712 {
713 case 2: out << ", int2 t"; break;
714 case 3: out << ", int3 t"; break;
715 default: UNREACHABLE();
716 }
717 }
718 else // Floating-point coordinates (except textureSize)
719 {
720 switch(textureFunction->coords)
721 {
722 case 1: out << ", int lod"; break; // textureSize()
723 case 2: out << ", float2 t"; break;
724 case 3: out << ", float3 t"; break;
725 case 4: out << ", float4 t"; break;
726 default: UNREACHABLE();
727 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000728 }
729
Nicolas Capensd11d5492014-02-19 17:06:10 -0500730 if (textureFunction->method == TextureFunction::GRAD)
731 {
732 switch(textureFunction->sampler)
733 {
734 case EbtSampler2D:
735 case EbtISampler2D:
736 case EbtUSampler2D:
737 case EbtSampler2DArray:
738 case EbtISampler2DArray:
739 case EbtUSampler2DArray:
740 case EbtSampler2DShadow:
741 case EbtSampler2DArrayShadow:
742 out << ", float2 ddx, float2 ddy";
743 break;
744 case EbtSampler3D:
745 case EbtISampler3D:
746 case EbtUSampler3D:
747 case EbtSamplerCube:
748 case EbtISamplerCube:
749 case EbtUSamplerCube:
750 case EbtSamplerCubeShadow:
751 out << ", float3 ddx, float3 ddy";
752 break;
753 default: UNREACHABLE();
754 }
755 }
756
Nicolas Capens75fb4752013-07-10 15:14:47 -0400757 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000758 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400759 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400760 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400761 case TextureFunction::LOD: out << ", float lod"; break;
762 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400763 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400764 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500765 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500766 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400767 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000768 }
769
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500770 if (textureFunction->offset)
771 {
772 switch(textureFunction->sampler)
773 {
774 case EbtSampler2D: out << ", int2 offset"; break;
775 case EbtSampler3D: out << ", int3 offset"; break;
776 case EbtSampler2DArray: out << ", int2 offset"; break;
777 case EbtISampler2D: out << ", int2 offset"; break;
778 case EbtISampler3D: out << ", int3 offset"; break;
779 case EbtISampler2DArray: out << ", int2 offset"; break;
780 case EbtUSampler2D: out << ", int2 offset"; break;
781 case EbtUSampler3D: out << ", int3 offset"; break;
782 case EbtUSampler2DArray: out << ", int2 offset"; break;
783 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500784 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500785 default: UNREACHABLE();
786 }
787 }
788
Nicolas Capens84cfa122014-04-14 13:48:45 -0400789 if (textureFunction->method == TextureFunction::BIAS ||
790 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500791 {
792 out << ", float bias";
793 }
794
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400795 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400796 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400797
Nicolas Capens75fb4752013-07-10 15:14:47 -0400798 if (textureFunction->method == TextureFunction::SIZE)
799 {
800 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
801 {
802 if (IsSamplerArray(textureFunction->sampler))
803 {
804 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
805 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
806 }
807 else
808 {
809 out << " uint width; uint height; uint numberOfLevels;\n"
810 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
811 }
812 }
813 else if (IsSampler3D(textureFunction->sampler))
814 {
815 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
816 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
817 }
818 else UNREACHABLE();
819
820 switch(textureFunction->sampler)
821 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400822 case EbtSampler2D: out << " return int2(width, height);"; break;
823 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
824 case EbtSamplerCube: out << " return int2(width, height);"; break;
825 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
826 case EbtISampler2D: out << " return int2(width, height);"; break;
827 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
828 case EbtISamplerCube: out << " return int2(width, height);"; break;
829 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
830 case EbtUSampler2D: out << " return int2(width, height);"; break;
831 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
832 case EbtUSamplerCube: out << " return int2(width, height);"; break;
833 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
834 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
835 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
836 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400837 default: UNREACHABLE();
838 }
839 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400840 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400841 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500842 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
843 {
844 out << " float width; float height; float layers; float levels;\n";
845
846 out << " uint mip = 0;\n";
847
848 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
849
850 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
851 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
852 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
853 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
854
855 // FACE_POSITIVE_X = 000b
856 // FACE_NEGATIVE_X = 001b
857 // FACE_POSITIVE_Y = 010b
858 // FACE_NEGATIVE_Y = 011b
859 // FACE_POSITIVE_Z = 100b
860 // FACE_NEGATIVE_Z = 101b
861 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
862
863 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
864 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
865 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
866
867 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
868 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
869 }
870 else if (IsIntegerSampler(textureFunction->sampler) &&
871 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400872 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400873 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400874 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400875 if (IsSamplerArray(textureFunction->sampler))
876 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400877 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400878
Nicolas Capens9edebd62013-08-06 10:59:10 -0400879 if (textureFunction->method == TextureFunction::LOD0)
880 {
881 out << " uint mip = 0;\n";
882 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400883 else if (textureFunction->method == TextureFunction::LOD0BIAS)
884 {
885 out << " uint mip = bias;\n";
886 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400887 else
888 {
889 if (textureFunction->method == TextureFunction::IMPLICIT ||
890 textureFunction->method == TextureFunction::BIAS)
891 {
892 out << " x.GetDimensions(0, width, height, layers, levels);\n"
893 " float2 tSized = float2(t.x * width, t.y * height);\n"
894 " float dx = length(ddx(tSized));\n"
895 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500896 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400897
898 if (textureFunction->method == TextureFunction::BIAS)
899 {
900 out << " lod += bias;\n";
901 }
902 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500903 else if (textureFunction->method == TextureFunction::GRAD)
904 {
905 out << " x.GetDimensions(0, width, height, layers, levels);\n"
906 " float lod = log2(max(length(ddx), length(ddy)));\n";
907 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400908
909 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
910 }
911
912 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400913 }
914 else
915 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400916 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400917
Nicolas Capens9edebd62013-08-06 10:59:10 -0400918 if (textureFunction->method == TextureFunction::LOD0)
919 {
920 out << " uint mip = 0;\n";
921 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400922 else if (textureFunction->method == TextureFunction::LOD0BIAS)
923 {
924 out << " uint mip = bias;\n";
925 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400926 else
927 {
928 if (textureFunction->method == TextureFunction::IMPLICIT ||
929 textureFunction->method == TextureFunction::BIAS)
930 {
931 out << " x.GetDimensions(0, width, height, levels);\n"
932 " float2 tSized = float2(t.x * width, t.y * height);\n"
933 " float dx = length(ddx(tSized));\n"
934 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500935 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400936
937 if (textureFunction->method == TextureFunction::BIAS)
938 {
939 out << " lod += bias;\n";
940 }
941 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500942 else if (textureFunction->method == TextureFunction::LOD)
943 {
944 out << " x.GetDimensions(0, width, height, levels);\n";
945 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500946 else if (textureFunction->method == TextureFunction::GRAD)
947 {
948 out << " x.GetDimensions(0, width, height, levels);\n"
949 " float lod = log2(max(length(ddx), length(ddy)));\n";
950 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400951
952 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
953 }
954
955 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400956 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400957 }
958 else if (IsSampler3D(textureFunction->sampler))
959 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400960 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400961
Nicolas Capens9edebd62013-08-06 10:59:10 -0400962 if (textureFunction->method == TextureFunction::LOD0)
963 {
964 out << " uint mip = 0;\n";
965 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400966 else if (textureFunction->method == TextureFunction::LOD0BIAS)
967 {
968 out << " uint mip = bias;\n";
969 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400970 else
971 {
972 if (textureFunction->method == TextureFunction::IMPLICIT ||
973 textureFunction->method == TextureFunction::BIAS)
974 {
975 out << " x.GetDimensions(0, width, height, depth, levels);\n"
976 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
977 " float dx = length(ddx(tSized));\n"
978 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500979 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400980
981 if (textureFunction->method == TextureFunction::BIAS)
982 {
983 out << " lod += bias;\n";
984 }
985 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500986 else if (textureFunction->method == TextureFunction::GRAD)
987 {
988 out << " x.GetDimensions(0, width, height, depth, levels);\n"
989 " float lod = log2(max(length(ddx), length(ddy)));\n";
990 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400991
992 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
993 }
994
995 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400996 }
997 else UNREACHABLE();
998 }
999
1000 out << " return ";
1001
1002 // HLSL intrinsic
1003 if (mOutputType == SH_HLSL9_OUTPUT)
1004 {
1005 switch(textureFunction->sampler)
1006 {
1007 case EbtSampler2D: out << "tex2D"; break;
1008 case EbtSamplerCube: out << "texCUBE"; break;
1009 default: UNREACHABLE();
1010 }
1011
Nicolas Capens75fb4752013-07-10 15:14:47 -04001012 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001013 {
1014 case TextureFunction::IMPLICIT: out << "(s, "; break;
1015 case TextureFunction::BIAS: out << "bias(s, "; break;
1016 case TextureFunction::LOD: out << "lod(s, "; break;
1017 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001018 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001019 default: UNREACHABLE();
1020 }
1021 }
1022 else if (mOutputType == SH_HLSL11_OUTPUT)
1023 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001024 if (textureFunction->method == TextureFunction::GRAD)
1025 {
1026 if (IsIntegerSampler(textureFunction->sampler))
1027 {
1028 out << "x.Load(";
1029 }
1030 else if (IsShadowSampler(textureFunction->sampler))
1031 {
1032 out << "x.SampleCmpLevelZero(s, ";
1033 }
1034 else
1035 {
1036 out << "x.SampleGrad(s, ";
1037 }
1038 }
1039 else if (IsIntegerSampler(textureFunction->sampler) ||
1040 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001041 {
1042 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001043 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001044 else if (IsShadowSampler(textureFunction->sampler))
1045 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001046 switch(textureFunction->method)
1047 {
1048 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1049 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1050 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1051 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1052 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1053 default: UNREACHABLE();
1054 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001055 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001056 else
1057 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001058 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001059 {
1060 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1061 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1062 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1063 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001064 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001065 default: UNREACHABLE();
1066 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001067 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001068 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001069 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001070
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001071 // Integer sampling requires integer addresses
1072 TString addressx = "";
1073 TString addressy = "";
1074 TString addressz = "";
1075 TString close = "";
1076
Nicolas Capensfc014542014-02-18 14:47:13 -05001077 if (IsIntegerSampler(textureFunction->sampler) ||
1078 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001079 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001080 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001081 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001082 case 2: out << "int3("; break;
1083 case 3: out << "int4("; break;
1084 default: UNREACHABLE();
1085 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001086
Nicolas Capensfc014542014-02-18 14:47:13 -05001087 // Convert from normalized floating-point to integer
1088 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001089 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001090 addressx = "int(floor(width * frac((";
1091 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001092
Nicolas Capensfc014542014-02-18 14:47:13 -05001093 if (IsSamplerArray(textureFunction->sampler))
1094 {
1095 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1096 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001097 else if (IsSamplerCube(textureFunction->sampler))
1098 {
1099 addressz = "((((";
1100 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001101 else
1102 {
1103 addressz = "int(floor(depth * frac((";
1104 }
1105
1106 close = "))))";
1107 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001108 }
1109 else
1110 {
1111 switch(hlslCoords)
1112 {
1113 case 2: out << "float2("; break;
1114 case 3: out << "float3("; break;
1115 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001116 default: UNREACHABLE();
1117 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001118 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001119
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001120 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001121
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001122 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001123 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001124 switch(textureFunction->coords)
1125 {
1126 case 3: proj = " / t.z"; break;
1127 case 4: proj = " / t.w"; break;
1128 default: UNREACHABLE();
1129 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001130 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001131
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001132 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001133
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001134 if (mOutputType == SH_HLSL9_OUTPUT)
1135 {
1136 if (hlslCoords >= 3)
1137 {
1138 if (textureFunction->coords < 3)
1139 {
1140 out << ", 0";
1141 }
1142 else
1143 {
1144 out << ", t.z" + proj;
1145 }
1146 }
1147
1148 if (hlslCoords == 4)
1149 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001150 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001151 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001152 case TextureFunction::BIAS: out << ", bias"; break;
1153 case TextureFunction::LOD: out << ", lod"; break;
1154 case TextureFunction::LOD0: out << ", 0"; break;
1155 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001156 default: UNREACHABLE();
1157 }
1158 }
1159
1160 out << "));\n";
1161 }
1162 else if (mOutputType == SH_HLSL11_OUTPUT)
1163 {
1164 if (hlslCoords >= 3)
1165 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001166 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1167 {
1168 out << ", face";
1169 }
1170 else
1171 {
1172 out << ", " + addressz + ("t.z" + proj) + close;
1173 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001174 }
1175
Nicolas Capensd11d5492014-02-19 17:06:10 -05001176 if (textureFunction->method == TextureFunction::GRAD)
1177 {
1178 if (IsIntegerSampler(textureFunction->sampler))
1179 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001180 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001181 }
1182 else if (IsShadowSampler(textureFunction->sampler))
1183 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001184 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001185 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001186 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001187 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1188 // The resulting third component of P' in the shadow forms is used as Dref
1189 out << "), t.z" << proj;
1190 }
1191 else
1192 {
1193 switch(textureFunction->coords)
1194 {
1195 case 3: out << "), t.z"; break;
1196 case 4: out << "), t.w"; break;
1197 default: UNREACHABLE();
1198 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001199 }
1200 }
1201 else
1202 {
1203 out << "), ddx, ddy";
1204 }
1205 }
1206 else if (IsIntegerSampler(textureFunction->sampler) ||
1207 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001208 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001209 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001210 }
1211 else if (IsShadowSampler(textureFunction->sampler))
1212 {
1213 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001214 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001215 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001216 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1217 // The resulting third component of P' in the shadow forms is used as Dref
1218 out << "), t.z" << proj;
1219 }
1220 else
1221 {
1222 switch(textureFunction->coords)
1223 {
1224 case 3: out << "), t.z"; break;
1225 case 4: out << "), t.w"; break;
1226 default: UNREACHABLE();
1227 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001228 }
1229 }
1230 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001231 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001232 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001233 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001234 case TextureFunction::IMPLICIT: out << ")"; break;
1235 case TextureFunction::BIAS: out << "), bias"; break;
1236 case TextureFunction::LOD: out << "), lod"; break;
1237 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001238 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001239 default: UNREACHABLE();
1240 }
1241 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001242
1243 if (textureFunction->offset)
1244 {
1245 out << ", offset";
1246 }
1247
1248 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001249 }
1250 else UNREACHABLE();
1251 }
1252
1253 out << "\n"
1254 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001255 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001256 }
1257
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001258 if (mUsesFragCoord)
1259 {
1260 out << "#define GL_USES_FRAG_COORD\n";
1261 }
1262
1263 if (mUsesPointCoord)
1264 {
1265 out << "#define GL_USES_POINT_COORD\n";
1266 }
1267
1268 if (mUsesFrontFacing)
1269 {
1270 out << "#define GL_USES_FRONT_FACING\n";
1271 }
1272
1273 if (mUsesPointSize)
1274 {
1275 out << "#define GL_USES_POINT_SIZE\n";
1276 }
1277
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001278 if (mUsesFragDepth)
1279 {
1280 out << "#define GL_USES_FRAG_DEPTH\n";
1281 }
1282
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001283 if (mUsesDepthRange)
1284 {
1285 out << "#define GL_USES_DEPTH_RANGE\n";
1286 }
1287
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001288 if (mUsesXor)
1289 {
1290 out << "bool xor(bool p, bool q)\n"
1291 "{\n"
1292 " return (p || q) && !(p && q);\n"
1293 "}\n"
1294 "\n";
1295 }
1296
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001297 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001298}
1299
1300void OutputHLSL::visitSymbol(TIntermSymbol *node)
1301{
Jamie Madill32aab012015-01-27 14:12:26 -05001302 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001303
Jamie Madill570e04d2013-06-21 09:15:33 -04001304 // Handle accessing std140 structs by value
1305 if (mFlaggedStructMappedNames.count(node) > 0)
1306 {
1307 out << mFlaggedStructMappedNames[node];
1308 return;
1309 }
1310
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001311 TString name = node->getSymbol();
1312
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001313 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001314 {
1315 mUsesDepthRange = true;
1316 out << name;
1317 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001318 else
1319 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001320 TQualifier qualifier = node->getQualifier();
1321
1322 if (qualifier == EvqUniform)
1323 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001324 const TType &nodeType = node->getType();
1325 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001326
1327 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001328 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001329 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001330 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001331 else
1332 {
1333 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001334 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001335
Jamie Madill2e295e22015-04-29 10:41:33 -04001336 ensureStructDefined(nodeType);
1337
Jamie Madill033dae62014-06-18 12:56:28 -04001338 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001339 }
Jamie Madill19571812013-08-12 15:26:34 -07001340 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001341 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001342 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001343 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001344 }
Jamie Madill033dae62014-06-18 12:56:28 -04001345 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001346 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001347 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001348 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001349 }
Jamie Madill19571812013-08-12 15:26:34 -07001350 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001351 {
1352 mReferencedOutputVariables[name] = node;
1353 out << "out_" << name;
1354 }
1355 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001356 {
1357 out << "gl_Color[0]";
1358 mUsesFragColor = true;
1359 }
1360 else if (qualifier == EvqFragData)
1361 {
1362 out << "gl_Color";
1363 mUsesFragData = true;
1364 }
1365 else if (qualifier == EvqFragCoord)
1366 {
1367 mUsesFragCoord = true;
1368 out << name;
1369 }
1370 else if (qualifier == EvqPointCoord)
1371 {
1372 mUsesPointCoord = true;
1373 out << name;
1374 }
1375 else if (qualifier == EvqFrontFacing)
1376 {
1377 mUsesFrontFacing = true;
1378 out << name;
1379 }
1380 else if (qualifier == EvqPointSize)
1381 {
1382 mUsesPointSize = true;
1383 out << name;
1384 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001385 else if (qualifier == EvqInstanceID)
1386 {
1387 mUsesInstanceID = true;
1388 out << name;
1389 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001390 else if (name == "gl_FragDepthEXT")
1391 {
1392 mUsesFragDepth = true;
1393 out << "gl_Depth";
1394 }
Olli Etuaho78174db2015-04-21 16:14:00 +03001395 else if (node->isInternal())
Jamie Madille53c98b2014-02-03 11:57:13 -05001396 {
1397 out << name;
1398 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001399 else
1400 {
Jamie Madill033dae62014-06-18 12:56:28 -04001401 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001402 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001403 }
1404}
1405
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001406void OutputHLSL::visitRaw(TIntermRaw *node)
1407{
Jamie Madill32aab012015-01-27 14:12:26 -05001408 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001409}
1410
Olli Etuaho7fb49552015-03-18 17:27:44 +02001411void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1412{
1413 if (type.isScalar() && !type.isArray())
1414 {
1415 if (op == EOpEqual)
1416 {
1417 outputTriplet(visit, "(", " == ", ")", out);
1418 }
1419 else
1420 {
1421 outputTriplet(visit, "(", " != ", ")", out);
1422 }
1423 }
1424 else
1425 {
1426 if (visit == PreVisit && op == EOpNotEqual)
1427 {
1428 out << "!";
1429 }
1430
1431 if (type.isArray())
1432 {
1433 const TString &functionName = addArrayEqualityFunction(type);
1434 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1435 }
1436 else if (type.getBasicType() == EbtStruct)
1437 {
1438 const TStructure &structure = *type.getStruct();
1439 const TString &functionName = addStructEqualityFunction(structure);
1440 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1441 }
1442 else
1443 {
1444 ASSERT(type.isMatrix() || type.isVector());
1445 outputTriplet(visit, "all(", " == ", ")", out);
1446 }
1447 }
1448}
1449
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001450bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1451{
Jamie Madill32aab012015-01-27 14:12:26 -05001452 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001453
Jamie Madill570e04d2013-06-21 09:15:33 -04001454 // Handle accessing std140 structs by value
1455 if (mFlaggedStructMappedNames.count(node) > 0)
1456 {
1457 out << mFlaggedStructMappedNames[node];
1458 return false;
1459 }
1460
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001461 switch (node->getOp())
1462 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001463 case EOpAssign:
1464 if (node->getLeft()->isArray())
1465 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001466 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1467 if (rightAgg != nullptr && rightAgg->isConstructor())
1468 {
1469 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1470 out << functionName << "(";
1471 node->getLeft()->traverse(this);
1472 TIntermSequence *seq = rightAgg->getSequence();
1473 for (auto &arrayElement : *seq)
1474 {
1475 out << ", ";
1476 arrayElement->traverse(this);
1477 }
1478 out << ")";
1479 return false;
1480 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001481 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1482 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1483
1484 const TString &functionName = addArrayAssignmentFunction(node->getType());
1485 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001486 }
1487 else
1488 {
1489 outputTriplet(visit, "(", " = ", ")");
1490 }
1491 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001492 case EOpInitialize:
1493 if (visit == PreVisit)
1494 {
1495 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1496 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1497 // new variable is created before the assignment is evaluated), so we need to convert
1498 // this to "float t = x, x = t;".
1499
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001500 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001501 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001502 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001503
Jamie Madill37997142015-01-28 10:06:34 -05001504 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1505 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001506 {
Jamie Madill37997142015-01-28 10:06:34 -05001507 // For variables which are not constant, defer their real initialization until
1508 // after we initialize other globals: uniforms, attributes and varyings.
1509 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1510 const TString &initString = initializer(node->getType());
1511 node->setRight(new TIntermRaw(node->getType(), initString));
1512 }
1513 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1514 {
1515 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001516 return false;
1517 }
1518 }
1519 else if (visit == InVisit)
1520 {
1521 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001522 }
1523 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001524 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1525 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1526 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1527 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1528 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1529 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001530 if (visit == PreVisit)
1531 {
1532 out << "(";
1533 }
1534 else if (visit == InVisit)
1535 {
1536 out << " = mul(";
1537 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001538 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001539 }
1540 else
1541 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001542 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001543 }
1544 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001545 case EOpMatrixTimesMatrixAssign:
1546 if (visit == PreVisit)
1547 {
1548 out << "(";
1549 }
1550 else if (visit == InVisit)
1551 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001552 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001553 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001554 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001555 }
1556 else
1557 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001558 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001559 }
1560 break;
1561 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001562 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001563 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1564 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1565 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1566 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1567 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001568 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001569 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001570 const TType& leftType = node->getLeft()->getType();
1571 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001572 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001573 if (visit == PreVisit)
1574 {
1575 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1576 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001577 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001578 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001579 return false;
1580 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001581 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001582 else
1583 {
1584 outputTriplet(visit, "", "[", "]");
1585 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001586 }
1587 break;
1588 case EOpIndexIndirect:
1589 // We do not currently support indirect references to interface blocks
1590 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1591 outputTriplet(visit, "", "[", "]");
1592 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001593 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001594 if (visit == InVisit)
1595 {
1596 const TStructure* structure = node->getLeft()->getType().getStruct();
1597 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1598 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001599 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001600
1601 return false;
1602 }
1603 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001604 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001605 if (visit == InVisit)
1606 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001607 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1608 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1609 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001610 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001611
1612 return false;
1613 }
1614 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001615 case EOpVectorSwizzle:
1616 if (visit == InVisit)
1617 {
1618 out << ".";
1619
1620 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1621
1622 if (swizzle)
1623 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001624 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001625
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001626 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001627 {
1628 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1629
1630 if (element)
1631 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001632 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001633
1634 switch (i)
1635 {
1636 case 0: out << "x"; break;
1637 case 1: out << "y"; break;
1638 case 2: out << "z"; break;
1639 case 3: out << "w"; break;
1640 default: UNREACHABLE();
1641 }
1642 }
1643 else UNREACHABLE();
1644 }
1645 }
1646 else UNREACHABLE();
1647
1648 return false; // Fully processed
1649 }
1650 break;
1651 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1652 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1653 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1654 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001655 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001656 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1657 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1658 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1659 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1660 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001661 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001662 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001663 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001664 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001665 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1666 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1667 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1668 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1669 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001670 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001671 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1672 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001673 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001674 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001675 if (node->getRight()->hasSideEffects())
1676 {
1677 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1678 return false;
1679 }
1680 else
1681 {
1682 outputTriplet(visit, "(", " || ", ")");
1683 return true;
1684 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001685 case EOpLogicalXor:
1686 mUsesXor = true;
1687 outputTriplet(visit, "xor(", ", ", ")");
1688 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001689 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001690 if (node->getRight()->hasSideEffects())
1691 {
1692 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1693 return false;
1694 }
1695 else
1696 {
1697 outputTriplet(visit, "(", " && ", ")");
1698 return true;
1699 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700 default: UNREACHABLE();
1701 }
1702
1703 return true;
1704}
1705
1706bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1707{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001708 switch (node->getOp())
1709 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001710 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001711 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001712 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1713 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001714 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001715 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1716 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1717 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1718 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001719 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1720 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1721 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1722 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1723 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1724 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1725 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1726 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001727 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1728 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1729 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1730 case EOpAsinh:
1731 ASSERT(node->getUseEmulatedFunction());
1732 writeEmulatedFunctionTriplet(visit, "asinh(");
1733 break;
1734 case EOpAcosh:
1735 ASSERT(node->getUseEmulatedFunction());
1736 writeEmulatedFunctionTriplet(visit, "acosh(");
1737 break;
1738 case EOpAtanh:
1739 ASSERT(node->getUseEmulatedFunction());
1740 writeEmulatedFunctionTriplet(visit, "atanh(");
1741 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001742 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1743 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1744 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1745 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1746 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1747 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1748 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1749 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1750 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001751 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1752 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1753 case EOpRoundEven:
1754 ASSERT(node->getUseEmulatedFunction());
1755 writeEmulatedFunctionTriplet(visit, "roundEven(");
1756 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001757 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1758 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301759 case EOpIsNan:
1760 outputTriplet(visit, "isnan(", "", ")");
1761 mRequiresIEEEStrictCompiling = true;
1762 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301763 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001764 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1765 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1766 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1767 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001768 case EOpPackSnorm2x16:
1769 ASSERT(node->getUseEmulatedFunction());
1770 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1771 break;
1772 case EOpPackUnorm2x16:
1773 ASSERT(node->getUseEmulatedFunction());
1774 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1775 break;
1776 case EOpPackHalf2x16:
1777 ASSERT(node->getUseEmulatedFunction());
1778 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1779 break;
1780 case EOpUnpackSnorm2x16:
1781 ASSERT(node->getUseEmulatedFunction());
1782 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1783 break;
1784 case EOpUnpackUnorm2x16:
1785 ASSERT(node->getUseEmulatedFunction());
1786 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1787 break;
1788 case EOpUnpackHalf2x16:
1789 ASSERT(node->getUseEmulatedFunction());
1790 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1791 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001792 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1793 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001794 case EOpDFdx:
1795 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1796 {
1797 outputTriplet(visit, "(", "", ", 0.0)");
1798 }
1799 else
1800 {
1801 outputTriplet(visit, "ddx(", "", ")");
1802 }
1803 break;
1804 case EOpDFdy:
1805 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1806 {
1807 outputTriplet(visit, "(", "", ", 0.0)");
1808 }
1809 else
1810 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001811 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001812 }
1813 break;
1814 case EOpFwidth:
1815 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1816 {
1817 outputTriplet(visit, "(", "", ", 0.0)");
1818 }
1819 else
1820 {
1821 outputTriplet(visit, "fwidth(", "", ")");
1822 }
1823 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001824 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1825 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001826 case EOpInverse:
1827 ASSERT(node->getUseEmulatedFunction());
1828 writeEmulatedFunctionTriplet(visit, "inverse(");
1829 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001830
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001831 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1832 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833 default: UNREACHABLE();
1834 }
1835
1836 return true;
1837}
1838
1839bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1840{
Jamie Madill32aab012015-01-27 14:12:26 -05001841 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843 switch (node->getOp())
1844 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001845 case EOpSequence:
1846 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001847 if (mInsideFunction)
1848 {
Jamie Madill075edd82013-07-08 13:30:19 -04001849 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001850 out << "{\n";
1851 }
1852
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001853 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001854 {
Jamie Madill075edd82013-07-08 13:30:19 -04001855 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001856
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001857 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001858
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001859 // Don't output ; after case labels, they're terminated by :
1860 // This is needed especially since outputting a ; after a case statement would turn empty
1861 // case statements into non-empty case statements, disallowing fall-through from them.
1862 if ((*sit)->getAsCaseNode() == nullptr)
1863 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001864 }
1865
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001866 if (mInsideFunction)
1867 {
Jamie Madill075edd82013-07-08 13:30:19 -04001868 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001869 out << "}\n";
1870 }
1871
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001872 return false;
1873 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874 case EOpDeclaration:
1875 if (visit == PreVisit)
1876 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001877 TIntermSequence *sequence = node->getSequence();
1878 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001880 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001882 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001883
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001884 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885 {
Jamie Madill37997142015-01-28 10:06:34 -05001886 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887 {
Jamie Madill37997142015-01-28 10:06:34 -05001888 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001889 {
Jamie Madill37997142015-01-28 10:06:34 -05001890 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001891 }
1892
Nicolas Capensd974db42014-10-07 10:50:19 -04001893 if (!mInsideFunction)
1894 {
1895 out << "static ";
1896 }
1897
1898 out << TypeString(variable->getType()) + " ";
1899
Jamie Madill37997142015-01-28 10:06:34 -05001900 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001901
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001902 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001904 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001905 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001906 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001907 }
1908 else
1909 {
Jamie Madill37997142015-01-28 10:06:34 -05001910 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001911 }
1912
Jamie Madill37997142015-01-28 10:06:34 -05001913 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001914 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001915 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001916 }
1917 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001919 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1920 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001921 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001922 }
1923 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924 }
Jamie Madill033dae62014-06-18 12:56:28 -04001925 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001926 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001927 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001928 {
1929 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1930
1931 if (symbol)
1932 {
1933 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1934 mReferencedVaryings[symbol->getSymbol()] = symbol;
1935 }
1936 else
1937 {
1938 (*sit)->traverse(this);
1939 }
1940 }
1941 }
1942
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943 return false;
1944 }
1945 else if (visit == InVisit)
1946 {
1947 out << ", ";
1948 }
1949 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001950 case EOpInvariantDeclaration:
1951 // Do not do any translation
1952 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001953 case EOpPrototype:
1954 if (visit == PreVisit)
1955 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001956 size_t index = mCallDag.findIndex(node);
1957 // Skip the prototype if it is not implemented (and thus not used)
1958 if (index == CallDAG::InvalidIndex)
1959 {
1960 return false;
1961 }
1962
Olli Etuaho76acee82014-11-04 13:44:03 +02001963 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001964
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001965 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001966
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001967 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001968 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001969 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001970
1971 if (symbol)
1972 {
1973 out << argumentString(symbol);
1974
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001975 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001976 {
1977 out << ", ";
1978 }
1979 }
1980 else UNREACHABLE();
1981 }
1982
1983 out << ");\n";
1984
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001985 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001986 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1987 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001988 {
1989 mOutputLod0Function = true;
1990 node->traverse(this);
1991 mOutputLod0Function = false;
1992 }
1993
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001994 return false;
1995 }
1996 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001997 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998 case EOpFunction:
1999 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002000 ASSERT(mCurrentFunctionMetadata == nullptr);
alokp@chromium.org43884872010-03-30 00:08:52 +00002001 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002002
Corentin Wallez1239ee92015-03-19 14:38:02 -07002003 size_t index = mCallDag.findIndex(node);
2004 ASSERT(index != CallDAG::InvalidIndex);
2005 mCurrentFunctionMetadata = &mASTMetadataList[index];
2006
Jamie Madill033dae62014-06-18 12:56:28 -04002007 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002008
2009 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002010 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002011 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002012 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002013 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002014 {
Jamie Madill033dae62014-06-18 12:56:28 -04002015 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002016 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002017
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002018 TIntermSequence *sequence = node->getSequence();
2019 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002020
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002021 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002022 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002023 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002024
2025 if (symbol)
2026 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002027 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002028
2029 out << argumentString(symbol);
2030
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002031 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002032 {
2033 out << ", ";
2034 }
2035 }
2036 else UNREACHABLE();
2037 }
2038
2039 out << ")\n"
2040 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002041
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002042 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002043 {
2044 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002045 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002046 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002047 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002048
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002049 out << "}\n";
2050
Corentin Wallez1239ee92015-03-19 14:38:02 -07002051 mCurrentFunctionMetadata = nullptr;
2052
2053 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2054 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002055 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002056 ASSERT(name != "main");
2057 mOutputLod0Function = true;
2058 node->traverse(this);
2059 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002060 }
2061
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002062 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002063 }
2064 break;
2065 case EOpFunctionCall:
2066 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002067 TString name = TFunction::unmangleName(node->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002068 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002069
Corentin Wallez1239ee92015-03-19 14:38:02 -07002070 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002071 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002072 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002073 if (node->isArray())
2074 {
2075 UNIMPLEMENTED();
2076 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002077 size_t index = mCallDag.findIndex(node);
2078 ASSERT(index != CallDAG::InvalidIndex);
2079 lod0 &= mASTMetadataList[index].mNeedsLod0;
2080
Jamie Madill033dae62014-06-18 12:56:28 -04002081 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002082 }
2083 else
2084 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002085 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002086
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002087 TextureFunction textureFunction;
2088 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002089 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002090 textureFunction.method = TextureFunction::IMPLICIT;
2091 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002092 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002093
2094 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002095 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002096 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002097 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002098 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002099 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002100 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002101 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002102 }
Nicolas Capens46485082014-04-15 13:12:50 -04002103 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2104 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002105 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002106 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002107 }
Nicolas Capens46485082014-04-15 13:12:50 -04002108 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002109 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002110 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002111 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002112 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002113 else if (name == "textureSize")
2114 {
2115 textureFunction.method = TextureFunction::SIZE;
2116 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002117 else if (name == "textureOffset")
2118 {
2119 textureFunction.method = TextureFunction::IMPLICIT;
2120 textureFunction.offset = true;
2121 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002122 else if (name == "textureProjOffset")
2123 {
2124 textureFunction.method = TextureFunction::IMPLICIT;
2125 textureFunction.offset = true;
2126 textureFunction.proj = true;
2127 }
2128 else if (name == "textureLodOffset")
2129 {
2130 textureFunction.method = TextureFunction::LOD;
2131 textureFunction.offset = true;
2132 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002133 else if (name == "textureProjLodOffset")
2134 {
2135 textureFunction.method = TextureFunction::LOD;
2136 textureFunction.proj = true;
2137 textureFunction.offset = true;
2138 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002139 else if (name == "texelFetch")
2140 {
2141 textureFunction.method = TextureFunction::FETCH;
2142 }
2143 else if (name == "texelFetchOffset")
2144 {
2145 textureFunction.method = TextureFunction::FETCH;
2146 textureFunction.offset = true;
2147 }
Nicolas Capens46485082014-04-15 13:12:50 -04002148 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002149 {
2150 textureFunction.method = TextureFunction::GRAD;
2151 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002152 else if (name == "textureGradOffset")
2153 {
2154 textureFunction.method = TextureFunction::GRAD;
2155 textureFunction.offset = true;
2156 }
Nicolas Capens46485082014-04-15 13:12:50 -04002157 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002158 {
2159 textureFunction.method = TextureFunction::GRAD;
2160 textureFunction.proj = true;
2161 }
2162 else if (name == "textureProjGradOffset")
2163 {
2164 textureFunction.method = TextureFunction::GRAD;
2165 textureFunction.proj = true;
2166 textureFunction.offset = true;
2167 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002168 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002169
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002170 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002171 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002172 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2173
2174 if (textureFunction.offset)
2175 {
2176 mandatoryArgumentCount++;
2177 }
2178
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002179 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002180
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002181 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002182 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002183 if (bias)
2184 {
2185 textureFunction.method = TextureFunction::LOD0BIAS;
2186 }
2187 else
2188 {
2189 textureFunction.method = TextureFunction::LOD0;
2190 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002191 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002192 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002193 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002194 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002195 }
2196 }
2197
2198 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002199
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002200 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002202
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002203 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002204 {
2205 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2206 {
2207 out << "texture_";
2208 (*arg)->traverse(this);
2209 out << ", sampler_";
2210 }
2211
2212 (*arg)->traverse(this);
2213
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002214 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002215 {
2216 out << ", ";
2217 }
2218 }
2219
2220 out << ")";
2221
2222 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002223 }
2224 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002225 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002226 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2227 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2228 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2229 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2230 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2231 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2232 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2233 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2234 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2235 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2236 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2237 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2238 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2239 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2240 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2241 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2242 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2243 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2244 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002245 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002246 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002247 if (node->getType().isArray())
2248 {
2249 UNIMPLEMENTED();
2250 }
Jamie Madill033dae62014-06-18 12:56:28 -04002251 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002252 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002253 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002254 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002255 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002256 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2257 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2258 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2259 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2260 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2261 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002262 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002263 ASSERT(node->getUseEmulatedFunction());
2264 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002265 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002266 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002267 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002268 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002269 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002270 ASSERT(node->getUseEmulatedFunction());
2271 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272 break;
2273 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2274 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2275 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2276 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2277 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2278 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2279 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2280 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2281 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002282 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002283 ASSERT(node->getUseEmulatedFunction());
2284 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002285 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2287 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002288 case EOpOuterProduct:
2289 ASSERT(node->getUseEmulatedFunction());
2290 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2291 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 default: UNREACHABLE();
2294 }
2295
2296 return true;
2297}
2298
2299bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2300{
Jamie Madill32aab012015-01-27 14:12:26 -05002301 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002303 if (node->usesTernaryOperator())
2304 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002305 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002306 }
2307 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002308 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002309 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002310
Corentin Wallez1239ee92015-03-19 14:38:02 -07002311 // D3D errors when there is a gradient operation in a loop in an unflattened if.
2312 if (mShaderType == GL_FRAGMENT_SHADER
2313 && mCurrentFunctionMetadata->hasDiscontinuousLoop(node)
2314 && mCurrentFunctionMetadata->hasGradientInCallGraph(node))
Corentin Wallez80bacde2014-11-10 12:07:37 -08002315 {
2316 out << "FLATTEN ";
2317 }
2318
2319 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002320
2321 node->getCondition()->traverse(this);
2322
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002323 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002324
Jamie Madill075edd82013-07-08 13:30:19 -04002325 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002326 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002328 bool discard = false;
2329
daniel@transgaming.combb885322010-04-15 20:45:24 +00002330 if (node->getTrueBlock())
2331 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002332 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002333
2334 // Detect true discard
2335 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002336 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337
Jamie Madill075edd82013-07-08 13:30:19 -04002338 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002339 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002340
2341 if (node->getFalseBlock())
2342 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002343 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002344
Jamie Madill075edd82013-07-08 13:30:19 -04002345 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002346 out << "{\n";
2347
Jamie Madill075edd82013-07-08 13:30:19 -04002348 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002349 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002350
Jamie Madill075edd82013-07-08 13:30:19 -04002351 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002352 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002353
2354 // Detect false discard
2355 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2356 }
2357
2358 // ANGLE issue 486: Detect problematic conditional discard
2359 if (discard && FindSideEffectRewriting::search(node))
2360 {
2361 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002362 }
2363 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002364
2365 return false;
2366}
2367
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002368bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002369{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002370 if (node->getStatementList())
2371 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002372 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002373 outputTriplet(visit, "switch (", ") ", "");
2374 // The curly braces get written when visiting the statementList aggregate
2375 }
2376 else
2377 {
2378 // No statementList, so it won't output curly braces
2379 outputTriplet(visit, "switch (", ") {", "}\n");
2380 }
2381 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002382}
2383
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002384bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002385{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002386 if (node->hasCondition())
2387 {
2388 outputTriplet(visit, "case (", "", "):\n");
2389 return true;
2390 }
2391 else
2392 {
2393 TInfoSinkBase &out = getInfoSink();
2394 out << "default:\n";
2395 return false;
2396 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002397}
2398
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2400{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002401 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402}
2403
2404bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2405{
Nicolas Capens655fe362014-04-11 13:12:34 -04002406 mNestedLoopDepth++;
2407
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002408 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002409 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002410 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002411
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002412 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002413 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002414 if (handleExcessiveLoop(node))
2415 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002416 mInsideDiscontinuousLoop = wasDiscontinuous;
2417 mNestedLoopDepth--;
2418
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002419 return false;
2420 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002421 }
2422
Jamie Madill32aab012015-01-27 14:12:26 -05002423 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424
Corentin Wallez1239ee92015-03-19 14:38:02 -07002425 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002426 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002428 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002429
Jamie Madill075edd82013-07-08 13:30:19 -04002430 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002431 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 }
2433 else
2434 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002435 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002436
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002437 if (node->getInit())
2438 {
2439 node->getInit()->traverse(this);
2440 }
2441
2442 out << "; ";
2443
alokp@chromium.org52813552010-11-16 18:36:09 +00002444 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002446 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447 }
2448
2449 out << "; ";
2450
alokp@chromium.org52813552010-11-16 18:36:09 +00002451 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002453 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 }
2455
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002456 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002457
Jamie Madill075edd82013-07-08 13:30:19 -04002458 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002459 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460 }
2461
2462 if (node->getBody())
2463 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002464 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 }
2466
Jamie Madill075edd82013-07-08 13:30:19 -04002467 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002468 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469
alokp@chromium.org52813552010-11-16 18:36:09 +00002470 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471 {
Jamie Madill075edd82013-07-08 13:30:19 -04002472 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 out << "while(\n";
2474
alokp@chromium.org52813552010-11-16 18:36:09 +00002475 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476
daniel@transgaming.com73536982012-03-21 20:45:49 +00002477 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478 }
2479
daniel@transgaming.com73536982012-03-21 20:45:49 +00002480 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002482 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002483 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002484
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485 return false;
2486}
2487
2488bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2489{
Jamie Madill32aab012015-01-27 14:12:26 -05002490 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491
2492 switch (node->getFlowOp())
2493 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002494 case EOpKill:
2495 outputTriplet(visit, "discard;\n", "", "");
2496 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002497 case EOpBreak:
2498 if (visit == PreVisit)
2499 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002500 if (mNestedLoopDepth > 1)
2501 {
2502 mUsesNestedBreak = true;
2503 }
2504
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002505 if (mExcessiveLoopIndex)
2506 {
2507 out << "{Break";
2508 mExcessiveLoopIndex->traverse(this);
2509 out << " = true; break;}\n";
2510 }
2511 else
2512 {
2513 out << "break;\n";
2514 }
2515 }
2516 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002517 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002518 case EOpReturn:
2519 if (visit == PreVisit)
2520 {
2521 if (node->getExpression())
2522 {
2523 out << "return ";
2524 }
2525 else
2526 {
2527 out << "return;\n";
2528 }
2529 }
2530 else if (visit == PostVisit)
2531 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002532 if (node->getExpression())
2533 {
2534 out << ";\n";
2535 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002536 }
2537 break;
2538 default: UNREACHABLE();
2539 }
2540
2541 return true;
2542}
2543
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002544void OutputHLSL::traverseStatements(TIntermNode *node)
2545{
2546 if (isSingleStatement(node))
2547 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002548 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002549 }
2550
2551 node->traverse(this);
2552}
2553
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002554bool OutputHLSL::isSingleStatement(TIntermNode *node)
2555{
2556 TIntermAggregate *aggregate = node->getAsAggregate();
2557
2558 if (aggregate)
2559 {
2560 if (aggregate->getOp() == EOpSequence)
2561 {
2562 return false;
2563 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002564 else if (aggregate->getOp() == EOpDeclaration)
2565 {
2566 // Declaring multiple comma-separated variables must be considered multiple statements
2567 // because each individual declaration has side effects which are visible in the next.
2568 return false;
2569 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002570 else
2571 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002572 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002573 {
2574 if (!isSingleStatement(*sit))
2575 {
2576 return false;
2577 }
2578 }
2579
2580 return true;
2581 }
2582 }
2583
2584 return true;
2585}
2586
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002587// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2588// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002589bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2590{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002591 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002592 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002593
2594 // Parse loops of the form:
2595 // for(int index = initial; index [comparator] limit; index += increment)
2596 TIntermSymbol *index = NULL;
2597 TOperator comparator = EOpNull;
2598 int initial = 0;
2599 int limit = 0;
2600 int increment = 0;
2601
2602 // Parse index name and intial value
2603 if (node->getInit())
2604 {
2605 TIntermAggregate *init = node->getInit()->getAsAggregate();
2606
2607 if (init)
2608 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002609 TIntermSequence *sequence = init->getSequence();
2610 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002611
2612 if (variable && variable->getQualifier() == EvqTemporary)
2613 {
2614 TIntermBinary *assign = variable->getAsBinaryNode();
2615
2616 if (assign->getOp() == EOpInitialize)
2617 {
2618 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2619 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2620
2621 if (symbol && constant)
2622 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002623 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002624 {
2625 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002626 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002627 }
2628 }
2629 }
2630 }
2631 }
2632 }
2633
2634 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002635 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002636 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002637 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002638
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002639 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2640 {
2641 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2642
2643 if (constant)
2644 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002645 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002646 {
2647 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002648 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002649 }
2650 }
2651 }
2652 }
2653
2654 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002655 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002656 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002657 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2658 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002659
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002660 if (binaryTerminal)
2661 {
2662 TOperator op = binaryTerminal->getOp();
2663 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2664
2665 if (constant)
2666 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002667 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002668 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002669 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002670
2671 switch (op)
2672 {
2673 case EOpAddAssign: increment = value; break;
2674 case EOpSubAssign: increment = -value; break;
2675 default: UNIMPLEMENTED();
2676 }
2677 }
2678 }
2679 }
2680 else if (unaryTerminal)
2681 {
2682 TOperator op = unaryTerminal->getOp();
2683
2684 switch (op)
2685 {
2686 case EOpPostIncrement: increment = 1; break;
2687 case EOpPostDecrement: increment = -1; break;
2688 case EOpPreIncrement: increment = 1; break;
2689 case EOpPreDecrement: increment = -1; break;
2690 default: UNIMPLEMENTED();
2691 }
2692 }
2693 }
2694
2695 if (index != NULL && comparator != EOpNull && increment != 0)
2696 {
2697 if (comparator == EOpLessThanEqual)
2698 {
2699 comparator = EOpLessThan;
2700 limit += 1;
2701 }
2702
2703 if (comparator == EOpLessThan)
2704 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002705 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002706
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002707 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002708 {
2709 return false; // Not an excessive loop
2710 }
2711
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002712 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2713 mExcessiveLoopIndex = index;
2714
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002715 out << "{int ";
2716 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002717 out << ";\n"
2718 "bool Break";
2719 index->traverse(this);
2720 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002721
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002722 bool firstLoopFragment = true;
2723
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002724 while (iterations > 0)
2725 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002726 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002727
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002728 if (!firstLoopFragment)
2729 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002730 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002731 index->traverse(this);
2732 out << ") {\n";
2733 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002734
2735 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2736 {
2737 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2738 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002739
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002740 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002741 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002742
Corentin Wallez1239ee92015-03-19 14:38:02 -07002743 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002744 index->traverse(this);
2745 out << " = ";
2746 out << initial;
2747
2748 out << "; ";
2749 index->traverse(this);
2750 out << " < ";
2751 out << clampedLimit;
2752
2753 out << "; ";
2754 index->traverse(this);
2755 out << " += ";
2756 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002757 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002758
Jamie Madill075edd82013-07-08 13:30:19 -04002759 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002760 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002761
2762 if (node->getBody())
2763 {
2764 node->getBody()->traverse(this);
2765 }
2766
Jamie Madill075edd82013-07-08 13:30:19 -04002767 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002768 out << ";}\n";
2769
2770 if (!firstLoopFragment)
2771 {
2772 out << "}\n";
2773 }
2774
2775 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002776
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002777 initial += MAX_LOOP_ITERATIONS * increment;
2778 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002779 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002780
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002781 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002782
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002783 mExcessiveLoopIndex = restoreIndex;
2784
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002785 return true;
2786 }
2787 else UNIMPLEMENTED();
2788 }
2789
2790 return false; // Not handled as an excessive loop
2791}
2792
Olli Etuaho7fb49552015-03-18 17:27:44 +02002793void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002794{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002795 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796 {
2797 out << preString;
2798 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002799 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002800 {
2801 out << inString;
2802 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002803 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804 {
2805 out << postString;
2806 }
2807}
2808
Olli Etuaho7fb49552015-03-18 17:27:44 +02002809void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2810{
2811 outputTriplet(visit, preString, inString, postString, getInfoSink());
2812}
2813
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002814void OutputHLSL::outputLineDirective(int line)
2815{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002816 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002817 {
Jamie Madill32aab012015-01-27 14:12:26 -05002818 TInfoSinkBase &out = getInfoSink();
2819
2820 out << "\n";
2821 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002822
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002823 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002824 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002825 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002826 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002827
Jamie Madill32aab012015-01-27 14:12:26 -05002828 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002829 }
2830}
2831
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002832TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2833{
2834 TQualifier qualifier = symbol->getQualifier();
2835 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002836 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002837
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002838 if (name.empty()) // HLSL demands named arguments, also for prototypes
2839 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002840 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002841 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002842 else if (!symbol->isInternal())
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002843 {
Jamie Madill033dae62014-06-18 12:56:28 -04002844 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002845 }
2846
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002847 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2848 {
Jamie Madill033dae62014-06-18 12:56:28 -04002849 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002850 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002851 }
2852
Jamie Madill033dae62014-06-18 12:56:28 -04002853 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002854}
2855
2856TString OutputHLSL::initializer(const TType &type)
2857{
2858 TString string;
2859
Jamie Madill94bf7f22013-07-08 13:31:15 -04002860 size_t size = type.getObjectSize();
2861 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002862 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002863 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002864
Jamie Madill94bf7f22013-07-08 13:31:15 -04002865 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002866 {
2867 string += ", ";
2868 }
2869 }
2870
daniel@transgaming.comead23042010-04-29 03:35:36 +00002871 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002872}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002873
Daniel Bratell29190082015-02-20 16:42:54 +01002874void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002875{
Olli Etuahof40319e2015-03-10 14:33:00 +02002876 if (type.isArray())
2877 {
2878 UNIMPLEMENTED();
2879 }
Jamie Madill32aab012015-01-27 14:12:26 -05002880 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002881
2882 if (visit == PreVisit)
2883 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002884 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002885
Daniel Bratell29190082015-02-20 16:42:54 +01002886 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002887 }
2888 else if (visit == InVisit)
2889 {
2890 out << ", ";
2891 }
2892 else if (visit == PostVisit)
2893 {
2894 out << ")";
2895 }
2896}
2897
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002898const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const TConstantUnion *constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002899{
Jamie Madill32aab012015-01-27 14:12:26 -05002900 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002901
Jamie Madill98493dd2013-07-08 14:39:03 -04002902 const TStructure* structure = type.getStruct();
2903 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002904 {
Jamie Madill033dae62014-06-18 12:56:28 -04002905 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002906
Jamie Madill98493dd2013-07-08 14:39:03 -04002907 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002908
Jamie Madill98493dd2013-07-08 14:39:03 -04002909 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002910 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002911 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002912 constUnion = writeConstantUnion(*fieldType, constUnion);
2913
Jamie Madill98493dd2013-07-08 14:39:03 -04002914 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002915 {
2916 out << ", ";
2917 }
2918 }
2919
2920 out << ")";
2921 }
2922 else
2923 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002924 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002925 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002926
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002927 if (writeType)
2928 {
Jamie Madill033dae62014-06-18 12:56:28 -04002929 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002930 }
2931
Jamie Madill94bf7f22013-07-08 13:31:15 -04002932 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002933 {
2934 switch (constUnion->getType())
2935 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002936 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002937 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002938 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002939 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002940 default: UNREACHABLE();
2941 }
2942
2943 if (i != size - 1)
2944 {
2945 out << ", ";
2946 }
2947 }
2948
2949 if (writeType)
2950 {
2951 out << ")";
2952 }
2953 }
2954
2955 return constUnion;
2956}
2957
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002958void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2959{
2960 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2961 outputTriplet(visit, preString.c_str(), ", ", ")");
2962}
2963
Jamie Madill37997142015-01-28 10:06:34 -05002964bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2965{
2966 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2967 expression->traverse(&searchSymbol);
2968
2969 if (searchSymbol.foundMatch())
2970 {
2971 // Type already printed
2972 out << "t" + str(mUniqueIndex) + " = ";
2973 expression->traverse(this);
2974 out << ", ";
2975 symbolNode->traverse(this);
2976 out << " = t" + str(mUniqueIndex);
2977
2978 mUniqueIndex++;
2979 return true;
2980 }
2981
2982 return false;
2983}
2984
2985void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2986{
2987 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2988 << "\n"
2989 << "void initializeDeferredGlobals()\n"
2990 << "{\n";
2991
2992 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2993 {
2994 TIntermSymbol *symbol = deferredGlobal.first;
2995 TIntermTyped *expression = deferredGlobal.second;
2996 ASSERT(symbol);
2997 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2998
2999 out << " " << Decorate(symbol->getSymbol()) << " = ";
3000
3001 if (!writeSameSymbolInitializer(out, symbol, expression))
3002 {
3003 ASSERT(mInfoSinkStack.top() == &out);
3004 expression->traverse(this);
3005 }
3006
3007 out << ";\n";
3008 }
3009
3010 out << "}\n"
3011 << "\n";
3012}
3013
Jamie Madill55e79e02015-02-09 15:35:00 -05003014TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3015{
3016 const TFieldList &fields = structure.fields();
3017
3018 for (const auto &eqFunction : mStructEqualityFunctions)
3019 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003020 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003021 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003022 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003023 }
3024 }
3025
3026 const TString &structNameString = StructNameString(structure);
3027
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003028 StructEqualityFunction *function = new StructEqualityFunction();
3029 function->structure = &structure;
3030 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003031
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003032 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003033
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003034 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3035 << "{\n"
3036 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003037
3038 for (size_t i = 0; i < fields.size(); i++)
3039 {
3040 const TField *field = fields[i];
3041 const TType *fieldType = field->type();
3042
3043 const TString &fieldNameA = "a." + Decorate(field->name());
3044 const TString &fieldNameB = "b." + Decorate(field->name());
3045
3046 if (i > 0)
3047 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003048 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003049 }
3050
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003051 fnOut << "(";
3052 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3053 fnOut << fieldNameA;
3054 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3055 fnOut << fieldNameB;
3056 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3057 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003058 }
3059
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003060 fnOut << ";\n" << "}\n";
3061
3062 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003063
3064 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003065 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003066
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003067 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003068}
3069
Olli Etuaho7fb49552015-03-18 17:27:44 +02003070TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3071{
3072 for (const auto &eqFunction : mArrayEqualityFunctions)
3073 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003074 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003075 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003076 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003077 }
3078 }
3079
3080 const TString &typeName = TypeString(type);
3081
Olli Etuaho12690762015-03-31 12:55:28 +03003082 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003083 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003084
3085 TInfoSinkBase fnNameOut;
3086 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003087 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003088
3089 TType nonArrayType = type;
3090 nonArrayType.clearArrayness();
3091
3092 TInfoSinkBase fnOut;
3093
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003094 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003095 << typeName << " a[" << type.getArraySize() << "], "
3096 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003097 << "{\n"
3098 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3099 " {\n"
3100 " if (";
3101
3102 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3103 fnOut << "a[i]";
3104 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3105 fnOut << "b[i]";
3106 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3107
3108 fnOut << ") { return false; }\n"
3109 " }\n"
3110 " return true;\n"
3111 "}\n";
3112
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003113 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003114
3115 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003116 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003117
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003118 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003119}
3120
Olli Etuaho12690762015-03-31 12:55:28 +03003121TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3122{
3123 for (const auto &assignFunction : mArrayAssignmentFunctions)
3124 {
3125 if (assignFunction.type == type)
3126 {
3127 return assignFunction.functionName;
3128 }
3129 }
3130
3131 const TString &typeName = TypeString(type);
3132
3133 ArrayHelperFunction function;
3134 function.type = type;
3135
3136 TInfoSinkBase fnNameOut;
3137 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3138 function.functionName = fnNameOut.c_str();
3139
3140 TInfoSinkBase fnOut;
3141
3142 fnOut << "void " << function.functionName << "(out "
3143 << typeName << " a[" << type.getArraySize() << "], "
3144 << typeName << " b[" << type.getArraySize() << "])\n"
3145 << "{\n"
3146 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3147 " {\n"
3148 " a[i] = b[i];\n"
3149 " }\n"
3150 "}\n";
3151
3152 function.functionDefinition = fnOut.c_str();
3153
3154 mArrayAssignmentFunctions.push_back(function);
3155
3156 return function.functionName;
3157}
3158
Olli Etuaho9638c352015-04-01 14:34:52 +03003159TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3160{
3161 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3162 {
3163 if (constructIntoFunction.type == type)
3164 {
3165 return constructIntoFunction.functionName;
3166 }
3167 }
3168
3169 const TString &typeName = TypeString(type);
3170
3171 ArrayHelperFunction function;
3172 function.type = type;
3173
3174 TInfoSinkBase fnNameOut;
3175 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3176 function.functionName = fnNameOut.c_str();
3177
3178 TInfoSinkBase fnOut;
3179
3180 fnOut << "void " << function.functionName << "(out "
3181 << typeName << " a[" << type.getArraySize() << "]";
3182 for (int i = 0; i < type.getArraySize(); ++i)
3183 {
3184 fnOut << ", " << typeName << " b" << i;
3185 }
3186 fnOut << ")\n"
3187 "{\n";
3188
3189 for (int i = 0; i < type.getArraySize(); ++i)
3190 {
3191 fnOut << " a[" << i << "] = b" << i << ";\n";
3192 }
3193 fnOut << "}\n";
3194
3195 function.functionDefinition = fnOut.c_str();
3196
3197 mArrayConstructIntoFunctions.push_back(function);
3198
3199 return function.functionName;
3200}
3201
Jamie Madill2e295e22015-04-29 10:41:33 -04003202void OutputHLSL::ensureStructDefined(const TType &type)
3203{
3204 TStructure *structure = type.getStruct();
3205
3206 if (structure)
3207 {
3208 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3209 }
3210}
3211
3212
Olli Etuaho9638c352015-04-01 14:34:52 +03003213
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003214}