blob: e094db2becc0e8ee6f7c8ae86ecc908e9a919b7f [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 }
Olli Etuaho78174db2015-04-21 16:14:00 +03001394 else if (node->isInternal())
Jamie Madille53c98b2014-02-03 11:57:13 -05001395 {
1396 out << name;
1397 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001398 else
1399 {
Jamie Madill033dae62014-06-18 12:56:28 -04001400 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001401 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001402 }
1403}
1404
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001405void OutputHLSL::visitRaw(TIntermRaw *node)
1406{
Jamie Madill32aab012015-01-27 14:12:26 -05001407 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001408}
1409
Olli Etuaho7fb49552015-03-18 17:27:44 +02001410void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1411{
1412 if (type.isScalar() && !type.isArray())
1413 {
1414 if (op == EOpEqual)
1415 {
1416 outputTriplet(visit, "(", " == ", ")", out);
1417 }
1418 else
1419 {
1420 outputTriplet(visit, "(", " != ", ")", out);
1421 }
1422 }
1423 else
1424 {
1425 if (visit == PreVisit && op == EOpNotEqual)
1426 {
1427 out << "!";
1428 }
1429
1430 if (type.isArray())
1431 {
1432 const TString &functionName = addArrayEqualityFunction(type);
1433 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1434 }
1435 else if (type.getBasicType() == EbtStruct)
1436 {
1437 const TStructure &structure = *type.getStruct();
1438 const TString &functionName = addStructEqualityFunction(structure);
1439 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1440 }
1441 else
1442 {
1443 ASSERT(type.isMatrix() || type.isVector());
1444 outputTriplet(visit, "all(", " == ", ")", out);
1445 }
1446 }
1447}
1448
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001449bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1450{
Jamie Madill32aab012015-01-27 14:12:26 -05001451 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001452
Jamie Madill570e04d2013-06-21 09:15:33 -04001453 // Handle accessing std140 structs by value
1454 if (mFlaggedStructMappedNames.count(node) > 0)
1455 {
1456 out << mFlaggedStructMappedNames[node];
1457 return false;
1458 }
1459
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001460 switch (node->getOp())
1461 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001462 case EOpAssign:
1463 if (node->getLeft()->isArray())
1464 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001465 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1466 if (rightAgg != nullptr && rightAgg->isConstructor())
1467 {
1468 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1469 out << functionName << "(";
1470 node->getLeft()->traverse(this);
1471 TIntermSequence *seq = rightAgg->getSequence();
1472 for (auto &arrayElement : *seq)
1473 {
1474 out << ", ";
1475 arrayElement->traverse(this);
1476 }
1477 out << ")";
1478 return false;
1479 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001480 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1481 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1482
1483 const TString &functionName = addArrayAssignmentFunction(node->getType());
1484 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001485 }
1486 else
1487 {
1488 outputTriplet(visit, "(", " = ", ")");
1489 }
1490 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001491 case EOpInitialize:
1492 if (visit == PreVisit)
1493 {
1494 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1495 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1496 // new variable is created before the assignment is evaluated), so we need to convert
1497 // this to "float t = x, x = t;".
1498
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001499 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001500 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001501 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001502
Jamie Madill37997142015-01-28 10:06:34 -05001503 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1504 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001505 {
Jamie Madill37997142015-01-28 10:06:34 -05001506 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001507 // after we initialize uniforms.
1508 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1509 deferredInit->setLeft(node->getLeft());
1510 deferredInit->setRight(node->getRight());
1511 deferredInit->setType(node->getType());
1512 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001513 const TString &initString = initializer(node->getType());
1514 node->setRight(new TIntermRaw(node->getType(), initString));
1515 }
1516 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1517 {
1518 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001519 return false;
1520 }
1521 }
1522 else if (visit == InVisit)
1523 {
1524 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001525 }
1526 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001527 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1528 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1529 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1530 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1531 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1532 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001533 if (visit == PreVisit)
1534 {
1535 out << "(";
1536 }
1537 else if (visit == InVisit)
1538 {
1539 out << " = mul(";
1540 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001541 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001542 }
1543 else
1544 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001545 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001546 }
1547 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001548 case EOpMatrixTimesMatrixAssign:
1549 if (visit == PreVisit)
1550 {
1551 out << "(";
1552 }
1553 else if (visit == InVisit)
1554 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001555 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001556 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001557 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001558 }
1559 else
1560 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001561 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001562 }
1563 break;
1564 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001565 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001566 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1567 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1568 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1569 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1570 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001571 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001572 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001573 const TType& leftType = node->getLeft()->getType();
1574 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001575 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001576 if (visit == PreVisit)
1577 {
1578 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1579 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001580 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001581 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001582 return false;
1583 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001584 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001585 else
1586 {
1587 outputTriplet(visit, "", "[", "]");
1588 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001589 }
1590 break;
1591 case EOpIndexIndirect:
1592 // We do not currently support indirect references to interface blocks
1593 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1594 outputTriplet(visit, "", "[", "]");
1595 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001596 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001597 if (visit == InVisit)
1598 {
1599 const TStructure* structure = node->getLeft()->getType().getStruct();
1600 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1601 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001602 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001603
1604 return false;
1605 }
1606 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001607 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001608 if (visit == InVisit)
1609 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001610 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1611 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1612 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001613 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001614
1615 return false;
1616 }
1617 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618 case EOpVectorSwizzle:
1619 if (visit == InVisit)
1620 {
1621 out << ".";
1622
1623 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1624
1625 if (swizzle)
1626 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001627 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001628
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001629 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001630 {
1631 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1632
1633 if (element)
1634 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001635 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001636
1637 switch (i)
1638 {
1639 case 0: out << "x"; break;
1640 case 1: out << "y"; break;
1641 case 2: out << "z"; break;
1642 case 3: out << "w"; break;
1643 default: UNREACHABLE();
1644 }
1645 }
1646 else UNREACHABLE();
1647 }
1648 }
1649 else UNREACHABLE();
1650
1651 return false; // Fully processed
1652 }
1653 break;
1654 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1655 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1656 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1657 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001658 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001659 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1660 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1661 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1662 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1663 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001664 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001665 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001666 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001667 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1669 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1670 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1671 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1672 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001673 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001674 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1675 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001676 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001677 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001678 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1679 ASSERT(!node->getRight()->hasSideEffects());
1680 outputTriplet(visit, "(", " || ", ")");
1681 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001682 case EOpLogicalXor:
1683 mUsesXor = true;
1684 outputTriplet(visit, "xor(", ", ", ")");
1685 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001686 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001687 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1688 ASSERT(!node->getRight()->hasSideEffects());
1689 outputTriplet(visit, "(", " && ", ")");
1690 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691 default: UNREACHABLE();
1692 }
1693
1694 return true;
1695}
1696
1697bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1698{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001699 switch (node->getOp())
1700 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001701 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001702 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001703 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1704 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001705 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001706 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1707 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1708 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1709 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001710 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1711 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1712 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1713 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1714 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1715 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1716 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1717 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001718 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1719 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1720 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1721 case EOpAsinh:
1722 ASSERT(node->getUseEmulatedFunction());
1723 writeEmulatedFunctionTriplet(visit, "asinh(");
1724 break;
1725 case EOpAcosh:
1726 ASSERT(node->getUseEmulatedFunction());
1727 writeEmulatedFunctionTriplet(visit, "acosh(");
1728 break;
1729 case EOpAtanh:
1730 ASSERT(node->getUseEmulatedFunction());
1731 writeEmulatedFunctionTriplet(visit, "atanh(");
1732 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001733 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1734 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1735 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1736 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1737 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1738 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1739 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1740 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1741 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001742 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1743 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1744 case EOpRoundEven:
1745 ASSERT(node->getUseEmulatedFunction());
1746 writeEmulatedFunctionTriplet(visit, "roundEven(");
1747 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001748 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1749 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301750 case EOpIsNan:
1751 outputTriplet(visit, "isnan(", "", ")");
1752 mRequiresIEEEStrictCompiling = true;
1753 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301754 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001755 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1756 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1757 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1758 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001759 case EOpPackSnorm2x16:
1760 ASSERT(node->getUseEmulatedFunction());
1761 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1762 break;
1763 case EOpPackUnorm2x16:
1764 ASSERT(node->getUseEmulatedFunction());
1765 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1766 break;
1767 case EOpPackHalf2x16:
1768 ASSERT(node->getUseEmulatedFunction());
1769 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1770 break;
1771 case EOpUnpackSnorm2x16:
1772 ASSERT(node->getUseEmulatedFunction());
1773 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1774 break;
1775 case EOpUnpackUnorm2x16:
1776 ASSERT(node->getUseEmulatedFunction());
1777 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1778 break;
1779 case EOpUnpackHalf2x16:
1780 ASSERT(node->getUseEmulatedFunction());
1781 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1782 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001783 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1784 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001785 case EOpDFdx:
1786 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1787 {
1788 outputTriplet(visit, "(", "", ", 0.0)");
1789 }
1790 else
1791 {
1792 outputTriplet(visit, "ddx(", "", ")");
1793 }
1794 break;
1795 case EOpDFdy:
1796 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1797 {
1798 outputTriplet(visit, "(", "", ", 0.0)");
1799 }
1800 else
1801 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001802 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001803 }
1804 break;
1805 case EOpFwidth:
1806 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1807 {
1808 outputTriplet(visit, "(", "", ", 0.0)");
1809 }
1810 else
1811 {
1812 outputTriplet(visit, "fwidth(", "", ")");
1813 }
1814 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001815 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1816 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001817 case EOpInverse:
1818 ASSERT(node->getUseEmulatedFunction());
1819 writeEmulatedFunctionTriplet(visit, "inverse(");
1820 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001821
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001822 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1823 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824 default: UNREACHABLE();
1825 }
1826
1827 return true;
1828}
1829
1830bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1831{
Jamie Madill32aab012015-01-27 14:12:26 -05001832 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001834 switch (node->getOp())
1835 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001836 case EOpSequence:
1837 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001838 if (mInsideFunction)
1839 {
Jamie Madill075edd82013-07-08 13:30:19 -04001840 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001841 out << "{\n";
1842 }
1843
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001844 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001845 {
Jamie Madill075edd82013-07-08 13:30:19 -04001846 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001847
Olli Etuahoa6f22092015-05-08 18:31:10 +03001848 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001849
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001850 // Don't output ; after case labels, they're terminated by :
1851 // This is needed especially since outputting a ; after a case statement would turn empty
1852 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03001853 // Also no need to output ; after selection (if) statements or sequences. This is done just
1854 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001855 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
1856 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03001857 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001858 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001859 }
1860
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001861 if (mInsideFunction)
1862 {
Jamie Madill075edd82013-07-08 13:30:19 -04001863 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001864 out << "}\n";
1865 }
1866
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001867 return false;
1868 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869 case EOpDeclaration:
1870 if (visit == PreVisit)
1871 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001872 TIntermSequence *sequence = node->getSequence();
1873 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001874 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001876 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001878 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001879
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001880 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001882 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001884 out << "static ";
1885 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001886
Olli Etuahoa6f22092015-05-08 18:31:10 +03001887 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001888
Olli Etuahoa6f22092015-05-08 18:31:10 +03001889 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001890
Olli Etuahoa6f22092015-05-08 18:31:10 +03001891 if (symbol)
1892 {
1893 symbol->traverse(this);
1894 out << ArrayString(symbol->getType());
1895 out << " = " + initializer(symbol->getType());
1896 }
1897 else
1898 {
1899 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001901 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001902 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1903 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001904 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001905 }
1906 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907 }
Jamie Madill033dae62014-06-18 12:56:28 -04001908 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001909 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001910 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001911 {
1912 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1913
1914 if (symbol)
1915 {
1916 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1917 mReferencedVaryings[symbol->getSymbol()] = symbol;
1918 }
1919 else
1920 {
1921 (*sit)->traverse(this);
1922 }
1923 }
1924 }
1925
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 return false;
1927 }
1928 else if (visit == InVisit)
1929 {
1930 out << ", ";
1931 }
1932 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001933 case EOpInvariantDeclaration:
1934 // Do not do any translation
1935 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001936 case EOpPrototype:
1937 if (visit == PreVisit)
1938 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001939 size_t index = mCallDag.findIndex(node);
1940 // Skip the prototype if it is not implemented (and thus not used)
1941 if (index == CallDAG::InvalidIndex)
1942 {
1943 return false;
1944 }
1945
Olli Etuaho76acee82014-11-04 13:44:03 +02001946 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001947
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001948 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001949
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001950 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001951 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001952 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001953
1954 if (symbol)
1955 {
1956 out << argumentString(symbol);
1957
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001958 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001959 {
1960 out << ", ";
1961 }
1962 }
1963 else UNREACHABLE();
1964 }
1965
1966 out << ");\n";
1967
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001968 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001969 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1970 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001971 {
1972 mOutputLod0Function = true;
1973 node->traverse(this);
1974 mOutputLod0Function = false;
1975 }
1976
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001977 return false;
1978 }
1979 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001980 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981 case EOpFunction:
1982 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001983 ASSERT(mCurrentFunctionMetadata == nullptr);
alokp@chromium.org43884872010-03-30 00:08:52 +00001984 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001985
Corentin Wallez1239ee92015-03-19 14:38:02 -07001986 size_t index = mCallDag.findIndex(node);
1987 ASSERT(index != CallDAG::InvalidIndex);
1988 mCurrentFunctionMetadata = &mASTMetadataList[index];
1989
Jamie Madill033dae62014-06-18 12:56:28 -04001990 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001991
1992 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001994 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001995 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001996 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997 {
Jamie Madill033dae62014-06-18 12:56:28 -04001998 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001999 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002000
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002001 TIntermSequence *sequence = node->getSequence();
2002 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002003
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002004 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002005 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002006 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002007
2008 if (symbol)
2009 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002010 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002011
2012 out << argumentString(symbol);
2013
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002014 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002015 {
2016 out << ", ";
2017 }
2018 }
2019 else UNREACHABLE();
2020 }
2021
Olli Etuaho4785fec2015-05-18 16:09:37 +03002022 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002023
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002024 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002025 {
2026 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002027 TIntermNode *body = (*sequence)[1];
2028 // The function body node will output braces.
2029 ASSERT(IsSequence(body));
2030 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002031 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002033 else
2034 {
2035 out << "{}\n";
2036 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002037
Corentin Wallez1239ee92015-03-19 14:38:02 -07002038 mCurrentFunctionMetadata = nullptr;
2039
2040 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2041 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002042 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002043 ASSERT(name != "main");
2044 mOutputLod0Function = true;
2045 node->traverse(this);
2046 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002047 }
2048
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002049 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050 }
2051 break;
2052 case EOpFunctionCall:
2053 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002054 TString name = TFunction::unmangleName(node->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002055 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002056
Corentin Wallez1239ee92015-03-19 14:38:02 -07002057 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002058 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002060 if (node->isArray())
2061 {
2062 UNIMPLEMENTED();
2063 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002064 size_t index = mCallDag.findIndex(node);
2065 ASSERT(index != CallDAG::InvalidIndex);
2066 lod0 &= mASTMetadataList[index].mNeedsLod0;
2067
Jamie Madill033dae62014-06-18 12:56:28 -04002068 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069 }
2070 else
2071 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002072 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002073
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002074 TextureFunction textureFunction;
2075 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002076 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002077 textureFunction.method = TextureFunction::IMPLICIT;
2078 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002079 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002080
2081 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002082 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002083 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002084 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002085 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002086 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002087 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002088 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002089 }
Nicolas Capens46485082014-04-15 13:12:50 -04002090 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2091 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002092 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002093 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002094 }
Nicolas Capens46485082014-04-15 13:12:50 -04002095 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002096 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002097 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002098 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002099 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002100 else if (name == "textureSize")
2101 {
2102 textureFunction.method = TextureFunction::SIZE;
2103 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002104 else if (name == "textureOffset")
2105 {
2106 textureFunction.method = TextureFunction::IMPLICIT;
2107 textureFunction.offset = true;
2108 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002109 else if (name == "textureProjOffset")
2110 {
2111 textureFunction.method = TextureFunction::IMPLICIT;
2112 textureFunction.offset = true;
2113 textureFunction.proj = true;
2114 }
2115 else if (name == "textureLodOffset")
2116 {
2117 textureFunction.method = TextureFunction::LOD;
2118 textureFunction.offset = true;
2119 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002120 else if (name == "textureProjLodOffset")
2121 {
2122 textureFunction.method = TextureFunction::LOD;
2123 textureFunction.proj = true;
2124 textureFunction.offset = true;
2125 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002126 else if (name == "texelFetch")
2127 {
2128 textureFunction.method = TextureFunction::FETCH;
2129 }
2130 else if (name == "texelFetchOffset")
2131 {
2132 textureFunction.method = TextureFunction::FETCH;
2133 textureFunction.offset = true;
2134 }
Nicolas Capens46485082014-04-15 13:12:50 -04002135 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002136 {
2137 textureFunction.method = TextureFunction::GRAD;
2138 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002139 else if (name == "textureGradOffset")
2140 {
2141 textureFunction.method = TextureFunction::GRAD;
2142 textureFunction.offset = true;
2143 }
Nicolas Capens46485082014-04-15 13:12:50 -04002144 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002145 {
2146 textureFunction.method = TextureFunction::GRAD;
2147 textureFunction.proj = true;
2148 }
2149 else if (name == "textureProjGradOffset")
2150 {
2151 textureFunction.method = TextureFunction::GRAD;
2152 textureFunction.proj = true;
2153 textureFunction.offset = true;
2154 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002155 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002156
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002157 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002158 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002159 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2160
2161 if (textureFunction.offset)
2162 {
2163 mandatoryArgumentCount++;
2164 }
2165
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002166 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002167
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002168 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002169 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002170 if (bias)
2171 {
2172 textureFunction.method = TextureFunction::LOD0BIAS;
2173 }
2174 else
2175 {
2176 textureFunction.method = TextureFunction::LOD0;
2177 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002178 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002179 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002180 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002181 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002182 }
2183 }
2184
2185 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002186
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002187 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002189
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002190 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002191 {
2192 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2193 {
2194 out << "texture_";
2195 (*arg)->traverse(this);
2196 out << ", sampler_";
2197 }
2198
2199 (*arg)->traverse(this);
2200
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002201 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002202 {
2203 out << ", ";
2204 }
2205 }
2206
2207 out << ")";
2208
2209 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002210 }
2211 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002212 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002213 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2214 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2215 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2216 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2217 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2218 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2219 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2220 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2221 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2222 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2223 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2224 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2225 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2226 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2227 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2228 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2229 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002230 case EOpConstructMat2x3: outputConstructor(visit, node->getType(), "mat2x3", node->getSequence()); break;
2231 case EOpConstructMat2x4: outputConstructor(visit, node->getType(), "mat2x4", node->getSequence()); break;
2232 case EOpConstructMat3x2: outputConstructor(visit, node->getType(), "mat3x2", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002233 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002234 case EOpConstructMat3x4: outputConstructor(visit, node->getType(), "mat3x4", node->getSequence()); break;
2235 case EOpConstructMat4x2: outputConstructor(visit, node->getType(), "mat4x2", node->getSequence()); break;
2236 case EOpConstructMat4x3: outputConstructor(visit, node->getType(), "mat4x3", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002237 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002238 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002239 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002240 if (node->getType().isArray())
2241 {
2242 UNIMPLEMENTED();
2243 }
Jamie Madill033dae62014-06-18 12:56:28 -04002244 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002245 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002246 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002247 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002248 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002249 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2250 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2251 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2252 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2253 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2254 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002255 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002256 ASSERT(node->getUseEmulatedFunction());
2257 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002258 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002259 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002260 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002261 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002262 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002263 ASSERT(node->getUseEmulatedFunction());
2264 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265 break;
2266 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2267 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2268 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
Arun Patoled94f6642015-05-18 16:25:12 +05302269 case EOpMix:
2270 {
2271 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2272 if (lastParamNode->getType().getBasicType() == EbtBool)
2273 {
2274 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2275 // so use emulated version.
2276 ASSERT(node->getUseEmulatedFunction());
2277 writeEmulatedFunctionTriplet(visit, "mix(");
2278 }
2279 else
2280 {
2281 outputTriplet(visit, "lerp(", ", ", ")");
2282 }
2283 }
2284 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2286 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2287 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2288 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2289 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002290 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002291 ASSERT(node->getUseEmulatedFunction());
2292 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002293 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2295 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002296 case EOpOuterProduct:
2297 ASSERT(node->getUseEmulatedFunction());
2298 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2299 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301 default: UNREACHABLE();
2302 }
2303
2304 return true;
2305}
2306
Olli Etuahod81ed842015-05-12 12:46:35 +03002307void OutputHLSL::writeSelection(TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002308{
Jamie Madill32aab012015-01-27 14:12:26 -05002309 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310
Olli Etuahoa6f22092015-05-08 18:31:10 +03002311 out << "if (";
2312
2313 node->getCondition()->traverse(this);
2314
2315 out << ")\n";
2316
2317 outputLineDirective(node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002318
2319 bool discard = false;
2320
2321 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002323 // The trueBlock child node will output braces.
2324 ASSERT(IsSequence(node->getTrueBlock()));
2325
Olli Etuahoa6f22092015-05-08 18:31:10 +03002326 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002327
Olli Etuahoa6f22092015-05-08 18:31:10 +03002328 // Detect true discard
2329 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2330 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002331 else
2332 {
2333 // TODO(oetuaho): Check if the semicolon inside is necessary.
2334 // It's there as a result of conservative refactoring of the output.
2335 out << "{;}\n";
2336 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002337
Olli Etuahoa6f22092015-05-08 18:31:10 +03002338 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002339
Olli Etuahoa6f22092015-05-08 18:31:10 +03002340 if (node->getFalseBlock())
2341 {
2342 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002343
Olli Etuahoa6f22092015-05-08 18:31:10 +03002344 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345
Olli Etuaho4785fec2015-05-18 16:09:37 +03002346 // Either this is "else if" or the falseBlock child node will output braces.
2347 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2348
Olli Etuahoa6f22092015-05-08 18:31:10 +03002349 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002350
Olli Etuahoa6f22092015-05-08 18:31:10 +03002351 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002352
Olli Etuahoa6f22092015-05-08 18:31:10 +03002353 // Detect false discard
2354 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2355 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002356
Olli Etuahoa6f22092015-05-08 18:31:10 +03002357 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002358 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002359 {
2360 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002361 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002362}
2363
2364bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2365{
2366 TInfoSinkBase &out = getInfoSink();
2367
2368 ASSERT(!node->usesTernaryOperator());
2369
2370 if (!mInsideFunction)
2371 {
2372 // This is part of unfolded global initialization.
2373 mDeferredGlobalInitializers.push_back(node);
2374 return false;
2375 }
2376
2377 // D3D errors when there is a gradient operation in a loop in an unflattened if.
2378 if (mShaderType == GL_FRAGMENT_SHADER &&
2379 mCurrentFunctionMetadata->hasDiscontinuousLoop(node) &&
2380 mCurrentFunctionMetadata->hasGradientInCallGraph(node))
2381 {
2382 out << "FLATTEN ";
2383 }
2384
2385 writeSelection(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386
2387 return false;
2388}
2389
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002390bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002391{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002392 if (node->getStatementList())
2393 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002394 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002395 outputTriplet(visit, "switch (", ") ", "");
2396 // The curly braces get written when visiting the statementList aggregate
2397 }
2398 else
2399 {
2400 // No statementList, so it won't output curly braces
2401 outputTriplet(visit, "switch (", ") {", "}\n");
2402 }
2403 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002404}
2405
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002406bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002407{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002408 if (node->hasCondition())
2409 {
2410 outputTriplet(visit, "case (", "", "):\n");
2411 return true;
2412 }
2413 else
2414 {
2415 TInfoSinkBase &out = getInfoSink();
2416 out << "default:\n";
2417 return false;
2418 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002419}
2420
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2422{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002423 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424}
2425
2426bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2427{
Nicolas Capens655fe362014-04-11 13:12:34 -04002428 mNestedLoopDepth++;
2429
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002430 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002431 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002432 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002433
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002434 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002435 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002436 if (handleExcessiveLoop(node))
2437 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002438 mInsideDiscontinuousLoop = wasDiscontinuous;
2439 mNestedLoopDepth--;
2440
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002441 return false;
2442 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002443 }
2444
Jamie Madill32aab012015-01-27 14:12:26 -05002445 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446
Corentin Wallez1239ee92015-03-19 14:38:02 -07002447 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002448 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002450 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002451
Jamie Madill075edd82013-07-08 13:30:19 -04002452 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002453 }
2454 else
2455 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002456 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002457
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458 if (node->getInit())
2459 {
2460 node->getInit()->traverse(this);
2461 }
2462
2463 out << "; ";
2464
alokp@chromium.org52813552010-11-16 18:36:09 +00002465 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002467 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468 }
2469
2470 out << "; ";
2471
alokp@chromium.org52813552010-11-16 18:36:09 +00002472 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002474 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002475 }
2476
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002477 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002478
Jamie Madill075edd82013-07-08 13:30:19 -04002479 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 }
2481
2482 if (node->getBody())
2483 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002484 // The loop body node will output braces.
2485 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002486 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002488 else
2489 {
2490 // TODO(oetuaho): Check if the semicolon inside is necessary.
2491 // It's there as a result of conservative refactoring of the output.
2492 out << "{;}\n";
2493 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002494
Jamie Madill075edd82013-07-08 13:30:19 -04002495 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496
alokp@chromium.org52813552010-11-16 18:36:09 +00002497 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002498 {
Jamie Madill075edd82013-07-08 13:30:19 -04002499 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002500 out << "while(\n";
2501
alokp@chromium.org52813552010-11-16 18:36:09 +00002502 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002503
daniel@transgaming.com73536982012-03-21 20:45:49 +00002504 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 }
2506
daniel@transgaming.com73536982012-03-21 20:45:49 +00002507 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002509 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002510 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002511
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512 return false;
2513}
2514
2515bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2516{
Jamie Madill32aab012015-01-27 14:12:26 -05002517 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002518
2519 switch (node->getFlowOp())
2520 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002521 case EOpKill:
2522 outputTriplet(visit, "discard;\n", "", "");
2523 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002524 case EOpBreak:
2525 if (visit == PreVisit)
2526 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002527 if (mNestedLoopDepth > 1)
2528 {
2529 mUsesNestedBreak = true;
2530 }
2531
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002532 if (mExcessiveLoopIndex)
2533 {
2534 out << "{Break";
2535 mExcessiveLoopIndex->traverse(this);
2536 out << " = true; break;}\n";
2537 }
2538 else
2539 {
2540 out << "break;\n";
2541 }
2542 }
2543 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002544 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545 case EOpReturn:
2546 if (visit == PreVisit)
2547 {
2548 if (node->getExpression())
2549 {
2550 out << "return ";
2551 }
2552 else
2553 {
2554 out << "return;\n";
2555 }
2556 }
2557 else if (visit == PostVisit)
2558 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002559 if (node->getExpression())
2560 {
2561 out << ";\n";
2562 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002563 }
2564 break;
2565 default: UNREACHABLE();
2566 }
2567
2568 return true;
2569}
2570
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002571bool OutputHLSL::isSingleStatement(TIntermNode *node)
2572{
2573 TIntermAggregate *aggregate = node->getAsAggregate();
2574
2575 if (aggregate)
2576 {
2577 if (aggregate->getOp() == EOpSequence)
2578 {
2579 return false;
2580 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002581 else if (aggregate->getOp() == EOpDeclaration)
2582 {
2583 // Declaring multiple comma-separated variables must be considered multiple statements
2584 // because each individual declaration has side effects which are visible in the next.
2585 return false;
2586 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002587 else
2588 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002589 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002590 {
2591 if (!isSingleStatement(*sit))
2592 {
2593 return false;
2594 }
2595 }
2596
2597 return true;
2598 }
2599 }
2600
2601 return true;
2602}
2603
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002604// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2605// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002606bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2607{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002608 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002609 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002610
2611 // Parse loops of the form:
2612 // for(int index = initial; index [comparator] limit; index += increment)
2613 TIntermSymbol *index = NULL;
2614 TOperator comparator = EOpNull;
2615 int initial = 0;
2616 int limit = 0;
2617 int increment = 0;
2618
2619 // Parse index name and intial value
2620 if (node->getInit())
2621 {
2622 TIntermAggregate *init = node->getInit()->getAsAggregate();
2623
2624 if (init)
2625 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002626 TIntermSequence *sequence = init->getSequence();
2627 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002628
2629 if (variable && variable->getQualifier() == EvqTemporary)
2630 {
2631 TIntermBinary *assign = variable->getAsBinaryNode();
2632
2633 if (assign->getOp() == EOpInitialize)
2634 {
2635 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2636 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2637
2638 if (symbol && constant)
2639 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002640 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002641 {
2642 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002643 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002644 }
2645 }
2646 }
2647 }
2648 }
2649 }
2650
2651 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002652 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002653 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002654 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002655
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002656 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2657 {
2658 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2659
2660 if (constant)
2661 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002662 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002663 {
2664 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002665 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002666 }
2667 }
2668 }
2669 }
2670
2671 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002672 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002673 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002674 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2675 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002676
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002677 if (binaryTerminal)
2678 {
2679 TOperator op = binaryTerminal->getOp();
2680 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2681
2682 if (constant)
2683 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002684 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002685 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002686 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002687
2688 switch (op)
2689 {
2690 case EOpAddAssign: increment = value; break;
2691 case EOpSubAssign: increment = -value; break;
2692 default: UNIMPLEMENTED();
2693 }
2694 }
2695 }
2696 }
2697 else if (unaryTerminal)
2698 {
2699 TOperator op = unaryTerminal->getOp();
2700
2701 switch (op)
2702 {
2703 case EOpPostIncrement: increment = 1; break;
2704 case EOpPostDecrement: increment = -1; break;
2705 case EOpPreIncrement: increment = 1; break;
2706 case EOpPreDecrement: increment = -1; break;
2707 default: UNIMPLEMENTED();
2708 }
2709 }
2710 }
2711
2712 if (index != NULL && comparator != EOpNull && increment != 0)
2713 {
2714 if (comparator == EOpLessThanEqual)
2715 {
2716 comparator = EOpLessThan;
2717 limit += 1;
2718 }
2719
2720 if (comparator == EOpLessThan)
2721 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002722 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002723
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002724 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002725 {
2726 return false; // Not an excessive loop
2727 }
2728
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002729 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2730 mExcessiveLoopIndex = index;
2731
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002732 out << "{int ";
2733 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002734 out << ";\n"
2735 "bool Break";
2736 index->traverse(this);
2737 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002738
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002739 bool firstLoopFragment = true;
2740
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002741 while (iterations > 0)
2742 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002743 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002744
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002745 if (!firstLoopFragment)
2746 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002747 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002748 index->traverse(this);
2749 out << ") {\n";
2750 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002751
2752 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2753 {
2754 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2755 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002756
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002757 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002758 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002759
Corentin Wallez1239ee92015-03-19 14:38:02 -07002760 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002761 index->traverse(this);
2762 out << " = ";
2763 out << initial;
2764
2765 out << "; ";
2766 index->traverse(this);
2767 out << " < ";
2768 out << clampedLimit;
2769
2770 out << "; ";
2771 index->traverse(this);
2772 out << " += ";
2773 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002774 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002775
Jamie Madill075edd82013-07-08 13:30:19 -04002776 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002777 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002778
2779 if (node->getBody())
2780 {
2781 node->getBody()->traverse(this);
2782 }
2783
Jamie Madill075edd82013-07-08 13:30:19 -04002784 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002785 out << ";}\n";
2786
2787 if (!firstLoopFragment)
2788 {
2789 out << "}\n";
2790 }
2791
2792 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002793
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002794 initial += MAX_LOOP_ITERATIONS * increment;
2795 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002796 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002797
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002798 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002799
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002800 mExcessiveLoopIndex = restoreIndex;
2801
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002802 return true;
2803 }
2804 else UNIMPLEMENTED();
2805 }
2806
2807 return false; // Not handled as an excessive loop
2808}
2809
Olli Etuaho7fb49552015-03-18 17:27:44 +02002810void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002811{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002812 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813 {
2814 out << preString;
2815 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002816 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002817 {
2818 out << inString;
2819 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002820 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002821 {
2822 out << postString;
2823 }
2824}
2825
Olli Etuaho7fb49552015-03-18 17:27:44 +02002826void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2827{
2828 outputTriplet(visit, preString, inString, postString, getInfoSink());
2829}
2830
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002831void OutputHLSL::outputLineDirective(int line)
2832{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002833 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002834 {
Jamie Madill32aab012015-01-27 14:12:26 -05002835 TInfoSinkBase &out = getInfoSink();
2836
2837 out << "\n";
2838 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002839
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002840 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002841 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002842 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002843 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002844
Jamie Madill32aab012015-01-27 14:12:26 -05002845 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002846 }
2847}
2848
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002849TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2850{
2851 TQualifier qualifier = symbol->getQualifier();
2852 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002853 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002854
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002855 if (name.empty()) // HLSL demands named arguments, also for prototypes
2856 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002857 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002858 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002859 else if (!symbol->isInternal())
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002860 {
Jamie Madill033dae62014-06-18 12:56:28 -04002861 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002862 }
2863
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002864 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2865 {
Jamie Madill033dae62014-06-18 12:56:28 -04002866 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002867 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002868 }
2869
Jamie Madill033dae62014-06-18 12:56:28 -04002870 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871}
2872
2873TString OutputHLSL::initializer(const TType &type)
2874{
2875 TString string;
2876
Jamie Madill94bf7f22013-07-08 13:31:15 -04002877 size_t size = type.getObjectSize();
2878 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002879 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002880 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002881
Jamie Madill94bf7f22013-07-08 13:31:15 -04002882 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002883 {
2884 string += ", ";
2885 }
2886 }
2887
daniel@transgaming.comead23042010-04-29 03:35:36 +00002888 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002889}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002890
Daniel Bratell29190082015-02-20 16:42:54 +01002891void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002892{
Olli Etuahof40319e2015-03-10 14:33:00 +02002893 if (type.isArray())
2894 {
2895 UNIMPLEMENTED();
2896 }
Jamie Madill32aab012015-01-27 14:12:26 -05002897 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002898
2899 if (visit == PreVisit)
2900 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002901 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002902
Daniel Bratell29190082015-02-20 16:42:54 +01002903 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002904 }
2905 else if (visit == InVisit)
2906 {
2907 out << ", ";
2908 }
2909 else if (visit == PostVisit)
2910 {
2911 out << ")";
2912 }
2913}
2914
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002915const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const TConstantUnion *constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002916{
Jamie Madill32aab012015-01-27 14:12:26 -05002917 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002918
Jamie Madill98493dd2013-07-08 14:39:03 -04002919 const TStructure* structure = type.getStruct();
2920 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002921 {
Jamie Madill033dae62014-06-18 12:56:28 -04002922 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002923
Jamie Madill98493dd2013-07-08 14:39:03 -04002924 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002925
Jamie Madill98493dd2013-07-08 14:39:03 -04002926 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002927 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002928 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002929 constUnion = writeConstantUnion(*fieldType, constUnion);
2930
Jamie Madill98493dd2013-07-08 14:39:03 -04002931 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002932 {
2933 out << ", ";
2934 }
2935 }
2936
2937 out << ")";
2938 }
2939 else
2940 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002941 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002942 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002943
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002944 if (writeType)
2945 {
Jamie Madill033dae62014-06-18 12:56:28 -04002946 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002947 }
2948
Jamie Madill94bf7f22013-07-08 13:31:15 -04002949 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002950 {
2951 switch (constUnion->getType())
2952 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002953 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002954 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002955 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002956 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002957 default: UNREACHABLE();
2958 }
2959
2960 if (i != size - 1)
2961 {
2962 out << ", ";
2963 }
2964 }
2965
2966 if (writeType)
2967 {
2968 out << ")";
2969 }
2970 }
2971
2972 return constUnion;
2973}
2974
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002975void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2976{
2977 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2978 outputTriplet(visit, preString.c_str(), ", ", ")");
2979}
2980
Jamie Madill37997142015-01-28 10:06:34 -05002981bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2982{
2983 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2984 expression->traverse(&searchSymbol);
2985
2986 if (searchSymbol.foundMatch())
2987 {
2988 // Type already printed
2989 out << "t" + str(mUniqueIndex) + " = ";
2990 expression->traverse(this);
2991 out << ", ";
2992 symbolNode->traverse(this);
2993 out << " = t" + str(mUniqueIndex);
2994
2995 mUniqueIndex++;
2996 return true;
2997 }
2998
2999 return false;
3000}
3001
3002void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3003{
3004 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3005 << "\n"
3006 << "void initializeDeferredGlobals()\n"
3007 << "{\n";
3008
3009 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3010 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003011 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3012 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3013 if (binary != nullptr)
3014 {
3015 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3016 TIntermTyped *expression = binary->getRight();
3017 ASSERT(symbol);
3018 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003019
Olli Etuahod81ed842015-05-12 12:46:35 +03003020 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003021
Olli Etuahod81ed842015-05-12 12:46:35 +03003022 if (!writeSameSymbolInitializer(out, symbol, expression))
3023 {
3024 ASSERT(mInfoSinkStack.top() == &out);
3025 expression->traverse(this);
3026 }
3027 out << ";\n";
3028 }
3029 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003030 {
3031 ASSERT(mInfoSinkStack.top() == &out);
Olli Etuahod81ed842015-05-12 12:46:35 +03003032 writeSelection(selection);
Jamie Madill37997142015-01-28 10:06:34 -05003033 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003034 else
3035 {
3036 UNREACHABLE();
3037 }
Jamie Madill37997142015-01-28 10:06:34 -05003038 }
3039
3040 out << "}\n"
3041 << "\n";
3042}
3043
Jamie Madill55e79e02015-02-09 15:35:00 -05003044TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3045{
3046 const TFieldList &fields = structure.fields();
3047
3048 for (const auto &eqFunction : mStructEqualityFunctions)
3049 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003050 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003051 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003052 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003053 }
3054 }
3055
3056 const TString &structNameString = StructNameString(structure);
3057
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003058 StructEqualityFunction *function = new StructEqualityFunction();
3059 function->structure = &structure;
3060 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003061
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003062 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003063
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003064 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3065 << "{\n"
3066 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003067
3068 for (size_t i = 0; i < fields.size(); i++)
3069 {
3070 const TField *field = fields[i];
3071 const TType *fieldType = field->type();
3072
3073 const TString &fieldNameA = "a." + Decorate(field->name());
3074 const TString &fieldNameB = "b." + Decorate(field->name());
3075
3076 if (i > 0)
3077 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003078 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003079 }
3080
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003081 fnOut << "(";
3082 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3083 fnOut << fieldNameA;
3084 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3085 fnOut << fieldNameB;
3086 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3087 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003088 }
3089
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003090 fnOut << ";\n" << "}\n";
3091
3092 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003093
3094 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003095 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003096
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003097 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003098}
3099
Olli Etuaho7fb49552015-03-18 17:27:44 +02003100TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3101{
3102 for (const auto &eqFunction : mArrayEqualityFunctions)
3103 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003104 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003105 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003106 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003107 }
3108 }
3109
3110 const TString &typeName = TypeString(type);
3111
Olli Etuaho12690762015-03-31 12:55:28 +03003112 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003113 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003114
3115 TInfoSinkBase fnNameOut;
3116 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003117 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003118
3119 TType nonArrayType = type;
3120 nonArrayType.clearArrayness();
3121
3122 TInfoSinkBase fnOut;
3123
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003124 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003125 << typeName << " a[" << type.getArraySize() << "], "
3126 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003127 << "{\n"
3128 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3129 " {\n"
3130 " if (";
3131
3132 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3133 fnOut << "a[i]";
3134 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3135 fnOut << "b[i]";
3136 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3137
3138 fnOut << ") { return false; }\n"
3139 " }\n"
3140 " return true;\n"
3141 "}\n";
3142
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003143 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003144
3145 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003146 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003147
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003148 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003149}
3150
Olli Etuaho12690762015-03-31 12:55:28 +03003151TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3152{
3153 for (const auto &assignFunction : mArrayAssignmentFunctions)
3154 {
3155 if (assignFunction.type == type)
3156 {
3157 return assignFunction.functionName;
3158 }
3159 }
3160
3161 const TString &typeName = TypeString(type);
3162
3163 ArrayHelperFunction function;
3164 function.type = type;
3165
3166 TInfoSinkBase fnNameOut;
3167 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3168 function.functionName = fnNameOut.c_str();
3169
3170 TInfoSinkBase fnOut;
3171
3172 fnOut << "void " << function.functionName << "(out "
3173 << typeName << " a[" << type.getArraySize() << "], "
3174 << typeName << " b[" << type.getArraySize() << "])\n"
3175 << "{\n"
3176 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3177 " {\n"
3178 " a[i] = b[i];\n"
3179 " }\n"
3180 "}\n";
3181
3182 function.functionDefinition = fnOut.c_str();
3183
3184 mArrayAssignmentFunctions.push_back(function);
3185
3186 return function.functionName;
3187}
3188
Olli Etuaho9638c352015-04-01 14:34:52 +03003189TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3190{
3191 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3192 {
3193 if (constructIntoFunction.type == type)
3194 {
3195 return constructIntoFunction.functionName;
3196 }
3197 }
3198
3199 const TString &typeName = TypeString(type);
3200
3201 ArrayHelperFunction function;
3202 function.type = type;
3203
3204 TInfoSinkBase fnNameOut;
3205 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3206 function.functionName = fnNameOut.c_str();
3207
3208 TInfoSinkBase fnOut;
3209
3210 fnOut << "void " << function.functionName << "(out "
3211 << typeName << " a[" << type.getArraySize() << "]";
3212 for (int i = 0; i < type.getArraySize(); ++i)
3213 {
3214 fnOut << ", " << typeName << " b" << i;
3215 }
3216 fnOut << ")\n"
3217 "{\n";
3218
3219 for (int i = 0; i < type.getArraySize(); ++i)
3220 {
3221 fnOut << " a[" << i << "] = b" << i << ";\n";
3222 }
3223 fnOut << "}\n";
3224
3225 function.functionDefinition = fnOut.c_str();
3226
3227 mArrayConstructIntoFunctions.push_back(function);
3228
3229 return function.functionName;
3230}
3231
Jamie Madill2e295e22015-04-29 10:41:33 -04003232void OutputHLSL::ensureStructDefined(const TType &type)
3233{
3234 TStructure *structure = type.getStruct();
3235
3236 if (structure)
3237 {
3238 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3239 }
3240}
3241
3242
Olli Etuaho9638c352015-04-01 14:34:52 +03003243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003244}