blob: b91a4a8f9b531c37860ad1ab55de95df347ee613 [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 }
Geoff Lange2bfe2c2015-07-23 21:25:45 +00001389 else if (name == "gl_FragDepthEXT")
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
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001872 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001874 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001875
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001876 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001878 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001880 out << "static ";
1881 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001882
Olli Etuahoa6f22092015-05-08 18:31:10 +03001883 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001884
Olli Etuahoa6f22092015-05-08 18:31:10 +03001885 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001886
Olli Etuahoa6f22092015-05-08 18:31:10 +03001887 if (symbol)
1888 {
1889 symbol->traverse(this);
1890 out << ArrayString(symbol->getType());
1891 out << " = " + initializer(symbol->getType());
1892 }
1893 else
1894 {
1895 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001898 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1899 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001900 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001901 }
1902 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903 }
Jamie Madill033dae62014-06-18 12:56:28 -04001904 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001905 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001906 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001907 {
1908 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1909
1910 if (symbol)
1911 {
1912 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1913 mReferencedVaryings[symbol->getSymbol()] = symbol;
1914 }
1915 else
1916 {
1917 (*sit)->traverse(this);
1918 }
1919 }
1920 }
1921
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922 return false;
1923 }
1924 else if (visit == InVisit)
1925 {
1926 out << ", ";
1927 }
1928 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001929 case EOpInvariantDeclaration:
1930 // Do not do any translation
1931 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001932 case EOpPrototype:
1933 if (visit == PreVisit)
1934 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001935 size_t index = mCallDag.findIndex(node);
1936 // Skip the prototype if it is not implemented (and thus not used)
1937 if (index == CallDAG::InvalidIndex)
1938 {
1939 return false;
1940 }
1941
Olli Etuaho76acee82014-11-04 13:44:03 +02001942 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001943
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001944 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001945
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001946 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001947 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001948 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001949
1950 if (symbol)
1951 {
1952 out << argumentString(symbol);
1953
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001954 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001955 {
1956 out << ", ";
1957 }
1958 }
1959 else UNREACHABLE();
1960 }
1961
1962 out << ");\n";
1963
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001964 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001965 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1966 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001967 {
1968 mOutputLod0Function = true;
1969 node->traverse(this);
1970 mOutputLod0Function = false;
1971 }
1972
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001973 return false;
1974 }
1975 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001976 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001977 case EOpFunction:
1978 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001979 ASSERT(mCurrentFunctionMetadata == nullptr);
alokp@chromium.org43884872010-03-30 00:08:52 +00001980 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981
Corentin Wallez1239ee92015-03-19 14:38:02 -07001982 size_t index = mCallDag.findIndex(node);
1983 ASSERT(index != CallDAG::InvalidIndex);
1984 mCurrentFunctionMetadata = &mASTMetadataList[index];
1985
Jamie Madill033dae62014-06-18 12:56:28 -04001986 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001987
1988 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001990 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001992 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 {
Jamie Madill033dae62014-06-18 12:56:28 -04001994 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001995 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001996
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001997 TIntermSequence *sequence = node->getSequence();
1998 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001999
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002000 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002001 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002002 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002003
2004 if (symbol)
2005 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002006 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002007
2008 out << argumentString(symbol);
2009
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002010 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002011 {
2012 out << ", ";
2013 }
2014 }
2015 else UNREACHABLE();
2016 }
2017
Olli Etuaho4785fec2015-05-18 16:09:37 +03002018 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002019
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002020 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002021 {
2022 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002023 TIntermNode *body = (*sequence)[1];
2024 // The function body node will output braces.
2025 ASSERT(IsSequence(body));
2026 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002027 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002029 else
2030 {
2031 out << "{}\n";
2032 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002033
Corentin Wallez1239ee92015-03-19 14:38:02 -07002034 mCurrentFunctionMetadata = nullptr;
2035
2036 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2037 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002038 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002039 ASSERT(name != "main");
2040 mOutputLod0Function = true;
2041 node->traverse(this);
2042 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002043 }
2044
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002045 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046 }
2047 break;
2048 case EOpFunctionCall:
2049 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002050 TString name = TFunction::unmangleName(node->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002051 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002052
Corentin Wallez1239ee92015-03-19 14:38:02 -07002053 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002054 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002056 if (node->isArray())
2057 {
2058 UNIMPLEMENTED();
2059 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002060 size_t index = mCallDag.findIndex(node);
2061 ASSERT(index != CallDAG::InvalidIndex);
2062 lod0 &= mASTMetadataList[index].mNeedsLod0;
2063
Jamie Madill033dae62014-06-18 12:56:28 -04002064 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002065 }
2066 else
2067 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002068 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002069
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002070 TextureFunction textureFunction;
2071 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002072 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002073 textureFunction.method = TextureFunction::IMPLICIT;
2074 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002075 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002076
2077 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002078 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002079 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002080 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002081 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002082 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002083 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002084 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002085 }
Nicolas Capens46485082014-04-15 13:12:50 -04002086 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2087 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002088 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002089 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002090 }
Nicolas Capens46485082014-04-15 13:12:50 -04002091 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002092 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002093 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002094 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002095 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002096 else if (name == "textureSize")
2097 {
2098 textureFunction.method = TextureFunction::SIZE;
2099 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002100 else if (name == "textureOffset")
2101 {
2102 textureFunction.method = TextureFunction::IMPLICIT;
2103 textureFunction.offset = true;
2104 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002105 else if (name == "textureProjOffset")
2106 {
2107 textureFunction.method = TextureFunction::IMPLICIT;
2108 textureFunction.offset = true;
2109 textureFunction.proj = true;
2110 }
2111 else if (name == "textureLodOffset")
2112 {
2113 textureFunction.method = TextureFunction::LOD;
2114 textureFunction.offset = true;
2115 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002116 else if (name == "textureProjLodOffset")
2117 {
2118 textureFunction.method = TextureFunction::LOD;
2119 textureFunction.proj = true;
2120 textureFunction.offset = true;
2121 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002122 else if (name == "texelFetch")
2123 {
2124 textureFunction.method = TextureFunction::FETCH;
2125 }
2126 else if (name == "texelFetchOffset")
2127 {
2128 textureFunction.method = TextureFunction::FETCH;
2129 textureFunction.offset = true;
2130 }
Nicolas Capens46485082014-04-15 13:12:50 -04002131 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002132 {
2133 textureFunction.method = TextureFunction::GRAD;
2134 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002135 else if (name == "textureGradOffset")
2136 {
2137 textureFunction.method = TextureFunction::GRAD;
2138 textureFunction.offset = true;
2139 }
Nicolas Capens46485082014-04-15 13:12:50 -04002140 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002141 {
2142 textureFunction.method = TextureFunction::GRAD;
2143 textureFunction.proj = true;
2144 }
2145 else if (name == "textureProjGradOffset")
2146 {
2147 textureFunction.method = TextureFunction::GRAD;
2148 textureFunction.proj = true;
2149 textureFunction.offset = true;
2150 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002151 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002152
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002153 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002154 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002155 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2156
2157 if (textureFunction.offset)
2158 {
2159 mandatoryArgumentCount++;
2160 }
2161
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002162 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002163
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002164 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002165 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002166 if (bias)
2167 {
2168 textureFunction.method = TextureFunction::LOD0BIAS;
2169 }
2170 else
2171 {
2172 textureFunction.method = TextureFunction::LOD0;
2173 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002174 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002175 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002176 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002177 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002178 }
2179 }
2180
2181 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002182
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002183 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002184 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002185
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002186 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002187 {
2188 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2189 {
2190 out << "texture_";
2191 (*arg)->traverse(this);
2192 out << ", sampler_";
2193 }
2194
2195 (*arg)->traverse(this);
2196
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002197 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002198 {
2199 out << ", ";
2200 }
2201 }
2202
2203 out << ")";
2204
2205 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206 }
2207 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002208 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002209 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2210 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2211 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2212 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2213 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2214 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2215 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2216 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2217 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2218 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2219 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2220 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2221 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2222 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2223 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2224 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2225 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002226 case EOpConstructMat2x3: outputConstructor(visit, node->getType(), "mat2x3", node->getSequence()); break;
2227 case EOpConstructMat2x4: outputConstructor(visit, node->getType(), "mat2x4", node->getSequence()); break;
2228 case EOpConstructMat3x2: outputConstructor(visit, node->getType(), "mat3x2", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002229 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002230 case EOpConstructMat3x4: outputConstructor(visit, node->getType(), "mat3x4", node->getSequence()); break;
2231 case EOpConstructMat4x2: outputConstructor(visit, node->getType(), "mat4x2", node->getSequence()); break;
2232 case EOpConstructMat4x3: outputConstructor(visit, node->getType(), "mat4x3", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002233 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002234 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002235 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002236 if (node->getType().isArray())
2237 {
2238 UNIMPLEMENTED();
2239 }
Jamie Madill033dae62014-06-18 12:56:28 -04002240 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002241 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002242 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002243 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002244 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002245 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2246 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2247 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2248 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2249 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2250 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002251 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002252 ASSERT(node->getUseEmulatedFunction());
2253 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002254 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002255 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002256 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002258 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002259 ASSERT(node->getUseEmulatedFunction());
2260 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002261 break;
2262 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2263 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2264 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
Arun Patoled94f6642015-05-18 16:25:12 +05302265 case EOpMix:
2266 {
2267 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2268 if (lastParamNode->getType().getBasicType() == EbtBool)
2269 {
2270 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2271 // so use emulated version.
2272 ASSERT(node->getUseEmulatedFunction());
2273 writeEmulatedFunctionTriplet(visit, "mix(");
2274 }
2275 else
2276 {
2277 outputTriplet(visit, "lerp(", ", ", ")");
2278 }
2279 }
2280 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2282 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2283 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2284 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2285 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002286 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002287 ASSERT(node->getUseEmulatedFunction());
2288 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002289 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2291 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002292 case EOpOuterProduct:
2293 ASSERT(node->getUseEmulatedFunction());
2294 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2295 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297 default: UNREACHABLE();
2298 }
2299
2300 return true;
2301}
2302
Olli Etuahod81ed842015-05-12 12:46:35 +03002303void OutputHLSL::writeSelection(TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304{
Jamie Madill32aab012015-01-27 14:12:26 -05002305 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306
Olli Etuahoa6f22092015-05-08 18:31:10 +03002307 out << "if (";
2308
2309 node->getCondition()->traverse(this);
2310
2311 out << ")\n";
2312
2313 outputLineDirective(node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002314
2315 bool discard = false;
2316
2317 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002319 // The trueBlock child node will output braces.
2320 ASSERT(IsSequence(node->getTrueBlock()));
2321
Olli Etuahoa6f22092015-05-08 18:31:10 +03002322 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002323
Olli Etuahoa6f22092015-05-08 18:31:10 +03002324 // Detect true discard
2325 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2326 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002327 else
2328 {
2329 // TODO(oetuaho): Check if the semicolon inside is necessary.
2330 // It's there as a result of conservative refactoring of the output.
2331 out << "{;}\n";
2332 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002333
Olli Etuahoa6f22092015-05-08 18:31:10 +03002334 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002335
Olli Etuahoa6f22092015-05-08 18:31:10 +03002336 if (node->getFalseBlock())
2337 {
2338 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002339
Olli Etuahoa6f22092015-05-08 18:31:10 +03002340 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341
Olli Etuaho4785fec2015-05-18 16:09:37 +03002342 // Either this is "else if" or the falseBlock child node will output braces.
2343 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2344
Olli Etuahoa6f22092015-05-08 18:31:10 +03002345 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002346
Olli Etuahoa6f22092015-05-08 18:31:10 +03002347 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002348
Olli Etuahoa6f22092015-05-08 18:31:10 +03002349 // Detect false discard
2350 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2351 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002352
Olli Etuahoa6f22092015-05-08 18:31:10 +03002353 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002354 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002355 {
2356 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002357 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002358}
2359
2360bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2361{
2362 TInfoSinkBase &out = getInfoSink();
2363
2364 ASSERT(!node->usesTernaryOperator());
2365
2366 if (!mInsideFunction)
2367 {
2368 // This is part of unfolded global initialization.
2369 mDeferredGlobalInitializers.push_back(node);
2370 return false;
2371 }
2372
2373 // D3D errors when there is a gradient operation in a loop in an unflattened if.
2374 if (mShaderType == GL_FRAGMENT_SHADER &&
2375 mCurrentFunctionMetadata->hasDiscontinuousLoop(node) &&
2376 mCurrentFunctionMetadata->hasGradientInCallGraph(node))
2377 {
2378 out << "FLATTEN ";
2379 }
2380
2381 writeSelection(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002382
2383 return false;
2384}
2385
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002386bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002387{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002388 if (node->getStatementList())
2389 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002390 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002391 outputTriplet(visit, "switch (", ") ", "");
2392 // The curly braces get written when visiting the statementList aggregate
2393 }
2394 else
2395 {
2396 // No statementList, so it won't output curly braces
2397 outputTriplet(visit, "switch (", ") {", "}\n");
2398 }
2399 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002400}
2401
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002402bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002403{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002404 if (node->hasCondition())
2405 {
2406 outputTriplet(visit, "case (", "", "):\n");
2407 return true;
2408 }
2409 else
2410 {
2411 TInfoSinkBase &out = getInfoSink();
2412 out << "default:\n";
2413 return false;
2414 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002415}
2416
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2418{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002419 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420}
2421
2422bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2423{
Nicolas Capens655fe362014-04-11 13:12:34 -04002424 mNestedLoopDepth++;
2425
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002426 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002427 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002428 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002429
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002430 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002431 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002432 if (handleExcessiveLoop(node))
2433 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002434 mInsideDiscontinuousLoop = wasDiscontinuous;
2435 mNestedLoopDepth--;
2436
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002437 return false;
2438 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002439 }
2440
Jamie Madill32aab012015-01-27 14:12:26 -05002441 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442
Corentin Wallez1239ee92015-03-19 14:38:02 -07002443 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002444 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002446 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002447
Jamie Madill075edd82013-07-08 13:30:19 -04002448 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 }
2450 else
2451 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002452 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002453
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 if (node->getInit())
2455 {
2456 node->getInit()->traverse(this);
2457 }
2458
2459 out << "; ";
2460
alokp@chromium.org52813552010-11-16 18:36:09 +00002461 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002463 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002464 }
2465
2466 out << "; ";
2467
alokp@chromium.org52813552010-11-16 18:36:09 +00002468 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002470 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471 }
2472
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002473 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002474
Jamie Madill075edd82013-07-08 13:30:19 -04002475 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 }
2477
2478 if (node->getBody())
2479 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002480 // The loop body node will output braces.
2481 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002482 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002484 else
2485 {
2486 // TODO(oetuaho): Check if the semicolon inside is necessary.
2487 // It's there as a result of conservative refactoring of the output.
2488 out << "{;}\n";
2489 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490
Jamie Madill075edd82013-07-08 13:30:19 -04002491 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492
alokp@chromium.org52813552010-11-16 18:36:09 +00002493 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002494 {
Jamie Madill075edd82013-07-08 13:30:19 -04002495 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496 out << "while(\n";
2497
alokp@chromium.org52813552010-11-16 18:36:09 +00002498 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002499
daniel@transgaming.com73536982012-03-21 20:45:49 +00002500 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501 }
2502
daniel@transgaming.com73536982012-03-21 20:45:49 +00002503 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002505 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002506 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002507
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508 return false;
2509}
2510
2511bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2512{
Jamie Madill32aab012015-01-27 14:12:26 -05002513 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514
2515 switch (node->getFlowOp())
2516 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002517 case EOpKill:
2518 outputTriplet(visit, "discard;\n", "", "");
2519 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002520 case EOpBreak:
2521 if (visit == PreVisit)
2522 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002523 if (mNestedLoopDepth > 1)
2524 {
2525 mUsesNestedBreak = true;
2526 }
2527
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002528 if (mExcessiveLoopIndex)
2529 {
2530 out << "{Break";
2531 mExcessiveLoopIndex->traverse(this);
2532 out << " = true; break;}\n";
2533 }
2534 else
2535 {
2536 out << "break;\n";
2537 }
2538 }
2539 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002540 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002541 case EOpReturn:
2542 if (visit == PreVisit)
2543 {
2544 if (node->getExpression())
2545 {
2546 out << "return ";
2547 }
2548 else
2549 {
2550 out << "return;\n";
2551 }
2552 }
2553 else if (visit == PostVisit)
2554 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002555 if (node->getExpression())
2556 {
2557 out << ";\n";
2558 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559 }
2560 break;
2561 default: UNREACHABLE();
2562 }
2563
2564 return true;
2565}
2566
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002567bool OutputHLSL::isSingleStatement(TIntermNode *node)
2568{
2569 TIntermAggregate *aggregate = node->getAsAggregate();
2570
2571 if (aggregate)
2572 {
2573 if (aggregate->getOp() == EOpSequence)
2574 {
2575 return false;
2576 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002577 else if (aggregate->getOp() == EOpDeclaration)
2578 {
2579 // Declaring multiple comma-separated variables must be considered multiple statements
2580 // because each individual declaration has side effects which are visible in the next.
2581 return false;
2582 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002583 else
2584 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002585 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002586 {
2587 if (!isSingleStatement(*sit))
2588 {
2589 return false;
2590 }
2591 }
2592
2593 return true;
2594 }
2595 }
2596
2597 return true;
2598}
2599
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002600// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2601// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002602bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2603{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002604 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002605 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002606
2607 // Parse loops of the form:
2608 // for(int index = initial; index [comparator] limit; index += increment)
2609 TIntermSymbol *index = NULL;
2610 TOperator comparator = EOpNull;
2611 int initial = 0;
2612 int limit = 0;
2613 int increment = 0;
2614
2615 // Parse index name and intial value
2616 if (node->getInit())
2617 {
2618 TIntermAggregate *init = node->getInit()->getAsAggregate();
2619
2620 if (init)
2621 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002622 TIntermSequence *sequence = init->getSequence();
2623 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002624
2625 if (variable && variable->getQualifier() == EvqTemporary)
2626 {
2627 TIntermBinary *assign = variable->getAsBinaryNode();
2628
2629 if (assign->getOp() == EOpInitialize)
2630 {
2631 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2632 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2633
2634 if (symbol && constant)
2635 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002636 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002637 {
2638 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002639 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002640 }
2641 }
2642 }
2643 }
2644 }
2645 }
2646
2647 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002648 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002649 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002650 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002651
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002652 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2653 {
2654 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2655
2656 if (constant)
2657 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002658 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002659 {
2660 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002661 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002662 }
2663 }
2664 }
2665 }
2666
2667 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002668 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002669 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002670 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2671 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002672
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002673 if (binaryTerminal)
2674 {
2675 TOperator op = binaryTerminal->getOp();
2676 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2677
2678 if (constant)
2679 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002680 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002681 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002682 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002683
2684 switch (op)
2685 {
2686 case EOpAddAssign: increment = value; break;
2687 case EOpSubAssign: increment = -value; break;
2688 default: UNIMPLEMENTED();
2689 }
2690 }
2691 }
2692 }
2693 else if (unaryTerminal)
2694 {
2695 TOperator op = unaryTerminal->getOp();
2696
2697 switch (op)
2698 {
2699 case EOpPostIncrement: increment = 1; break;
2700 case EOpPostDecrement: increment = -1; break;
2701 case EOpPreIncrement: increment = 1; break;
2702 case EOpPreDecrement: increment = -1; break;
2703 default: UNIMPLEMENTED();
2704 }
2705 }
2706 }
2707
2708 if (index != NULL && comparator != EOpNull && increment != 0)
2709 {
2710 if (comparator == EOpLessThanEqual)
2711 {
2712 comparator = EOpLessThan;
2713 limit += 1;
2714 }
2715
2716 if (comparator == EOpLessThan)
2717 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002718 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002719
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002720 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002721 {
2722 return false; // Not an excessive loop
2723 }
2724
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002725 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2726 mExcessiveLoopIndex = index;
2727
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002728 out << "{int ";
2729 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002730 out << ";\n"
2731 "bool Break";
2732 index->traverse(this);
2733 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002734
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002735 bool firstLoopFragment = true;
2736
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002737 while (iterations > 0)
2738 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002739 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002740
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002741 if (!firstLoopFragment)
2742 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002743 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002744 index->traverse(this);
2745 out << ") {\n";
2746 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002747
2748 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2749 {
2750 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2751 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002752
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002753 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002754 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002755
Corentin Wallez1239ee92015-03-19 14:38:02 -07002756 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002757 index->traverse(this);
2758 out << " = ";
2759 out << initial;
2760
2761 out << "; ";
2762 index->traverse(this);
2763 out << " < ";
2764 out << clampedLimit;
2765
2766 out << "; ";
2767 index->traverse(this);
2768 out << " += ";
2769 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002770 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002771
Jamie Madill075edd82013-07-08 13:30:19 -04002772 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002773 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002774
2775 if (node->getBody())
2776 {
2777 node->getBody()->traverse(this);
2778 }
2779
Jamie Madill075edd82013-07-08 13:30:19 -04002780 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002781 out << ";}\n";
2782
2783 if (!firstLoopFragment)
2784 {
2785 out << "}\n";
2786 }
2787
2788 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002789
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002790 initial += MAX_LOOP_ITERATIONS * increment;
2791 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002792 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002793
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002794 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002795
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002796 mExcessiveLoopIndex = restoreIndex;
2797
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002798 return true;
2799 }
2800 else UNIMPLEMENTED();
2801 }
2802
2803 return false; // Not handled as an excessive loop
2804}
2805
Olli Etuaho7fb49552015-03-18 17:27:44 +02002806void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002808 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002809 {
2810 out << preString;
2811 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002812 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813 {
2814 out << inString;
2815 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002816 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002817 {
2818 out << postString;
2819 }
2820}
2821
Olli Etuaho7fb49552015-03-18 17:27:44 +02002822void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2823{
2824 outputTriplet(visit, preString, inString, postString, getInfoSink());
2825}
2826
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002827void OutputHLSL::outputLineDirective(int line)
2828{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002829 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002830 {
Jamie Madill32aab012015-01-27 14:12:26 -05002831 TInfoSinkBase &out = getInfoSink();
2832
2833 out << "\n";
2834 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002835
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002836 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002837 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002838 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002839 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002840
Jamie Madill32aab012015-01-27 14:12:26 -05002841 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002842 }
2843}
2844
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002845TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2846{
2847 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002848 const TType &type = symbol->getType();
2849 const TName &name = symbol->getName();
2850 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002851
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002852 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002853 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002854 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002855 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002856 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002857 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002858 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002859 }
2860
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002861 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2862 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002863 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + nameStr +
2864 ArrayString(type) + ", " + QualifierString(qualifier) + " " + SamplerString(type) +
2865 " sampler_" + nameStr + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002866 }
2867
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002868 return QualifierString(qualifier) + " " + TypeString(type) + " " + nameStr + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869}
2870
2871TString OutputHLSL::initializer(const TType &type)
2872{
2873 TString string;
2874
Jamie Madill94bf7f22013-07-08 13:31:15 -04002875 size_t size = type.getObjectSize();
2876 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002877 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002878 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002879
Jamie Madill94bf7f22013-07-08 13:31:15 -04002880 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002881 {
2882 string += ", ";
2883 }
2884 }
2885
daniel@transgaming.comead23042010-04-29 03:35:36 +00002886 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002887}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002888
Daniel Bratell29190082015-02-20 16:42:54 +01002889void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002890{
Olli Etuahof40319e2015-03-10 14:33:00 +02002891 if (type.isArray())
2892 {
2893 UNIMPLEMENTED();
2894 }
Jamie Madill32aab012015-01-27 14:12:26 -05002895 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002896
2897 if (visit == PreVisit)
2898 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002899 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002900
Daniel Bratell29190082015-02-20 16:42:54 +01002901 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002902 }
2903 else if (visit == InVisit)
2904 {
2905 out << ", ";
2906 }
2907 else if (visit == PostVisit)
2908 {
2909 out << ")";
2910 }
2911}
2912
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002913const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const TConstantUnion *constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002914{
Jamie Madill32aab012015-01-27 14:12:26 -05002915 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002916
Jamie Madill98493dd2013-07-08 14:39:03 -04002917 const TStructure* structure = type.getStruct();
2918 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002919 {
Jamie Madill033dae62014-06-18 12:56:28 -04002920 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002921
Jamie Madill98493dd2013-07-08 14:39:03 -04002922 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002923
Jamie Madill98493dd2013-07-08 14:39:03 -04002924 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002925 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002926 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002927 constUnion = writeConstantUnion(*fieldType, constUnion);
2928
Jamie Madill98493dd2013-07-08 14:39:03 -04002929 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002930 {
2931 out << ", ";
2932 }
2933 }
2934
2935 out << ")";
2936 }
2937 else
2938 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002939 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002940 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002941
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002942 if (writeType)
2943 {
Jamie Madill033dae62014-06-18 12:56:28 -04002944 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002945 }
2946
Jamie Madill94bf7f22013-07-08 13:31:15 -04002947 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002948 {
2949 switch (constUnion->getType())
2950 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002951 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002952 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002953 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002954 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002955 default: UNREACHABLE();
2956 }
2957
2958 if (i != size - 1)
2959 {
2960 out << ", ";
2961 }
2962 }
2963
2964 if (writeType)
2965 {
2966 out << ")";
2967 }
2968 }
2969
2970 return constUnion;
2971}
2972
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002973void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2974{
2975 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2976 outputTriplet(visit, preString.c_str(), ", ", ")");
2977}
2978
Jamie Madill37997142015-01-28 10:06:34 -05002979bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2980{
2981 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2982 expression->traverse(&searchSymbol);
2983
2984 if (searchSymbol.foundMatch())
2985 {
2986 // Type already printed
2987 out << "t" + str(mUniqueIndex) + " = ";
2988 expression->traverse(this);
2989 out << ", ";
2990 symbolNode->traverse(this);
2991 out << " = t" + str(mUniqueIndex);
2992
2993 mUniqueIndex++;
2994 return true;
2995 }
2996
2997 return false;
2998}
2999
3000void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3001{
3002 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3003 << "\n"
3004 << "void initializeDeferredGlobals()\n"
3005 << "{\n";
3006
3007 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3008 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003009 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3010 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3011 if (binary != nullptr)
3012 {
3013 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3014 TIntermTyped *expression = binary->getRight();
3015 ASSERT(symbol);
3016 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003017
Olli Etuahod81ed842015-05-12 12:46:35 +03003018 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003019
Olli Etuahod81ed842015-05-12 12:46:35 +03003020 if (!writeSameSymbolInitializer(out, symbol, expression))
3021 {
3022 ASSERT(mInfoSinkStack.top() == &out);
3023 expression->traverse(this);
3024 }
3025 out << ";\n";
3026 }
3027 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003028 {
3029 ASSERT(mInfoSinkStack.top() == &out);
Olli Etuahod81ed842015-05-12 12:46:35 +03003030 writeSelection(selection);
Jamie Madill37997142015-01-28 10:06:34 -05003031 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003032 else
3033 {
3034 UNREACHABLE();
3035 }
Jamie Madill37997142015-01-28 10:06:34 -05003036 }
3037
3038 out << "}\n"
3039 << "\n";
3040}
3041
Jamie Madill55e79e02015-02-09 15:35:00 -05003042TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3043{
3044 const TFieldList &fields = structure.fields();
3045
3046 for (const auto &eqFunction : mStructEqualityFunctions)
3047 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003048 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003049 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003050 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003051 }
3052 }
3053
3054 const TString &structNameString = StructNameString(structure);
3055
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003056 StructEqualityFunction *function = new StructEqualityFunction();
3057 function->structure = &structure;
3058 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003059
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003060 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003061
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003062 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3063 << "{\n"
3064 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003065
3066 for (size_t i = 0; i < fields.size(); i++)
3067 {
3068 const TField *field = fields[i];
3069 const TType *fieldType = field->type();
3070
3071 const TString &fieldNameA = "a." + Decorate(field->name());
3072 const TString &fieldNameB = "b." + Decorate(field->name());
3073
3074 if (i > 0)
3075 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003076 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003077 }
3078
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003079 fnOut << "(";
3080 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3081 fnOut << fieldNameA;
3082 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3083 fnOut << fieldNameB;
3084 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3085 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003086 }
3087
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003088 fnOut << ";\n" << "}\n";
3089
3090 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003091
3092 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003093 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003094
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003095 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003096}
3097
Olli Etuaho7fb49552015-03-18 17:27:44 +02003098TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3099{
3100 for (const auto &eqFunction : mArrayEqualityFunctions)
3101 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003102 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003103 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003104 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003105 }
3106 }
3107
3108 const TString &typeName = TypeString(type);
3109
Olli Etuaho12690762015-03-31 12:55:28 +03003110 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003111 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003112
3113 TInfoSinkBase fnNameOut;
3114 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003115 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003116
3117 TType nonArrayType = type;
3118 nonArrayType.clearArrayness();
3119
3120 TInfoSinkBase fnOut;
3121
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003122 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003123 << typeName << " a[" << type.getArraySize() << "], "
3124 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003125 << "{\n"
3126 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3127 " {\n"
3128 " if (";
3129
3130 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3131 fnOut << "a[i]";
3132 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3133 fnOut << "b[i]";
3134 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3135
3136 fnOut << ") { return false; }\n"
3137 " }\n"
3138 " return true;\n"
3139 "}\n";
3140
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003141 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003142
3143 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003144 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003145
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003146 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003147}
3148
Olli Etuaho12690762015-03-31 12:55:28 +03003149TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3150{
3151 for (const auto &assignFunction : mArrayAssignmentFunctions)
3152 {
3153 if (assignFunction.type == type)
3154 {
3155 return assignFunction.functionName;
3156 }
3157 }
3158
3159 const TString &typeName = TypeString(type);
3160
3161 ArrayHelperFunction function;
3162 function.type = type;
3163
3164 TInfoSinkBase fnNameOut;
3165 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3166 function.functionName = fnNameOut.c_str();
3167
3168 TInfoSinkBase fnOut;
3169
3170 fnOut << "void " << function.functionName << "(out "
3171 << typeName << " a[" << type.getArraySize() << "], "
3172 << typeName << " b[" << type.getArraySize() << "])\n"
3173 << "{\n"
3174 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3175 " {\n"
3176 " a[i] = b[i];\n"
3177 " }\n"
3178 "}\n";
3179
3180 function.functionDefinition = fnOut.c_str();
3181
3182 mArrayAssignmentFunctions.push_back(function);
3183
3184 return function.functionName;
3185}
3186
Olli Etuaho9638c352015-04-01 14:34:52 +03003187TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3188{
3189 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3190 {
3191 if (constructIntoFunction.type == type)
3192 {
3193 return constructIntoFunction.functionName;
3194 }
3195 }
3196
3197 const TString &typeName = TypeString(type);
3198
3199 ArrayHelperFunction function;
3200 function.type = type;
3201
3202 TInfoSinkBase fnNameOut;
3203 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3204 function.functionName = fnNameOut.c_str();
3205
3206 TInfoSinkBase fnOut;
3207
3208 fnOut << "void " << function.functionName << "(out "
3209 << typeName << " a[" << type.getArraySize() << "]";
3210 for (int i = 0; i < type.getArraySize(); ++i)
3211 {
3212 fnOut << ", " << typeName << " b" << i;
3213 }
3214 fnOut << ")\n"
3215 "{\n";
3216
3217 for (int i = 0; i < type.getArraySize(); ++i)
3218 {
3219 fnOut << " a[" << i << "] = b" << i << ";\n";
3220 }
3221 fnOut << "}\n";
3222
3223 function.functionDefinition = fnOut.c_str();
3224
3225 mArrayConstructIntoFunctions.push_back(function);
3226
3227 return function.functionName;
3228}
3229
Jamie Madill2e295e22015-04-29 10:41:33 -04003230void OutputHLSL::ensureStructDefined(const TType &type)
3231{
3232 TStructure *structure = type.getStruct();
3233
3234 if (structure)
3235 {
3236 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3237 }
3238}
3239
3240
Olli Etuaho9638c352015-04-01 14:34:52 +03003241
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003242}