blob: dfee1d9f9585822b66d19d61d84f3f2b0b07d667 [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 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001389 else if (name == "gl_FragDepthEXT")
1390 {
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;
2230 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2231 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002232 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002233 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002234 if (node->getType().isArray())
2235 {
2236 UNIMPLEMENTED();
2237 }
Jamie Madill033dae62014-06-18 12:56:28 -04002238 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002239 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002240 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002241 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002242 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002243 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2244 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2245 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2246 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2247 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2248 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002249 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002250 ASSERT(node->getUseEmulatedFunction());
2251 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002252 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002253 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002254 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002256 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002257 ASSERT(node->getUseEmulatedFunction());
2258 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 break;
2260 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2261 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2262 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
Arun Patoled94f6642015-05-18 16:25:12 +05302263 case EOpMix:
2264 {
2265 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2266 if (lastParamNode->getType().getBasicType() == EbtBool)
2267 {
2268 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2269 // so use emulated version.
2270 ASSERT(node->getUseEmulatedFunction());
2271 writeEmulatedFunctionTriplet(visit, "mix(");
2272 }
2273 else
2274 {
2275 outputTriplet(visit, "lerp(", ", ", ")");
2276 }
2277 }
2278 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2280 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2281 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2282 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2283 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002284 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002285 ASSERT(node->getUseEmulatedFunction());
2286 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002287 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2289 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002290 case EOpOuterProduct:
2291 ASSERT(node->getUseEmulatedFunction());
2292 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2293 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295 default: UNREACHABLE();
2296 }
2297
2298 return true;
2299}
2300
Olli Etuahod81ed842015-05-12 12:46:35 +03002301void OutputHLSL::writeSelection(TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302{
Jamie Madill32aab012015-01-27 14:12:26 -05002303 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304
Olli Etuahoa6f22092015-05-08 18:31:10 +03002305 out << "if (";
2306
2307 node->getCondition()->traverse(this);
2308
2309 out << ")\n";
2310
2311 outputLineDirective(node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002312
2313 bool discard = false;
2314
2315 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002317 // The trueBlock child node will output braces.
2318 ASSERT(IsSequence(node->getTrueBlock()));
2319
Olli Etuahoa6f22092015-05-08 18:31:10 +03002320 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002321
Olli Etuahoa6f22092015-05-08 18:31:10 +03002322 // Detect true discard
2323 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2324 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002325 else
2326 {
2327 // TODO(oetuaho): Check if the semicolon inside is necessary.
2328 // It's there as a result of conservative refactoring of the output.
2329 out << "{;}\n";
2330 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002331
Olli Etuahoa6f22092015-05-08 18:31:10 +03002332 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002333
Olli Etuahoa6f22092015-05-08 18:31:10 +03002334 if (node->getFalseBlock())
2335 {
2336 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002337
Olli Etuahoa6f22092015-05-08 18:31:10 +03002338 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339
Olli Etuaho4785fec2015-05-18 16:09:37 +03002340 // Either this is "else if" or the falseBlock child node will output braces.
2341 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2342
Olli Etuahoa6f22092015-05-08 18:31:10 +03002343 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002344
Olli Etuahoa6f22092015-05-08 18:31:10 +03002345 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002346
Olli Etuahoa6f22092015-05-08 18:31:10 +03002347 // Detect false discard
2348 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2349 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002350
Olli Etuahoa6f22092015-05-08 18:31:10 +03002351 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002352 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002353 {
2354 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002355 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002356}
2357
2358bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2359{
2360 TInfoSinkBase &out = getInfoSink();
2361
2362 ASSERT(!node->usesTernaryOperator());
2363
2364 if (!mInsideFunction)
2365 {
2366 // This is part of unfolded global initialization.
2367 mDeferredGlobalInitializers.push_back(node);
2368 return false;
2369 }
2370
2371 // D3D errors when there is a gradient operation in a loop in an unflattened if.
2372 if (mShaderType == GL_FRAGMENT_SHADER &&
2373 mCurrentFunctionMetadata->hasDiscontinuousLoop(node) &&
2374 mCurrentFunctionMetadata->hasGradientInCallGraph(node))
2375 {
2376 out << "FLATTEN ";
2377 }
2378
2379 writeSelection(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380
2381 return false;
2382}
2383
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002384bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002385{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002386 if (node->getStatementList())
2387 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002388 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002389 outputTriplet(visit, "switch (", ") ", "");
2390 // The curly braces get written when visiting the statementList aggregate
2391 }
2392 else
2393 {
2394 // No statementList, so it won't output curly braces
2395 outputTriplet(visit, "switch (", ") {", "}\n");
2396 }
2397 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002398}
2399
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002400bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002401{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002402 if (node->hasCondition())
2403 {
2404 outputTriplet(visit, "case (", "", "):\n");
2405 return true;
2406 }
2407 else
2408 {
2409 TInfoSinkBase &out = getInfoSink();
2410 out << "default:\n";
2411 return false;
2412 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002413}
2414
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2416{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002417 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418}
2419
2420bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2421{
Nicolas Capens655fe362014-04-11 13:12:34 -04002422 mNestedLoopDepth++;
2423
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002424 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002425 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002426 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002427
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002428 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002429 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002430 if (handleExcessiveLoop(node))
2431 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002432 mInsideDiscontinuousLoop = wasDiscontinuous;
2433 mNestedLoopDepth--;
2434
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002435 return false;
2436 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002437 }
2438
Jamie Madill32aab012015-01-27 14:12:26 -05002439 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002440
Corentin Wallez1239ee92015-03-19 14:38:02 -07002441 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002442 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002443 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002444 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002445
Jamie Madill075edd82013-07-08 13:30:19 -04002446 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447 }
2448 else
2449 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002450 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002451
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 if (node->getInit())
2453 {
2454 node->getInit()->traverse(this);
2455 }
2456
2457 out << "; ";
2458
alokp@chromium.org52813552010-11-16 18:36:09 +00002459 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002461 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 }
2463
2464 out << "; ";
2465
alokp@chromium.org52813552010-11-16 18:36:09 +00002466 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002468 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469 }
2470
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002471 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002472
Jamie Madill075edd82013-07-08 13:30:19 -04002473 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474 }
2475
2476 if (node->getBody())
2477 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002478 // The loop body node will output braces.
2479 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002480 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002482 else
2483 {
2484 // TODO(oetuaho): Check if the semicolon inside is necessary.
2485 // It's there as a result of conservative refactoring of the output.
2486 out << "{;}\n";
2487 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488
Jamie Madill075edd82013-07-08 13:30:19 -04002489 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490
alokp@chromium.org52813552010-11-16 18:36:09 +00002491 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492 {
Jamie Madill075edd82013-07-08 13:30:19 -04002493 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002494 out << "while(\n";
2495
alokp@chromium.org52813552010-11-16 18:36:09 +00002496 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497
daniel@transgaming.com73536982012-03-21 20:45:49 +00002498 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002499 }
2500
daniel@transgaming.com73536982012-03-21 20:45:49 +00002501 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002502
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002503 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002504 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002505
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002506 return false;
2507}
2508
2509bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2510{
Jamie Madill32aab012015-01-27 14:12:26 -05002511 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512
2513 switch (node->getFlowOp())
2514 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002515 case EOpKill:
2516 outputTriplet(visit, "discard;\n", "", "");
2517 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002518 case EOpBreak:
2519 if (visit == PreVisit)
2520 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002521 if (mNestedLoopDepth > 1)
2522 {
2523 mUsesNestedBreak = true;
2524 }
2525
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002526 if (mExcessiveLoopIndex)
2527 {
2528 out << "{Break";
2529 mExcessiveLoopIndex->traverse(this);
2530 out << " = true; break;}\n";
2531 }
2532 else
2533 {
2534 out << "break;\n";
2535 }
2536 }
2537 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002538 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 case EOpReturn:
2540 if (visit == PreVisit)
2541 {
2542 if (node->getExpression())
2543 {
2544 out << "return ";
2545 }
2546 else
2547 {
2548 out << "return;\n";
2549 }
2550 }
2551 else if (visit == PostVisit)
2552 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002553 if (node->getExpression())
2554 {
2555 out << ";\n";
2556 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557 }
2558 break;
2559 default: UNREACHABLE();
2560 }
2561
2562 return true;
2563}
2564
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002565bool OutputHLSL::isSingleStatement(TIntermNode *node)
2566{
2567 TIntermAggregate *aggregate = node->getAsAggregate();
2568
2569 if (aggregate)
2570 {
2571 if (aggregate->getOp() == EOpSequence)
2572 {
2573 return false;
2574 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002575 else if (aggregate->getOp() == EOpDeclaration)
2576 {
2577 // Declaring multiple comma-separated variables must be considered multiple statements
2578 // because each individual declaration has side effects which are visible in the next.
2579 return false;
2580 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002581 else
2582 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002583 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002584 {
2585 if (!isSingleStatement(*sit))
2586 {
2587 return false;
2588 }
2589 }
2590
2591 return true;
2592 }
2593 }
2594
2595 return true;
2596}
2597
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002598// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2599// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002600bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2601{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002602 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002603 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002604
2605 // Parse loops of the form:
2606 // for(int index = initial; index [comparator] limit; index += increment)
2607 TIntermSymbol *index = NULL;
2608 TOperator comparator = EOpNull;
2609 int initial = 0;
2610 int limit = 0;
2611 int increment = 0;
2612
2613 // Parse index name and intial value
2614 if (node->getInit())
2615 {
2616 TIntermAggregate *init = node->getInit()->getAsAggregate();
2617
2618 if (init)
2619 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002620 TIntermSequence *sequence = init->getSequence();
2621 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002622
2623 if (variable && variable->getQualifier() == EvqTemporary)
2624 {
2625 TIntermBinary *assign = variable->getAsBinaryNode();
2626
2627 if (assign->getOp() == EOpInitialize)
2628 {
2629 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2630 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2631
2632 if (symbol && constant)
2633 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002634 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002635 {
2636 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002637 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002638 }
2639 }
2640 }
2641 }
2642 }
2643 }
2644
2645 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002646 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002647 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002648 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002649
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002650 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2651 {
2652 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2653
2654 if (constant)
2655 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002656 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002657 {
2658 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002659 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002660 }
2661 }
2662 }
2663 }
2664
2665 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002666 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002667 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002668 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2669 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002670
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002671 if (binaryTerminal)
2672 {
2673 TOperator op = binaryTerminal->getOp();
2674 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2675
2676 if (constant)
2677 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002678 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002679 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002680 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002681
2682 switch (op)
2683 {
2684 case EOpAddAssign: increment = value; break;
2685 case EOpSubAssign: increment = -value; break;
2686 default: UNIMPLEMENTED();
2687 }
2688 }
2689 }
2690 }
2691 else if (unaryTerminal)
2692 {
2693 TOperator op = unaryTerminal->getOp();
2694
2695 switch (op)
2696 {
2697 case EOpPostIncrement: increment = 1; break;
2698 case EOpPostDecrement: increment = -1; break;
2699 case EOpPreIncrement: increment = 1; break;
2700 case EOpPreDecrement: increment = -1; break;
2701 default: UNIMPLEMENTED();
2702 }
2703 }
2704 }
2705
2706 if (index != NULL && comparator != EOpNull && increment != 0)
2707 {
2708 if (comparator == EOpLessThanEqual)
2709 {
2710 comparator = EOpLessThan;
2711 limit += 1;
2712 }
2713
2714 if (comparator == EOpLessThan)
2715 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002716 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002717
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002718 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002719 {
2720 return false; // Not an excessive loop
2721 }
2722
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002723 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2724 mExcessiveLoopIndex = index;
2725
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002726 out << "{int ";
2727 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002728 out << ";\n"
2729 "bool Break";
2730 index->traverse(this);
2731 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002732
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002733 bool firstLoopFragment = true;
2734
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002735 while (iterations > 0)
2736 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002737 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002738
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002739 if (!firstLoopFragment)
2740 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002741 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002742 index->traverse(this);
2743 out << ") {\n";
2744 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002745
2746 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2747 {
2748 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2749 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002750
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002751 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002752 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002753
Corentin Wallez1239ee92015-03-19 14:38:02 -07002754 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002755 index->traverse(this);
2756 out << " = ";
2757 out << initial;
2758
2759 out << "; ";
2760 index->traverse(this);
2761 out << " < ";
2762 out << clampedLimit;
2763
2764 out << "; ";
2765 index->traverse(this);
2766 out << " += ";
2767 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002768 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002769
Jamie Madill075edd82013-07-08 13:30:19 -04002770 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002771 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002772
2773 if (node->getBody())
2774 {
2775 node->getBody()->traverse(this);
2776 }
2777
Jamie Madill075edd82013-07-08 13:30:19 -04002778 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002779 out << ";}\n";
2780
2781 if (!firstLoopFragment)
2782 {
2783 out << "}\n";
2784 }
2785
2786 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002787
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002788 initial += MAX_LOOP_ITERATIONS * increment;
2789 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002790 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002791
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002792 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002793
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002794 mExcessiveLoopIndex = restoreIndex;
2795
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002796 return true;
2797 }
2798 else UNIMPLEMENTED();
2799 }
2800
2801 return false; // Not handled as an excessive loop
2802}
2803
Olli Etuaho7fb49552015-03-18 17:27:44 +02002804void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002805{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002806 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807 {
2808 out << preString;
2809 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002810 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002811 {
2812 out << inString;
2813 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002814 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 {
2816 out << postString;
2817 }
2818}
2819
Olli Etuaho7fb49552015-03-18 17:27:44 +02002820void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2821{
2822 outputTriplet(visit, preString, inString, postString, getInfoSink());
2823}
2824
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002825void OutputHLSL::outputLineDirective(int line)
2826{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002827 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002828 {
Jamie Madill32aab012015-01-27 14:12:26 -05002829 TInfoSinkBase &out = getInfoSink();
2830
2831 out << "\n";
2832 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002833
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002834 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002835 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002836 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002837 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002838
Jamie Madill32aab012015-01-27 14:12:26 -05002839 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002840 }
2841}
2842
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002843TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2844{
2845 TQualifier qualifier = symbol->getQualifier();
2846 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002847 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002848
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002849 if (name.empty()) // HLSL demands named arguments, also for prototypes
2850 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002851 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002852 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002853 else if (!symbol->isInternal())
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002854 {
Jamie Madill033dae62014-06-18 12:56:28 -04002855 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002856 }
2857
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002858 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2859 {
Jamie Madill033dae62014-06-18 12:56:28 -04002860 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002861 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002862 }
2863
Jamie Madill033dae62014-06-18 12:56:28 -04002864 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002865}
2866
2867TString OutputHLSL::initializer(const TType &type)
2868{
2869 TString string;
2870
Jamie Madill94bf7f22013-07-08 13:31:15 -04002871 size_t size = type.getObjectSize();
2872 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002874 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002875
Jamie Madill94bf7f22013-07-08 13:31:15 -04002876 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002877 {
2878 string += ", ";
2879 }
2880 }
2881
daniel@transgaming.comead23042010-04-29 03:35:36 +00002882 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002883}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002884
Daniel Bratell29190082015-02-20 16:42:54 +01002885void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002886{
Olli Etuahof40319e2015-03-10 14:33:00 +02002887 if (type.isArray())
2888 {
2889 UNIMPLEMENTED();
2890 }
Jamie Madill32aab012015-01-27 14:12:26 -05002891 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002892
2893 if (visit == PreVisit)
2894 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002895 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002896
Daniel Bratell29190082015-02-20 16:42:54 +01002897 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002898 }
2899 else if (visit == InVisit)
2900 {
2901 out << ", ";
2902 }
2903 else if (visit == PostVisit)
2904 {
2905 out << ")";
2906 }
2907}
2908
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002909const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const TConstantUnion *constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002910{
Jamie Madill32aab012015-01-27 14:12:26 -05002911 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002912
Jamie Madill98493dd2013-07-08 14:39:03 -04002913 const TStructure* structure = type.getStruct();
2914 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002915 {
Jamie Madill033dae62014-06-18 12:56:28 -04002916 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002917
Jamie Madill98493dd2013-07-08 14:39:03 -04002918 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002919
Jamie Madill98493dd2013-07-08 14:39:03 -04002920 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002921 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002922 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002923 constUnion = writeConstantUnion(*fieldType, constUnion);
2924
Jamie Madill98493dd2013-07-08 14:39:03 -04002925 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002926 {
2927 out << ", ";
2928 }
2929 }
2930
2931 out << ")";
2932 }
2933 else
2934 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002935 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002936 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002937
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002938 if (writeType)
2939 {
Jamie Madill033dae62014-06-18 12:56:28 -04002940 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002941 }
2942
Jamie Madill94bf7f22013-07-08 13:31:15 -04002943 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002944 {
2945 switch (constUnion->getType())
2946 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002947 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002948 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002949 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002950 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002951 default: UNREACHABLE();
2952 }
2953
2954 if (i != size - 1)
2955 {
2956 out << ", ";
2957 }
2958 }
2959
2960 if (writeType)
2961 {
2962 out << ")";
2963 }
2964 }
2965
2966 return constUnion;
2967}
2968
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002969void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2970{
2971 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2972 outputTriplet(visit, preString.c_str(), ", ", ")");
2973}
2974
Jamie Madill37997142015-01-28 10:06:34 -05002975bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2976{
2977 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2978 expression->traverse(&searchSymbol);
2979
2980 if (searchSymbol.foundMatch())
2981 {
2982 // Type already printed
2983 out << "t" + str(mUniqueIndex) + " = ";
2984 expression->traverse(this);
2985 out << ", ";
2986 symbolNode->traverse(this);
2987 out << " = t" + str(mUniqueIndex);
2988
2989 mUniqueIndex++;
2990 return true;
2991 }
2992
2993 return false;
2994}
2995
2996void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2997{
2998 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2999 << "\n"
3000 << "void initializeDeferredGlobals()\n"
3001 << "{\n";
3002
3003 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3004 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003005 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3006 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3007 if (binary != nullptr)
3008 {
3009 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3010 TIntermTyped *expression = binary->getRight();
3011 ASSERT(symbol);
3012 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003013
Olli Etuahod81ed842015-05-12 12:46:35 +03003014 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003015
Olli Etuahod81ed842015-05-12 12:46:35 +03003016 if (!writeSameSymbolInitializer(out, symbol, expression))
3017 {
3018 ASSERT(mInfoSinkStack.top() == &out);
3019 expression->traverse(this);
3020 }
3021 out << ";\n";
3022 }
3023 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003024 {
3025 ASSERT(mInfoSinkStack.top() == &out);
Olli Etuahod81ed842015-05-12 12:46:35 +03003026 writeSelection(selection);
Jamie Madill37997142015-01-28 10:06:34 -05003027 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003028 else
3029 {
3030 UNREACHABLE();
3031 }
Jamie Madill37997142015-01-28 10:06:34 -05003032 }
3033
3034 out << "}\n"
3035 << "\n";
3036}
3037
Jamie Madill55e79e02015-02-09 15:35:00 -05003038TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3039{
3040 const TFieldList &fields = structure.fields();
3041
3042 for (const auto &eqFunction : mStructEqualityFunctions)
3043 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003044 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003045 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003046 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003047 }
3048 }
3049
3050 const TString &structNameString = StructNameString(structure);
3051
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003052 StructEqualityFunction *function = new StructEqualityFunction();
3053 function->structure = &structure;
3054 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003055
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003056 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003057
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003058 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3059 << "{\n"
3060 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003061
3062 for (size_t i = 0; i < fields.size(); i++)
3063 {
3064 const TField *field = fields[i];
3065 const TType *fieldType = field->type();
3066
3067 const TString &fieldNameA = "a." + Decorate(field->name());
3068 const TString &fieldNameB = "b." + Decorate(field->name());
3069
3070 if (i > 0)
3071 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003072 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003073 }
3074
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003075 fnOut << "(";
3076 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3077 fnOut << fieldNameA;
3078 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3079 fnOut << fieldNameB;
3080 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3081 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003082 }
3083
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003084 fnOut << ";\n" << "}\n";
3085
3086 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003087
3088 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003089 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003090
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003091 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003092}
3093
Olli Etuaho7fb49552015-03-18 17:27:44 +02003094TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3095{
3096 for (const auto &eqFunction : mArrayEqualityFunctions)
3097 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003098 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003099 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003100 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003101 }
3102 }
3103
3104 const TString &typeName = TypeString(type);
3105
Olli Etuaho12690762015-03-31 12:55:28 +03003106 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003107 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003108
3109 TInfoSinkBase fnNameOut;
3110 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003111 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003112
3113 TType nonArrayType = type;
3114 nonArrayType.clearArrayness();
3115
3116 TInfoSinkBase fnOut;
3117
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003118 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003119 << typeName << " a[" << type.getArraySize() << "], "
3120 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003121 << "{\n"
3122 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3123 " {\n"
3124 " if (";
3125
3126 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3127 fnOut << "a[i]";
3128 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3129 fnOut << "b[i]";
3130 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3131
3132 fnOut << ") { return false; }\n"
3133 " }\n"
3134 " return true;\n"
3135 "}\n";
3136
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003137 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003138
3139 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003140 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003141
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003142 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003143}
3144
Olli Etuaho12690762015-03-31 12:55:28 +03003145TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3146{
3147 for (const auto &assignFunction : mArrayAssignmentFunctions)
3148 {
3149 if (assignFunction.type == type)
3150 {
3151 return assignFunction.functionName;
3152 }
3153 }
3154
3155 const TString &typeName = TypeString(type);
3156
3157 ArrayHelperFunction function;
3158 function.type = type;
3159
3160 TInfoSinkBase fnNameOut;
3161 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3162 function.functionName = fnNameOut.c_str();
3163
3164 TInfoSinkBase fnOut;
3165
3166 fnOut << "void " << function.functionName << "(out "
3167 << typeName << " a[" << type.getArraySize() << "], "
3168 << typeName << " b[" << type.getArraySize() << "])\n"
3169 << "{\n"
3170 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3171 " {\n"
3172 " a[i] = b[i];\n"
3173 " }\n"
3174 "}\n";
3175
3176 function.functionDefinition = fnOut.c_str();
3177
3178 mArrayAssignmentFunctions.push_back(function);
3179
3180 return function.functionName;
3181}
3182
Olli Etuaho9638c352015-04-01 14:34:52 +03003183TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3184{
3185 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3186 {
3187 if (constructIntoFunction.type == type)
3188 {
3189 return constructIntoFunction.functionName;
3190 }
3191 }
3192
3193 const TString &typeName = TypeString(type);
3194
3195 ArrayHelperFunction function;
3196 function.type = type;
3197
3198 TInfoSinkBase fnNameOut;
3199 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3200 function.functionName = fnNameOut.c_str();
3201
3202 TInfoSinkBase fnOut;
3203
3204 fnOut << "void " << function.functionName << "(out "
3205 << typeName << " a[" << type.getArraySize() << "]";
3206 for (int i = 0; i < type.getArraySize(); ++i)
3207 {
3208 fnOut << ", " << typeName << " b" << i;
3209 }
3210 fnOut << ")\n"
3211 "{\n";
3212
3213 for (int i = 0; i < type.getArraySize(); ++i)
3214 {
3215 fnOut << " a[" << i << "] = b" << i << ";\n";
3216 }
3217 fnOut << "}\n";
3218
3219 function.functionDefinition = fnOut.c_str();
3220
3221 mArrayConstructIntoFunctions.push_back(function);
3222
3223 return function.functionName;
3224}
3225
Jamie Madill2e295e22015-04-29 10:41:33 -04003226void OutputHLSL::ensureStructDefined(const TType &type)
3227{
3228 TStructure *structure = type.getStruct();
3229
3230 if (structure)
3231 {
3232 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3233 }
3234}
3235
3236
Olli Etuaho9638c352015-04-01 14:34:52 +03003237
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003238}