blob: 1c7b0c48cfd0b4edc03d6229d75b362eab597ae4 [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/SearchSymbol.h"
23#include "compiler/translator/StructureHLSL.h"
24#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050025#include "compiler/translator/UniformHLSL.h"
26#include "compiler/translator/UtilsHLSL.h"
27#include "compiler/translator/blocklayout.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050028#include "compiler/translator/util.h"
29
Olli Etuaho4785fec2015-05-18 16:09:37 +030030namespace
31{
32
33bool IsSequence(TIntermNode *node)
34{
35 return node->getAsAggregate() != nullptr && node->getAsAggregate()->getOp() == EOpSequence;
36}
37
38} // namespace
39
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040namespace sh
41{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000042
Nicolas Capense0ba27a2013-06-24 16:10:52 -040043TString OutputHLSL::TextureFunction::name() const
44{
45 TString name = "gl_texture";
46
Nicolas Capens6d232bb2013-07-08 15:56:38 -040047 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040048 {
49 name += "2D";
50 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040051 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040052 {
53 name += "3D";
54 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040055 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040056 {
57 name += "Cube";
58 }
59 else UNREACHABLE();
60
61 if (proj)
62 {
63 name += "Proj";
64 }
65
Nicolas Capensb1f45b72013-12-19 17:37:19 -050066 if (offset)
67 {
68 name += "Offset";
69 }
70
Nicolas Capens75fb4752013-07-10 15:14:47 -040071 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040072 {
Nicolas Capensfc014542014-02-18 14:47:13 -050073 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040074 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050075 case LOD: name += "Lod"; break;
76 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040077 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050078 case SIZE: name += "Size"; break;
79 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050080 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040081 default: UNREACHABLE();
82 }
83
84 return name + "(";
85}
86
87bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
88{
89 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040090 if (sampler > rhs.sampler) return false;
91
Nicolas Capense0ba27a2013-06-24 16:10:52 -040092 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040093 if (coords > rhs.coords) return false;
94
Nicolas Capense0ba27a2013-06-24 16:10:52 -040095 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040096 if (proj && !rhs.proj) return false;
97
98 if (!offset && rhs.offset) return true;
99 if (offset && !rhs.offset) return false;
100
Nicolas Capens75fb4752013-07-10 15:14:47 -0400101 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400102 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400103
104 return false;
105}
106
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200107OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
108 const TExtensionBehavior &extensionBehavior,
109 const char *sourcePath, ShShaderOutput outputType,
110 int numRenderTargets, const std::vector<Uniform> &uniforms,
111 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400112 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200113 mShaderType(shaderType),
114 mShaderVersion(shaderVersion),
115 mExtensionBehavior(extensionBehavior),
116 mSourcePath(sourcePath),
117 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700118 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000119 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700120 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000122 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000123
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000124 mUsesFragColor = false;
125 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000126 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000127 mUsesFragCoord = false;
128 mUsesPointCoord = false;
129 mUsesFrontFacing = false;
130 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000131 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400132 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000133 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500134 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400135 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530136 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000137
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000138 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000139
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000140 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000141 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400142 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000143
144 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000145
Jamie Madill8daaba12014-06-13 10:04:33 -0400146 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200147 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400148
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000149 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000150 {
Arun Patole63419392015-03-13 11:51:07 +0530151 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
152 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
153 // In both cases total 3 uniform registers need to be reserved.
154 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000155 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000156
Jamie Madillf91ce812014-06-13 10:04:34 -0400157 // Reserve registers for the default uniform block and driver constants
158 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159}
160
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000161OutputHLSL::~OutputHLSL()
162{
Jamie Madill8daaba12014-06-13 10:04:33 -0400163 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400164 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200165 for (auto &eqFunction : mStructEqualityFunctions)
166 {
167 SafeDelete(eqFunction);
168 }
169 for (auto &eqFunction : mArrayEqualityFunctions)
170 {
171 SafeDelete(eqFunction);
172 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000173}
174
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200175void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000176{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200177 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400178 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000179
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200180 BuiltInFunctionEmulator builtInFunctionEmulator;
181 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200182 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500183
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700184 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700185 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
186 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300187 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700188 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700189
Jamie Madill37997142015-01-28 10:06:34 -0500190 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500191 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200192 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500193 mInfoSinkStack.pop();
194
Jamie Madill37997142015-01-28 10:06:34 -0500195 mInfoSinkStack.push(&mFooter);
196 if (!mDeferredGlobalInitializers.empty())
197 {
198 writeDeferredGlobalInitializers(mFooter);
199 }
200 mInfoSinkStack.pop();
201
Jamie Madill32aab012015-01-27 14:12:26 -0500202 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200203 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500204 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000205
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200206 objSink << mHeader.c_str();
207 objSink << mBody.c_str();
208 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200209
210 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000211}
212
Jamie Madill570e04d2013-06-21 09:15:33 -0400213void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
214{
215 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
216 {
217 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
218
Jamie Madill32aab012015-01-27 14:12:26 -0500219 TInfoSinkBase structInfoSink;
220 mInfoSinkStack.push(&structInfoSink);
221
Jamie Madill570e04d2013-06-21 09:15:33 -0400222 // This will mark the necessary block elements as referenced
223 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500224
225 TString structName(structInfoSink.c_str());
226 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400227
228 mFlaggedStructOriginalNames[flaggedNode] = structName;
229
230 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
231 {
232 structName.erase(pos, 1);
233 }
234
235 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
236 }
237}
238
Jamie Madill4e1fd412014-07-10 17:50:10 -0400239const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
240{
241 return mUniformHLSL->getInterfaceBlockRegisterMap();
242}
243
Jamie Madill9fe25e92014-07-18 10:33:08 -0400244const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
245{
246 return mUniformHLSL->getUniformRegisterMap();
247}
248
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000249int OutputHLSL::vectorSize(const TType &type) const
250{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000251 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000252 int arraySize = type.isArray() ? type.getArraySize() : 1;
253
254 return elementSize * arraySize;
255}
256
Jamie Madill98493dd2013-07-08 14:39:03 -0400257TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400258{
259 TString init;
260
261 TString preIndentString;
262 TString fullIndentString;
263
264 for (int spaces = 0; spaces < (indent * 4); spaces++)
265 {
266 preIndentString += ' ';
267 }
268
269 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
270 {
271 fullIndentString += ' ';
272 }
273
274 init += preIndentString + "{\n";
275
Jamie Madill98493dd2013-07-08 14:39:03 -0400276 const TFieldList &fields = structure.fields();
277 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400278 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400279 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400280 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400281 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400282
Jamie Madill98493dd2013-07-08 14:39:03 -0400283 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400284 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400285 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 }
287 else
288 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400289 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400290 }
291 }
292
293 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
294
295 return init;
296}
297
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200298void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299{
Jamie Madill32aab012015-01-27 14:12:26 -0500300 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000302 TString varyings;
303 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400304 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000305
Jamie Madill829f59e2013-11-13 19:40:54 -0500306 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400307 {
308 TIntermTyped *structNode = flaggedStructIt->first;
309 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400310 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400311 const TString &originalName = mFlaggedStructOriginalNames[structNode];
312
Jamie Madill033dae62014-06-18 12:56:28 -0400313 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400314 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400315 flaggedStructs += "\n";
316 }
317
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000318 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
319 {
320 const TType &type = varying->second->getType();
321 const TString &name = varying->second->getSymbol();
322
323 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400324 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
325 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000326 }
327
328 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
329 {
330 const TType &type = attribute->second->getType();
331 const TString &name = attribute->second->getSymbol();
332
Jamie Madill033dae62014-06-18 12:56:28 -0400333 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000334 }
335
Jamie Madill8daaba12014-06-13 10:04:33 -0400336 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400337
Jamie Madillf91ce812014-06-13 10:04:34 -0400338 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
339 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
340
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200341 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500342 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200343 out << "\n// Equality functions\n\n";
344 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500345 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200346 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200347 }
348 }
Olli Etuaho12690762015-03-31 12:55:28 +0300349 if (!mArrayAssignmentFunctions.empty())
350 {
351 out << "\n// Assignment functions\n\n";
352 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
353 {
354 out << assignmentFunction.functionDefinition << "\n";
355 }
356 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300357 if (!mArrayConstructIntoFunctions.empty())
358 {
359 out << "\n// Array constructor functions\n\n";
360 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
361 {
362 out << constructIntoFunction.functionDefinition << "\n";
363 }
364 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200365
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500366 if (mUsesDiscardRewriting)
367 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400368 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500369 }
370
Nicolas Capens655fe362014-04-11 13:12:34 -0400371 if (mUsesNestedBreak)
372 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400373 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400374 }
375
Arun Patole44efa0b2015-03-04 17:11:05 +0530376 if (mRequiresIEEEStrictCompiling)
377 {
378 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
379 }
380
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400381 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
382 "#define LOOP [loop]\n"
383 "#define FLATTEN [flatten]\n"
384 "#else\n"
385 "#define LOOP\n"
386 "#define FLATTEN\n"
387 "#endif\n";
388
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200389 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200391 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
392 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000393
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000394 out << "// Varyings\n";
395 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400396 out << "\n";
397
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200398 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000399 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500400 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000401 {
Jamie Madill46131a32013-06-20 11:55:50 -0400402 const TString &variableName = outputVariableIt->first;
403 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400404
Jamie Madill033dae62014-06-18 12:56:28 -0400405 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400406 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000407 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000408 }
Jamie Madill46131a32013-06-20 11:55:50 -0400409 else
410 {
411 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
412
413 out << "static float4 gl_Color[" << numColorValues << "] =\n"
414 "{\n";
415 for (unsigned int i = 0; i < numColorValues; i++)
416 {
417 out << " float4(0, 0, 0, 0)";
418 if (i + 1 != numColorValues)
419 {
420 out << ",";
421 }
422 out << "\n";
423 }
424
425 out << "};\n";
426 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000427
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400428 if (mUsesFragDepth)
429 {
430 out << "static float gl_Depth = 0.0;\n";
431 }
432
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000433 if (mUsesFragCoord)
434 {
435 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
436 }
437
438 if (mUsesPointCoord)
439 {
440 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
441 }
442
443 if (mUsesFrontFacing)
444 {
445 out << "static bool gl_FrontFacing = false;\n";
446 }
447
448 out << "\n";
449
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000450 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000451 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000452 out << "struct gl_DepthRangeParameters\n"
453 "{\n"
454 " float near;\n"
455 " float far;\n"
456 " float diff;\n"
457 "};\n"
458 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000459 }
460
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000461 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000462 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000463 out << "cbuffer DriverConstants : register(b1)\n"
464 "{\n";
465
466 if (mUsesDepthRange)
467 {
468 out << " float3 dx_DepthRange : packoffset(c0);\n";
469 }
470
471 if (mUsesFragCoord)
472 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000473 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000474 }
475
476 if (mUsesFragCoord || mUsesFrontFacing)
477 {
478 out << " float3 dx_DepthFront : packoffset(c2);\n";
479 }
480
481 out << "};\n";
482 }
483 else
484 {
485 if (mUsesDepthRange)
486 {
487 out << "uniform float3 dx_DepthRange : register(c0);";
488 }
489
490 if (mUsesFragCoord)
491 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000492 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000493 }
494
495 if (mUsesFragCoord || mUsesFrontFacing)
496 {
497 out << "uniform float3 dx_DepthFront : register(c2);\n";
498 }
499 }
500
501 out << "\n";
502
503 if (mUsesDepthRange)
504 {
505 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
506 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000507 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000508
Jamie Madillf91ce812014-06-13 10:04:34 -0400509 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000510 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400511 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000512 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400513 out << flaggedStructs;
514 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000515 }
516
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000517 if (usingMRTExtension && mNumRenderTargets > 1)
518 {
519 out << "#define GL_USES_MRT\n";
520 }
521
522 if (mUsesFragColor)
523 {
524 out << "#define GL_USES_FRAG_COLOR\n";
525 }
526
527 if (mUsesFragData)
528 {
529 out << "#define GL_USES_FRAG_DATA\n";
530 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000532 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000534 out << "// Attributes\n";
535 out << attributes;
536 out << "\n"
537 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400538
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000539 if (mUsesPointSize)
540 {
541 out << "static float gl_PointSize = float(1);\n";
542 }
543
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000544 if (mUsesInstanceID)
545 {
546 out << "static int gl_InstanceID;";
547 }
548
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000549 out << "\n"
550 "// Varyings\n";
551 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000552 out << "\n";
553
554 if (mUsesDepthRange)
555 {
556 out << "struct gl_DepthRangeParameters\n"
557 "{\n"
558 " float near;\n"
559 " float far;\n"
560 " float diff;\n"
561 "};\n"
562 "\n";
563 }
564
565 if (mOutputType == SH_HLSL11_OUTPUT)
566 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800567 out << "cbuffer DriverConstants : register(b1)\n"
568 "{\n";
569
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000570 if (mUsesDepthRange)
571 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800572 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000573 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800574
Cooper Partine6664f02015-01-09 16:22:24 -0800575 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800576 // However, we declare it for all shaders (including Feature Level 10+).
577 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
578 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800579 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800580
581 out << "};\n"
582 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000583 }
584 else
585 {
586 if (mUsesDepthRange)
587 {
588 out << "uniform float3 dx_DepthRange : register(c0);\n";
589 }
590
Cooper Partine6664f02015-01-09 16:22:24 -0800591 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
592 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000593 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000594 }
595
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000596 if (mUsesDepthRange)
597 {
598 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
599 "\n";
600 }
601
Jamie Madillf91ce812014-06-13 10:04:34 -0400602 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000603 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400604 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000605 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400606 out << flaggedStructs;
607 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000608 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400609 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000610
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400611 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
612 {
613 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400614 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000615 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400616 switch(textureFunction->sampler)
617 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400618 case EbtSampler2D: out << "int2 "; break;
619 case EbtSampler3D: out << "int3 "; break;
620 case EbtSamplerCube: out << "int2 "; break;
621 case EbtSampler2DArray: out << "int3 "; break;
622 case EbtISampler2D: out << "int2 "; break;
623 case EbtISampler3D: out << "int3 "; break;
624 case EbtISamplerCube: out << "int2 "; break;
625 case EbtISampler2DArray: out << "int3 "; break;
626 case EbtUSampler2D: out << "int2 "; break;
627 case EbtUSampler3D: out << "int3 "; break;
628 case EbtUSamplerCube: out << "int2 "; break;
629 case EbtUSampler2DArray: out << "int3 "; break;
630 case EbtSampler2DShadow: out << "int2 "; break;
631 case EbtSamplerCubeShadow: out << "int2 "; break;
632 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400633 default: UNREACHABLE();
634 }
635 }
636 else // Sampling function
637 {
638 switch(textureFunction->sampler)
639 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400640 case EbtSampler2D: out << "float4 "; break;
641 case EbtSampler3D: out << "float4 "; break;
642 case EbtSamplerCube: out << "float4 "; break;
643 case EbtSampler2DArray: out << "float4 "; break;
644 case EbtISampler2D: out << "int4 "; break;
645 case EbtISampler3D: out << "int4 "; break;
646 case EbtISamplerCube: out << "int4 "; break;
647 case EbtISampler2DArray: out << "int4 "; break;
648 case EbtUSampler2D: out << "uint4 "; break;
649 case EbtUSampler3D: out << "uint4 "; break;
650 case EbtUSamplerCube: out << "uint4 "; break;
651 case EbtUSampler2DArray: out << "uint4 "; break;
652 case EbtSampler2DShadow: out << "float "; break;
653 case EbtSamplerCubeShadow: out << "float "; break;
654 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400655 default: UNREACHABLE();
656 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000657 }
658
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400659 // Function name
660 out << textureFunction->name();
661
662 // Argument list
663 int hlslCoords = 4;
664
665 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000666 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400667 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000668 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400669 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
670 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
671 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000672 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400673
Nicolas Capens75fb4752013-07-10 15:14:47 -0400674 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000675 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400676 case TextureFunction::IMPLICIT: break;
677 case TextureFunction::BIAS: hlslCoords = 4; break;
678 case TextureFunction::LOD: hlslCoords = 4; break;
679 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400680 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400681 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000682 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400683 }
684 else if (mOutputType == SH_HLSL11_OUTPUT)
685 {
686 switch(textureFunction->sampler)
687 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400688 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
689 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
690 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
691 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
692 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
693 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500694 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400695 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
696 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
697 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500698 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400699 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
700 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
701 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
702 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400703 default: UNREACHABLE();
704 }
705 }
706 else UNREACHABLE();
707
Nicolas Capensfc014542014-02-18 14:47:13 -0500708 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400709 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500710 switch(textureFunction->coords)
711 {
712 case 2: out << ", int2 t"; break;
713 case 3: out << ", int3 t"; break;
714 default: UNREACHABLE();
715 }
716 }
717 else // Floating-point coordinates (except textureSize)
718 {
719 switch(textureFunction->coords)
720 {
721 case 1: out << ", int lod"; break; // textureSize()
722 case 2: out << ", float2 t"; break;
723 case 3: out << ", float3 t"; break;
724 case 4: out << ", float4 t"; break;
725 default: UNREACHABLE();
726 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000727 }
728
Nicolas Capensd11d5492014-02-19 17:06:10 -0500729 if (textureFunction->method == TextureFunction::GRAD)
730 {
731 switch(textureFunction->sampler)
732 {
733 case EbtSampler2D:
734 case EbtISampler2D:
735 case EbtUSampler2D:
736 case EbtSampler2DArray:
737 case EbtISampler2DArray:
738 case EbtUSampler2DArray:
739 case EbtSampler2DShadow:
740 case EbtSampler2DArrayShadow:
741 out << ", float2 ddx, float2 ddy";
742 break;
743 case EbtSampler3D:
744 case EbtISampler3D:
745 case EbtUSampler3D:
746 case EbtSamplerCube:
747 case EbtISamplerCube:
748 case EbtUSamplerCube:
749 case EbtSamplerCubeShadow:
750 out << ", float3 ddx, float3 ddy";
751 break;
752 default: UNREACHABLE();
753 }
754 }
755
Nicolas Capens75fb4752013-07-10 15:14:47 -0400756 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000757 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400758 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400759 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400760 case TextureFunction::LOD: out << ", float lod"; break;
761 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400762 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400763 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500764 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500765 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400766 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000767 }
768
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500769 if (textureFunction->offset)
770 {
771 switch(textureFunction->sampler)
772 {
773 case EbtSampler2D: out << ", int2 offset"; break;
774 case EbtSampler3D: out << ", int3 offset"; break;
775 case EbtSampler2DArray: out << ", int2 offset"; break;
776 case EbtISampler2D: out << ", int2 offset"; break;
777 case EbtISampler3D: out << ", int3 offset"; break;
778 case EbtISampler2DArray: out << ", int2 offset"; break;
779 case EbtUSampler2D: out << ", int2 offset"; break;
780 case EbtUSampler3D: out << ", int3 offset"; break;
781 case EbtUSampler2DArray: out << ", int2 offset"; break;
782 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500783 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500784 default: UNREACHABLE();
785 }
786 }
787
Nicolas Capens84cfa122014-04-14 13:48:45 -0400788 if (textureFunction->method == TextureFunction::BIAS ||
789 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500790 {
791 out << ", float bias";
792 }
793
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400794 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400795 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400796
Nicolas Capens75fb4752013-07-10 15:14:47 -0400797 if (textureFunction->method == TextureFunction::SIZE)
798 {
799 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
800 {
801 if (IsSamplerArray(textureFunction->sampler))
802 {
803 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
804 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
805 }
806 else
807 {
808 out << " uint width; uint height; uint numberOfLevels;\n"
809 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
810 }
811 }
812 else if (IsSampler3D(textureFunction->sampler))
813 {
814 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
815 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
816 }
817 else UNREACHABLE();
818
819 switch(textureFunction->sampler)
820 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400821 case EbtSampler2D: out << " return int2(width, height);"; break;
822 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
823 case EbtSamplerCube: out << " return int2(width, height);"; break;
824 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
825 case EbtISampler2D: out << " return int2(width, height);"; break;
826 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
827 case EbtISamplerCube: out << " return int2(width, height);"; break;
828 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
829 case EbtUSampler2D: out << " return int2(width, height);"; break;
830 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
831 case EbtUSamplerCube: out << " return int2(width, height);"; break;
832 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
833 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
834 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
835 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400836 default: UNREACHABLE();
837 }
838 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400839 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400840 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500841 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
842 {
843 out << " float width; float height; float layers; float levels;\n";
844
845 out << " uint mip = 0;\n";
846
847 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
848
849 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
850 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
851 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
852 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
853
854 // FACE_POSITIVE_X = 000b
855 // FACE_NEGATIVE_X = 001b
856 // FACE_POSITIVE_Y = 010b
857 // FACE_NEGATIVE_Y = 011b
858 // FACE_POSITIVE_Z = 100b
859 // FACE_NEGATIVE_Z = 101b
860 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
861
862 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
863 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
864 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
865
866 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
867 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
868 }
869 else if (IsIntegerSampler(textureFunction->sampler) &&
870 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400871 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400872 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400873 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400874 if (IsSamplerArray(textureFunction->sampler))
875 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400876 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400877
Nicolas Capens9edebd62013-08-06 10:59:10 -0400878 if (textureFunction->method == TextureFunction::LOD0)
879 {
880 out << " uint mip = 0;\n";
881 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400882 else if (textureFunction->method == TextureFunction::LOD0BIAS)
883 {
884 out << " uint mip = bias;\n";
885 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400886 else
887 {
888 if (textureFunction->method == TextureFunction::IMPLICIT ||
889 textureFunction->method == TextureFunction::BIAS)
890 {
891 out << " x.GetDimensions(0, width, height, layers, levels);\n"
892 " float2 tSized = float2(t.x * width, t.y * height);\n"
893 " float dx = length(ddx(tSized));\n"
894 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500895 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400896
897 if (textureFunction->method == TextureFunction::BIAS)
898 {
899 out << " lod += bias;\n";
900 }
901 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500902 else if (textureFunction->method == TextureFunction::GRAD)
903 {
904 out << " x.GetDimensions(0, width, height, layers, levels);\n"
905 " float lod = log2(max(length(ddx), length(ddy)));\n";
906 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400907
908 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
909 }
910
911 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400912 }
913 else
914 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400915 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400916
Nicolas Capens9edebd62013-08-06 10:59:10 -0400917 if (textureFunction->method == TextureFunction::LOD0)
918 {
919 out << " uint mip = 0;\n";
920 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400921 else if (textureFunction->method == TextureFunction::LOD0BIAS)
922 {
923 out << " uint mip = bias;\n";
924 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400925 else
926 {
927 if (textureFunction->method == TextureFunction::IMPLICIT ||
928 textureFunction->method == TextureFunction::BIAS)
929 {
930 out << " x.GetDimensions(0, width, height, levels);\n"
931 " float2 tSized = float2(t.x * width, t.y * height);\n"
932 " float dx = length(ddx(tSized));\n"
933 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500934 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400935
936 if (textureFunction->method == TextureFunction::BIAS)
937 {
938 out << " lod += bias;\n";
939 }
940 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500941 else if (textureFunction->method == TextureFunction::LOD)
942 {
943 out << " x.GetDimensions(0, width, height, levels);\n";
944 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500945 else if (textureFunction->method == TextureFunction::GRAD)
946 {
947 out << " x.GetDimensions(0, width, height, levels);\n"
948 " float lod = log2(max(length(ddx), length(ddy)));\n";
949 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400950
951 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
952 }
953
954 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400955 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400956 }
957 else if (IsSampler3D(textureFunction->sampler))
958 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400959 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400960
Nicolas Capens9edebd62013-08-06 10:59:10 -0400961 if (textureFunction->method == TextureFunction::LOD0)
962 {
963 out << " uint mip = 0;\n";
964 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400965 else if (textureFunction->method == TextureFunction::LOD0BIAS)
966 {
967 out << " uint mip = bias;\n";
968 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400969 else
970 {
971 if (textureFunction->method == TextureFunction::IMPLICIT ||
972 textureFunction->method == TextureFunction::BIAS)
973 {
974 out << " x.GetDimensions(0, width, height, depth, levels);\n"
975 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
976 " float dx = length(ddx(tSized));\n"
977 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500978 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400979
980 if (textureFunction->method == TextureFunction::BIAS)
981 {
982 out << " lod += bias;\n";
983 }
984 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500985 else if (textureFunction->method == TextureFunction::GRAD)
986 {
987 out << " x.GetDimensions(0, width, height, depth, levels);\n"
988 " float lod = log2(max(length(ddx), length(ddy)));\n";
989 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400990
991 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
992 }
993
994 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400995 }
996 else UNREACHABLE();
997 }
998
999 out << " return ";
1000
1001 // HLSL intrinsic
1002 if (mOutputType == SH_HLSL9_OUTPUT)
1003 {
1004 switch(textureFunction->sampler)
1005 {
1006 case EbtSampler2D: out << "tex2D"; break;
1007 case EbtSamplerCube: out << "texCUBE"; break;
1008 default: UNREACHABLE();
1009 }
1010
Nicolas Capens75fb4752013-07-10 15:14:47 -04001011 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001012 {
1013 case TextureFunction::IMPLICIT: out << "(s, "; break;
1014 case TextureFunction::BIAS: out << "bias(s, "; break;
1015 case TextureFunction::LOD: out << "lod(s, "; break;
1016 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001017 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001018 default: UNREACHABLE();
1019 }
1020 }
1021 else if (mOutputType == SH_HLSL11_OUTPUT)
1022 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001023 if (textureFunction->method == TextureFunction::GRAD)
1024 {
1025 if (IsIntegerSampler(textureFunction->sampler))
1026 {
1027 out << "x.Load(";
1028 }
1029 else if (IsShadowSampler(textureFunction->sampler))
1030 {
1031 out << "x.SampleCmpLevelZero(s, ";
1032 }
1033 else
1034 {
1035 out << "x.SampleGrad(s, ";
1036 }
1037 }
1038 else if (IsIntegerSampler(textureFunction->sampler) ||
1039 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001040 {
1041 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001042 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001043 else if (IsShadowSampler(textureFunction->sampler))
1044 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001045 switch(textureFunction->method)
1046 {
1047 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1048 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1049 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1050 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1051 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1052 default: UNREACHABLE();
1053 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001054 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001055 else
1056 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001057 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001058 {
1059 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1060 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1061 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1062 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001063 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001064 default: UNREACHABLE();
1065 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001066 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001067 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001068 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001069
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001070 // Integer sampling requires integer addresses
1071 TString addressx = "";
1072 TString addressy = "";
1073 TString addressz = "";
1074 TString close = "";
1075
Nicolas Capensfc014542014-02-18 14:47:13 -05001076 if (IsIntegerSampler(textureFunction->sampler) ||
1077 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001078 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001079 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001080 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001081 case 2: out << "int3("; break;
1082 case 3: out << "int4("; break;
1083 default: UNREACHABLE();
1084 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001085
Nicolas Capensfc014542014-02-18 14:47:13 -05001086 // Convert from normalized floating-point to integer
1087 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001088 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001089 addressx = "int(floor(width * frac((";
1090 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001091
Nicolas Capensfc014542014-02-18 14:47:13 -05001092 if (IsSamplerArray(textureFunction->sampler))
1093 {
1094 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1095 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001096 else if (IsSamplerCube(textureFunction->sampler))
1097 {
1098 addressz = "((((";
1099 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001100 else
1101 {
1102 addressz = "int(floor(depth * frac((";
1103 }
1104
1105 close = "))))";
1106 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001107 }
1108 else
1109 {
1110 switch(hlslCoords)
1111 {
1112 case 2: out << "float2("; break;
1113 case 3: out << "float3("; break;
1114 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001115 default: UNREACHABLE();
1116 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001117 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001118
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001119 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001120
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001121 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001122 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001123 switch(textureFunction->coords)
1124 {
1125 case 3: proj = " / t.z"; break;
1126 case 4: proj = " / t.w"; break;
1127 default: UNREACHABLE();
1128 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001129 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001130
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001131 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001132
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001133 if (mOutputType == SH_HLSL9_OUTPUT)
1134 {
1135 if (hlslCoords >= 3)
1136 {
1137 if (textureFunction->coords < 3)
1138 {
1139 out << ", 0";
1140 }
1141 else
1142 {
1143 out << ", t.z" + proj;
1144 }
1145 }
1146
1147 if (hlslCoords == 4)
1148 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001149 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001150 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001151 case TextureFunction::BIAS: out << ", bias"; break;
1152 case TextureFunction::LOD: out << ", lod"; break;
1153 case TextureFunction::LOD0: out << ", 0"; break;
1154 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001155 default: UNREACHABLE();
1156 }
1157 }
1158
1159 out << "));\n";
1160 }
1161 else if (mOutputType == SH_HLSL11_OUTPUT)
1162 {
1163 if (hlslCoords >= 3)
1164 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001165 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1166 {
1167 out << ", face";
1168 }
1169 else
1170 {
1171 out << ", " + addressz + ("t.z" + proj) + close;
1172 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001173 }
1174
Nicolas Capensd11d5492014-02-19 17:06:10 -05001175 if (textureFunction->method == TextureFunction::GRAD)
1176 {
1177 if (IsIntegerSampler(textureFunction->sampler))
1178 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001179 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001180 }
1181 else if (IsShadowSampler(textureFunction->sampler))
1182 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001183 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001184 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001185 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001186 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1187 // The resulting third component of P' in the shadow forms is used as Dref
1188 out << "), t.z" << proj;
1189 }
1190 else
1191 {
1192 switch(textureFunction->coords)
1193 {
1194 case 3: out << "), t.z"; break;
1195 case 4: out << "), t.w"; break;
1196 default: UNREACHABLE();
1197 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001198 }
1199 }
1200 else
1201 {
1202 out << "), ddx, ddy";
1203 }
1204 }
1205 else if (IsIntegerSampler(textureFunction->sampler) ||
1206 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001207 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001208 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001209 }
1210 else if (IsShadowSampler(textureFunction->sampler))
1211 {
1212 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001213 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001214 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001215 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1216 // The resulting third component of P' in the shadow forms is used as Dref
1217 out << "), t.z" << proj;
1218 }
1219 else
1220 {
1221 switch(textureFunction->coords)
1222 {
1223 case 3: out << "), t.z"; break;
1224 case 4: out << "), t.w"; break;
1225 default: UNREACHABLE();
1226 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001227 }
1228 }
1229 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001230 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001231 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001232 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001233 case TextureFunction::IMPLICIT: out << ")"; break;
1234 case TextureFunction::BIAS: out << "), bias"; break;
1235 case TextureFunction::LOD: out << "), lod"; break;
1236 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001237 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001238 default: UNREACHABLE();
1239 }
1240 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001241
1242 if (textureFunction->offset)
1243 {
1244 out << ", offset";
1245 }
1246
1247 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001248 }
1249 else UNREACHABLE();
1250 }
1251
1252 out << "\n"
1253 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001254 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001255 }
1256
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001257 if (mUsesFragCoord)
1258 {
1259 out << "#define GL_USES_FRAG_COORD\n";
1260 }
1261
1262 if (mUsesPointCoord)
1263 {
1264 out << "#define GL_USES_POINT_COORD\n";
1265 }
1266
1267 if (mUsesFrontFacing)
1268 {
1269 out << "#define GL_USES_FRONT_FACING\n";
1270 }
1271
1272 if (mUsesPointSize)
1273 {
1274 out << "#define GL_USES_POINT_SIZE\n";
1275 }
1276
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001277 if (mUsesFragDepth)
1278 {
1279 out << "#define GL_USES_FRAG_DEPTH\n";
1280 }
1281
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001282 if (mUsesDepthRange)
1283 {
1284 out << "#define GL_USES_DEPTH_RANGE\n";
1285 }
1286
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001287 if (mUsesXor)
1288 {
1289 out << "bool xor(bool p, bool q)\n"
1290 "{\n"
1291 " return (p || q) && !(p && q);\n"
1292 "}\n"
1293 "\n";
1294 }
1295
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001296 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001297}
1298
1299void OutputHLSL::visitSymbol(TIntermSymbol *node)
1300{
Jamie Madill32aab012015-01-27 14:12:26 -05001301 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001302
Jamie Madill570e04d2013-06-21 09:15:33 -04001303 // Handle accessing std140 structs by value
1304 if (mFlaggedStructMappedNames.count(node) > 0)
1305 {
1306 out << mFlaggedStructMappedNames[node];
1307 return;
1308 }
1309
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001310 TString name = node->getSymbol();
1311
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001312 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001313 {
1314 mUsesDepthRange = true;
1315 out << name;
1316 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001317 else
1318 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001319 TQualifier qualifier = node->getQualifier();
1320
1321 if (qualifier == EvqUniform)
1322 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001323 const TType &nodeType = node->getType();
1324 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001325
1326 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001327 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001328 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001329 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001330 else
1331 {
1332 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001333 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001334
Jamie Madill2e295e22015-04-29 10:41:33 -04001335 ensureStructDefined(nodeType);
1336
Jamie Madill033dae62014-06-18 12:56:28 -04001337 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001338 }
Jamie Madill19571812013-08-12 15:26:34 -07001339 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001340 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001341 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001342 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001343 }
Jamie Madill033dae62014-06-18 12:56:28 -04001344 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001345 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001346 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001347 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001348 }
Jamie Madill19571812013-08-12 15:26:34 -07001349 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001350 {
1351 mReferencedOutputVariables[name] = node;
1352 out << "out_" << name;
1353 }
1354 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001355 {
1356 out << "gl_Color[0]";
1357 mUsesFragColor = true;
1358 }
1359 else if (qualifier == EvqFragData)
1360 {
1361 out << "gl_Color";
1362 mUsesFragData = true;
1363 }
1364 else if (qualifier == EvqFragCoord)
1365 {
1366 mUsesFragCoord = true;
1367 out << name;
1368 }
1369 else if (qualifier == EvqPointCoord)
1370 {
1371 mUsesPointCoord = true;
1372 out << name;
1373 }
1374 else if (qualifier == EvqFrontFacing)
1375 {
1376 mUsesFrontFacing = true;
1377 out << name;
1378 }
1379 else if (qualifier == EvqPointSize)
1380 {
1381 mUsesPointSize = true;
1382 out << name;
1383 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001384 else if (qualifier == EvqInstanceID)
1385 {
1386 mUsesInstanceID = true;
1387 out << name;
1388 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001389 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001390 {
1391 mUsesFragDepth = true;
1392 out << "gl_Depth";
1393 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001394 else
1395 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001396 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001397 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001398 }
1399}
1400
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001401void OutputHLSL::visitRaw(TIntermRaw *node)
1402{
Jamie Madill32aab012015-01-27 14:12:26 -05001403 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001404}
1405
Olli Etuaho7fb49552015-03-18 17:27:44 +02001406void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1407{
1408 if (type.isScalar() && !type.isArray())
1409 {
1410 if (op == EOpEqual)
1411 {
1412 outputTriplet(visit, "(", " == ", ")", out);
1413 }
1414 else
1415 {
1416 outputTriplet(visit, "(", " != ", ")", out);
1417 }
1418 }
1419 else
1420 {
1421 if (visit == PreVisit && op == EOpNotEqual)
1422 {
1423 out << "!";
1424 }
1425
1426 if (type.isArray())
1427 {
1428 const TString &functionName = addArrayEqualityFunction(type);
1429 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1430 }
1431 else if (type.getBasicType() == EbtStruct)
1432 {
1433 const TStructure &structure = *type.getStruct();
1434 const TString &functionName = addStructEqualityFunction(structure);
1435 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1436 }
1437 else
1438 {
1439 ASSERT(type.isMatrix() || type.isVector());
1440 outputTriplet(visit, "all(", " == ", ")", out);
1441 }
1442 }
1443}
1444
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001445bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1446{
Jamie Madill32aab012015-01-27 14:12:26 -05001447 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001448
Jamie Madill570e04d2013-06-21 09:15:33 -04001449 // Handle accessing std140 structs by value
1450 if (mFlaggedStructMappedNames.count(node) > 0)
1451 {
1452 out << mFlaggedStructMappedNames[node];
1453 return false;
1454 }
1455
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001456 switch (node->getOp())
1457 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001458 case EOpAssign:
1459 if (node->getLeft()->isArray())
1460 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001461 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1462 if (rightAgg != nullptr && rightAgg->isConstructor())
1463 {
1464 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1465 out << functionName << "(";
1466 node->getLeft()->traverse(this);
1467 TIntermSequence *seq = rightAgg->getSequence();
1468 for (auto &arrayElement : *seq)
1469 {
1470 out << ", ";
1471 arrayElement->traverse(this);
1472 }
1473 out << ")";
1474 return false;
1475 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001476 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1477 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1478
1479 const TString &functionName = addArrayAssignmentFunction(node->getType());
1480 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001481 }
1482 else
1483 {
1484 outputTriplet(visit, "(", " = ", ")");
1485 }
1486 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001487 case EOpInitialize:
1488 if (visit == PreVisit)
1489 {
1490 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1491 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1492 // new variable is created before the assignment is evaluated), so we need to convert
1493 // this to "float t = x, x = t;".
1494
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001495 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001496 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001497 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001498
Jamie Madill37997142015-01-28 10:06:34 -05001499 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1500 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001501 {
Jamie Madill37997142015-01-28 10:06:34 -05001502 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001503 // after we initialize uniforms.
1504 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1505 deferredInit->setLeft(node->getLeft());
1506 deferredInit->setRight(node->getRight());
1507 deferredInit->setType(node->getType());
1508 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001509 const TString &initString = initializer(node->getType());
1510 node->setRight(new TIntermRaw(node->getType(), initString));
1511 }
1512 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1513 {
1514 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001515 return false;
1516 }
1517 }
1518 else if (visit == InVisit)
1519 {
1520 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001521 }
1522 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001523 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1524 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1525 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1526 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1527 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1528 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001529 if (visit == PreVisit)
1530 {
1531 out << "(";
1532 }
1533 else if (visit == InVisit)
1534 {
1535 out << " = mul(";
1536 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001537 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001538 }
1539 else
1540 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001541 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001542 }
1543 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001544 case EOpMatrixTimesMatrixAssign:
1545 if (visit == PreVisit)
1546 {
1547 out << "(";
1548 }
1549 else if (visit == InVisit)
1550 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001551 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001552 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001553 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001554 }
1555 else
1556 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001557 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001558 }
1559 break;
1560 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001561 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001562 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1563 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1564 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1565 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1566 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001567 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001568 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001569 const TType& leftType = node->getLeft()->getType();
1570 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001571 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001572 if (visit == PreVisit)
1573 {
1574 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1575 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001576 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001577 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001578 return false;
1579 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001580 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001581 else
1582 {
1583 outputTriplet(visit, "", "[", "]");
1584 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001585 }
1586 break;
1587 case EOpIndexIndirect:
1588 // We do not currently support indirect references to interface blocks
1589 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1590 outputTriplet(visit, "", "[", "]");
1591 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001592 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001593 if (visit == InVisit)
1594 {
1595 const TStructure* structure = node->getLeft()->getType().getStruct();
1596 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1597 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001598 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001599
1600 return false;
1601 }
1602 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001603 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001604 if (visit == InVisit)
1605 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001606 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1607 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1608 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001609 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001610
1611 return false;
1612 }
1613 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001614 case EOpVectorSwizzle:
1615 if (visit == InVisit)
1616 {
1617 out << ".";
1618
1619 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1620
1621 if (swizzle)
1622 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001623 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001624
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001625 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626 {
1627 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1628
1629 if (element)
1630 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001631 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632
1633 switch (i)
1634 {
1635 case 0: out << "x"; break;
1636 case 1: out << "y"; break;
1637 case 2: out << "z"; break;
1638 case 3: out << "w"; break;
1639 default: UNREACHABLE();
1640 }
1641 }
1642 else UNREACHABLE();
1643 }
1644 }
1645 else UNREACHABLE();
1646
1647 return false; // Fully processed
1648 }
1649 break;
1650 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1651 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1652 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1653 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001654 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001655 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1656 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1657 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1658 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1659 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001660 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001661 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001662 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001663 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1665 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1666 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1667 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1668 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001669 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001670 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1671 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001672 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001673 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001674 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1675 ASSERT(!node->getRight()->hasSideEffects());
1676 outputTriplet(visit, "(", " || ", ")");
1677 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001678 case EOpLogicalXor:
1679 mUsesXor = true;
1680 outputTriplet(visit, "xor(", ", ", ")");
1681 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001682 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001683 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1684 ASSERT(!node->getRight()->hasSideEffects());
1685 outputTriplet(visit, "(", " && ", ")");
1686 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687 default: UNREACHABLE();
1688 }
1689
1690 return true;
1691}
1692
1693bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1694{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695 switch (node->getOp())
1696 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001697 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001698 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001699 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1700 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001701 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001702 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1703 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1704 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1705 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001706 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1707 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1708 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1709 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1710 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1711 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1712 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1713 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001714 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1715 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1716 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1717 case EOpAsinh:
1718 ASSERT(node->getUseEmulatedFunction());
1719 writeEmulatedFunctionTriplet(visit, "asinh(");
1720 break;
1721 case EOpAcosh:
1722 ASSERT(node->getUseEmulatedFunction());
1723 writeEmulatedFunctionTriplet(visit, "acosh(");
1724 break;
1725 case EOpAtanh:
1726 ASSERT(node->getUseEmulatedFunction());
1727 writeEmulatedFunctionTriplet(visit, "atanh(");
1728 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001729 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1730 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1731 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1732 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1733 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1734 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1735 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1736 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1737 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001738 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1739 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1740 case EOpRoundEven:
1741 ASSERT(node->getUseEmulatedFunction());
1742 writeEmulatedFunctionTriplet(visit, "roundEven(");
1743 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001744 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1745 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301746 case EOpIsNan:
1747 outputTriplet(visit, "isnan(", "", ")");
1748 mRequiresIEEEStrictCompiling = true;
1749 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301750 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001751 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1752 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1753 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1754 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001755 case EOpPackSnorm2x16:
1756 ASSERT(node->getUseEmulatedFunction());
1757 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1758 break;
1759 case EOpPackUnorm2x16:
1760 ASSERT(node->getUseEmulatedFunction());
1761 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1762 break;
1763 case EOpPackHalf2x16:
1764 ASSERT(node->getUseEmulatedFunction());
1765 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1766 break;
1767 case EOpUnpackSnorm2x16:
1768 ASSERT(node->getUseEmulatedFunction());
1769 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1770 break;
1771 case EOpUnpackUnorm2x16:
1772 ASSERT(node->getUseEmulatedFunction());
1773 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1774 break;
1775 case EOpUnpackHalf2x16:
1776 ASSERT(node->getUseEmulatedFunction());
1777 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1778 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001779 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1780 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001781 case EOpDFdx:
1782 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1783 {
1784 outputTriplet(visit, "(", "", ", 0.0)");
1785 }
1786 else
1787 {
1788 outputTriplet(visit, "ddx(", "", ")");
1789 }
1790 break;
1791 case EOpDFdy:
1792 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1793 {
1794 outputTriplet(visit, "(", "", ", 0.0)");
1795 }
1796 else
1797 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001798 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001799 }
1800 break;
1801 case EOpFwidth:
1802 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1803 {
1804 outputTriplet(visit, "(", "", ", 0.0)");
1805 }
1806 else
1807 {
1808 outputTriplet(visit, "fwidth(", "", ")");
1809 }
1810 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001811 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1812 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001813 case EOpInverse:
1814 ASSERT(node->getUseEmulatedFunction());
1815 writeEmulatedFunctionTriplet(visit, "inverse(");
1816 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001817
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001818 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1819 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820 default: UNREACHABLE();
1821 }
1822
1823 return true;
1824}
1825
1826bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1827{
Jamie Madill32aab012015-01-27 14:12:26 -05001828 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 switch (node->getOp())
1831 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001832 case EOpSequence:
1833 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001834 if (mInsideFunction)
1835 {
Jamie Madill075edd82013-07-08 13:30:19 -04001836 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001837 out << "{\n";
1838 }
1839
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001840 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001841 {
Jamie Madill075edd82013-07-08 13:30:19 -04001842 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001843
Olli Etuahoa6f22092015-05-08 18:31:10 +03001844 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001845
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001846 // Don't output ; after case labels, they're terminated by :
1847 // This is needed especially since outputting a ; after a case statement would turn empty
1848 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03001849 // Also no need to output ; after selection (if) statements or sequences. This is done just
1850 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001851 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
1852 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03001853 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001854 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001855 }
1856
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001857 if (mInsideFunction)
1858 {
Jamie Madill075edd82013-07-08 13:30:19 -04001859 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001860 out << "}\n";
1861 }
1862
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001863 return false;
1864 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001865 case EOpDeclaration:
1866 if (visit == PreVisit)
1867 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001868 TIntermSequence *sequence = node->getSequence();
1869 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001870 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001872 if (variable &&
1873 (variable->getQualifier() == EvqTemporary ||
1874 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001876 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001877
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001878 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001880 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001882 out << "static ";
1883 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001884
Olli Etuahoa6f22092015-05-08 18:31:10 +03001885 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001886
Olli Etuahoa6f22092015-05-08 18:31:10 +03001887 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001888
Olli Etuahoa6f22092015-05-08 18:31:10 +03001889 if (symbol)
1890 {
1891 symbol->traverse(this);
1892 out << ArrayString(symbol->getType());
1893 out << " = " + initializer(symbol->getType());
1894 }
1895 else
1896 {
1897 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001899 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001900 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1901 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001902 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001903 }
1904 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905 }
Jamie Madill033dae62014-06-18 12:56:28 -04001906 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001907 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001908 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001909 {
1910 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1911
1912 if (symbol)
1913 {
1914 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1915 mReferencedVaryings[symbol->getSymbol()] = symbol;
1916 }
1917 else
1918 {
1919 (*sit)->traverse(this);
1920 }
1921 }
1922 }
1923
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924 return false;
1925 }
1926 else if (visit == InVisit)
1927 {
1928 out << ", ";
1929 }
1930 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001931 case EOpInvariantDeclaration:
1932 // Do not do any translation
1933 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001934 case EOpPrototype:
1935 if (visit == PreVisit)
1936 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001937 size_t index = mCallDag.findIndex(node);
1938 // Skip the prototype if it is not implemented (and thus not used)
1939 if (index == CallDAG::InvalidIndex)
1940 {
1941 return false;
1942 }
1943
Olli Etuaho59f9a642015-08-06 20:38:26 +03001944 TString name = DecorateFunctionIfNeeded(node->getNameObj());
1945 out << TypeString(node->getType()) << " " << name
1946 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001947
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001948 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001949
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001950 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001951 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001952 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001953
1954 if (symbol)
1955 {
1956 out << argumentString(symbol);
1957
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001958 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001959 {
1960 out << ", ";
1961 }
1962 }
1963 else UNREACHABLE();
1964 }
1965
1966 out << ");\n";
1967
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001968 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001969 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1970 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001971 {
1972 mOutputLod0Function = true;
1973 node->traverse(this);
1974 mOutputLod0Function = false;
1975 }
1976
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001977 return false;
1978 }
1979 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001980 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981 case EOpFunction:
1982 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001983 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001984 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001985
Corentin Wallez1239ee92015-03-19 14:38:02 -07001986 size_t index = mCallDag.findIndex(node);
1987 ASSERT(index != CallDAG::InvalidIndex);
1988 mCurrentFunctionMetadata = &mASTMetadataList[index];
1989
Jamie Madill033dae62014-06-18 12:56:28 -04001990 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001991
1992 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001994 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001995 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001996 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001998 out << DecorateFunctionIfNeeded(node->getNameObj())
1999 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002000 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002001
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002002 TIntermSequence *sequence = node->getSequence();
2003 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002004
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002005 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002006 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002007 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002008
2009 if (symbol)
2010 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002011 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002012
2013 out << argumentString(symbol);
2014
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002015 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002016 {
2017 out << ", ";
2018 }
2019 }
2020 else UNREACHABLE();
2021 }
2022
Olli Etuaho4785fec2015-05-18 16:09:37 +03002023 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002024
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002025 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002026 {
2027 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002028 TIntermNode *body = (*sequence)[1];
2029 // The function body node will output braces.
2030 ASSERT(IsSequence(body));
2031 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002032 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002034 else
2035 {
2036 out << "{}\n";
2037 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002038
Corentin Wallez1239ee92015-03-19 14:38:02 -07002039 mCurrentFunctionMetadata = nullptr;
2040
2041 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2042 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002043 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002044 ASSERT(name != "main");
2045 mOutputLod0Function = true;
2046 node->traverse(this);
2047 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002048 }
2049
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002050 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051 }
2052 break;
2053 case EOpFunctionCall:
2054 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002055 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002056
Corentin Wallez1239ee92015-03-19 14:38:02 -07002057 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002058 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002060 if (node->isArray())
2061 {
2062 UNIMPLEMENTED();
2063 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002064 size_t index = mCallDag.findIndex(node);
2065 ASSERT(index != CallDAG::InvalidIndex);
2066 lod0 &= mASTMetadataList[index].mNeedsLod0;
2067
Olli Etuaho59f9a642015-08-06 20:38:26 +03002068 out << DecorateFunctionIfNeeded(node->getNameObj()) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069 }
2070 else
2071 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002072 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002073 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002074
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002075 TextureFunction textureFunction;
2076 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002077 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002078 textureFunction.method = TextureFunction::IMPLICIT;
2079 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002080 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002081
2082 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002083 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002084 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002085 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002086 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002087 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002088 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002089 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002090 }
Nicolas Capens46485082014-04-15 13:12:50 -04002091 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2092 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002093 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002094 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002095 }
Nicolas Capens46485082014-04-15 13:12:50 -04002096 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002097 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002098 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002099 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002100 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002101 else if (name == "textureSize")
2102 {
2103 textureFunction.method = TextureFunction::SIZE;
2104 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002105 else if (name == "textureOffset")
2106 {
2107 textureFunction.method = TextureFunction::IMPLICIT;
2108 textureFunction.offset = true;
2109 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002110 else if (name == "textureProjOffset")
2111 {
2112 textureFunction.method = TextureFunction::IMPLICIT;
2113 textureFunction.offset = true;
2114 textureFunction.proj = true;
2115 }
2116 else if (name == "textureLodOffset")
2117 {
2118 textureFunction.method = TextureFunction::LOD;
2119 textureFunction.offset = true;
2120 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002121 else if (name == "textureProjLodOffset")
2122 {
2123 textureFunction.method = TextureFunction::LOD;
2124 textureFunction.proj = true;
2125 textureFunction.offset = true;
2126 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002127 else if (name == "texelFetch")
2128 {
2129 textureFunction.method = TextureFunction::FETCH;
2130 }
2131 else if (name == "texelFetchOffset")
2132 {
2133 textureFunction.method = TextureFunction::FETCH;
2134 textureFunction.offset = true;
2135 }
Nicolas Capens46485082014-04-15 13:12:50 -04002136 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002137 {
2138 textureFunction.method = TextureFunction::GRAD;
2139 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002140 else if (name == "textureGradOffset")
2141 {
2142 textureFunction.method = TextureFunction::GRAD;
2143 textureFunction.offset = true;
2144 }
Nicolas Capens46485082014-04-15 13:12:50 -04002145 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002146 {
2147 textureFunction.method = TextureFunction::GRAD;
2148 textureFunction.proj = true;
2149 }
2150 else if (name == "textureProjGradOffset")
2151 {
2152 textureFunction.method = TextureFunction::GRAD;
2153 textureFunction.proj = true;
2154 textureFunction.offset = true;
2155 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002156 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002157
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002158 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002159 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002160 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2161
2162 if (textureFunction.offset)
2163 {
2164 mandatoryArgumentCount++;
2165 }
2166
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002167 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002168
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002169 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002170 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002171 if (bias)
2172 {
2173 textureFunction.method = TextureFunction::LOD0BIAS;
2174 }
2175 else
2176 {
2177 textureFunction.method = TextureFunction::LOD0;
2178 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002179 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002180 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002181 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002182 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002183 }
2184 }
2185
2186 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002187
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002188 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002190
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002191 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002192 {
2193 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2194 {
2195 out << "texture_";
2196 (*arg)->traverse(this);
2197 out << ", sampler_";
2198 }
2199
2200 (*arg)->traverse(this);
2201
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002202 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002203 {
2204 out << ", ";
2205 }
2206 }
2207
2208 out << ")";
2209
2210 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211 }
2212 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002213 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002214 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2215 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2216 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2217 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2218 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2219 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2220 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2221 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2222 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2223 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2224 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2225 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2226 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2227 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2228 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2229 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2230 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002231 case EOpConstructMat2x3: outputConstructor(visit, node->getType(), "mat2x3", node->getSequence()); break;
2232 case EOpConstructMat2x4: outputConstructor(visit, node->getType(), "mat2x4", node->getSequence()); break;
2233 case EOpConstructMat3x2: outputConstructor(visit, node->getType(), "mat3x2", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002234 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002235 case EOpConstructMat3x4: outputConstructor(visit, node->getType(), "mat3x4", node->getSequence()); break;
2236 case EOpConstructMat4x2: outputConstructor(visit, node->getType(), "mat4x2", node->getSequence()); break;
2237 case EOpConstructMat4x3: outputConstructor(visit, node->getType(), "mat4x3", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002238 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002239 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002240 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002241 if (node->getType().isArray())
2242 {
2243 UNIMPLEMENTED();
2244 }
Jamie Madill033dae62014-06-18 12:56:28 -04002245 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002246 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002247 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002248 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002249 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002250 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2251 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2252 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2253 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2254 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2255 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002256 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002257 ASSERT(node->getUseEmulatedFunction());
2258 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002259 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002260 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002261 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002263 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002264 ASSERT(node->getUseEmulatedFunction());
2265 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002266 break;
2267 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2268 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2269 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
Arun Patoled94f6642015-05-18 16:25:12 +05302270 case EOpMix:
2271 {
2272 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2273 if (lastParamNode->getType().getBasicType() == EbtBool)
2274 {
2275 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2276 // so use emulated version.
2277 ASSERT(node->getUseEmulatedFunction());
2278 writeEmulatedFunctionTriplet(visit, "mix(");
2279 }
2280 else
2281 {
2282 outputTriplet(visit, "lerp(", ", ", ")");
2283 }
2284 }
2285 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2287 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2288 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2289 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2290 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002291 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002292 ASSERT(node->getUseEmulatedFunction());
2293 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002294 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2296 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002297 case EOpOuterProduct:
2298 ASSERT(node->getUseEmulatedFunction());
2299 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2300 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302 default: UNREACHABLE();
2303 }
2304
2305 return true;
2306}
2307
Olli Etuahod81ed842015-05-12 12:46:35 +03002308void OutputHLSL::writeSelection(TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002309{
Jamie Madill32aab012015-01-27 14:12:26 -05002310 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311
Olli Etuahoa6f22092015-05-08 18:31:10 +03002312 out << "if (";
2313
2314 node->getCondition()->traverse(this);
2315
2316 out << ")\n";
2317
2318 outputLineDirective(node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002319
2320 bool discard = false;
2321
2322 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002324 // The trueBlock child node will output braces.
2325 ASSERT(IsSequence(node->getTrueBlock()));
2326
Olli Etuahoa6f22092015-05-08 18:31:10 +03002327 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002328
Olli Etuahoa6f22092015-05-08 18:31:10 +03002329 // Detect true discard
2330 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2331 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002332 else
2333 {
2334 // TODO(oetuaho): Check if the semicolon inside is necessary.
2335 // It's there as a result of conservative refactoring of the output.
2336 out << "{;}\n";
2337 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002338
Olli Etuahoa6f22092015-05-08 18:31:10 +03002339 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002340
Olli Etuahoa6f22092015-05-08 18:31:10 +03002341 if (node->getFalseBlock())
2342 {
2343 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002344
Olli Etuahoa6f22092015-05-08 18:31:10 +03002345 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002346
Olli Etuaho4785fec2015-05-18 16:09:37 +03002347 // Either this is "else if" or the falseBlock child node will output braces.
2348 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2349
Olli Etuahoa6f22092015-05-08 18:31:10 +03002350 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002351
Olli Etuahoa6f22092015-05-08 18:31:10 +03002352 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002353
Olli Etuahoa6f22092015-05-08 18:31:10 +03002354 // Detect false discard
2355 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2356 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002357
Olli Etuahoa6f22092015-05-08 18:31:10 +03002358 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002359 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002360 {
2361 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002362 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002363}
2364
2365bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2366{
2367 TInfoSinkBase &out = getInfoSink();
2368
2369 ASSERT(!node->usesTernaryOperator());
2370
2371 if (!mInsideFunction)
2372 {
2373 // This is part of unfolded global initialization.
2374 mDeferredGlobalInitializers.push_back(node);
2375 return false;
2376 }
2377
2378 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002379 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002380 {
2381 out << "FLATTEN ";
2382 }
2383
2384 writeSelection(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385
2386 return false;
2387}
2388
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002389bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002390{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002391 if (node->getStatementList())
2392 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002393 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002394 outputTriplet(visit, "switch (", ") ", "");
2395 // The curly braces get written when visiting the statementList aggregate
2396 }
2397 else
2398 {
2399 // No statementList, so it won't output curly braces
2400 outputTriplet(visit, "switch (", ") {", "}\n");
2401 }
2402 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002403}
2404
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002405bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002406{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002407 if (node->hasCondition())
2408 {
2409 outputTriplet(visit, "case (", "", "):\n");
2410 return true;
2411 }
2412 else
2413 {
2414 TInfoSinkBase &out = getInfoSink();
2415 out << "default:\n";
2416 return false;
2417 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002418}
2419
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2421{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002422 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423}
2424
2425bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2426{
Nicolas Capens655fe362014-04-11 13:12:34 -04002427 mNestedLoopDepth++;
2428
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002429 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002430 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002431 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002432
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002433 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002434 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002435 if (handleExcessiveLoop(node))
2436 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002437 mInsideDiscontinuousLoop = wasDiscontinuous;
2438 mNestedLoopDepth--;
2439
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002440 return false;
2441 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002442 }
2443
Jamie Madill32aab012015-01-27 14:12:26 -05002444 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445
Corentin Wallez1239ee92015-03-19 14:38:02 -07002446 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002447 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002449 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002450
Jamie Madill075edd82013-07-08 13:30:19 -04002451 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 }
2453 else
2454 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002455 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002456
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002457 if (node->getInit())
2458 {
2459 node->getInit()->traverse(this);
2460 }
2461
2462 out << "; ";
2463
alokp@chromium.org52813552010-11-16 18:36:09 +00002464 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002466 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 }
2468
2469 out << "; ";
2470
alokp@chromium.org52813552010-11-16 18:36:09 +00002471 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002472 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002473 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474 }
2475
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002476 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002477
Jamie Madill075edd82013-07-08 13:30:19 -04002478 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479 }
2480
2481 if (node->getBody())
2482 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002483 // The loop body node will output braces.
2484 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002485 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002486 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002487 else
2488 {
2489 // TODO(oetuaho): Check if the semicolon inside is necessary.
2490 // It's there as a result of conservative refactoring of the output.
2491 out << "{;}\n";
2492 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493
Jamie Madill075edd82013-07-08 13:30:19 -04002494 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002495
alokp@chromium.org52813552010-11-16 18:36:09 +00002496 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 {
Jamie Madill075edd82013-07-08 13:30:19 -04002498 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002499 out << "while(\n";
2500
alokp@chromium.org52813552010-11-16 18:36:09 +00002501 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002502
daniel@transgaming.com73536982012-03-21 20:45:49 +00002503 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504 }
2505
daniel@transgaming.com73536982012-03-21 20:45:49 +00002506 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002508 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002509 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002510
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002511 return false;
2512}
2513
2514bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2515{
Jamie Madill32aab012015-01-27 14:12:26 -05002516 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002517
2518 switch (node->getFlowOp())
2519 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002520 case EOpKill:
2521 outputTriplet(visit, "discard;\n", "", "");
2522 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002523 case EOpBreak:
2524 if (visit == PreVisit)
2525 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002526 if (mNestedLoopDepth > 1)
2527 {
2528 mUsesNestedBreak = true;
2529 }
2530
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002531 if (mExcessiveLoopIndex)
2532 {
2533 out << "{Break";
2534 mExcessiveLoopIndex->traverse(this);
2535 out << " = true; break;}\n";
2536 }
2537 else
2538 {
2539 out << "break;\n";
2540 }
2541 }
2542 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002543 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 case EOpReturn:
2545 if (visit == PreVisit)
2546 {
2547 if (node->getExpression())
2548 {
2549 out << "return ";
2550 }
2551 else
2552 {
2553 out << "return;\n";
2554 }
2555 }
2556 else if (visit == PostVisit)
2557 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002558 if (node->getExpression())
2559 {
2560 out << ";\n";
2561 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 }
2563 break;
2564 default: UNREACHABLE();
2565 }
2566
2567 return true;
2568}
2569
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002570bool OutputHLSL::isSingleStatement(TIntermNode *node)
2571{
2572 TIntermAggregate *aggregate = node->getAsAggregate();
2573
2574 if (aggregate)
2575 {
2576 if (aggregate->getOp() == EOpSequence)
2577 {
2578 return false;
2579 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002580 else if (aggregate->getOp() == EOpDeclaration)
2581 {
2582 // Declaring multiple comma-separated variables must be considered multiple statements
2583 // because each individual declaration has side effects which are visible in the next.
2584 return false;
2585 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002586 else
2587 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002588 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002589 {
2590 if (!isSingleStatement(*sit))
2591 {
2592 return false;
2593 }
2594 }
2595
2596 return true;
2597 }
2598 }
2599
2600 return true;
2601}
2602
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002603// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2604// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002605bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2606{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002607 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002608 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002609
2610 // Parse loops of the form:
2611 // for(int index = initial; index [comparator] limit; index += increment)
2612 TIntermSymbol *index = NULL;
2613 TOperator comparator = EOpNull;
2614 int initial = 0;
2615 int limit = 0;
2616 int increment = 0;
2617
2618 // Parse index name and intial value
2619 if (node->getInit())
2620 {
2621 TIntermAggregate *init = node->getInit()->getAsAggregate();
2622
2623 if (init)
2624 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002625 TIntermSequence *sequence = init->getSequence();
2626 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002627
2628 if (variable && variable->getQualifier() == EvqTemporary)
2629 {
2630 TIntermBinary *assign = variable->getAsBinaryNode();
2631
2632 if (assign->getOp() == EOpInitialize)
2633 {
2634 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2635 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2636
2637 if (symbol && constant)
2638 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002639 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002640 {
2641 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002642 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002643 }
2644 }
2645 }
2646 }
2647 }
2648 }
2649
2650 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002651 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002652 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002653 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002654
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002655 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2656 {
2657 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2658
2659 if (constant)
2660 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002661 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002662 {
2663 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002664 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002665 }
2666 }
2667 }
2668 }
2669
2670 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002671 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002672 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002673 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2674 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002675
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002676 if (binaryTerminal)
2677 {
2678 TOperator op = binaryTerminal->getOp();
2679 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2680
2681 if (constant)
2682 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002683 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002684 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002685 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002686
2687 switch (op)
2688 {
2689 case EOpAddAssign: increment = value; break;
2690 case EOpSubAssign: increment = -value; break;
2691 default: UNIMPLEMENTED();
2692 }
2693 }
2694 }
2695 }
2696 else if (unaryTerminal)
2697 {
2698 TOperator op = unaryTerminal->getOp();
2699
2700 switch (op)
2701 {
2702 case EOpPostIncrement: increment = 1; break;
2703 case EOpPostDecrement: increment = -1; break;
2704 case EOpPreIncrement: increment = 1; break;
2705 case EOpPreDecrement: increment = -1; break;
2706 default: UNIMPLEMENTED();
2707 }
2708 }
2709 }
2710
2711 if (index != NULL && comparator != EOpNull && increment != 0)
2712 {
2713 if (comparator == EOpLessThanEqual)
2714 {
2715 comparator = EOpLessThan;
2716 limit += 1;
2717 }
2718
2719 if (comparator == EOpLessThan)
2720 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002721 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002722
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002723 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002724 {
2725 return false; // Not an excessive loop
2726 }
2727
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002728 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2729 mExcessiveLoopIndex = index;
2730
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002731 out << "{int ";
2732 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002733 out << ";\n"
2734 "bool Break";
2735 index->traverse(this);
2736 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002737
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002738 bool firstLoopFragment = true;
2739
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002740 while (iterations > 0)
2741 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002742 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002743
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002744 if (!firstLoopFragment)
2745 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002746 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002747 index->traverse(this);
2748 out << ") {\n";
2749 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002750
2751 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2752 {
2753 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2754 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002755
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002756 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002757 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002758
Corentin Wallez1239ee92015-03-19 14:38:02 -07002759 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002760 index->traverse(this);
2761 out << " = ";
2762 out << initial;
2763
2764 out << "; ";
2765 index->traverse(this);
2766 out << " < ";
2767 out << clampedLimit;
2768
2769 out << "; ";
2770 index->traverse(this);
2771 out << " += ";
2772 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002773 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002774
Jamie Madill075edd82013-07-08 13:30:19 -04002775 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002776 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002777
2778 if (node->getBody())
2779 {
2780 node->getBody()->traverse(this);
2781 }
2782
Jamie Madill075edd82013-07-08 13:30:19 -04002783 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002784 out << ";}\n";
2785
2786 if (!firstLoopFragment)
2787 {
2788 out << "}\n";
2789 }
2790
2791 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002792
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002793 initial += MAX_LOOP_ITERATIONS * increment;
2794 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002795 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002796
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002797 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002798
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002799 mExcessiveLoopIndex = restoreIndex;
2800
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002801 return true;
2802 }
2803 else UNIMPLEMENTED();
2804 }
2805
2806 return false; // Not handled as an excessive loop
2807}
2808
Olli Etuaho7fb49552015-03-18 17:27:44 +02002809void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002811 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002812 {
2813 out << preString;
2814 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002815 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816 {
2817 out << inString;
2818 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002819 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002820 {
2821 out << postString;
2822 }
2823}
2824
Olli Etuaho7fb49552015-03-18 17:27:44 +02002825void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2826{
2827 outputTriplet(visit, preString, inString, postString, getInfoSink());
2828}
2829
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002830void OutputHLSL::outputLineDirective(int line)
2831{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002832 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002833 {
Jamie Madill32aab012015-01-27 14:12:26 -05002834 TInfoSinkBase &out = getInfoSink();
2835
2836 out << "\n";
2837 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002838
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002839 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002840 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002841 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002842 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002843
Jamie Madill32aab012015-01-27 14:12:26 -05002844 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002845 }
2846}
2847
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002848TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2849{
2850 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002851 const TType &type = symbol->getType();
2852 const TName &name = symbol->getName();
2853 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002854
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002855 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002856 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002857 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002858 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002859 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002860 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002861 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002862 }
2863
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002864 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2865 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002866 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + nameStr +
2867 ArrayString(type) + ", " + QualifierString(qualifier) + " " + SamplerString(type) +
2868 " sampler_" + nameStr + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002869 }
2870
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002871 return QualifierString(qualifier) + " " + TypeString(type) + " " + nameStr + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002872}
2873
2874TString OutputHLSL::initializer(const TType &type)
2875{
2876 TString string;
2877
Jamie Madill94bf7f22013-07-08 13:31:15 -04002878 size_t size = type.getObjectSize();
2879 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002880 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002881 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002882
Jamie Madill94bf7f22013-07-08 13:31:15 -04002883 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002884 {
2885 string += ", ";
2886 }
2887 }
2888
daniel@transgaming.comead23042010-04-29 03:35:36 +00002889 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002890}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002891
Daniel Bratell29190082015-02-20 16:42:54 +01002892void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002893{
Olli Etuahof40319e2015-03-10 14:33:00 +02002894 if (type.isArray())
2895 {
2896 UNIMPLEMENTED();
2897 }
Jamie Madill32aab012015-01-27 14:12:26 -05002898 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002899
2900 if (visit == PreVisit)
2901 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002902 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002903
Daniel Bratell29190082015-02-20 16:42:54 +01002904 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002905 }
2906 else if (visit == InVisit)
2907 {
2908 out << ", ";
2909 }
2910 else if (visit == PostVisit)
2911 {
2912 out << ")";
2913 }
2914}
2915
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002916const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const TConstantUnion *constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002917{
Jamie Madill32aab012015-01-27 14:12:26 -05002918 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002919
Jamie Madill98493dd2013-07-08 14:39:03 -04002920 const TStructure* structure = type.getStruct();
2921 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002922 {
Jamie Madill033dae62014-06-18 12:56:28 -04002923 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002924
Jamie Madill98493dd2013-07-08 14:39:03 -04002925 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002926
Jamie Madill98493dd2013-07-08 14:39:03 -04002927 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002928 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002929 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002930 constUnion = writeConstantUnion(*fieldType, constUnion);
2931
Jamie Madill98493dd2013-07-08 14:39:03 -04002932 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002933 {
2934 out << ", ";
2935 }
2936 }
2937
2938 out << ")";
2939 }
2940 else
2941 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002942 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002943 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002944
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002945 if (writeType)
2946 {
Jamie Madill033dae62014-06-18 12:56:28 -04002947 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002948 }
2949
Jamie Madill94bf7f22013-07-08 13:31:15 -04002950 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002951 {
2952 switch (constUnion->getType())
2953 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002954 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002955 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002956 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002957 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002958 default: UNREACHABLE();
2959 }
2960
2961 if (i != size - 1)
2962 {
2963 out << ", ";
2964 }
2965 }
2966
2967 if (writeType)
2968 {
2969 out << ")";
2970 }
2971 }
2972
2973 return constUnion;
2974}
2975
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002976void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2977{
2978 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2979 outputTriplet(visit, preString.c_str(), ", ", ")");
2980}
2981
Jamie Madill37997142015-01-28 10:06:34 -05002982bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2983{
2984 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2985 expression->traverse(&searchSymbol);
2986
2987 if (searchSymbol.foundMatch())
2988 {
2989 // Type already printed
2990 out << "t" + str(mUniqueIndex) + " = ";
2991 expression->traverse(this);
2992 out << ", ";
2993 symbolNode->traverse(this);
2994 out << " = t" + str(mUniqueIndex);
2995
2996 mUniqueIndex++;
2997 return true;
2998 }
2999
3000 return false;
3001}
3002
3003void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3004{
3005 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3006 << "\n"
3007 << "void initializeDeferredGlobals()\n"
3008 << "{\n";
3009
3010 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3011 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003012 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3013 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3014 if (binary != nullptr)
3015 {
3016 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3017 TIntermTyped *expression = binary->getRight();
3018 ASSERT(symbol);
3019 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003020
Olli Etuahod81ed842015-05-12 12:46:35 +03003021 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003022
Olli Etuahod81ed842015-05-12 12:46:35 +03003023 if (!writeSameSymbolInitializer(out, symbol, expression))
3024 {
3025 ASSERT(mInfoSinkStack.top() == &out);
3026 expression->traverse(this);
3027 }
3028 out << ";\n";
3029 }
3030 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003031 {
3032 ASSERT(mInfoSinkStack.top() == &out);
Olli Etuahod81ed842015-05-12 12:46:35 +03003033 writeSelection(selection);
Jamie Madill37997142015-01-28 10:06:34 -05003034 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003035 else
3036 {
3037 UNREACHABLE();
3038 }
Jamie Madill37997142015-01-28 10:06:34 -05003039 }
3040
3041 out << "}\n"
3042 << "\n";
3043}
3044
Jamie Madill55e79e02015-02-09 15:35:00 -05003045TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3046{
3047 const TFieldList &fields = structure.fields();
3048
3049 for (const auto &eqFunction : mStructEqualityFunctions)
3050 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003051 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003052 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003053 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003054 }
3055 }
3056
3057 const TString &structNameString = StructNameString(structure);
3058
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003059 StructEqualityFunction *function = new StructEqualityFunction();
3060 function->structure = &structure;
3061 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003062
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003063 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003064
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003065 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3066 << "{\n"
3067 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003068
3069 for (size_t i = 0; i < fields.size(); i++)
3070 {
3071 const TField *field = fields[i];
3072 const TType *fieldType = field->type();
3073
3074 const TString &fieldNameA = "a." + Decorate(field->name());
3075 const TString &fieldNameB = "b." + Decorate(field->name());
3076
3077 if (i > 0)
3078 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003079 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003080 }
3081
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003082 fnOut << "(";
3083 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3084 fnOut << fieldNameA;
3085 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3086 fnOut << fieldNameB;
3087 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3088 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003089 }
3090
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003091 fnOut << ";\n" << "}\n";
3092
3093 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003094
3095 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003096 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003097
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003098 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003099}
3100
Olli Etuaho7fb49552015-03-18 17:27:44 +02003101TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3102{
3103 for (const auto &eqFunction : mArrayEqualityFunctions)
3104 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003105 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003106 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003107 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003108 }
3109 }
3110
3111 const TString &typeName = TypeString(type);
3112
Olli Etuaho12690762015-03-31 12:55:28 +03003113 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003114 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003115
3116 TInfoSinkBase fnNameOut;
3117 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003118 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003119
3120 TType nonArrayType = type;
3121 nonArrayType.clearArrayness();
3122
3123 TInfoSinkBase fnOut;
3124
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003125 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003126 << typeName << " a[" << type.getArraySize() << "], "
3127 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003128 << "{\n"
3129 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3130 " {\n"
3131 " if (";
3132
3133 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3134 fnOut << "a[i]";
3135 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3136 fnOut << "b[i]";
3137 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3138
3139 fnOut << ") { return false; }\n"
3140 " }\n"
3141 " return true;\n"
3142 "}\n";
3143
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003144 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003145
3146 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003147 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003148
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003149 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003150}
3151
Olli Etuaho12690762015-03-31 12:55:28 +03003152TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3153{
3154 for (const auto &assignFunction : mArrayAssignmentFunctions)
3155 {
3156 if (assignFunction.type == type)
3157 {
3158 return assignFunction.functionName;
3159 }
3160 }
3161
3162 const TString &typeName = TypeString(type);
3163
3164 ArrayHelperFunction function;
3165 function.type = type;
3166
3167 TInfoSinkBase fnNameOut;
3168 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3169 function.functionName = fnNameOut.c_str();
3170
3171 TInfoSinkBase fnOut;
3172
3173 fnOut << "void " << function.functionName << "(out "
3174 << typeName << " a[" << type.getArraySize() << "], "
3175 << typeName << " b[" << type.getArraySize() << "])\n"
3176 << "{\n"
3177 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3178 " {\n"
3179 " a[i] = b[i];\n"
3180 " }\n"
3181 "}\n";
3182
3183 function.functionDefinition = fnOut.c_str();
3184
3185 mArrayAssignmentFunctions.push_back(function);
3186
3187 return function.functionName;
3188}
3189
Olli Etuaho9638c352015-04-01 14:34:52 +03003190TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3191{
3192 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3193 {
3194 if (constructIntoFunction.type == type)
3195 {
3196 return constructIntoFunction.functionName;
3197 }
3198 }
3199
3200 const TString &typeName = TypeString(type);
3201
3202 ArrayHelperFunction function;
3203 function.type = type;
3204
3205 TInfoSinkBase fnNameOut;
3206 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3207 function.functionName = fnNameOut.c_str();
3208
3209 TInfoSinkBase fnOut;
3210
3211 fnOut << "void " << function.functionName << "(out "
3212 << typeName << " a[" << type.getArraySize() << "]";
3213 for (int i = 0; i < type.getArraySize(); ++i)
3214 {
3215 fnOut << ", " << typeName << " b" << i;
3216 }
3217 fnOut << ")\n"
3218 "{\n";
3219
3220 for (int i = 0; i < type.getArraySize(); ++i)
3221 {
3222 fnOut << " a[" << i << "] = b" << i << ";\n";
3223 }
3224 fnOut << "}\n";
3225
3226 function.functionDefinition = fnOut.c_str();
3227
3228 mArrayConstructIntoFunctions.push_back(function);
3229
3230 return function.functionName;
3231}
3232
Jamie Madill2e295e22015-04-29 10:41:33 -04003233void OutputHLSL::ensureStructDefined(const TType &type)
3234{
3235 TStructure *structure = type.getStruct();
3236
3237 if (structure)
3238 {
3239 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3240 }
3241}
3242
3243
Olli Etuaho9638c352015-04-01 14:34:52 +03003244
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003245}