blob: b9ba0c2d491f8bd7b1893491ca9adda56390107d [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 Madill98493dd2013-07-08 14:39:03 -04001324 const TType& nodeType = node->getType();
1325 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1326
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 Madill033dae62014-06-18 12:56:28 -04001336 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001337 }
Jamie Madill19571812013-08-12 15:26:34 -07001338 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001339 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001340 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001341 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001342 }
Jamie Madill033dae62014-06-18 12:56:28 -04001343 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001344 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001345 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001346 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001347 }
Jamie Madill19571812013-08-12 15:26:34 -07001348 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001349 {
1350 mReferencedOutputVariables[name] = node;
1351 out << "out_" << name;
1352 }
1353 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001354 {
1355 out << "gl_Color[0]";
1356 mUsesFragColor = true;
1357 }
1358 else if (qualifier == EvqFragData)
1359 {
1360 out << "gl_Color";
1361 mUsesFragData = true;
1362 }
1363 else if (qualifier == EvqFragCoord)
1364 {
1365 mUsesFragCoord = true;
1366 out << name;
1367 }
1368 else if (qualifier == EvqPointCoord)
1369 {
1370 mUsesPointCoord = true;
1371 out << name;
1372 }
1373 else if (qualifier == EvqFrontFacing)
1374 {
1375 mUsesFrontFacing = true;
1376 out << name;
1377 }
1378 else if (qualifier == EvqPointSize)
1379 {
1380 mUsesPointSize = true;
1381 out << name;
1382 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001383 else if (qualifier == EvqInstanceID)
1384 {
1385 mUsesInstanceID = true;
1386 out << name;
1387 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001388 else if (name == "gl_FragDepthEXT")
1389 {
1390 mUsesFragDepth = true;
1391 out << "gl_Depth";
1392 }
Olli Etuaho78174db2015-04-21 16:14:00 +03001393 else if (node->isInternal())
Jamie Madille53c98b2014-02-03 11:57:13 -05001394 {
1395 out << name;
1396 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001397 else
1398 {
Jamie Madill033dae62014-06-18 12:56:28 -04001399 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001400 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001401 }
1402}
1403
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001404void OutputHLSL::visitRaw(TIntermRaw *node)
1405{
Jamie Madill32aab012015-01-27 14:12:26 -05001406 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001407}
1408
Olli Etuaho7fb49552015-03-18 17:27:44 +02001409void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1410{
1411 if (type.isScalar() && !type.isArray())
1412 {
1413 if (op == EOpEqual)
1414 {
1415 outputTriplet(visit, "(", " == ", ")", out);
1416 }
1417 else
1418 {
1419 outputTriplet(visit, "(", " != ", ")", out);
1420 }
1421 }
1422 else
1423 {
1424 if (visit == PreVisit && op == EOpNotEqual)
1425 {
1426 out << "!";
1427 }
1428
1429 if (type.isArray())
1430 {
1431 const TString &functionName = addArrayEqualityFunction(type);
1432 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1433 }
1434 else if (type.getBasicType() == EbtStruct)
1435 {
1436 const TStructure &structure = *type.getStruct();
1437 const TString &functionName = addStructEqualityFunction(structure);
1438 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1439 }
1440 else
1441 {
1442 ASSERT(type.isMatrix() || type.isVector());
1443 outputTriplet(visit, "all(", " == ", ")", out);
1444 }
1445 }
1446}
1447
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001448bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1449{
Jamie Madill32aab012015-01-27 14:12:26 -05001450 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001451
Jamie Madill570e04d2013-06-21 09:15:33 -04001452 // Handle accessing std140 structs by value
1453 if (mFlaggedStructMappedNames.count(node) > 0)
1454 {
1455 out << mFlaggedStructMappedNames[node];
1456 return false;
1457 }
1458
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001459 switch (node->getOp())
1460 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001461 case EOpAssign:
1462 if (node->getLeft()->isArray())
1463 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001464 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1465 if (rightAgg != nullptr && rightAgg->isConstructor())
1466 {
1467 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1468 out << functionName << "(";
1469 node->getLeft()->traverse(this);
1470 TIntermSequence *seq = rightAgg->getSequence();
1471 for (auto &arrayElement : *seq)
1472 {
1473 out << ", ";
1474 arrayElement->traverse(this);
1475 }
1476 out << ")";
1477 return false;
1478 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001479 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1480 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1481
1482 const TString &functionName = addArrayAssignmentFunction(node->getType());
1483 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001484 }
1485 else
1486 {
1487 outputTriplet(visit, "(", " = ", ")");
1488 }
1489 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001490 case EOpInitialize:
1491 if (visit == PreVisit)
1492 {
1493 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1494 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1495 // new variable is created before the assignment is evaluated), so we need to convert
1496 // this to "float t = x, x = t;".
1497
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001498 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001499 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001500 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001501
Jamie Madill37997142015-01-28 10:06:34 -05001502 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1503 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001504 {
Jamie Madill37997142015-01-28 10:06:34 -05001505 // For variables which are not constant, defer their real initialization until
1506 // after we initialize other globals: uniforms, attributes and varyings.
1507 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1508 const TString &initString = initializer(node->getType());
1509 node->setRight(new TIntermRaw(node->getType(), initString));
1510 }
1511 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1512 {
1513 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001514 return false;
1515 }
1516 }
1517 else if (visit == InVisit)
1518 {
1519 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001520 }
1521 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001522 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1523 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1524 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1525 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1526 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1527 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001528 if (visit == PreVisit)
1529 {
1530 out << "(";
1531 }
1532 else if (visit == InVisit)
1533 {
1534 out << " = mul(";
1535 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001536 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001537 }
1538 else
1539 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001540 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001541 }
1542 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001543 case EOpMatrixTimesMatrixAssign:
1544 if (visit == PreVisit)
1545 {
1546 out << "(";
1547 }
1548 else if (visit == InVisit)
1549 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001550 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001551 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001552 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001553 }
1554 else
1555 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001556 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001557 }
1558 break;
1559 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001560 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001561 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1562 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1563 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1564 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1565 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001566 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001567 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001568 const TType& leftType = node->getLeft()->getType();
1569 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001570 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001571 if (visit == PreVisit)
1572 {
1573 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1574 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001575 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001576 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001577 return false;
1578 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001579 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001580 else
1581 {
1582 outputTriplet(visit, "", "[", "]");
1583 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001584 }
1585 break;
1586 case EOpIndexIndirect:
1587 // We do not currently support indirect references to interface blocks
1588 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1589 outputTriplet(visit, "", "[", "]");
1590 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001591 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001592 if (visit == InVisit)
1593 {
1594 const TStructure* structure = node->getLeft()->getType().getStruct();
1595 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1596 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001597 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001598
1599 return false;
1600 }
1601 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001602 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001603 if (visit == InVisit)
1604 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001605 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1606 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1607 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001608 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001609
1610 return false;
1611 }
1612 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001613 case EOpVectorSwizzle:
1614 if (visit == InVisit)
1615 {
1616 out << ".";
1617
1618 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1619
1620 if (swizzle)
1621 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001622 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001623
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001624 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001625 {
1626 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1627
1628 if (element)
1629 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001630 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001631
1632 switch (i)
1633 {
1634 case 0: out << "x"; break;
1635 case 1: out << "y"; break;
1636 case 2: out << "z"; break;
1637 case 3: out << "w"; break;
1638 default: UNREACHABLE();
1639 }
1640 }
1641 else UNREACHABLE();
1642 }
1643 }
1644 else UNREACHABLE();
1645
1646 return false; // Fully processed
1647 }
1648 break;
1649 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1650 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1651 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1652 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001653 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001654 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1655 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1656 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1657 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1658 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001659 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001660 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001661 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001662 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001663 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1664 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1665 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1666 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1667 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001668 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001669 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1670 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001671 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001672 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001673 if (node->getRight()->hasSideEffects())
1674 {
1675 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1676 return false;
1677 }
1678 else
1679 {
1680 outputTriplet(visit, "(", " || ", ")");
1681 return true;
1682 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001683 case EOpLogicalXor:
1684 mUsesXor = true;
1685 outputTriplet(visit, "xor(", ", ", ")");
1686 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001687 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001688 if (node->getRight()->hasSideEffects())
1689 {
1690 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1691 return false;
1692 }
1693 else
1694 {
1695 outputTriplet(visit, "(", " && ", ")");
1696 return true;
1697 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001698 default: UNREACHABLE();
1699 }
1700
1701 return true;
1702}
1703
1704bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1705{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001706 switch (node->getOp())
1707 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001708 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001709 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001710 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1711 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001712 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001713 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1714 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1715 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1716 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001717 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1718 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1719 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1720 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1721 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1722 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1723 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1724 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001725 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1726 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1727 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1728 case EOpAsinh:
1729 ASSERT(node->getUseEmulatedFunction());
1730 writeEmulatedFunctionTriplet(visit, "asinh(");
1731 break;
1732 case EOpAcosh:
1733 ASSERT(node->getUseEmulatedFunction());
1734 writeEmulatedFunctionTriplet(visit, "acosh(");
1735 break;
1736 case EOpAtanh:
1737 ASSERT(node->getUseEmulatedFunction());
1738 writeEmulatedFunctionTriplet(visit, "atanh(");
1739 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001740 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1741 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1742 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1743 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1744 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1745 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1746 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1747 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1748 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001749 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1750 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1751 case EOpRoundEven:
1752 ASSERT(node->getUseEmulatedFunction());
1753 writeEmulatedFunctionTriplet(visit, "roundEven(");
1754 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001755 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1756 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301757 case EOpIsNan:
1758 outputTriplet(visit, "isnan(", "", ")");
1759 mRequiresIEEEStrictCompiling = true;
1760 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301761 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001762 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1763 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1764 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1765 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001766 case EOpPackSnorm2x16:
1767 ASSERT(node->getUseEmulatedFunction());
1768 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1769 break;
1770 case EOpPackUnorm2x16:
1771 ASSERT(node->getUseEmulatedFunction());
1772 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1773 break;
1774 case EOpPackHalf2x16:
1775 ASSERT(node->getUseEmulatedFunction());
1776 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1777 break;
1778 case EOpUnpackSnorm2x16:
1779 ASSERT(node->getUseEmulatedFunction());
1780 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1781 break;
1782 case EOpUnpackUnorm2x16:
1783 ASSERT(node->getUseEmulatedFunction());
1784 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1785 break;
1786 case EOpUnpackHalf2x16:
1787 ASSERT(node->getUseEmulatedFunction());
1788 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1789 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001790 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1791 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001792 case EOpDFdx:
1793 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1794 {
1795 outputTriplet(visit, "(", "", ", 0.0)");
1796 }
1797 else
1798 {
1799 outputTriplet(visit, "ddx(", "", ")");
1800 }
1801 break;
1802 case EOpDFdy:
1803 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1804 {
1805 outputTriplet(visit, "(", "", ", 0.0)");
1806 }
1807 else
1808 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001809 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001810 }
1811 break;
1812 case EOpFwidth:
1813 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1814 {
1815 outputTriplet(visit, "(", "", ", 0.0)");
1816 }
1817 else
1818 {
1819 outputTriplet(visit, "fwidth(", "", ")");
1820 }
1821 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001822 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1823 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001824 case EOpInverse:
1825 ASSERT(node->getUseEmulatedFunction());
1826 writeEmulatedFunctionTriplet(visit, "inverse(");
1827 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001828
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001829 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1830 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831 default: UNREACHABLE();
1832 }
1833
1834 return true;
1835}
1836
1837bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1838{
Jamie Madill32aab012015-01-27 14:12:26 -05001839 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841 switch (node->getOp())
1842 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001843 case EOpSequence:
1844 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001845 if (mInsideFunction)
1846 {
Jamie Madill075edd82013-07-08 13:30:19 -04001847 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001848 out << "{\n";
1849 }
1850
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001851 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001852 {
Jamie Madill075edd82013-07-08 13:30:19 -04001853 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001854
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001855 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001856
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001857 // Don't output ; after case labels, they're terminated by :
1858 // This is needed especially since outputting a ; after a case statement would turn empty
1859 // case statements into non-empty case statements, disallowing fall-through from them.
1860 if ((*sit)->getAsCaseNode() == nullptr)
1861 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001862 }
1863
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001864 if (mInsideFunction)
1865 {
Jamie Madill075edd82013-07-08 13:30:19 -04001866 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001867 out << "}\n";
1868 }
1869
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001870 return false;
1871 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001872 case EOpDeclaration:
1873 if (visit == PreVisit)
1874 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001875 TIntermSequence *sequence = node->getSequence();
1876 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001878 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001880 TStructure *structure = variable->getType().getStruct();
1881
1882 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001883 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001884 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001885 }
1886
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001887 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888 {
Jamie Madill37997142015-01-28 10:06:34 -05001889 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001890 {
Jamie Madill37997142015-01-28 10:06:34 -05001891 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001892 {
Jamie Madill37997142015-01-28 10:06:34 -05001893 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001894 }
1895
Nicolas Capensd974db42014-10-07 10:50:19 -04001896 if (!mInsideFunction)
1897 {
1898 out << "static ";
1899 }
1900
1901 out << TypeString(variable->getType()) + " ";
1902
Jamie Madill37997142015-01-28 10:06:34 -05001903 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001905 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001906 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001907 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001908 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001909 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001910 }
1911 else
1912 {
Jamie Madill37997142015-01-28 10:06:34 -05001913 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001914 }
1915
Jamie Madill37997142015-01-28 10:06:34 -05001916 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001917 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001918 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919 }
1920 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001922 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1923 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001924 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001925 }
1926 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927 }
Jamie Madill033dae62014-06-18 12:56:28 -04001928 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001929 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001930 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001931 {
1932 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1933
1934 if (symbol)
1935 {
1936 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1937 mReferencedVaryings[symbol->getSymbol()] = symbol;
1938 }
1939 else
1940 {
1941 (*sit)->traverse(this);
1942 }
1943 }
1944 }
1945
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946 return false;
1947 }
1948 else if (visit == InVisit)
1949 {
1950 out << ", ";
1951 }
1952 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001953 case EOpInvariantDeclaration:
1954 // Do not do any translation
1955 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001956 case EOpPrototype:
1957 if (visit == PreVisit)
1958 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001959 size_t index = mCallDag.findIndex(node);
1960 // Skip the prototype if it is not implemented (and thus not used)
1961 if (index == CallDAG::InvalidIndex)
1962 {
1963 return false;
1964 }
1965
Olli Etuaho76acee82014-11-04 13:44:03 +02001966 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001967
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001968 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001969
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001970 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001971 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001972 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001973
1974 if (symbol)
1975 {
1976 out << argumentString(symbol);
1977
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001978 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001979 {
1980 out << ", ";
1981 }
1982 }
1983 else UNREACHABLE();
1984 }
1985
1986 out << ");\n";
1987
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001988 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001989 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1990 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001991 {
1992 mOutputLod0Function = true;
1993 node->traverse(this);
1994 mOutputLod0Function = false;
1995 }
1996
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001997 return false;
1998 }
1999 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002000 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002001 case EOpFunction:
2002 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002003 ASSERT(mCurrentFunctionMetadata == nullptr);
alokp@chromium.org43884872010-03-30 00:08:52 +00002004 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002005
Corentin Wallez1239ee92015-03-19 14:38:02 -07002006 size_t index = mCallDag.findIndex(node);
2007 ASSERT(index != CallDAG::InvalidIndex);
2008 mCurrentFunctionMetadata = &mASTMetadataList[index];
2009
Jamie Madill033dae62014-06-18 12:56:28 -04002010 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002011
2012 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002014 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002016 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002017 {
Jamie Madill033dae62014-06-18 12:56:28 -04002018 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002019 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002020
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002021 TIntermSequence *sequence = node->getSequence();
2022 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002023
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002024 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002025 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002026 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002027
2028 if (symbol)
2029 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002030 TStructure *structure = symbol->getType().getStruct();
2031
2032 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002033 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002034 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002035 }
2036
2037 out << argumentString(symbol);
2038
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002039 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002040 {
2041 out << ", ";
2042 }
2043 }
2044 else UNREACHABLE();
2045 }
2046
2047 out << ")\n"
2048 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002049
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002050 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002051 {
2052 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002053 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002054 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002056
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002057 out << "}\n";
2058
Corentin Wallez1239ee92015-03-19 14:38:02 -07002059 mCurrentFunctionMetadata = nullptr;
2060
2061 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2062 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002063 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002064 ASSERT(name != "main");
2065 mOutputLod0Function = true;
2066 node->traverse(this);
2067 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002068 }
2069
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002070 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071 }
2072 break;
2073 case EOpFunctionCall:
2074 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002075 TString name = TFunction::unmangleName(node->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002076 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002077
Corentin Wallez1239ee92015-03-19 14:38:02 -07002078 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002079 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002081 if (node->isArray())
2082 {
2083 UNIMPLEMENTED();
2084 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002085 size_t index = mCallDag.findIndex(node);
2086 ASSERT(index != CallDAG::InvalidIndex);
2087 lod0 &= mASTMetadataList[index].mNeedsLod0;
2088
Jamie Madill033dae62014-06-18 12:56:28 -04002089 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 }
2091 else
2092 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002093 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002094
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002095 TextureFunction textureFunction;
2096 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002097 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002098 textureFunction.method = TextureFunction::IMPLICIT;
2099 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002100 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002101
2102 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002103 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002104 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002105 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002106 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002107 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002108 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002109 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002110 }
Nicolas Capens46485082014-04-15 13:12:50 -04002111 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2112 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002113 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002114 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002115 }
Nicolas Capens46485082014-04-15 13:12:50 -04002116 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002117 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002118 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002119 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002120 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002121 else if (name == "textureSize")
2122 {
2123 textureFunction.method = TextureFunction::SIZE;
2124 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002125 else if (name == "textureOffset")
2126 {
2127 textureFunction.method = TextureFunction::IMPLICIT;
2128 textureFunction.offset = true;
2129 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002130 else if (name == "textureProjOffset")
2131 {
2132 textureFunction.method = TextureFunction::IMPLICIT;
2133 textureFunction.offset = true;
2134 textureFunction.proj = true;
2135 }
2136 else if (name == "textureLodOffset")
2137 {
2138 textureFunction.method = TextureFunction::LOD;
2139 textureFunction.offset = true;
2140 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002141 else if (name == "textureProjLodOffset")
2142 {
2143 textureFunction.method = TextureFunction::LOD;
2144 textureFunction.proj = true;
2145 textureFunction.offset = true;
2146 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002147 else if (name == "texelFetch")
2148 {
2149 textureFunction.method = TextureFunction::FETCH;
2150 }
2151 else if (name == "texelFetchOffset")
2152 {
2153 textureFunction.method = TextureFunction::FETCH;
2154 textureFunction.offset = true;
2155 }
Nicolas Capens46485082014-04-15 13:12:50 -04002156 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002157 {
2158 textureFunction.method = TextureFunction::GRAD;
2159 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002160 else if (name == "textureGradOffset")
2161 {
2162 textureFunction.method = TextureFunction::GRAD;
2163 textureFunction.offset = true;
2164 }
Nicolas Capens46485082014-04-15 13:12:50 -04002165 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002166 {
2167 textureFunction.method = TextureFunction::GRAD;
2168 textureFunction.proj = true;
2169 }
2170 else if (name == "textureProjGradOffset")
2171 {
2172 textureFunction.method = TextureFunction::GRAD;
2173 textureFunction.proj = true;
2174 textureFunction.offset = true;
2175 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002176 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002177
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002178 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002179 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002180 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2181
2182 if (textureFunction.offset)
2183 {
2184 mandatoryArgumentCount++;
2185 }
2186
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002187 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002188
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002189 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002190 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002191 if (bias)
2192 {
2193 textureFunction.method = TextureFunction::LOD0BIAS;
2194 }
2195 else
2196 {
2197 textureFunction.method = TextureFunction::LOD0;
2198 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002199 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002200 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002201 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002202 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002203 }
2204 }
2205
2206 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002207
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002208 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002210
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002211 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002212 {
2213 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2214 {
2215 out << "texture_";
2216 (*arg)->traverse(this);
2217 out << ", sampler_";
2218 }
2219
2220 (*arg)->traverse(this);
2221
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002222 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002223 {
2224 out << ", ";
2225 }
2226 }
2227
2228 out << ")";
2229
2230 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002231 }
2232 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002233 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002234 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2235 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2236 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2237 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2238 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2239 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2240 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2241 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2242 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2243 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2244 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2245 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2246 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2247 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2248 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2249 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2250 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2251 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2252 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002253 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002254 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002255 if (node->getType().isArray())
2256 {
2257 UNIMPLEMENTED();
2258 }
Jamie Madill033dae62014-06-18 12:56:28 -04002259 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002260 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002261 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002262 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002263 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002264 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2265 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2266 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2267 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2268 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2269 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002270 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002271 ASSERT(node->getUseEmulatedFunction());
2272 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002273 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002274 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002275 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002277 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002278 ASSERT(node->getUseEmulatedFunction());
2279 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280 break;
2281 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2282 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2283 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2284 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2285 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2286 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2287 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2288 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2289 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002290 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002291 ASSERT(node->getUseEmulatedFunction());
2292 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002293 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2295 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002296 case EOpOuterProduct:
2297 ASSERT(node->getUseEmulatedFunction());
2298 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2299 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301 default: UNREACHABLE();
2302 }
2303
2304 return true;
2305}
2306
2307bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2308{
Jamie Madill32aab012015-01-27 14:12:26 -05002309 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002311 if (node->usesTernaryOperator())
2312 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002313 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002314 }
2315 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002317 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002318
Corentin Wallez1239ee92015-03-19 14:38:02 -07002319 // D3D errors when there is a gradient operation in a loop in an unflattened if.
2320 if (mShaderType == GL_FRAGMENT_SHADER
2321 && mCurrentFunctionMetadata->hasDiscontinuousLoop(node)
2322 && mCurrentFunctionMetadata->hasGradientInCallGraph(node))
Corentin Wallez80bacde2014-11-10 12:07:37 -08002323 {
2324 out << "FLATTEN ";
2325 }
2326
2327 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002328
2329 node->getCondition()->traverse(this);
2330
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002331 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002332
Jamie Madill075edd82013-07-08 13:30:19 -04002333 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002334 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002336 bool discard = false;
2337
daniel@transgaming.combb885322010-04-15 20:45:24 +00002338 if (node->getTrueBlock())
2339 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002340 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002341
2342 // Detect true discard
2343 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002344 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345
Jamie Madill075edd82013-07-08 13:30:19 -04002346 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002347 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002348
2349 if (node->getFalseBlock())
2350 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002351 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002352
Jamie Madill075edd82013-07-08 13:30:19 -04002353 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002354 out << "{\n";
2355
Jamie Madill075edd82013-07-08 13:30:19 -04002356 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002357 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002358
Jamie Madill075edd82013-07-08 13:30:19 -04002359 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002360 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002361
2362 // Detect false discard
2363 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2364 }
2365
2366 // ANGLE issue 486: Detect problematic conditional discard
2367 if (discard && FindSideEffectRewriting::search(node))
2368 {
2369 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002370 }
2371 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372
2373 return false;
2374}
2375
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002376bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002377{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002378 if (node->getStatementList())
2379 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002380 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002381 outputTriplet(visit, "switch (", ") ", "");
2382 // The curly braces get written when visiting the statementList aggregate
2383 }
2384 else
2385 {
2386 // No statementList, so it won't output curly braces
2387 outputTriplet(visit, "switch (", ") {", "}\n");
2388 }
2389 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002390}
2391
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002392bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002393{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002394 if (node->hasCondition())
2395 {
2396 outputTriplet(visit, "case (", "", "):\n");
2397 return true;
2398 }
2399 else
2400 {
2401 TInfoSinkBase &out = getInfoSink();
2402 out << "default:\n";
2403 return false;
2404 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002405}
2406
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2408{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002409 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410}
2411
2412bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2413{
Nicolas Capens655fe362014-04-11 13:12:34 -04002414 mNestedLoopDepth++;
2415
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002416 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002417 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002418 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002419
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002420 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002421 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002422 if (handleExcessiveLoop(node))
2423 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002424 mInsideDiscontinuousLoop = wasDiscontinuous;
2425 mNestedLoopDepth--;
2426
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002427 return false;
2428 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002429 }
2430
Jamie Madill32aab012015-01-27 14:12:26 -05002431 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432
Corentin Wallez1239ee92015-03-19 14:38:02 -07002433 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002434 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002436 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002437
Jamie Madill075edd82013-07-08 13:30:19 -04002438 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002439 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002440 }
2441 else
2442 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002443 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002444
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445 if (node->getInit())
2446 {
2447 node->getInit()->traverse(this);
2448 }
2449
2450 out << "; ";
2451
alokp@chromium.org52813552010-11-16 18:36:09 +00002452 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002453 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002454 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002455 }
2456
2457 out << "; ";
2458
alokp@chromium.org52813552010-11-16 18:36:09 +00002459 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002461 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 }
2463
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002464 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002465
Jamie Madill075edd82013-07-08 13:30:19 -04002466 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002467 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468 }
2469
2470 if (node->getBody())
2471 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002472 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 }
2474
Jamie Madill075edd82013-07-08 13:30:19 -04002475 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002476 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477
alokp@chromium.org52813552010-11-16 18:36:09 +00002478 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479 {
Jamie Madill075edd82013-07-08 13:30:19 -04002480 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481 out << "while(\n";
2482
alokp@chromium.org52813552010-11-16 18:36:09 +00002483 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002484
daniel@transgaming.com73536982012-03-21 20:45:49 +00002485 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002486 }
2487
daniel@transgaming.com73536982012-03-21 20:45:49 +00002488 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002489
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002490 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002491 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002492
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493 return false;
2494}
2495
2496bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2497{
Jamie Madill32aab012015-01-27 14:12:26 -05002498 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002499
2500 switch (node->getFlowOp())
2501 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002502 case EOpKill:
2503 outputTriplet(visit, "discard;\n", "", "");
2504 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002505 case EOpBreak:
2506 if (visit == PreVisit)
2507 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002508 if (mNestedLoopDepth > 1)
2509 {
2510 mUsesNestedBreak = true;
2511 }
2512
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002513 if (mExcessiveLoopIndex)
2514 {
2515 out << "{Break";
2516 mExcessiveLoopIndex->traverse(this);
2517 out << " = true; break;}\n";
2518 }
2519 else
2520 {
2521 out << "break;\n";
2522 }
2523 }
2524 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002525 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002526 case EOpReturn:
2527 if (visit == PreVisit)
2528 {
2529 if (node->getExpression())
2530 {
2531 out << "return ";
2532 }
2533 else
2534 {
2535 out << "return;\n";
2536 }
2537 }
2538 else if (visit == PostVisit)
2539 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002540 if (node->getExpression())
2541 {
2542 out << ";\n";
2543 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 }
2545 break;
2546 default: UNREACHABLE();
2547 }
2548
2549 return true;
2550}
2551
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002552void OutputHLSL::traverseStatements(TIntermNode *node)
2553{
2554 if (isSingleStatement(node))
2555 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002556 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002557 }
2558
2559 node->traverse(this);
2560}
2561
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002562bool OutputHLSL::isSingleStatement(TIntermNode *node)
2563{
2564 TIntermAggregate *aggregate = node->getAsAggregate();
2565
2566 if (aggregate)
2567 {
2568 if (aggregate->getOp() == EOpSequence)
2569 {
2570 return false;
2571 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002572 else if (aggregate->getOp() == EOpDeclaration)
2573 {
2574 // Declaring multiple comma-separated variables must be considered multiple statements
2575 // because each individual declaration has side effects which are visible in the next.
2576 return false;
2577 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002578 else
2579 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002580 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002581 {
2582 if (!isSingleStatement(*sit))
2583 {
2584 return false;
2585 }
2586 }
2587
2588 return true;
2589 }
2590 }
2591
2592 return true;
2593}
2594
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002595// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2596// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002597bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2598{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002599 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002600 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002601
2602 // Parse loops of the form:
2603 // for(int index = initial; index [comparator] limit; index += increment)
2604 TIntermSymbol *index = NULL;
2605 TOperator comparator = EOpNull;
2606 int initial = 0;
2607 int limit = 0;
2608 int increment = 0;
2609
2610 // Parse index name and intial value
2611 if (node->getInit())
2612 {
2613 TIntermAggregate *init = node->getInit()->getAsAggregate();
2614
2615 if (init)
2616 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002617 TIntermSequence *sequence = init->getSequence();
2618 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002619
2620 if (variable && variable->getQualifier() == EvqTemporary)
2621 {
2622 TIntermBinary *assign = variable->getAsBinaryNode();
2623
2624 if (assign->getOp() == EOpInitialize)
2625 {
2626 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2627 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2628
2629 if (symbol && constant)
2630 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002631 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002632 {
2633 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002634 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002635 }
2636 }
2637 }
2638 }
2639 }
2640 }
2641
2642 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002643 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002644 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002645 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002646
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002647 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2648 {
2649 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2650
2651 if (constant)
2652 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002653 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002654 {
2655 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002656 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002657 }
2658 }
2659 }
2660 }
2661
2662 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002663 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002664 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002665 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2666 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002667
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002668 if (binaryTerminal)
2669 {
2670 TOperator op = binaryTerminal->getOp();
2671 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2672
2673 if (constant)
2674 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002675 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002676 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002677 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002678
2679 switch (op)
2680 {
2681 case EOpAddAssign: increment = value; break;
2682 case EOpSubAssign: increment = -value; break;
2683 default: UNIMPLEMENTED();
2684 }
2685 }
2686 }
2687 }
2688 else if (unaryTerminal)
2689 {
2690 TOperator op = unaryTerminal->getOp();
2691
2692 switch (op)
2693 {
2694 case EOpPostIncrement: increment = 1; break;
2695 case EOpPostDecrement: increment = -1; break;
2696 case EOpPreIncrement: increment = 1; break;
2697 case EOpPreDecrement: increment = -1; break;
2698 default: UNIMPLEMENTED();
2699 }
2700 }
2701 }
2702
2703 if (index != NULL && comparator != EOpNull && increment != 0)
2704 {
2705 if (comparator == EOpLessThanEqual)
2706 {
2707 comparator = EOpLessThan;
2708 limit += 1;
2709 }
2710
2711 if (comparator == EOpLessThan)
2712 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002713 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002714
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002715 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002716 {
2717 return false; // Not an excessive loop
2718 }
2719
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002720 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2721 mExcessiveLoopIndex = index;
2722
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002723 out << "{int ";
2724 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002725 out << ";\n"
2726 "bool Break";
2727 index->traverse(this);
2728 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002729
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002730 bool firstLoopFragment = true;
2731
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002732 while (iterations > 0)
2733 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002734 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002735
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002736 if (!firstLoopFragment)
2737 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002738 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002739 index->traverse(this);
2740 out << ") {\n";
2741 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002742
2743 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2744 {
2745 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2746 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002747
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002748 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002749 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002750
Corentin Wallez1239ee92015-03-19 14:38:02 -07002751 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002752 index->traverse(this);
2753 out << " = ";
2754 out << initial;
2755
2756 out << "; ";
2757 index->traverse(this);
2758 out << " < ";
2759 out << clampedLimit;
2760
2761 out << "; ";
2762 index->traverse(this);
2763 out << " += ";
2764 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002765 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002766
Jamie Madill075edd82013-07-08 13:30:19 -04002767 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002768 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002769
2770 if (node->getBody())
2771 {
2772 node->getBody()->traverse(this);
2773 }
2774
Jamie Madill075edd82013-07-08 13:30:19 -04002775 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002776 out << ";}\n";
2777
2778 if (!firstLoopFragment)
2779 {
2780 out << "}\n";
2781 }
2782
2783 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002784
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002785 initial += MAX_LOOP_ITERATIONS * increment;
2786 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002787 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002788
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002789 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002790
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002791 mExcessiveLoopIndex = restoreIndex;
2792
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002793 return true;
2794 }
2795 else UNIMPLEMENTED();
2796 }
2797
2798 return false; // Not handled as an excessive loop
2799}
2800
Olli Etuaho7fb49552015-03-18 17:27:44 +02002801void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002802{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002803 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804 {
2805 out << preString;
2806 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002807 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808 {
2809 out << inString;
2810 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002811 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002812 {
2813 out << postString;
2814 }
2815}
2816
Olli Etuaho7fb49552015-03-18 17:27:44 +02002817void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2818{
2819 outputTriplet(visit, preString, inString, postString, getInfoSink());
2820}
2821
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002822void OutputHLSL::outputLineDirective(int line)
2823{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002824 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002825 {
Jamie Madill32aab012015-01-27 14:12:26 -05002826 TInfoSinkBase &out = getInfoSink();
2827
2828 out << "\n";
2829 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002830
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002831 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002832 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002833 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002834 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002835
Jamie Madill32aab012015-01-27 14:12:26 -05002836 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002837 }
2838}
2839
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002840TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2841{
2842 TQualifier qualifier = symbol->getQualifier();
2843 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002844 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002845
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002846 if (name.empty()) // HLSL demands named arguments, also for prototypes
2847 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002848 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002849 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002850 else if (!symbol->isInternal())
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002851 {
Jamie Madill033dae62014-06-18 12:56:28 -04002852 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002853 }
2854
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002855 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2856 {
Jamie Madill033dae62014-06-18 12:56:28 -04002857 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002858 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002859 }
2860
Jamie Madill033dae62014-06-18 12:56:28 -04002861 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002862}
2863
2864TString OutputHLSL::initializer(const TType &type)
2865{
2866 TString string;
2867
Jamie Madill94bf7f22013-07-08 13:31:15 -04002868 size_t size = type.getObjectSize();
2869 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002870 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002871 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002872
Jamie Madill94bf7f22013-07-08 13:31:15 -04002873 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874 {
2875 string += ", ";
2876 }
2877 }
2878
daniel@transgaming.comead23042010-04-29 03:35:36 +00002879 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002880}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002881
Daniel Bratell29190082015-02-20 16:42:54 +01002882void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002883{
Olli Etuahof40319e2015-03-10 14:33:00 +02002884 if (type.isArray())
2885 {
2886 UNIMPLEMENTED();
2887 }
Jamie Madill32aab012015-01-27 14:12:26 -05002888 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002889
2890 if (visit == PreVisit)
2891 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002892 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002893
Daniel Bratell29190082015-02-20 16:42:54 +01002894 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002895 }
2896 else if (visit == InVisit)
2897 {
2898 out << ", ";
2899 }
2900 else if (visit == PostVisit)
2901 {
2902 out << ")";
2903 }
2904}
2905
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002906const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2907{
Jamie Madill32aab012015-01-27 14:12:26 -05002908 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002909
Jamie Madill98493dd2013-07-08 14:39:03 -04002910 const TStructure* structure = type.getStruct();
2911 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002912 {
Jamie Madill033dae62014-06-18 12:56:28 -04002913 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002914
Jamie Madill98493dd2013-07-08 14:39:03 -04002915 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002916
Jamie Madill98493dd2013-07-08 14:39:03 -04002917 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002918 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002919 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002920 constUnion = writeConstantUnion(*fieldType, constUnion);
2921
Jamie Madill98493dd2013-07-08 14:39:03 -04002922 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002923 {
2924 out << ", ";
2925 }
2926 }
2927
2928 out << ")";
2929 }
2930 else
2931 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002932 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002933 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002934
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002935 if (writeType)
2936 {
Jamie Madill033dae62014-06-18 12:56:28 -04002937 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002938 }
2939
Jamie Madill94bf7f22013-07-08 13:31:15 -04002940 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002941 {
2942 switch (constUnion->getType())
2943 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002944 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002945 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002946 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002947 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002948 default: UNREACHABLE();
2949 }
2950
2951 if (i != size - 1)
2952 {
2953 out << ", ";
2954 }
2955 }
2956
2957 if (writeType)
2958 {
2959 out << ")";
2960 }
2961 }
2962
2963 return constUnion;
2964}
2965
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002966void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2967{
2968 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2969 outputTriplet(visit, preString.c_str(), ", ", ")");
2970}
2971
Jamie Madill37997142015-01-28 10:06:34 -05002972bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2973{
2974 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2975 expression->traverse(&searchSymbol);
2976
2977 if (searchSymbol.foundMatch())
2978 {
2979 // Type already printed
2980 out << "t" + str(mUniqueIndex) + " = ";
2981 expression->traverse(this);
2982 out << ", ";
2983 symbolNode->traverse(this);
2984 out << " = t" + str(mUniqueIndex);
2985
2986 mUniqueIndex++;
2987 return true;
2988 }
2989
2990 return false;
2991}
2992
2993void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2994{
2995 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2996 << "\n"
2997 << "void initializeDeferredGlobals()\n"
2998 << "{\n";
2999
3000 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3001 {
3002 TIntermSymbol *symbol = deferredGlobal.first;
3003 TIntermTyped *expression = deferredGlobal.second;
3004 ASSERT(symbol);
3005 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
3006
3007 out << " " << Decorate(symbol->getSymbol()) << " = ";
3008
3009 if (!writeSameSymbolInitializer(out, symbol, expression))
3010 {
3011 ASSERT(mInfoSinkStack.top() == &out);
3012 expression->traverse(this);
3013 }
3014
3015 out << ";\n";
3016 }
3017
3018 out << "}\n"
3019 << "\n";
3020}
3021
Jamie Madill55e79e02015-02-09 15:35:00 -05003022TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3023{
3024 const TFieldList &fields = structure.fields();
3025
3026 for (const auto &eqFunction : mStructEqualityFunctions)
3027 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003028 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003029 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003030 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003031 }
3032 }
3033
3034 const TString &structNameString = StructNameString(structure);
3035
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003036 StructEqualityFunction *function = new StructEqualityFunction();
3037 function->structure = &structure;
3038 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003039
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003040 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003041
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003042 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3043 << "{\n"
3044 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003045
3046 for (size_t i = 0; i < fields.size(); i++)
3047 {
3048 const TField *field = fields[i];
3049 const TType *fieldType = field->type();
3050
3051 const TString &fieldNameA = "a." + Decorate(field->name());
3052 const TString &fieldNameB = "b." + Decorate(field->name());
3053
3054 if (i > 0)
3055 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003056 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003057 }
3058
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003059 fnOut << "(";
3060 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3061 fnOut << fieldNameA;
3062 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3063 fnOut << fieldNameB;
3064 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3065 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003066 }
3067
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003068 fnOut << ";\n" << "}\n";
3069
3070 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003071
3072 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003073 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003074
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003075 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003076}
3077
Olli Etuaho7fb49552015-03-18 17:27:44 +02003078TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3079{
3080 for (const auto &eqFunction : mArrayEqualityFunctions)
3081 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003082 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003083 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003084 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003085 }
3086 }
3087
3088 const TString &typeName = TypeString(type);
3089
Olli Etuaho12690762015-03-31 12:55:28 +03003090 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003091 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003092
3093 TInfoSinkBase fnNameOut;
3094 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003095 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003096
3097 TType nonArrayType = type;
3098 nonArrayType.clearArrayness();
3099
3100 TInfoSinkBase fnOut;
3101
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003102 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003103 << typeName << " a[" << type.getArraySize() << "], "
3104 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003105 << "{\n"
3106 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3107 " {\n"
3108 " if (";
3109
3110 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3111 fnOut << "a[i]";
3112 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3113 fnOut << "b[i]";
3114 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3115
3116 fnOut << ") { return false; }\n"
3117 " }\n"
3118 " return true;\n"
3119 "}\n";
3120
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003121 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003122
3123 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003124 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003125
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003126 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003127}
3128
Olli Etuaho12690762015-03-31 12:55:28 +03003129TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3130{
3131 for (const auto &assignFunction : mArrayAssignmentFunctions)
3132 {
3133 if (assignFunction.type == type)
3134 {
3135 return assignFunction.functionName;
3136 }
3137 }
3138
3139 const TString &typeName = TypeString(type);
3140
3141 ArrayHelperFunction function;
3142 function.type = type;
3143
3144 TInfoSinkBase fnNameOut;
3145 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3146 function.functionName = fnNameOut.c_str();
3147
3148 TInfoSinkBase fnOut;
3149
3150 fnOut << "void " << function.functionName << "(out "
3151 << typeName << " a[" << type.getArraySize() << "], "
3152 << typeName << " b[" << type.getArraySize() << "])\n"
3153 << "{\n"
3154 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3155 " {\n"
3156 " a[i] = b[i];\n"
3157 " }\n"
3158 "}\n";
3159
3160 function.functionDefinition = fnOut.c_str();
3161
3162 mArrayAssignmentFunctions.push_back(function);
3163
3164 return function.functionName;
3165}
3166
Olli Etuaho9638c352015-04-01 14:34:52 +03003167TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3168{
3169 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3170 {
3171 if (constructIntoFunction.type == type)
3172 {
3173 return constructIntoFunction.functionName;
3174 }
3175 }
3176
3177 const TString &typeName = TypeString(type);
3178
3179 ArrayHelperFunction function;
3180 function.type = type;
3181
3182 TInfoSinkBase fnNameOut;
3183 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3184 function.functionName = fnNameOut.c_str();
3185
3186 TInfoSinkBase fnOut;
3187
3188 fnOut << "void " << function.functionName << "(out "
3189 << typeName << " a[" << type.getArraySize() << "]";
3190 for (int i = 0; i < type.getArraySize(); ++i)
3191 {
3192 fnOut << ", " << typeName << " b" << i;
3193 }
3194 fnOut << ")\n"
3195 "{\n";
3196
3197 for (int i = 0; i < type.getArraySize(); ++i)
3198 {
3199 fnOut << " a[" << i << "] = b" << i << ";\n";
3200 }
3201 fnOut << "}\n";
3202
3203 function.functionDefinition = fnOut.c_str();
3204
3205 mArrayConstructIntoFunctions.push_back(function);
3206
3207 return function.functionName;
3208}
3209
3210
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003211}