blob: 2c4ad3339b70b15d992cafd3c8f499d82ccb07f5 [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
Olli Etuaho18b9deb2015-11-05 12:14:50 +020038void WriteSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
39{
40 ASSERT(constUnion != nullptr);
41 switch (constUnion->getType())
42 {
43 case EbtFloat:
44 out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst()));
45 break;
46 case EbtInt:
47 out << constUnion->getIConst();
48 break;
49 case EbtUInt:
50 out << constUnion->getUConst();
51 break;
52 case EbtBool:
53 out << constUnion->getBConst();
54 break;
55 default:
56 UNREACHABLE();
57 }
58}
59
60const TConstantUnion *WriteConstantUnionArray(TInfoSinkBase &out,
61 const TConstantUnion *const constUnion,
62 const size_t size)
63{
64 const TConstantUnion *constUnionIterated = constUnion;
65 for (size_t i = 0; i < size; i++, constUnionIterated++)
66 {
67 WriteSingleConstant(out, constUnionIterated);
68
69 if (i != size - 1)
70 {
71 out << ", ";
72 }
73 }
74 return constUnionIterated;
75}
76
Olli Etuaho4785fec2015-05-18 16:09:37 +030077} // namespace
78
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079namespace sh
80{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000081
Nicolas Capense0ba27a2013-06-24 16:10:52 -040082TString OutputHLSL::TextureFunction::name() const
83{
84 TString name = "gl_texture";
85
Nicolas Capens6d232bb2013-07-08 15:56:38 -040086 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040087 {
88 name += "2D";
89 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040090 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040091 {
92 name += "3D";
93 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040094 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040095 {
96 name += "Cube";
97 }
98 else UNREACHABLE();
99
100 if (proj)
101 {
102 name += "Proj";
103 }
104
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500105 if (offset)
106 {
107 name += "Offset";
108 }
109
Nicolas Capens75fb4752013-07-10 15:14:47 -0400110 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400111 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500112 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400113 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500114 case LOD: name += "Lod"; break;
115 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400116 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500117 case SIZE: name += "Size"; break;
118 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500119 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400120 default: UNREACHABLE();
121 }
122
123 return name + "(";
124}
125
126bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
127{
128 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400129 if (sampler > rhs.sampler) return false;
130
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400131 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400132 if (coords > rhs.coords) return false;
133
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400134 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400135 if (proj && !rhs.proj) return false;
136
137 if (!offset && rhs.offset) return true;
138 if (offset && !rhs.offset) return false;
139
Nicolas Capens75fb4752013-07-10 15:14:47 -0400140 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400141 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400142
143 return false;
144}
145
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200146OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
147 const TExtensionBehavior &extensionBehavior,
148 const char *sourcePath, ShShaderOutput outputType,
149 int numRenderTargets, const std::vector<Uniform> &uniforms,
150 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400151 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200152 mShaderType(shaderType),
153 mShaderVersion(shaderVersion),
154 mExtensionBehavior(extensionBehavior),
155 mSourcePath(sourcePath),
156 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700157 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000158 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700159 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000161 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000162
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000163 mUsesFragColor = false;
164 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000165 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000166 mUsesFragCoord = false;
167 mUsesPointCoord = false;
168 mUsesFrontFacing = false;
169 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000170 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400171 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000172 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500173 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400174 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530175 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000176
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000177 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000178
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000179 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000180 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400181 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000182
183 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000184
Jamie Madill8daaba12014-06-13 10:04:33 -0400185 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200186 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400187
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000188 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000189 {
Arun Patole63419392015-03-13 11:51:07 +0530190 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
191 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
192 // In both cases total 3 uniform registers need to be reserved.
193 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000194 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000195
Jamie Madillf91ce812014-06-13 10:04:34 -0400196 // Reserve registers for the default uniform block and driver constants
197 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000198}
199
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000200OutputHLSL::~OutputHLSL()
201{
Jamie Madill8daaba12014-06-13 10:04:33 -0400202 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400203 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200204 for (auto &eqFunction : mStructEqualityFunctions)
205 {
206 SafeDelete(eqFunction);
207 }
208 for (auto &eqFunction : mArrayEqualityFunctions)
209 {
210 SafeDelete(eqFunction);
211 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000212}
213
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200214void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000215{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200216 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400217 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000218
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200219 BuiltInFunctionEmulator builtInFunctionEmulator;
220 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200221 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500222
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700223 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700224 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
225 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300226 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700227 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700228
Jamie Madill37997142015-01-28 10:06:34 -0500229 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500230 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200231 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500232 mInfoSinkStack.pop();
233
Jamie Madill37997142015-01-28 10:06:34 -0500234 mInfoSinkStack.push(&mFooter);
235 if (!mDeferredGlobalInitializers.empty())
236 {
237 writeDeferredGlobalInitializers(mFooter);
238 }
239 mInfoSinkStack.pop();
240
Jamie Madill32aab012015-01-27 14:12:26 -0500241 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200242 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500243 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000244
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200245 objSink << mHeader.c_str();
246 objSink << mBody.c_str();
247 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200248
249 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000250}
251
Jamie Madill570e04d2013-06-21 09:15:33 -0400252void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
253{
254 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
255 {
256 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
257
Jamie Madill32aab012015-01-27 14:12:26 -0500258 TInfoSinkBase structInfoSink;
259 mInfoSinkStack.push(&structInfoSink);
260
Jamie Madill570e04d2013-06-21 09:15:33 -0400261 // This will mark the necessary block elements as referenced
262 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500263
264 TString structName(structInfoSink.c_str());
265 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400266
267 mFlaggedStructOriginalNames[flaggedNode] = structName;
268
269 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
270 {
271 structName.erase(pos, 1);
272 }
273
274 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
275 }
276}
277
Jamie Madill4e1fd412014-07-10 17:50:10 -0400278const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
279{
280 return mUniformHLSL->getInterfaceBlockRegisterMap();
281}
282
Jamie Madill9fe25e92014-07-18 10:33:08 -0400283const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
284{
285 return mUniformHLSL->getUniformRegisterMap();
286}
287
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000288int OutputHLSL::vectorSize(const TType &type) const
289{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000290 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000291 int arraySize = type.isArray() ? type.getArraySize() : 1;
292
293 return elementSize * arraySize;
294}
295
Jamie Madill98493dd2013-07-08 14:39:03 -0400296TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400297{
298 TString init;
299
300 TString preIndentString;
301 TString fullIndentString;
302
303 for (int spaces = 0; spaces < (indent * 4); spaces++)
304 {
305 preIndentString += ' ';
306 }
307
308 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
309 {
310 fullIndentString += ' ';
311 }
312
313 init += preIndentString + "{\n";
314
Jamie Madill98493dd2013-07-08 14:39:03 -0400315 const TFieldList &fields = structure.fields();
316 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400317 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400318 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400319 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400320 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400321
Jamie Madill98493dd2013-07-08 14:39:03 -0400322 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400323 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400324 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400325 }
326 else
327 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400328 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400329 }
330 }
331
332 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
333
334 return init;
335}
336
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200337void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338{
Jamie Madill32aab012015-01-27 14:12:26 -0500339 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000341 TString varyings;
342 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400343 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000344
Jamie Madill829f59e2013-11-13 19:40:54 -0500345 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400346 {
347 TIntermTyped *structNode = flaggedStructIt->first;
348 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400349 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400350 const TString &originalName = mFlaggedStructOriginalNames[structNode];
351
Jamie Madill033dae62014-06-18 12:56:28 -0400352 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400353 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400354 flaggedStructs += "\n";
355 }
356
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000357 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
358 {
359 const TType &type = varying->second->getType();
360 const TString &name = varying->second->getSymbol();
361
362 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400363 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
364 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000365 }
366
367 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
368 {
369 const TType &type = attribute->second->getType();
370 const TString &name = attribute->second->getSymbol();
371
Jamie Madill033dae62014-06-18 12:56:28 -0400372 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000373 }
374
Jamie Madill8daaba12014-06-13 10:04:33 -0400375 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400376
Jamie Madillf91ce812014-06-13 10:04:34 -0400377 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
378 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
379
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200380 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500381 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200382 out << "\n// Equality functions\n\n";
383 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500384 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200385 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200386 }
387 }
Olli Etuaho12690762015-03-31 12:55:28 +0300388 if (!mArrayAssignmentFunctions.empty())
389 {
390 out << "\n// Assignment functions\n\n";
391 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
392 {
393 out << assignmentFunction.functionDefinition << "\n";
394 }
395 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300396 if (!mArrayConstructIntoFunctions.empty())
397 {
398 out << "\n// Array constructor functions\n\n";
399 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
400 {
401 out << constructIntoFunction.functionDefinition << "\n";
402 }
403 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200404
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500405 if (mUsesDiscardRewriting)
406 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400407 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500408 }
409
Nicolas Capens655fe362014-04-11 13:12:34 -0400410 if (mUsesNestedBreak)
411 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400412 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400413 }
414
Arun Patole44efa0b2015-03-04 17:11:05 +0530415 if (mRequiresIEEEStrictCompiling)
416 {
417 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
418 }
419
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400420 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
421 "#define LOOP [loop]\n"
422 "#define FLATTEN [flatten]\n"
423 "#else\n"
424 "#define LOOP\n"
425 "#define FLATTEN\n"
426 "#endif\n";
427
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200428 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200430 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
431 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000432
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000433 out << "// Varyings\n";
434 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400435 out << "\n";
436
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200437 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000438 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500439 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000440 {
Jamie Madill46131a32013-06-20 11:55:50 -0400441 const TString &variableName = outputVariableIt->first;
442 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400443
Jamie Madill033dae62014-06-18 12:56:28 -0400444 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400445 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000446 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000447 }
Jamie Madill46131a32013-06-20 11:55:50 -0400448 else
449 {
450 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
451
452 out << "static float4 gl_Color[" << numColorValues << "] =\n"
453 "{\n";
454 for (unsigned int i = 0; i < numColorValues; i++)
455 {
456 out << " float4(0, 0, 0, 0)";
457 if (i + 1 != numColorValues)
458 {
459 out << ",";
460 }
461 out << "\n";
462 }
463
464 out << "};\n";
465 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000466
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400467 if (mUsesFragDepth)
468 {
469 out << "static float gl_Depth = 0.0;\n";
470 }
471
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000472 if (mUsesFragCoord)
473 {
474 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
475 }
476
477 if (mUsesPointCoord)
478 {
479 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
480 }
481
482 if (mUsesFrontFacing)
483 {
484 out << "static bool gl_FrontFacing = false;\n";
485 }
486
487 out << "\n";
488
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000489 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000490 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000491 out << "struct gl_DepthRangeParameters\n"
492 "{\n"
493 " float near;\n"
494 " float far;\n"
495 " float diff;\n"
496 "};\n"
497 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000498 }
499
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000500 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000501 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000502 out << "cbuffer DriverConstants : register(b1)\n"
503 "{\n";
504
505 if (mUsesDepthRange)
506 {
507 out << " float3 dx_DepthRange : packoffset(c0);\n";
508 }
509
510 if (mUsesFragCoord)
511 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000512 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000513 }
514
515 if (mUsesFragCoord || mUsesFrontFacing)
516 {
517 out << " float3 dx_DepthFront : packoffset(c2);\n";
518 }
519
520 out << "};\n";
521 }
522 else
523 {
524 if (mUsesDepthRange)
525 {
526 out << "uniform float3 dx_DepthRange : register(c0);";
527 }
528
529 if (mUsesFragCoord)
530 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000531 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000532 }
533
534 if (mUsesFragCoord || mUsesFrontFacing)
535 {
536 out << "uniform float3 dx_DepthFront : register(c2);\n";
537 }
538 }
539
540 out << "\n";
541
542 if (mUsesDepthRange)
543 {
544 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
545 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000546 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000547
Jamie Madillf91ce812014-06-13 10:04:34 -0400548 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000549 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400550 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000551 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400552 out << flaggedStructs;
553 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000554 }
555
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000556 if (usingMRTExtension && mNumRenderTargets > 1)
557 {
558 out << "#define GL_USES_MRT\n";
559 }
560
561 if (mUsesFragColor)
562 {
563 out << "#define GL_USES_FRAG_COLOR\n";
564 }
565
566 if (mUsesFragData)
567 {
568 out << "#define GL_USES_FRAG_DATA\n";
569 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000571 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000573 out << "// Attributes\n";
574 out << attributes;
575 out << "\n"
576 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400577
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000578 if (mUsesPointSize)
579 {
580 out << "static float gl_PointSize = float(1);\n";
581 }
582
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000583 if (mUsesInstanceID)
584 {
585 out << "static int gl_InstanceID;";
586 }
587
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000588 out << "\n"
589 "// Varyings\n";
590 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000591 out << "\n";
592
593 if (mUsesDepthRange)
594 {
595 out << "struct gl_DepthRangeParameters\n"
596 "{\n"
597 " float near;\n"
598 " float far;\n"
599 " float diff;\n"
600 "};\n"
601 "\n";
602 }
603
604 if (mOutputType == SH_HLSL11_OUTPUT)
605 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800606 out << "cbuffer DriverConstants : register(b1)\n"
607 "{\n";
608
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000609 if (mUsesDepthRange)
610 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800611 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000612 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800613
Cooper Partine6664f02015-01-09 16:22:24 -0800614 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800615 // However, we declare it for all shaders (including Feature Level 10+).
616 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
617 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800618 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800619
620 out << "};\n"
621 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000622 }
623 else
624 {
625 if (mUsesDepthRange)
626 {
627 out << "uniform float3 dx_DepthRange : register(c0);\n";
628 }
629
Cooper Partine6664f02015-01-09 16:22:24 -0800630 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
631 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000632 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000633 }
634
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000635 if (mUsesDepthRange)
636 {
637 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
638 "\n";
639 }
640
Jamie Madillf91ce812014-06-13 10:04:34 -0400641 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000642 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400643 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000644 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400645 out << flaggedStructs;
646 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000647 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400648 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000649
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400650 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
651 {
652 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400653 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000654 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400655 switch(textureFunction->sampler)
656 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400657 case EbtSampler2D: out << "int2 "; break;
658 case EbtSampler3D: out << "int3 "; break;
659 case EbtSamplerCube: out << "int2 "; break;
660 case EbtSampler2DArray: out << "int3 "; break;
661 case EbtISampler2D: out << "int2 "; break;
662 case EbtISampler3D: out << "int3 "; break;
663 case EbtISamplerCube: out << "int2 "; break;
664 case EbtISampler2DArray: out << "int3 "; break;
665 case EbtUSampler2D: out << "int2 "; break;
666 case EbtUSampler3D: out << "int3 "; break;
667 case EbtUSamplerCube: out << "int2 "; break;
668 case EbtUSampler2DArray: out << "int3 "; break;
669 case EbtSampler2DShadow: out << "int2 "; break;
670 case EbtSamplerCubeShadow: out << "int2 "; break;
671 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400672 default: UNREACHABLE();
673 }
674 }
675 else // Sampling function
676 {
677 switch(textureFunction->sampler)
678 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400679 case EbtSampler2D: out << "float4 "; break;
680 case EbtSampler3D: out << "float4 "; break;
681 case EbtSamplerCube: out << "float4 "; break;
682 case EbtSampler2DArray: out << "float4 "; break;
683 case EbtISampler2D: out << "int4 "; break;
684 case EbtISampler3D: out << "int4 "; break;
685 case EbtISamplerCube: out << "int4 "; break;
686 case EbtISampler2DArray: out << "int4 "; break;
687 case EbtUSampler2D: out << "uint4 "; break;
688 case EbtUSampler3D: out << "uint4 "; break;
689 case EbtUSamplerCube: out << "uint4 "; break;
690 case EbtUSampler2DArray: out << "uint4 "; break;
691 case EbtSampler2DShadow: out << "float "; break;
692 case EbtSamplerCubeShadow: out << "float "; break;
693 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400694 default: UNREACHABLE();
695 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000696 }
697
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400698 // Function name
699 out << textureFunction->name();
700
701 // Argument list
702 int hlslCoords = 4;
703
704 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000705 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400706 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000707 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400708 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
709 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
710 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000711 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400712
Nicolas Capens75fb4752013-07-10 15:14:47 -0400713 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000714 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400715 case TextureFunction::IMPLICIT: break;
716 case TextureFunction::BIAS: hlslCoords = 4; break;
717 case TextureFunction::LOD: hlslCoords = 4; break;
718 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400719 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400720 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000721 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400722 }
723 else if (mOutputType == SH_HLSL11_OUTPUT)
724 {
725 switch(textureFunction->sampler)
726 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400727 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
728 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
729 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
730 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
731 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
732 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500733 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400734 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
735 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
736 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500737 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400738 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
739 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
740 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
741 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400742 default: UNREACHABLE();
743 }
744 }
745 else UNREACHABLE();
746
Nicolas Capensfc014542014-02-18 14:47:13 -0500747 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400748 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500749 switch(textureFunction->coords)
750 {
751 case 2: out << ", int2 t"; break;
752 case 3: out << ", int3 t"; break;
753 default: UNREACHABLE();
754 }
755 }
756 else // Floating-point coordinates (except textureSize)
757 {
758 switch(textureFunction->coords)
759 {
760 case 1: out << ", int lod"; break; // textureSize()
761 case 2: out << ", float2 t"; break;
762 case 3: out << ", float3 t"; break;
763 case 4: out << ", float4 t"; break;
764 default: UNREACHABLE();
765 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000766 }
767
Nicolas Capensd11d5492014-02-19 17:06:10 -0500768 if (textureFunction->method == TextureFunction::GRAD)
769 {
770 switch(textureFunction->sampler)
771 {
772 case EbtSampler2D:
773 case EbtISampler2D:
774 case EbtUSampler2D:
775 case EbtSampler2DArray:
776 case EbtISampler2DArray:
777 case EbtUSampler2DArray:
778 case EbtSampler2DShadow:
779 case EbtSampler2DArrayShadow:
780 out << ", float2 ddx, float2 ddy";
781 break;
782 case EbtSampler3D:
783 case EbtISampler3D:
784 case EbtUSampler3D:
785 case EbtSamplerCube:
786 case EbtISamplerCube:
787 case EbtUSamplerCube:
788 case EbtSamplerCubeShadow:
789 out << ", float3 ddx, float3 ddy";
790 break;
791 default: UNREACHABLE();
792 }
793 }
794
Nicolas Capens75fb4752013-07-10 15:14:47 -0400795 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000796 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400797 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400798 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400799 case TextureFunction::LOD: out << ", float lod"; break;
800 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400801 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400802 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500803 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500804 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400805 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000806 }
807
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500808 if (textureFunction->offset)
809 {
810 switch(textureFunction->sampler)
811 {
812 case EbtSampler2D: out << ", int2 offset"; break;
813 case EbtSampler3D: out << ", int3 offset"; break;
814 case EbtSampler2DArray: out << ", int2 offset"; break;
815 case EbtISampler2D: out << ", int2 offset"; break;
816 case EbtISampler3D: out << ", int3 offset"; break;
817 case EbtISampler2DArray: out << ", int2 offset"; break;
818 case EbtUSampler2D: out << ", int2 offset"; break;
819 case EbtUSampler3D: out << ", int3 offset"; break;
820 case EbtUSampler2DArray: out << ", int2 offset"; break;
821 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500822 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500823 default: UNREACHABLE();
824 }
825 }
826
Nicolas Capens84cfa122014-04-14 13:48:45 -0400827 if (textureFunction->method == TextureFunction::BIAS ||
828 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500829 {
830 out << ", float bias";
831 }
832
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400833 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400834 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400835
Nicolas Capens75fb4752013-07-10 15:14:47 -0400836 if (textureFunction->method == TextureFunction::SIZE)
837 {
838 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
839 {
840 if (IsSamplerArray(textureFunction->sampler))
841 {
842 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
843 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
844 }
845 else
846 {
847 out << " uint width; uint height; uint numberOfLevels;\n"
848 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
849 }
850 }
851 else if (IsSampler3D(textureFunction->sampler))
852 {
853 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
854 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
855 }
856 else UNREACHABLE();
857
858 switch(textureFunction->sampler)
859 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400860 case EbtSampler2D: out << " return int2(width, height);"; break;
861 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
862 case EbtSamplerCube: out << " return int2(width, height);"; break;
863 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
864 case EbtISampler2D: out << " return int2(width, height);"; break;
865 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
866 case EbtISamplerCube: out << " return int2(width, height);"; break;
867 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
868 case EbtUSampler2D: out << " return int2(width, height);"; break;
869 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
870 case EbtUSamplerCube: out << " return int2(width, height);"; break;
871 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
872 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
873 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
874 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400875 default: UNREACHABLE();
876 }
877 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400878 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400879 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500880 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
881 {
882 out << " float width; float height; float layers; float levels;\n";
883
884 out << " uint mip = 0;\n";
885
886 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
887
888 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
889 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
890 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
891 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
892
893 // FACE_POSITIVE_X = 000b
894 // FACE_NEGATIVE_X = 001b
895 // FACE_POSITIVE_Y = 010b
896 // FACE_NEGATIVE_Y = 011b
897 // FACE_POSITIVE_Z = 100b
898 // FACE_NEGATIVE_Z = 101b
899 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
900
901 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
902 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
903 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
904
905 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
906 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500907
908 // Mip level computation.
909 if (textureFunction->method == TextureFunction::IMPLICIT)
910 {
911 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
912 " float2 dx = ddx(tSized);\n"
913 " float2 dy = ddy(tSized);\n"
914 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n"
915 " mip = uint(min(max(round(lod), 0), levels - 1));\n"
916 " x.GetDimensions(mip, width, height, layers, levels);\n";
917 }
Nicolas Capens0027fa92014-02-20 14:26:42 -0500918 }
919 else if (IsIntegerSampler(textureFunction->sampler) &&
920 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400921 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400922 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400923 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400924 if (IsSamplerArray(textureFunction->sampler))
925 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400926 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400927
Nicolas Capens9edebd62013-08-06 10:59:10 -0400928 if (textureFunction->method == TextureFunction::LOD0)
929 {
930 out << " uint mip = 0;\n";
931 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400932 else if (textureFunction->method == TextureFunction::LOD0BIAS)
933 {
934 out << " uint mip = bias;\n";
935 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400936 else
937 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200938
939 out << " x.GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400940 if (textureFunction->method == TextureFunction::IMPLICIT ||
941 textureFunction->method == TextureFunction::BIAS)
942 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200943 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400944 " float dx = length(ddx(tSized));\n"
945 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500946 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400947
948 if (textureFunction->method == TextureFunction::BIAS)
949 {
950 out << " lod += bias;\n";
951 }
952 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500953 else if (textureFunction->method == TextureFunction::GRAD)
954 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200955 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500956 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400957
958 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
959 }
960
961 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400962 }
963 else
964 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400965 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400966
Nicolas Capens9edebd62013-08-06 10:59:10 -0400967 if (textureFunction->method == TextureFunction::LOD0)
968 {
969 out << " uint mip = 0;\n";
970 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400971 else if (textureFunction->method == TextureFunction::LOD0BIAS)
972 {
973 out << " uint mip = bias;\n";
974 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400975 else
976 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200977 out << " x.GetDimensions(0, width, height, levels);\n";
978
Nicolas Capens9edebd62013-08-06 10:59:10 -0400979 if (textureFunction->method == TextureFunction::IMPLICIT ||
980 textureFunction->method == TextureFunction::BIAS)
981 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200982 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400983 " float dx = length(ddx(tSized));\n"
984 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500985 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400986
987 if (textureFunction->method == TextureFunction::BIAS)
988 {
989 out << " lod += bias;\n";
990 }
991 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500992 else if (textureFunction->method == TextureFunction::GRAD)
993 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200994 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500995 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400996
997 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
998 }
999
1000 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001001 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001002 }
1003 else if (IsSampler3D(textureFunction->sampler))
1004 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001005 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001006
Nicolas Capens9edebd62013-08-06 10:59:10 -04001007 if (textureFunction->method == TextureFunction::LOD0)
1008 {
1009 out << " uint mip = 0;\n";
1010 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001011 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1012 {
1013 out << " uint mip = bias;\n";
1014 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001015 else
1016 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001017 out << " x.GetDimensions(0, width, height, depth, levels);\n";
1018
Nicolas Capens9edebd62013-08-06 10:59:10 -04001019 if (textureFunction->method == TextureFunction::IMPLICIT ||
1020 textureFunction->method == TextureFunction::BIAS)
1021 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001022 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1023 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001024 " float dx = length(ddx(tSized));\n"
1025 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001026 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001027
1028 if (textureFunction->method == TextureFunction::BIAS)
1029 {
1030 out << " lod += bias;\n";
1031 }
1032 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001033 else if (textureFunction->method == TextureFunction::GRAD)
1034 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001035 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001036 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001037
1038 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1039 }
1040
1041 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001042 }
1043 else UNREACHABLE();
1044 }
1045
1046 out << " return ";
1047
1048 // HLSL intrinsic
1049 if (mOutputType == SH_HLSL9_OUTPUT)
1050 {
1051 switch(textureFunction->sampler)
1052 {
1053 case EbtSampler2D: out << "tex2D"; break;
1054 case EbtSamplerCube: out << "texCUBE"; break;
1055 default: UNREACHABLE();
1056 }
1057
Nicolas Capens75fb4752013-07-10 15:14:47 -04001058 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001059 {
1060 case TextureFunction::IMPLICIT: out << "(s, "; break;
1061 case TextureFunction::BIAS: out << "bias(s, "; break;
1062 case TextureFunction::LOD: out << "lod(s, "; break;
1063 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001064 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001065 default: UNREACHABLE();
1066 }
1067 }
1068 else if (mOutputType == SH_HLSL11_OUTPUT)
1069 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001070 if (textureFunction->method == TextureFunction::GRAD)
1071 {
1072 if (IsIntegerSampler(textureFunction->sampler))
1073 {
1074 out << "x.Load(";
1075 }
1076 else if (IsShadowSampler(textureFunction->sampler))
1077 {
1078 out << "x.SampleCmpLevelZero(s, ";
1079 }
1080 else
1081 {
1082 out << "x.SampleGrad(s, ";
1083 }
1084 }
1085 else if (IsIntegerSampler(textureFunction->sampler) ||
1086 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001087 {
1088 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001089 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001090 else if (IsShadowSampler(textureFunction->sampler))
1091 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001092 switch(textureFunction->method)
1093 {
1094 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1095 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1096 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1097 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1098 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1099 default: UNREACHABLE();
1100 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001101 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001102 else
1103 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001104 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001105 {
1106 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1107 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1108 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1109 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001110 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001111 default: UNREACHABLE();
1112 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001113 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001114 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001115 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001116
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001117 // Integer sampling requires integer addresses
1118 TString addressx = "";
1119 TString addressy = "";
1120 TString addressz = "";
1121 TString close = "";
1122
Nicolas Capensfc014542014-02-18 14:47:13 -05001123 if (IsIntegerSampler(textureFunction->sampler) ||
1124 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001125 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001126 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001127 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001128 case 2: out << "int3("; break;
1129 case 3: out << "int4("; break;
1130 default: UNREACHABLE();
1131 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001132
Nicolas Capensfc014542014-02-18 14:47:13 -05001133 // Convert from normalized floating-point to integer
1134 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001135 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001136 addressx = "int(floor(width * frac((";
1137 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001138
Nicolas Capensfc014542014-02-18 14:47:13 -05001139 if (IsSamplerArray(textureFunction->sampler))
1140 {
1141 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1142 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001143 else if (IsSamplerCube(textureFunction->sampler))
1144 {
1145 addressz = "((((";
1146 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001147 else
1148 {
1149 addressz = "int(floor(depth * frac((";
1150 }
1151
1152 close = "))))";
1153 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001154 }
1155 else
1156 {
1157 switch(hlslCoords)
1158 {
1159 case 2: out << "float2("; break;
1160 case 3: out << "float3("; break;
1161 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001162 default: UNREACHABLE();
1163 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001164 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001165
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001166 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001167
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001168 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001169 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001170 switch(textureFunction->coords)
1171 {
1172 case 3: proj = " / t.z"; break;
1173 case 4: proj = " / t.w"; break;
1174 default: UNREACHABLE();
1175 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001176 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001177
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001178 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001179
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001180 if (mOutputType == SH_HLSL9_OUTPUT)
1181 {
1182 if (hlslCoords >= 3)
1183 {
1184 if (textureFunction->coords < 3)
1185 {
1186 out << ", 0";
1187 }
1188 else
1189 {
1190 out << ", t.z" + proj;
1191 }
1192 }
1193
1194 if (hlslCoords == 4)
1195 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001196 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001197 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001198 case TextureFunction::BIAS: out << ", bias"; break;
1199 case TextureFunction::LOD: out << ", lod"; break;
1200 case TextureFunction::LOD0: out << ", 0"; break;
1201 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001202 default: UNREACHABLE();
1203 }
1204 }
1205
1206 out << "));\n";
1207 }
1208 else if (mOutputType == SH_HLSL11_OUTPUT)
1209 {
1210 if (hlslCoords >= 3)
1211 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001212 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1213 {
1214 out << ", face";
1215 }
1216 else
1217 {
1218 out << ", " + addressz + ("t.z" + proj) + close;
1219 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001220 }
1221
Nicolas Capensd11d5492014-02-19 17:06:10 -05001222 if (textureFunction->method == TextureFunction::GRAD)
1223 {
1224 if (IsIntegerSampler(textureFunction->sampler))
1225 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001226 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001227 }
1228 else if (IsShadowSampler(textureFunction->sampler))
1229 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001230 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001231 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001232 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001233 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1234 // The resulting third component of P' in the shadow forms is used as Dref
1235 out << "), t.z" << proj;
1236 }
1237 else
1238 {
1239 switch(textureFunction->coords)
1240 {
1241 case 3: out << "), t.z"; break;
1242 case 4: out << "), t.w"; break;
1243 default: UNREACHABLE();
1244 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001245 }
1246 }
1247 else
1248 {
1249 out << "), ddx, ddy";
1250 }
1251 }
1252 else if (IsIntegerSampler(textureFunction->sampler) ||
1253 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001254 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001255 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001256 }
1257 else if (IsShadowSampler(textureFunction->sampler))
1258 {
1259 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001260 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001261 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001262 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1263 // The resulting third component of P' in the shadow forms is used as Dref
1264 out << "), t.z" << proj;
1265 }
1266 else
1267 {
1268 switch(textureFunction->coords)
1269 {
1270 case 3: out << "), t.z"; break;
1271 case 4: out << "), t.w"; break;
1272 default: UNREACHABLE();
1273 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001274 }
1275 }
1276 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001277 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001278 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001279 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001280 case TextureFunction::IMPLICIT: out << ")"; break;
1281 case TextureFunction::BIAS: out << "), bias"; break;
1282 case TextureFunction::LOD: out << "), lod"; break;
1283 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001284 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001285 default: UNREACHABLE();
1286 }
1287 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001288
1289 if (textureFunction->offset)
1290 {
1291 out << ", offset";
1292 }
1293
1294 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001295 }
1296 else UNREACHABLE();
1297 }
1298
1299 out << "\n"
1300 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001301 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001302 }
1303
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001304 if (mUsesFragCoord)
1305 {
1306 out << "#define GL_USES_FRAG_COORD\n";
1307 }
1308
1309 if (mUsesPointCoord)
1310 {
1311 out << "#define GL_USES_POINT_COORD\n";
1312 }
1313
1314 if (mUsesFrontFacing)
1315 {
1316 out << "#define GL_USES_FRONT_FACING\n";
1317 }
1318
1319 if (mUsesPointSize)
1320 {
1321 out << "#define GL_USES_POINT_SIZE\n";
1322 }
1323
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001324 if (mUsesFragDepth)
1325 {
1326 out << "#define GL_USES_FRAG_DEPTH\n";
1327 }
1328
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001329 if (mUsesDepthRange)
1330 {
1331 out << "#define GL_USES_DEPTH_RANGE\n";
1332 }
1333
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001334 if (mUsesXor)
1335 {
1336 out << "bool xor(bool p, bool q)\n"
1337 "{\n"
1338 " return (p || q) && !(p && q);\n"
1339 "}\n"
1340 "\n";
1341 }
1342
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001343 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001344}
1345
1346void OutputHLSL::visitSymbol(TIntermSymbol *node)
1347{
Jamie Madill32aab012015-01-27 14:12:26 -05001348 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001349
Jamie Madill570e04d2013-06-21 09:15:33 -04001350 // Handle accessing std140 structs by value
1351 if (mFlaggedStructMappedNames.count(node) > 0)
1352 {
1353 out << mFlaggedStructMappedNames[node];
1354 return;
1355 }
1356
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001357 TString name = node->getSymbol();
1358
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001359 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001360 {
1361 mUsesDepthRange = true;
1362 out << name;
1363 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001364 else
1365 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001366 TQualifier qualifier = node->getQualifier();
1367
1368 if (qualifier == EvqUniform)
1369 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001370 const TType &nodeType = node->getType();
1371 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001372
1373 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001374 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001375 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001376 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001377 else
1378 {
1379 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001380 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001381
Jamie Madill2e295e22015-04-29 10:41:33 -04001382 ensureStructDefined(nodeType);
1383
Jamie Madill033dae62014-06-18 12:56:28 -04001384 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001385 }
Jamie Madill19571812013-08-12 15:26:34 -07001386 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001387 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001388 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001389 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001390 }
Jamie Madill033dae62014-06-18 12:56:28 -04001391 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001392 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001393 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001394 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001395 }
Jamie Madill19571812013-08-12 15:26:34 -07001396 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001397 {
1398 mReferencedOutputVariables[name] = node;
1399 out << "out_" << name;
1400 }
1401 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001402 {
1403 out << "gl_Color[0]";
1404 mUsesFragColor = true;
1405 }
1406 else if (qualifier == EvqFragData)
1407 {
1408 out << "gl_Color";
1409 mUsesFragData = true;
1410 }
1411 else if (qualifier == EvqFragCoord)
1412 {
1413 mUsesFragCoord = true;
1414 out << name;
1415 }
1416 else if (qualifier == EvqPointCoord)
1417 {
1418 mUsesPointCoord = true;
1419 out << name;
1420 }
1421 else if (qualifier == EvqFrontFacing)
1422 {
1423 mUsesFrontFacing = true;
1424 out << name;
1425 }
1426 else if (qualifier == EvqPointSize)
1427 {
1428 mUsesPointSize = true;
1429 out << name;
1430 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001431 else if (qualifier == EvqInstanceID)
1432 {
1433 mUsesInstanceID = true;
1434 out << name;
1435 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001436 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001437 {
1438 mUsesFragDepth = true;
1439 out << "gl_Depth";
1440 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001441 else
1442 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001443 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001444 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001445 }
1446}
1447
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001448void OutputHLSL::visitRaw(TIntermRaw *node)
1449{
Jamie Madill32aab012015-01-27 14:12:26 -05001450 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001451}
1452
Olli Etuaho7fb49552015-03-18 17:27:44 +02001453void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1454{
1455 if (type.isScalar() && !type.isArray())
1456 {
1457 if (op == EOpEqual)
1458 {
1459 outputTriplet(visit, "(", " == ", ")", out);
1460 }
1461 else
1462 {
1463 outputTriplet(visit, "(", " != ", ")", out);
1464 }
1465 }
1466 else
1467 {
1468 if (visit == PreVisit && op == EOpNotEqual)
1469 {
1470 out << "!";
1471 }
1472
1473 if (type.isArray())
1474 {
1475 const TString &functionName = addArrayEqualityFunction(type);
1476 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1477 }
1478 else if (type.getBasicType() == EbtStruct)
1479 {
1480 const TStructure &structure = *type.getStruct();
1481 const TString &functionName = addStructEqualityFunction(structure);
1482 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1483 }
1484 else
1485 {
1486 ASSERT(type.isMatrix() || type.isVector());
1487 outputTriplet(visit, "all(", " == ", ")", out);
1488 }
1489 }
1490}
1491
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001492bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1493{
Jamie Madill32aab012015-01-27 14:12:26 -05001494 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001495
Jamie Madill570e04d2013-06-21 09:15:33 -04001496 // Handle accessing std140 structs by value
1497 if (mFlaggedStructMappedNames.count(node) > 0)
1498 {
1499 out << mFlaggedStructMappedNames[node];
1500 return false;
1501 }
1502
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001503 switch (node->getOp())
1504 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001505 case EOpAssign:
1506 if (node->getLeft()->isArray())
1507 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001508 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1509 if (rightAgg != nullptr && rightAgg->isConstructor())
1510 {
1511 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1512 out << functionName << "(";
1513 node->getLeft()->traverse(this);
1514 TIntermSequence *seq = rightAgg->getSequence();
1515 for (auto &arrayElement : *seq)
1516 {
1517 out << ", ";
1518 arrayElement->traverse(this);
1519 }
1520 out << ")";
1521 return false;
1522 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001523 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1524 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1525
1526 const TString &functionName = addArrayAssignmentFunction(node->getType());
1527 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001528 }
1529 else
1530 {
1531 outputTriplet(visit, "(", " = ", ")");
1532 }
1533 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001534 case EOpInitialize:
1535 if (visit == PreVisit)
1536 {
1537 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1538 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1539 // new variable is created before the assignment is evaluated), so we need to convert
1540 // this to "float t = x, x = t;".
1541
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001542 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001543 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001544 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001545
Jamie Madill37997142015-01-28 10:06:34 -05001546 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1547 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001548 {
Jamie Madill37997142015-01-28 10:06:34 -05001549 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001550 // after we initialize uniforms.
1551 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1552 deferredInit->setLeft(node->getLeft());
1553 deferredInit->setRight(node->getRight());
1554 deferredInit->setType(node->getType());
1555 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001556 const TString &initString = initializer(node->getType());
1557 node->setRight(new TIntermRaw(node->getType(), initString));
1558 }
1559 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1560 {
1561 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001562 return false;
1563 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001564 else if (writeConstantInitialization(out, symbolNode, expression))
1565 {
1566 return false;
1567 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001568 }
1569 else if (visit == InVisit)
1570 {
1571 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001572 }
1573 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001574 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1575 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1576 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1577 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1578 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1579 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001580 if (visit == PreVisit)
1581 {
1582 out << "(";
1583 }
1584 else if (visit == InVisit)
1585 {
1586 out << " = mul(";
1587 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001588 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001589 }
1590 else
1591 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001592 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001593 }
1594 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001595 case EOpMatrixTimesMatrixAssign:
1596 if (visit == PreVisit)
1597 {
1598 out << "(";
1599 }
1600 else if (visit == InVisit)
1601 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001602 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001603 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001604 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001605 }
1606 else
1607 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001608 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001609 }
1610 break;
1611 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001612 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001613 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1614 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1615 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1616 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1617 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001618 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001619 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001620 const TType& leftType = node->getLeft()->getType();
1621 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001622 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001623 if (visit == PreVisit)
1624 {
1625 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1626 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001627 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001628 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001629 return false;
1630 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001631 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001632 else
1633 {
1634 outputTriplet(visit, "", "[", "]");
1635 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001636 }
1637 break;
1638 case EOpIndexIndirect:
1639 // We do not currently support indirect references to interface blocks
1640 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1641 outputTriplet(visit, "", "[", "]");
1642 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001643 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001644 if (visit == InVisit)
1645 {
1646 const TStructure* structure = node->getLeft()->getType().getStruct();
1647 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1648 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001649 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001650
1651 return false;
1652 }
1653 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001654 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001655 if (visit == InVisit)
1656 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001657 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1658 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1659 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001660 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001661
1662 return false;
1663 }
1664 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001665 case EOpVectorSwizzle:
1666 if (visit == InVisit)
1667 {
1668 out << ".";
1669
1670 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1671
1672 if (swizzle)
1673 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001674 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001676 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677 {
1678 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1679
1680 if (element)
1681 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001682 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001683
1684 switch (i)
1685 {
1686 case 0: out << "x"; break;
1687 case 1: out << "y"; break;
1688 case 2: out << "z"; break;
1689 case 3: out << "w"; break;
1690 default: UNREACHABLE();
1691 }
1692 }
1693 else UNREACHABLE();
1694 }
1695 }
1696 else UNREACHABLE();
1697
1698 return false; // Fully processed
1699 }
1700 break;
1701 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1702 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1703 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1704 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001705 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001706 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1707 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1708 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1709 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1710 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001711 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001712 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001713 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001714 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001715 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1716 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1717 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1718 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1719 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001720 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001721 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1722 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001723 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001724 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001725 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1726 ASSERT(!node->getRight()->hasSideEffects());
1727 outputTriplet(visit, "(", " || ", ")");
1728 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001729 case EOpLogicalXor:
1730 mUsesXor = true;
1731 outputTriplet(visit, "xor(", ", ", ")");
1732 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001733 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001734 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1735 ASSERT(!node->getRight()->hasSideEffects());
1736 outputTriplet(visit, "(", " && ", ")");
1737 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738 default: UNREACHABLE();
1739 }
1740
1741 return true;
1742}
1743
1744bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1745{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746 switch (node->getOp())
1747 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001748 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001749 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001750 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1751 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001752 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001753 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1754 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1755 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1756 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001757 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1758 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1759 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1760 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1761 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1762 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1763 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1764 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001765 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1766 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1767 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1768 case EOpAsinh:
1769 ASSERT(node->getUseEmulatedFunction());
1770 writeEmulatedFunctionTriplet(visit, "asinh(");
1771 break;
1772 case EOpAcosh:
1773 ASSERT(node->getUseEmulatedFunction());
1774 writeEmulatedFunctionTriplet(visit, "acosh(");
1775 break;
1776 case EOpAtanh:
1777 ASSERT(node->getUseEmulatedFunction());
1778 writeEmulatedFunctionTriplet(visit, "atanh(");
1779 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001780 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1781 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1782 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1783 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1784 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1785 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1786 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1787 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1788 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001789 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1790 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1791 case EOpRoundEven:
1792 ASSERT(node->getUseEmulatedFunction());
1793 writeEmulatedFunctionTriplet(visit, "roundEven(");
1794 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001795 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1796 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301797 case EOpIsNan:
1798 outputTriplet(visit, "isnan(", "", ")");
1799 mRequiresIEEEStrictCompiling = true;
1800 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301801 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001802 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1803 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1804 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1805 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001806 case EOpPackSnorm2x16:
1807 ASSERT(node->getUseEmulatedFunction());
1808 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1809 break;
1810 case EOpPackUnorm2x16:
1811 ASSERT(node->getUseEmulatedFunction());
1812 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1813 break;
1814 case EOpPackHalf2x16:
1815 ASSERT(node->getUseEmulatedFunction());
1816 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1817 break;
1818 case EOpUnpackSnorm2x16:
1819 ASSERT(node->getUseEmulatedFunction());
1820 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1821 break;
1822 case EOpUnpackUnorm2x16:
1823 ASSERT(node->getUseEmulatedFunction());
1824 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1825 break;
1826 case EOpUnpackHalf2x16:
1827 ASSERT(node->getUseEmulatedFunction());
1828 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1829 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001830 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1831 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001832 case EOpDFdx:
1833 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1834 {
1835 outputTriplet(visit, "(", "", ", 0.0)");
1836 }
1837 else
1838 {
1839 outputTriplet(visit, "ddx(", "", ")");
1840 }
1841 break;
1842 case EOpDFdy:
1843 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1844 {
1845 outputTriplet(visit, "(", "", ", 0.0)");
1846 }
1847 else
1848 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001849 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001850 }
1851 break;
1852 case EOpFwidth:
1853 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1854 {
1855 outputTriplet(visit, "(", "", ", 0.0)");
1856 }
1857 else
1858 {
1859 outputTriplet(visit, "fwidth(", "", ")");
1860 }
1861 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001862 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1863 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001864 case EOpInverse:
1865 ASSERT(node->getUseEmulatedFunction());
1866 writeEmulatedFunctionTriplet(visit, "inverse(");
1867 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001868
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001869 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1870 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871 default: UNREACHABLE();
1872 }
1873
1874 return true;
1875}
1876
1877bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1878{
Jamie Madill32aab012015-01-27 14:12:26 -05001879 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881 switch (node->getOp())
1882 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001883 case EOpSequence:
1884 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001885 if (mInsideFunction)
1886 {
Jamie Madill075edd82013-07-08 13:30:19 -04001887 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001888 out << "{\n";
1889 }
1890
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001891 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001892 {
Jamie Madill075edd82013-07-08 13:30:19 -04001893 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001894
Olli Etuahoa6f22092015-05-08 18:31:10 +03001895 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001896
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001897 // Don't output ; after case labels, they're terminated by :
1898 // This is needed especially since outputting a ; after a case statement would turn empty
1899 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03001900 // Also no need to output ; after selection (if) statements or sequences. This is done just
1901 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001902 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
1903 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03001904 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001905 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001906 }
1907
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001908 if (mInsideFunction)
1909 {
Jamie Madill075edd82013-07-08 13:30:19 -04001910 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001911 out << "}\n";
1912 }
1913
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001914 return false;
1915 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001916 case EOpDeclaration:
1917 if (visit == PreVisit)
1918 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001919 TIntermSequence *sequence = node->getSequence();
1920 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001921 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001923 if (variable &&
1924 (variable->getQualifier() == EvqTemporary ||
1925 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001927 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001928
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001929 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001930 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001931 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001932 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001933 out << "static ";
1934 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001935
Olli Etuahoa6f22092015-05-08 18:31:10 +03001936 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001937
Olli Etuahoa6f22092015-05-08 18:31:10 +03001938 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001939
Olli Etuahoa6f22092015-05-08 18:31:10 +03001940 if (symbol)
1941 {
1942 symbol->traverse(this);
1943 out << ArrayString(symbol->getType());
1944 out << " = " + initializer(symbol->getType());
1945 }
1946 else
1947 {
1948 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001950 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001951 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1952 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001953 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001954 }
1955 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001956 }
Jamie Madill033dae62014-06-18 12:56:28 -04001957 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001958 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001959 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001960 {
1961 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1962
1963 if (symbol)
1964 {
1965 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1966 mReferencedVaryings[symbol->getSymbol()] = symbol;
1967 }
1968 else
1969 {
1970 (*sit)->traverse(this);
1971 }
1972 }
1973 }
1974
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001975 return false;
1976 }
1977 else if (visit == InVisit)
1978 {
1979 out << ", ";
1980 }
1981 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001982 case EOpInvariantDeclaration:
1983 // Do not do any translation
1984 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001985 case EOpPrototype:
1986 if (visit == PreVisit)
1987 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001988 size_t index = mCallDag.findIndex(node);
1989 // Skip the prototype if it is not implemented (and thus not used)
1990 if (index == CallDAG::InvalidIndex)
1991 {
1992 return false;
1993 }
1994
Olli Etuaho59f9a642015-08-06 20:38:26 +03001995 TString name = DecorateFunctionIfNeeded(node->getNameObj());
1996 out << TypeString(node->getType()) << " " << name
1997 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001998
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001999 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002000
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002001 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002002 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002003 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002004
2005 if (symbol)
2006 {
2007 out << argumentString(symbol);
2008
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002009 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002010 {
2011 out << ", ";
2012 }
2013 }
2014 else UNREACHABLE();
2015 }
2016
2017 out << ");\n";
2018
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002019 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002020 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2021 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002022 {
2023 mOutputLod0Function = true;
2024 node->traverse(this);
2025 mOutputLod0Function = false;
2026 }
2027
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002028 return false;
2029 }
2030 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002031 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032 case EOpFunction:
2033 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002034 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002035 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002036
Corentin Wallez1239ee92015-03-19 14:38:02 -07002037 size_t index = mCallDag.findIndex(node);
2038 ASSERT(index != CallDAG::InvalidIndex);
2039 mCurrentFunctionMetadata = &mASTMetadataList[index];
2040
Jamie Madill033dae62014-06-18 12:56:28 -04002041 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002042
2043 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002044 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002045 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002047 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002048 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002049 out << DecorateFunctionIfNeeded(node->getNameObj())
2050 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002051 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002052
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002053 TIntermSequence *sequence = node->getSequence();
2054 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002055
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002056 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002057 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002058 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002059
2060 if (symbol)
2061 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002062 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002063
2064 out << argumentString(symbol);
2065
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002066 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002067 {
2068 out << ", ";
2069 }
2070 }
2071 else UNREACHABLE();
2072 }
2073
Olli Etuaho4785fec2015-05-18 16:09:37 +03002074 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002075
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002076 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002077 {
2078 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002079 TIntermNode *body = (*sequence)[1];
2080 // The function body node will output braces.
2081 ASSERT(IsSequence(body));
2082 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002083 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002085 else
2086 {
2087 out << "{}\n";
2088 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002089
Corentin Wallez1239ee92015-03-19 14:38:02 -07002090 mCurrentFunctionMetadata = nullptr;
2091
2092 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2093 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002094 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002095 ASSERT(name != "main");
2096 mOutputLod0Function = true;
2097 node->traverse(this);
2098 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002099 }
2100
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002101 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 }
2103 break;
2104 case EOpFunctionCall:
2105 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002106 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002107
Corentin Wallez1239ee92015-03-19 14:38:02 -07002108 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002109 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002111 if (node->isArray())
2112 {
2113 UNIMPLEMENTED();
2114 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002115 size_t index = mCallDag.findIndex(node);
2116 ASSERT(index != CallDAG::InvalidIndex);
2117 lod0 &= mASTMetadataList[index].mNeedsLod0;
2118
Olli Etuaho59f9a642015-08-06 20:38:26 +03002119 out << DecorateFunctionIfNeeded(node->getNameObj()) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120 }
2121 else
2122 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002123 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002124 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002125
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002126 TextureFunction textureFunction;
2127 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002128 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002129 textureFunction.method = TextureFunction::IMPLICIT;
2130 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002131 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002132
2133 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002134 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002135 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002136 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002137 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002138 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002139 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002140 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002141 }
Nicolas Capens46485082014-04-15 13:12:50 -04002142 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2143 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002144 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002145 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002146 }
Nicolas Capens46485082014-04-15 13:12:50 -04002147 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002148 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002149 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002150 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002151 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002152 else if (name == "textureSize")
2153 {
2154 textureFunction.method = TextureFunction::SIZE;
2155 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002156 else if (name == "textureOffset")
2157 {
2158 textureFunction.method = TextureFunction::IMPLICIT;
2159 textureFunction.offset = true;
2160 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002161 else if (name == "textureProjOffset")
2162 {
2163 textureFunction.method = TextureFunction::IMPLICIT;
2164 textureFunction.offset = true;
2165 textureFunction.proj = true;
2166 }
2167 else if (name == "textureLodOffset")
2168 {
2169 textureFunction.method = TextureFunction::LOD;
2170 textureFunction.offset = true;
2171 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002172 else if (name == "textureProjLodOffset")
2173 {
2174 textureFunction.method = TextureFunction::LOD;
2175 textureFunction.proj = true;
2176 textureFunction.offset = true;
2177 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002178 else if (name == "texelFetch")
2179 {
2180 textureFunction.method = TextureFunction::FETCH;
2181 }
2182 else if (name == "texelFetchOffset")
2183 {
2184 textureFunction.method = TextureFunction::FETCH;
2185 textureFunction.offset = true;
2186 }
Nicolas Capens46485082014-04-15 13:12:50 -04002187 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002188 {
2189 textureFunction.method = TextureFunction::GRAD;
2190 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002191 else if (name == "textureGradOffset")
2192 {
2193 textureFunction.method = TextureFunction::GRAD;
2194 textureFunction.offset = true;
2195 }
Nicolas Capens46485082014-04-15 13:12:50 -04002196 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002197 {
2198 textureFunction.method = TextureFunction::GRAD;
2199 textureFunction.proj = true;
2200 }
2201 else if (name == "textureProjGradOffset")
2202 {
2203 textureFunction.method = TextureFunction::GRAD;
2204 textureFunction.proj = true;
2205 textureFunction.offset = true;
2206 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002207 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002208
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002209 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002210 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002211 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2212
2213 if (textureFunction.offset)
2214 {
2215 mandatoryArgumentCount++;
2216 }
2217
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002218 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002219
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002220 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002221 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002222 if (bias)
2223 {
2224 textureFunction.method = TextureFunction::LOD0BIAS;
2225 }
2226 else
2227 {
2228 textureFunction.method = TextureFunction::LOD0;
2229 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002230 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002231 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002232 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002233 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002234 }
2235 }
2236
2237 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002238
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002239 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002241
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002242 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002243 {
2244 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2245 {
2246 out << "texture_";
2247 (*arg)->traverse(this);
2248 out << ", sampler_";
2249 }
2250
2251 (*arg)->traverse(this);
2252
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002253 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002254 {
2255 out << ", ";
2256 }
2257 }
2258
2259 out << ")";
2260
2261 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 }
2263 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002264 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002265 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2266 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2267 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2268 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2269 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2270 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2271 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2272 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2273 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2274 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2275 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2276 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2277 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2278 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2279 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2280 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2281 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002282 case EOpConstructMat2x3: outputConstructor(visit, node->getType(), "mat2x3", node->getSequence()); break;
2283 case EOpConstructMat2x4: outputConstructor(visit, node->getType(), "mat2x4", node->getSequence()); break;
2284 case EOpConstructMat3x2: outputConstructor(visit, node->getType(), "mat3x2", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002285 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002286 case EOpConstructMat3x4: outputConstructor(visit, node->getType(), "mat3x4", node->getSequence()); break;
2287 case EOpConstructMat4x2: outputConstructor(visit, node->getType(), "mat4x2", node->getSequence()); break;
2288 case EOpConstructMat4x3: outputConstructor(visit, node->getType(), "mat4x3", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002289 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002290 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002291 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002292 if (node->getType().isArray())
2293 {
2294 UNIMPLEMENTED();
2295 }
Jamie Madill033dae62014-06-18 12:56:28 -04002296 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002297 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002298 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002299 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002300 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002301 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2302 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2303 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2304 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2305 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2306 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002307 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002308 ASSERT(node->getUseEmulatedFunction());
2309 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002310 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002311 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002312 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002314 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002315 ASSERT(node->getUseEmulatedFunction());
2316 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317 break;
2318 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2319 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2320 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
Arun Patoled94f6642015-05-18 16:25:12 +05302321 case EOpMix:
2322 {
2323 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2324 if (lastParamNode->getType().getBasicType() == EbtBool)
2325 {
2326 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2327 // so use emulated version.
2328 ASSERT(node->getUseEmulatedFunction());
2329 writeEmulatedFunctionTriplet(visit, "mix(");
2330 }
2331 else
2332 {
2333 outputTriplet(visit, "lerp(", ", ", ")");
2334 }
2335 }
2336 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2338 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2339 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2340 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2341 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002342 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002343 ASSERT(node->getUseEmulatedFunction());
2344 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002345 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002346 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2347 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002348 case EOpOuterProduct:
2349 ASSERT(node->getUseEmulatedFunction());
2350 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2351 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353 default: UNREACHABLE();
2354 }
2355
2356 return true;
2357}
2358
Olli Etuahod81ed842015-05-12 12:46:35 +03002359void OutputHLSL::writeSelection(TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002360{
Jamie Madill32aab012015-01-27 14:12:26 -05002361 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362
Olli Etuahoa6f22092015-05-08 18:31:10 +03002363 out << "if (";
2364
2365 node->getCondition()->traverse(this);
2366
2367 out << ")\n";
2368
2369 outputLineDirective(node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002370
2371 bool discard = false;
2372
2373 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002375 // The trueBlock child node will output braces.
2376 ASSERT(IsSequence(node->getTrueBlock()));
2377
Olli Etuahoa6f22092015-05-08 18:31:10 +03002378 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002379
Olli Etuahoa6f22092015-05-08 18:31:10 +03002380 // Detect true discard
2381 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2382 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002383 else
2384 {
2385 // TODO(oetuaho): Check if the semicolon inside is necessary.
2386 // It's there as a result of conservative refactoring of the output.
2387 out << "{;}\n";
2388 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002389
Olli Etuahoa6f22092015-05-08 18:31:10 +03002390 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002391
Olli Etuahoa6f22092015-05-08 18:31:10 +03002392 if (node->getFalseBlock())
2393 {
2394 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002395
Olli Etuahoa6f22092015-05-08 18:31:10 +03002396 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002397
Olli Etuaho4785fec2015-05-18 16:09:37 +03002398 // Either this is "else if" or the falseBlock child node will output braces.
2399 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2400
Olli Etuahoa6f22092015-05-08 18:31:10 +03002401 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002402
Olli Etuahoa6f22092015-05-08 18:31:10 +03002403 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002404
Olli Etuahoa6f22092015-05-08 18:31:10 +03002405 // Detect false discard
2406 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2407 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002408
Olli Etuahoa6f22092015-05-08 18:31:10 +03002409 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002410 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002411 {
2412 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002413 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002414}
2415
2416bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2417{
2418 TInfoSinkBase &out = getInfoSink();
2419
2420 ASSERT(!node->usesTernaryOperator());
2421
2422 if (!mInsideFunction)
2423 {
2424 // This is part of unfolded global initialization.
2425 mDeferredGlobalInitializers.push_back(node);
2426 return false;
2427 }
2428
2429 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002430 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002431 {
2432 out << "FLATTEN ";
2433 }
2434
2435 writeSelection(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436
2437 return false;
2438}
2439
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002440bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002441{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002442 if (node->getStatementList())
2443 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002444 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002445 outputTriplet(visit, "switch (", ") ", "");
2446 // The curly braces get written when visiting the statementList aggregate
2447 }
2448 else
2449 {
2450 // No statementList, so it won't output curly braces
2451 outputTriplet(visit, "switch (", ") {", "}\n");
2452 }
2453 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002454}
2455
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002456bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002457{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002458 if (node->hasCondition())
2459 {
2460 outputTriplet(visit, "case (", "", "):\n");
2461 return true;
2462 }
2463 else
2464 {
2465 TInfoSinkBase &out = getInfoSink();
2466 out << "default:\n";
2467 return false;
2468 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002469}
2470
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2472{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002473 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474}
2475
2476bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2477{
Nicolas Capens655fe362014-04-11 13:12:34 -04002478 mNestedLoopDepth++;
2479
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002480 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002481 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002482 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002483
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002484 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002485 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002486 if (handleExcessiveLoop(node))
2487 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002488 mInsideDiscontinuousLoop = wasDiscontinuous;
2489 mNestedLoopDepth--;
2490
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002491 return false;
2492 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002493 }
2494
Jamie Madill32aab012015-01-27 14:12:26 -05002495 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496
Corentin Wallez1239ee92015-03-19 14:38:02 -07002497 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002498 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002499 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002500 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002501
Jamie Madill075edd82013-07-08 13:30:19 -04002502 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002503 }
2504 else
2505 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002506 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002507
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508 if (node->getInit())
2509 {
2510 node->getInit()->traverse(this);
2511 }
2512
2513 out << "; ";
2514
alokp@chromium.org52813552010-11-16 18:36:09 +00002515 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002517 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002518 }
2519
2520 out << "; ";
2521
alokp@chromium.org52813552010-11-16 18:36:09 +00002522 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002524 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002525 }
2526
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002527 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002528
Jamie Madill075edd82013-07-08 13:30:19 -04002529 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530 }
2531
2532 if (node->getBody())
2533 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002534 // The loop body node will output braces.
2535 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002536 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002538 else
2539 {
2540 // TODO(oetuaho): Check if the semicolon inside is necessary.
2541 // It's there as a result of conservative refactoring of the output.
2542 out << "{;}\n";
2543 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544
Jamie Madill075edd82013-07-08 13:30:19 -04002545 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546
alokp@chromium.org52813552010-11-16 18:36:09 +00002547 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548 {
Jamie Madill075edd82013-07-08 13:30:19 -04002549 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550 out << "while(\n";
2551
alokp@chromium.org52813552010-11-16 18:36:09 +00002552 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553
daniel@transgaming.com73536982012-03-21 20:45:49 +00002554 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555 }
2556
daniel@transgaming.com73536982012-03-21 20:45:49 +00002557 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002559 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002560 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002561
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 return false;
2563}
2564
2565bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2566{
Jamie Madill32aab012015-01-27 14:12:26 -05002567 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002568
2569 switch (node->getFlowOp())
2570 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002571 case EOpKill:
2572 outputTriplet(visit, "discard;\n", "", "");
2573 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002574 case EOpBreak:
2575 if (visit == PreVisit)
2576 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002577 if (mNestedLoopDepth > 1)
2578 {
2579 mUsesNestedBreak = true;
2580 }
2581
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002582 if (mExcessiveLoopIndex)
2583 {
2584 out << "{Break";
2585 mExcessiveLoopIndex->traverse(this);
2586 out << " = true; break;}\n";
2587 }
2588 else
2589 {
2590 out << "break;\n";
2591 }
2592 }
2593 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002594 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595 case EOpReturn:
2596 if (visit == PreVisit)
2597 {
2598 if (node->getExpression())
2599 {
2600 out << "return ";
2601 }
2602 else
2603 {
2604 out << "return;\n";
2605 }
2606 }
2607 else if (visit == PostVisit)
2608 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002609 if (node->getExpression())
2610 {
2611 out << ";\n";
2612 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002613 }
2614 break;
2615 default: UNREACHABLE();
2616 }
2617
2618 return true;
2619}
2620
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002621bool OutputHLSL::isSingleStatement(TIntermNode *node)
2622{
2623 TIntermAggregate *aggregate = node->getAsAggregate();
2624
2625 if (aggregate)
2626 {
2627 if (aggregate->getOp() == EOpSequence)
2628 {
2629 return false;
2630 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002631 else if (aggregate->getOp() == EOpDeclaration)
2632 {
2633 // Declaring multiple comma-separated variables must be considered multiple statements
2634 // because each individual declaration has side effects which are visible in the next.
2635 return false;
2636 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002637 else
2638 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002639 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002640 {
2641 if (!isSingleStatement(*sit))
2642 {
2643 return false;
2644 }
2645 }
2646
2647 return true;
2648 }
2649 }
2650
2651 return true;
2652}
2653
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002654// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2655// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002656bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2657{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002658 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002659 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002660
2661 // Parse loops of the form:
2662 // for(int index = initial; index [comparator] limit; index += increment)
2663 TIntermSymbol *index = NULL;
2664 TOperator comparator = EOpNull;
2665 int initial = 0;
2666 int limit = 0;
2667 int increment = 0;
2668
2669 // Parse index name and intial value
2670 if (node->getInit())
2671 {
2672 TIntermAggregate *init = node->getInit()->getAsAggregate();
2673
2674 if (init)
2675 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002676 TIntermSequence *sequence = init->getSequence();
2677 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002678
2679 if (variable && variable->getQualifier() == EvqTemporary)
2680 {
2681 TIntermBinary *assign = variable->getAsBinaryNode();
2682
2683 if (assign->getOp() == EOpInitialize)
2684 {
2685 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2686 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2687
2688 if (symbol && constant)
2689 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002690 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002691 {
2692 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002693 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002694 }
2695 }
2696 }
2697 }
2698 }
2699 }
2700
2701 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002702 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002703 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002704 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002705
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002706 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2707 {
2708 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2709
2710 if (constant)
2711 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002712 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002713 {
2714 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002715 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002716 }
2717 }
2718 }
2719 }
2720
2721 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002722 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002723 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002724 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2725 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002726
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002727 if (binaryTerminal)
2728 {
2729 TOperator op = binaryTerminal->getOp();
2730 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2731
2732 if (constant)
2733 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002734 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002735 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002736 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002737
2738 switch (op)
2739 {
2740 case EOpAddAssign: increment = value; break;
2741 case EOpSubAssign: increment = -value; break;
2742 default: UNIMPLEMENTED();
2743 }
2744 }
2745 }
2746 }
2747 else if (unaryTerminal)
2748 {
2749 TOperator op = unaryTerminal->getOp();
2750
2751 switch (op)
2752 {
2753 case EOpPostIncrement: increment = 1; break;
2754 case EOpPostDecrement: increment = -1; break;
2755 case EOpPreIncrement: increment = 1; break;
2756 case EOpPreDecrement: increment = -1; break;
2757 default: UNIMPLEMENTED();
2758 }
2759 }
2760 }
2761
2762 if (index != NULL && comparator != EOpNull && increment != 0)
2763 {
2764 if (comparator == EOpLessThanEqual)
2765 {
2766 comparator = EOpLessThan;
2767 limit += 1;
2768 }
2769
2770 if (comparator == EOpLessThan)
2771 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002772 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002773
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002774 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002775 {
2776 return false; // Not an excessive loop
2777 }
2778
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002779 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2780 mExcessiveLoopIndex = index;
2781
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002782 out << "{int ";
2783 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002784 out << ";\n"
2785 "bool Break";
2786 index->traverse(this);
2787 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002788
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002789 bool firstLoopFragment = true;
2790
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002791 while (iterations > 0)
2792 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002793 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002794
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002795 if (!firstLoopFragment)
2796 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002797 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002798 index->traverse(this);
2799 out << ") {\n";
2800 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002801
2802 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2803 {
2804 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2805 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002806
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002807 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002808 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002809
Corentin Wallez1239ee92015-03-19 14:38:02 -07002810 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002811 index->traverse(this);
2812 out << " = ";
2813 out << initial;
2814
2815 out << "; ";
2816 index->traverse(this);
2817 out << " < ";
2818 out << clampedLimit;
2819
2820 out << "; ";
2821 index->traverse(this);
2822 out << " += ";
2823 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002824 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002825
Jamie Madill075edd82013-07-08 13:30:19 -04002826 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002827 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002828
2829 if (node->getBody())
2830 {
2831 node->getBody()->traverse(this);
2832 }
2833
Jamie Madill075edd82013-07-08 13:30:19 -04002834 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002835 out << ";}\n";
2836
2837 if (!firstLoopFragment)
2838 {
2839 out << "}\n";
2840 }
2841
2842 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002843
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002844 initial += MAX_LOOP_ITERATIONS * increment;
2845 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002846 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002847
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002848 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002849
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002850 mExcessiveLoopIndex = restoreIndex;
2851
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002852 return true;
2853 }
2854 else UNIMPLEMENTED();
2855 }
2856
2857 return false; // Not handled as an excessive loop
2858}
2859
Olli Etuaho7fb49552015-03-18 17:27:44 +02002860void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002861{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002862 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002863 {
2864 out << preString;
2865 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002866 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867 {
2868 out << inString;
2869 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002870 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871 {
2872 out << postString;
2873 }
2874}
2875
Olli Etuaho7fb49552015-03-18 17:27:44 +02002876void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2877{
2878 outputTriplet(visit, preString, inString, postString, getInfoSink());
2879}
2880
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002881void OutputHLSL::outputLineDirective(int line)
2882{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002883 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002884 {
Jamie Madill32aab012015-01-27 14:12:26 -05002885 TInfoSinkBase &out = getInfoSink();
2886
2887 out << "\n";
2888 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002889
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002890 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002891 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002892 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002893 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002894
Jamie Madill32aab012015-01-27 14:12:26 -05002895 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002896 }
2897}
2898
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002899TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2900{
2901 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002902 const TType &type = symbol->getType();
2903 const TName &name = symbol->getName();
2904 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002905
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002906 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002907 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002908 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002909 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002910 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002911 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002912 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002913 }
2914
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002915 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2916 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002917 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + nameStr +
2918 ArrayString(type) + ", " + QualifierString(qualifier) + " " + SamplerString(type) +
2919 " sampler_" + nameStr + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002920 }
2921
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002922 return QualifierString(qualifier) + " " + TypeString(type) + " " + nameStr + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002923}
2924
2925TString OutputHLSL::initializer(const TType &type)
2926{
2927 TString string;
2928
Jamie Madill94bf7f22013-07-08 13:31:15 -04002929 size_t size = type.getObjectSize();
2930 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002931 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002932 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002933
Jamie Madill94bf7f22013-07-08 13:31:15 -04002934 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002935 {
2936 string += ", ";
2937 }
2938 }
2939
daniel@transgaming.comead23042010-04-29 03:35:36 +00002940 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002941}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002942
Daniel Bratell29190082015-02-20 16:42:54 +01002943void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002944{
Olli Etuahof40319e2015-03-10 14:33:00 +02002945 if (type.isArray())
2946 {
2947 UNIMPLEMENTED();
2948 }
Jamie Madill32aab012015-01-27 14:12:26 -05002949 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002950
2951 if (visit == PreVisit)
2952 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002953 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002954
Daniel Bratell29190082015-02-20 16:42:54 +01002955 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002956 }
2957 else if (visit == InVisit)
2958 {
2959 out << ", ";
2960 }
2961 else if (visit == PostVisit)
2962 {
2963 out << ")";
2964 }
2965}
2966
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002967const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type,
2968 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002969{
Jamie Madill32aab012015-01-27 14:12:26 -05002970 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002971
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002972 const TConstantUnion *constUnionIterated = constUnion;
2973
Jamie Madill98493dd2013-07-08 14:39:03 -04002974 const TStructure* structure = type.getStruct();
2975 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002976 {
Jamie Madill033dae62014-06-18 12:56:28 -04002977 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002978
Jamie Madill98493dd2013-07-08 14:39:03 -04002979 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002980
Jamie Madill98493dd2013-07-08 14:39:03 -04002981 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002982 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002983 const TType *fieldType = fields[i]->type();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002984 constUnionIterated = writeConstantUnion(*fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002985
Jamie Madill98493dd2013-07-08 14:39:03 -04002986 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002987 {
2988 out << ", ";
2989 }
2990 }
2991
2992 out << ")";
2993 }
2994 else
2995 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002996 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002997 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002998
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002999 if (writeType)
3000 {
Jamie Madill033dae62014-06-18 12:56:28 -04003001 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003002 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003003 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003004 if (writeType)
3005 {
3006 out << ")";
3007 }
3008 }
3009
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003010 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003011}
3012
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003013void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
3014{
3015 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
3016 outputTriplet(visit, preString.c_str(), ", ", ")");
3017}
3018
Jamie Madill37997142015-01-28 10:06:34 -05003019bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3020{
3021 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3022 expression->traverse(&searchSymbol);
3023
3024 if (searchSymbol.foundMatch())
3025 {
3026 // Type already printed
3027 out << "t" + str(mUniqueIndex) + " = ";
3028 expression->traverse(this);
3029 out << ", ";
3030 symbolNode->traverse(this);
3031 out << " = t" + str(mUniqueIndex);
3032
3033 mUniqueIndex++;
3034 return true;
3035 }
3036
3037 return false;
3038}
3039
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003040bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3041{
3042 // We support writing constant unions and constructors that only take constant unions as
3043 // parameters as HLSL literals.
3044 if (expression->getAsConstantUnion())
3045 {
3046 return true;
3047 }
3048 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3049 !expression->getAsAggregate()->isConstructor())
3050 {
3051 return false;
3052 }
3053 TIntermAggregate *constructor = expression->getAsAggregate();
3054 for (TIntermNode *&node : *constructor->getSequence())
3055 {
3056 if (!node->getAsConstantUnion())
3057 return false;
3058 }
3059 return true;
3060}
3061
3062bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3063 TIntermSymbol *symbolNode,
3064 TIntermTyped *expression)
3065{
3066 if (canWriteAsHLSLLiteral(expression))
3067 {
3068 symbolNode->traverse(this);
3069 if (expression->getType().isArray())
3070 {
3071 out << "[" << expression->getType().getArraySize() << "]";
3072 }
3073 out << " = {";
3074 if (expression->getAsConstantUnion())
3075 {
3076 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3077 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3078 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3079 }
3080 else
3081 {
3082 TIntermAggregate *constructor = expression->getAsAggregate();
3083 ASSERT(constructor != nullptr);
3084 for (TIntermNode *&node : *constructor->getSequence())
3085 {
3086 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3087 ASSERT(nodeConst);
3088 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3089 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3090 if (node != constructor->getSequence()->back())
3091 {
3092 out << ", ";
3093 }
3094 }
3095 }
3096 out << "}";
3097 return true;
3098 }
3099 return false;
3100}
3101
Jamie Madill37997142015-01-28 10:06:34 -05003102void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3103{
3104 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3105 << "\n"
3106 << "void initializeDeferredGlobals()\n"
3107 << "{\n";
3108
3109 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3110 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003111 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3112 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3113 if (binary != nullptr)
3114 {
3115 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3116 TIntermTyped *expression = binary->getRight();
3117 ASSERT(symbol);
3118 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003119
Olli Etuahod81ed842015-05-12 12:46:35 +03003120 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003121
Olli Etuahod81ed842015-05-12 12:46:35 +03003122 if (!writeSameSymbolInitializer(out, symbol, expression))
3123 {
3124 ASSERT(mInfoSinkStack.top() == &out);
3125 expression->traverse(this);
3126 }
3127 out << ";\n";
3128 }
3129 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003130 {
3131 ASSERT(mInfoSinkStack.top() == &out);
Olli Etuahod81ed842015-05-12 12:46:35 +03003132 writeSelection(selection);
Jamie Madill37997142015-01-28 10:06:34 -05003133 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003134 else
3135 {
3136 UNREACHABLE();
3137 }
Jamie Madill37997142015-01-28 10:06:34 -05003138 }
3139
3140 out << "}\n"
3141 << "\n";
3142}
3143
Jamie Madill55e79e02015-02-09 15:35:00 -05003144TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3145{
3146 const TFieldList &fields = structure.fields();
3147
3148 for (const auto &eqFunction : mStructEqualityFunctions)
3149 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003150 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003151 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003152 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003153 }
3154 }
3155
3156 const TString &structNameString = StructNameString(structure);
3157
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003158 StructEqualityFunction *function = new StructEqualityFunction();
3159 function->structure = &structure;
3160 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003161
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003162 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003163
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003164 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3165 << "{\n"
3166 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003167
3168 for (size_t i = 0; i < fields.size(); i++)
3169 {
3170 const TField *field = fields[i];
3171 const TType *fieldType = field->type();
3172
3173 const TString &fieldNameA = "a." + Decorate(field->name());
3174 const TString &fieldNameB = "b." + Decorate(field->name());
3175
3176 if (i > 0)
3177 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003178 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003179 }
3180
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003181 fnOut << "(";
3182 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3183 fnOut << fieldNameA;
3184 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3185 fnOut << fieldNameB;
3186 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3187 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003188 }
3189
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003190 fnOut << ";\n" << "}\n";
3191
3192 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003193
3194 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003195 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003196
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003197 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003198}
3199
Olli Etuaho7fb49552015-03-18 17:27:44 +02003200TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3201{
3202 for (const auto &eqFunction : mArrayEqualityFunctions)
3203 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003204 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003205 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003206 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003207 }
3208 }
3209
3210 const TString &typeName = TypeString(type);
3211
Olli Etuaho12690762015-03-31 12:55:28 +03003212 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003213 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003214
3215 TInfoSinkBase fnNameOut;
3216 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003217 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003218
3219 TType nonArrayType = type;
3220 nonArrayType.clearArrayness();
3221
3222 TInfoSinkBase fnOut;
3223
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003224 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003225 << typeName << " a[" << type.getArraySize() << "], "
3226 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003227 << "{\n"
3228 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3229 " {\n"
3230 " if (";
3231
3232 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3233 fnOut << "a[i]";
3234 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3235 fnOut << "b[i]";
3236 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3237
3238 fnOut << ") { return false; }\n"
3239 " }\n"
3240 " return true;\n"
3241 "}\n";
3242
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003243 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003244
3245 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003246 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003247
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003248 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003249}
3250
Olli Etuaho12690762015-03-31 12:55:28 +03003251TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3252{
3253 for (const auto &assignFunction : mArrayAssignmentFunctions)
3254 {
3255 if (assignFunction.type == type)
3256 {
3257 return assignFunction.functionName;
3258 }
3259 }
3260
3261 const TString &typeName = TypeString(type);
3262
3263 ArrayHelperFunction function;
3264 function.type = type;
3265
3266 TInfoSinkBase fnNameOut;
3267 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3268 function.functionName = fnNameOut.c_str();
3269
3270 TInfoSinkBase fnOut;
3271
3272 fnOut << "void " << function.functionName << "(out "
3273 << typeName << " a[" << type.getArraySize() << "], "
3274 << typeName << " b[" << type.getArraySize() << "])\n"
3275 << "{\n"
3276 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3277 " {\n"
3278 " a[i] = b[i];\n"
3279 " }\n"
3280 "}\n";
3281
3282 function.functionDefinition = fnOut.c_str();
3283
3284 mArrayAssignmentFunctions.push_back(function);
3285
3286 return function.functionName;
3287}
3288
Olli Etuaho9638c352015-04-01 14:34:52 +03003289TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3290{
3291 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3292 {
3293 if (constructIntoFunction.type == type)
3294 {
3295 return constructIntoFunction.functionName;
3296 }
3297 }
3298
3299 const TString &typeName = TypeString(type);
3300
3301 ArrayHelperFunction function;
3302 function.type = type;
3303
3304 TInfoSinkBase fnNameOut;
3305 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3306 function.functionName = fnNameOut.c_str();
3307
3308 TInfoSinkBase fnOut;
3309
3310 fnOut << "void " << function.functionName << "(out "
3311 << typeName << " a[" << type.getArraySize() << "]";
3312 for (int i = 0; i < type.getArraySize(); ++i)
3313 {
3314 fnOut << ", " << typeName << " b" << i;
3315 }
3316 fnOut << ")\n"
3317 "{\n";
3318
3319 for (int i = 0; i < type.getArraySize(); ++i)
3320 {
3321 fnOut << " a[" << i << "] = b" << i << ";\n";
3322 }
3323 fnOut << "}\n";
3324
3325 function.functionDefinition = fnOut.c_str();
3326
3327 mArrayConstructIntoFunctions.push_back(function);
3328
3329 return function.functionName;
3330}
3331
Jamie Madill2e295e22015-04-29 10:41:33 -04003332void OutputHLSL::ensureStructDefined(const TType &type)
3333{
3334 TStructure *structure = type.getStruct();
3335
3336 if (structure)
3337 {
3338 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3339 }
3340}
3341
3342
Olli Etuaho9638c352015-04-01 14:34:52 +03003343
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003344}