blob: 7e8302f264361bfaada695a3efdbc429a7e29d80 [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";
907 }
908 else if (IsIntegerSampler(textureFunction->sampler) &&
909 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400910 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400911 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400912 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400913 if (IsSamplerArray(textureFunction->sampler))
914 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400915 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400916
Nicolas Capens9edebd62013-08-06 10:59:10 -0400917 if (textureFunction->method == TextureFunction::LOD0)
918 {
919 out << " uint mip = 0;\n";
920 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400921 else if (textureFunction->method == TextureFunction::LOD0BIAS)
922 {
923 out << " uint mip = bias;\n";
924 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400925 else
926 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200927
928 out << " x.GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400929 if (textureFunction->method == TextureFunction::IMPLICIT ||
930 textureFunction->method == TextureFunction::BIAS)
931 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200932 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400933 " float dx = length(ddx(tSized));\n"
934 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500935 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400936
937 if (textureFunction->method == TextureFunction::BIAS)
938 {
939 out << " lod += bias;\n";
940 }
941 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500942 else if (textureFunction->method == TextureFunction::GRAD)
943 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200944 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500945 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400946
947 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
948 }
949
950 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400951 }
952 else
953 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400954 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400955
Nicolas Capens9edebd62013-08-06 10:59:10 -0400956 if (textureFunction->method == TextureFunction::LOD0)
957 {
958 out << " uint mip = 0;\n";
959 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400960 else if (textureFunction->method == TextureFunction::LOD0BIAS)
961 {
962 out << " uint mip = bias;\n";
963 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400964 else
965 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200966 out << " x.GetDimensions(0, width, height, levels);\n";
967
Nicolas Capens9edebd62013-08-06 10:59:10 -0400968 if (textureFunction->method == TextureFunction::IMPLICIT ||
969 textureFunction->method == TextureFunction::BIAS)
970 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200971 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400972 " float dx = length(ddx(tSized));\n"
973 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500974 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400975
976 if (textureFunction->method == TextureFunction::BIAS)
977 {
978 out << " lod += bias;\n";
979 }
980 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500981 else if (textureFunction->method == TextureFunction::GRAD)
982 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200983 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500984 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400985
986 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
987 }
988
989 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400990 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400991 }
992 else if (IsSampler3D(textureFunction->sampler))
993 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400994 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400995
Nicolas Capens9edebd62013-08-06 10:59:10 -0400996 if (textureFunction->method == TextureFunction::LOD0)
997 {
998 out << " uint mip = 0;\n";
999 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001000 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1001 {
1002 out << " uint mip = bias;\n";
1003 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001004 else
1005 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001006 out << " x.GetDimensions(0, width, height, depth, levels);\n";
1007
Nicolas Capens9edebd62013-08-06 10:59:10 -04001008 if (textureFunction->method == TextureFunction::IMPLICIT ||
1009 textureFunction->method == TextureFunction::BIAS)
1010 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001011 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1012 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001013 " float dx = length(ddx(tSized));\n"
1014 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001015 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001016
1017 if (textureFunction->method == TextureFunction::BIAS)
1018 {
1019 out << " lod += bias;\n";
1020 }
1021 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001022 else if (textureFunction->method == TextureFunction::GRAD)
1023 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001024 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001025 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001026
1027 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1028 }
1029
1030 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001031 }
1032 else UNREACHABLE();
1033 }
1034
1035 out << " return ";
1036
1037 // HLSL intrinsic
1038 if (mOutputType == SH_HLSL9_OUTPUT)
1039 {
1040 switch(textureFunction->sampler)
1041 {
1042 case EbtSampler2D: out << "tex2D"; break;
1043 case EbtSamplerCube: out << "texCUBE"; break;
1044 default: UNREACHABLE();
1045 }
1046
Nicolas Capens75fb4752013-07-10 15:14:47 -04001047 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001048 {
1049 case TextureFunction::IMPLICIT: out << "(s, "; break;
1050 case TextureFunction::BIAS: out << "bias(s, "; break;
1051 case TextureFunction::LOD: out << "lod(s, "; break;
1052 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001053 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001054 default: UNREACHABLE();
1055 }
1056 }
1057 else if (mOutputType == SH_HLSL11_OUTPUT)
1058 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001059 if (textureFunction->method == TextureFunction::GRAD)
1060 {
1061 if (IsIntegerSampler(textureFunction->sampler))
1062 {
1063 out << "x.Load(";
1064 }
1065 else if (IsShadowSampler(textureFunction->sampler))
1066 {
1067 out << "x.SampleCmpLevelZero(s, ";
1068 }
1069 else
1070 {
1071 out << "x.SampleGrad(s, ";
1072 }
1073 }
1074 else if (IsIntegerSampler(textureFunction->sampler) ||
1075 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001076 {
1077 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001078 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001079 else if (IsShadowSampler(textureFunction->sampler))
1080 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001081 switch(textureFunction->method)
1082 {
1083 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1084 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1085 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1086 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1087 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1088 default: UNREACHABLE();
1089 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001090 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001091 else
1092 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001093 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001094 {
1095 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1096 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1097 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1098 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001099 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001100 default: UNREACHABLE();
1101 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001102 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001103 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001104 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001105
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001106 // Integer sampling requires integer addresses
1107 TString addressx = "";
1108 TString addressy = "";
1109 TString addressz = "";
1110 TString close = "";
1111
Nicolas Capensfc014542014-02-18 14:47:13 -05001112 if (IsIntegerSampler(textureFunction->sampler) ||
1113 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001114 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001115 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001116 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001117 case 2: out << "int3("; break;
1118 case 3: out << "int4("; break;
1119 default: UNREACHABLE();
1120 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001121
Nicolas Capensfc014542014-02-18 14:47:13 -05001122 // Convert from normalized floating-point to integer
1123 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001124 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001125 addressx = "int(floor(width * frac((";
1126 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001127
Nicolas Capensfc014542014-02-18 14:47:13 -05001128 if (IsSamplerArray(textureFunction->sampler))
1129 {
1130 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1131 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001132 else if (IsSamplerCube(textureFunction->sampler))
1133 {
1134 addressz = "((((";
1135 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001136 else
1137 {
1138 addressz = "int(floor(depth * frac((";
1139 }
1140
1141 close = "))))";
1142 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001143 }
1144 else
1145 {
1146 switch(hlslCoords)
1147 {
1148 case 2: out << "float2("; break;
1149 case 3: out << "float3("; break;
1150 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001151 default: UNREACHABLE();
1152 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001153 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001154
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001155 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001156
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001157 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001158 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001159 switch(textureFunction->coords)
1160 {
1161 case 3: proj = " / t.z"; break;
1162 case 4: proj = " / t.w"; break;
1163 default: UNREACHABLE();
1164 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001165 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001166
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001167 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001168
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001169 if (mOutputType == SH_HLSL9_OUTPUT)
1170 {
1171 if (hlslCoords >= 3)
1172 {
1173 if (textureFunction->coords < 3)
1174 {
1175 out << ", 0";
1176 }
1177 else
1178 {
1179 out << ", t.z" + proj;
1180 }
1181 }
1182
1183 if (hlslCoords == 4)
1184 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001185 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001186 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001187 case TextureFunction::BIAS: out << ", bias"; break;
1188 case TextureFunction::LOD: out << ", lod"; break;
1189 case TextureFunction::LOD0: out << ", 0"; break;
1190 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001191 default: UNREACHABLE();
1192 }
1193 }
1194
1195 out << "));\n";
1196 }
1197 else if (mOutputType == SH_HLSL11_OUTPUT)
1198 {
1199 if (hlslCoords >= 3)
1200 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001201 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1202 {
1203 out << ", face";
1204 }
1205 else
1206 {
1207 out << ", " + addressz + ("t.z" + proj) + close;
1208 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001209 }
1210
Nicolas Capensd11d5492014-02-19 17:06:10 -05001211 if (textureFunction->method == TextureFunction::GRAD)
1212 {
1213 if (IsIntegerSampler(textureFunction->sampler))
1214 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001215 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001216 }
1217 else if (IsShadowSampler(textureFunction->sampler))
1218 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001219 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001220 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001221 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001222 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1223 // The resulting third component of P' in the shadow forms is used as Dref
1224 out << "), t.z" << proj;
1225 }
1226 else
1227 {
1228 switch(textureFunction->coords)
1229 {
1230 case 3: out << "), t.z"; break;
1231 case 4: out << "), t.w"; break;
1232 default: UNREACHABLE();
1233 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001234 }
1235 }
1236 else
1237 {
1238 out << "), ddx, ddy";
1239 }
1240 }
1241 else if (IsIntegerSampler(textureFunction->sampler) ||
1242 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001243 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001244 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001245 }
1246 else if (IsShadowSampler(textureFunction->sampler))
1247 {
1248 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001249 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001250 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001251 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1252 // The resulting third component of P' in the shadow forms is used as Dref
1253 out << "), t.z" << proj;
1254 }
1255 else
1256 {
1257 switch(textureFunction->coords)
1258 {
1259 case 3: out << "), t.z"; break;
1260 case 4: out << "), t.w"; break;
1261 default: UNREACHABLE();
1262 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001263 }
1264 }
1265 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001266 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001267 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001268 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001269 case TextureFunction::IMPLICIT: out << ")"; break;
1270 case TextureFunction::BIAS: out << "), bias"; break;
1271 case TextureFunction::LOD: out << "), lod"; break;
1272 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001273 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001274 default: UNREACHABLE();
1275 }
1276 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001277
1278 if (textureFunction->offset)
1279 {
1280 out << ", offset";
1281 }
1282
1283 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001284 }
1285 else UNREACHABLE();
1286 }
1287
1288 out << "\n"
1289 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001290 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001291 }
1292
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001293 if (mUsesFragCoord)
1294 {
1295 out << "#define GL_USES_FRAG_COORD\n";
1296 }
1297
1298 if (mUsesPointCoord)
1299 {
1300 out << "#define GL_USES_POINT_COORD\n";
1301 }
1302
1303 if (mUsesFrontFacing)
1304 {
1305 out << "#define GL_USES_FRONT_FACING\n";
1306 }
1307
1308 if (mUsesPointSize)
1309 {
1310 out << "#define GL_USES_POINT_SIZE\n";
1311 }
1312
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001313 if (mUsesFragDepth)
1314 {
1315 out << "#define GL_USES_FRAG_DEPTH\n";
1316 }
1317
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001318 if (mUsesDepthRange)
1319 {
1320 out << "#define GL_USES_DEPTH_RANGE\n";
1321 }
1322
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001323 if (mUsesXor)
1324 {
1325 out << "bool xor(bool p, bool q)\n"
1326 "{\n"
1327 " return (p || q) && !(p && q);\n"
1328 "}\n"
1329 "\n";
1330 }
1331
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001332 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001333}
1334
1335void OutputHLSL::visitSymbol(TIntermSymbol *node)
1336{
Jamie Madill32aab012015-01-27 14:12:26 -05001337 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001338
Jamie Madill570e04d2013-06-21 09:15:33 -04001339 // Handle accessing std140 structs by value
1340 if (mFlaggedStructMappedNames.count(node) > 0)
1341 {
1342 out << mFlaggedStructMappedNames[node];
1343 return;
1344 }
1345
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001346 TString name = node->getSymbol();
1347
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001348 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001349 {
1350 mUsesDepthRange = true;
1351 out << name;
1352 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001353 else
1354 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001355 TQualifier qualifier = node->getQualifier();
1356
1357 if (qualifier == EvqUniform)
1358 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001359 const TType &nodeType = node->getType();
1360 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001361
1362 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001363 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001364 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001365 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001366 else
1367 {
1368 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001369 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001370
Jamie Madill2e295e22015-04-29 10:41:33 -04001371 ensureStructDefined(nodeType);
1372
Jamie Madill033dae62014-06-18 12:56:28 -04001373 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001374 }
Jamie Madill19571812013-08-12 15:26:34 -07001375 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001376 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001377 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001378 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001379 }
Jamie Madill033dae62014-06-18 12:56:28 -04001380 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001381 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001382 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001383 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001384 }
Jamie Madill19571812013-08-12 15:26:34 -07001385 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001386 {
1387 mReferencedOutputVariables[name] = node;
1388 out << "out_" << name;
1389 }
1390 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001391 {
1392 out << "gl_Color[0]";
1393 mUsesFragColor = true;
1394 }
1395 else if (qualifier == EvqFragData)
1396 {
1397 out << "gl_Color";
1398 mUsesFragData = true;
1399 }
1400 else if (qualifier == EvqFragCoord)
1401 {
1402 mUsesFragCoord = true;
1403 out << name;
1404 }
1405 else if (qualifier == EvqPointCoord)
1406 {
1407 mUsesPointCoord = true;
1408 out << name;
1409 }
1410 else if (qualifier == EvqFrontFacing)
1411 {
1412 mUsesFrontFacing = true;
1413 out << name;
1414 }
1415 else if (qualifier == EvqPointSize)
1416 {
1417 mUsesPointSize = true;
1418 out << name;
1419 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001420 else if (qualifier == EvqInstanceID)
1421 {
1422 mUsesInstanceID = true;
1423 out << name;
1424 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001425 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001426 {
1427 mUsesFragDepth = true;
1428 out << "gl_Depth";
1429 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001430 else
1431 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001432 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001433 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001434 }
1435}
1436
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001437void OutputHLSL::visitRaw(TIntermRaw *node)
1438{
Jamie Madill32aab012015-01-27 14:12:26 -05001439 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001440}
1441
Olli Etuaho7fb49552015-03-18 17:27:44 +02001442void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1443{
1444 if (type.isScalar() && !type.isArray())
1445 {
1446 if (op == EOpEqual)
1447 {
1448 outputTriplet(visit, "(", " == ", ")", out);
1449 }
1450 else
1451 {
1452 outputTriplet(visit, "(", " != ", ")", out);
1453 }
1454 }
1455 else
1456 {
1457 if (visit == PreVisit && op == EOpNotEqual)
1458 {
1459 out << "!";
1460 }
1461
1462 if (type.isArray())
1463 {
1464 const TString &functionName = addArrayEqualityFunction(type);
1465 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1466 }
1467 else if (type.getBasicType() == EbtStruct)
1468 {
1469 const TStructure &structure = *type.getStruct();
1470 const TString &functionName = addStructEqualityFunction(structure);
1471 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1472 }
1473 else
1474 {
1475 ASSERT(type.isMatrix() || type.isVector());
1476 outputTriplet(visit, "all(", " == ", ")", out);
1477 }
1478 }
1479}
1480
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001481bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1482{
Jamie Madill32aab012015-01-27 14:12:26 -05001483 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001484
Jamie Madill570e04d2013-06-21 09:15:33 -04001485 // Handle accessing std140 structs by value
1486 if (mFlaggedStructMappedNames.count(node) > 0)
1487 {
1488 out << mFlaggedStructMappedNames[node];
1489 return false;
1490 }
1491
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001492 switch (node->getOp())
1493 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001494 case EOpAssign:
1495 if (node->getLeft()->isArray())
1496 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001497 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1498 if (rightAgg != nullptr && rightAgg->isConstructor())
1499 {
1500 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1501 out << functionName << "(";
1502 node->getLeft()->traverse(this);
1503 TIntermSequence *seq = rightAgg->getSequence();
1504 for (auto &arrayElement : *seq)
1505 {
1506 out << ", ";
1507 arrayElement->traverse(this);
1508 }
1509 out << ")";
1510 return false;
1511 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001512 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1513 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1514
1515 const TString &functionName = addArrayAssignmentFunction(node->getType());
1516 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001517 }
1518 else
1519 {
1520 outputTriplet(visit, "(", " = ", ")");
1521 }
1522 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001523 case EOpInitialize:
1524 if (visit == PreVisit)
1525 {
1526 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1527 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1528 // new variable is created before the assignment is evaluated), so we need to convert
1529 // this to "float t = x, x = t;".
1530
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001531 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001532 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001533 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001534
Jamie Madill37997142015-01-28 10:06:34 -05001535 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1536 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001537 {
Jamie Madill37997142015-01-28 10:06:34 -05001538 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001539 // after we initialize uniforms.
1540 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1541 deferredInit->setLeft(node->getLeft());
1542 deferredInit->setRight(node->getRight());
1543 deferredInit->setType(node->getType());
1544 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001545 const TString &initString = initializer(node->getType());
1546 node->setRight(new TIntermRaw(node->getType(), initString));
1547 }
1548 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1549 {
1550 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001551 return false;
1552 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001553 else if (writeConstantInitialization(out, symbolNode, expression))
1554 {
1555 return false;
1556 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001557 }
1558 else if (visit == InVisit)
1559 {
1560 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001561 }
1562 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001563 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1564 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1565 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1566 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1567 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1568 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001569 if (visit == PreVisit)
1570 {
1571 out << "(";
1572 }
1573 else if (visit == InVisit)
1574 {
1575 out << " = mul(";
1576 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001577 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001578 }
1579 else
1580 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001581 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001582 }
1583 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001584 case EOpMatrixTimesMatrixAssign:
1585 if (visit == PreVisit)
1586 {
1587 out << "(";
1588 }
1589 else if (visit == InVisit)
1590 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001591 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001592 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001593 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001594 }
1595 else
1596 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001597 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001598 }
1599 break;
1600 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001601 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001602 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1603 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1604 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1605 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1606 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001607 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001608 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001609 const TType& leftType = node->getLeft()->getType();
1610 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001611 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001612 if (visit == PreVisit)
1613 {
1614 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1615 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001616 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001617 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001618 return false;
1619 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001620 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001621 else
1622 {
1623 outputTriplet(visit, "", "[", "]");
1624 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001625 }
1626 break;
1627 case EOpIndexIndirect:
1628 // We do not currently support indirect references to interface blocks
1629 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1630 outputTriplet(visit, "", "[", "]");
1631 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001632 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001633 if (visit == InVisit)
1634 {
1635 const TStructure* structure = node->getLeft()->getType().getStruct();
1636 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1637 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001638 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001639
1640 return false;
1641 }
1642 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001643 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001644 if (visit == InVisit)
1645 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001646 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1647 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1648 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001649 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001650
1651 return false;
1652 }
1653 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001654 case EOpVectorSwizzle:
1655 if (visit == InVisit)
1656 {
1657 out << ".";
1658
1659 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1660
1661 if (swizzle)
1662 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001663 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001665 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666 {
1667 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1668
1669 if (element)
1670 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001671 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672
1673 switch (i)
1674 {
1675 case 0: out << "x"; break;
1676 case 1: out << "y"; break;
1677 case 2: out << "z"; break;
1678 case 3: out << "w"; break;
1679 default: UNREACHABLE();
1680 }
1681 }
1682 else UNREACHABLE();
1683 }
1684 }
1685 else UNREACHABLE();
1686
1687 return false; // Fully processed
1688 }
1689 break;
1690 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1691 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1692 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1693 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001694 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001695 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1696 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1697 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1698 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1699 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001700 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001701 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001702 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001703 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1705 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1706 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1707 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1708 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001709 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001710 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1711 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001712 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001713 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001714 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1715 ASSERT(!node->getRight()->hasSideEffects());
1716 outputTriplet(visit, "(", " || ", ")");
1717 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001718 case EOpLogicalXor:
1719 mUsesXor = true;
1720 outputTriplet(visit, "xor(", ", ", ")");
1721 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001722 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001723 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1724 ASSERT(!node->getRight()->hasSideEffects());
1725 outputTriplet(visit, "(", " && ", ")");
1726 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 default: UNREACHABLE();
1728 }
1729
1730 return true;
1731}
1732
1733bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1734{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 switch (node->getOp())
1736 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001737 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001738 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001739 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1740 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001741 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001742 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1743 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1744 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1745 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001746 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1747 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1748 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1749 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1750 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1751 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1752 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1753 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001754 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1755 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1756 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1757 case EOpAsinh:
1758 ASSERT(node->getUseEmulatedFunction());
1759 writeEmulatedFunctionTriplet(visit, "asinh(");
1760 break;
1761 case EOpAcosh:
1762 ASSERT(node->getUseEmulatedFunction());
1763 writeEmulatedFunctionTriplet(visit, "acosh(");
1764 break;
1765 case EOpAtanh:
1766 ASSERT(node->getUseEmulatedFunction());
1767 writeEmulatedFunctionTriplet(visit, "atanh(");
1768 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001769 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1770 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1771 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1772 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1773 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1774 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1775 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1776 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1777 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001778 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1779 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1780 case EOpRoundEven:
1781 ASSERT(node->getUseEmulatedFunction());
1782 writeEmulatedFunctionTriplet(visit, "roundEven(");
1783 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001784 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1785 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301786 case EOpIsNan:
1787 outputTriplet(visit, "isnan(", "", ")");
1788 mRequiresIEEEStrictCompiling = true;
1789 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301790 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001791 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1792 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1793 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1794 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001795 case EOpPackSnorm2x16:
1796 ASSERT(node->getUseEmulatedFunction());
1797 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1798 break;
1799 case EOpPackUnorm2x16:
1800 ASSERT(node->getUseEmulatedFunction());
1801 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1802 break;
1803 case EOpPackHalf2x16:
1804 ASSERT(node->getUseEmulatedFunction());
1805 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1806 break;
1807 case EOpUnpackSnorm2x16:
1808 ASSERT(node->getUseEmulatedFunction());
1809 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1810 break;
1811 case EOpUnpackUnorm2x16:
1812 ASSERT(node->getUseEmulatedFunction());
1813 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1814 break;
1815 case EOpUnpackHalf2x16:
1816 ASSERT(node->getUseEmulatedFunction());
1817 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1818 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001819 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1820 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001821 case EOpDFdx:
1822 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1823 {
1824 outputTriplet(visit, "(", "", ", 0.0)");
1825 }
1826 else
1827 {
1828 outputTriplet(visit, "ddx(", "", ")");
1829 }
1830 break;
1831 case EOpDFdy:
1832 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1833 {
1834 outputTriplet(visit, "(", "", ", 0.0)");
1835 }
1836 else
1837 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001838 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001839 }
1840 break;
1841 case EOpFwidth:
1842 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1843 {
1844 outputTriplet(visit, "(", "", ", 0.0)");
1845 }
1846 else
1847 {
1848 outputTriplet(visit, "fwidth(", "", ")");
1849 }
1850 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001851 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1852 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001853 case EOpInverse:
1854 ASSERT(node->getUseEmulatedFunction());
1855 writeEmulatedFunctionTriplet(visit, "inverse(");
1856 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001857
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001858 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1859 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860 default: UNREACHABLE();
1861 }
1862
1863 return true;
1864}
1865
1866bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1867{
Jamie Madill32aab012015-01-27 14:12:26 -05001868 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 switch (node->getOp())
1871 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001872 case EOpSequence:
1873 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001874 if (mInsideFunction)
1875 {
Jamie Madill075edd82013-07-08 13:30:19 -04001876 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001877 out << "{\n";
1878 }
1879
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001880 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001881 {
Jamie Madill075edd82013-07-08 13:30:19 -04001882 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001883
Olli Etuahoa6f22092015-05-08 18:31:10 +03001884 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001885
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001886 // Don't output ; after case labels, they're terminated by :
1887 // This is needed especially since outputting a ; after a case statement would turn empty
1888 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03001889 // Also no need to output ; after selection (if) statements or sequences. This is done just
1890 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001891 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
1892 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03001893 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001894 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001895 }
1896
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001897 if (mInsideFunction)
1898 {
Jamie Madill075edd82013-07-08 13:30:19 -04001899 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001900 out << "}\n";
1901 }
1902
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001903 return false;
1904 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905 case EOpDeclaration:
1906 if (visit == PreVisit)
1907 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001908 TIntermSequence *sequence = node->getSequence();
1909 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001910 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001912 if (variable &&
1913 (variable->getQualifier() == EvqTemporary ||
1914 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001916 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001917
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001918 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001920 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001922 out << "static ";
1923 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001924
Olli Etuahoa6f22092015-05-08 18:31:10 +03001925 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001926
Olli Etuahoa6f22092015-05-08 18:31:10 +03001927 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001928
Olli Etuahoa6f22092015-05-08 18:31:10 +03001929 if (symbol)
1930 {
1931 symbol->traverse(this);
1932 out << ArrayString(symbol->getType());
1933 out << " = " + initializer(symbol->getType());
1934 }
1935 else
1936 {
1937 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001940 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1941 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001942 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001943 }
1944 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945 }
Jamie Madill033dae62014-06-18 12:56:28 -04001946 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001947 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001948 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001949 {
1950 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1951
1952 if (symbol)
1953 {
1954 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1955 mReferencedVaryings[symbol->getSymbol()] = symbol;
1956 }
1957 else
1958 {
1959 (*sit)->traverse(this);
1960 }
1961 }
1962 }
1963
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001964 return false;
1965 }
1966 else if (visit == InVisit)
1967 {
1968 out << ", ";
1969 }
1970 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001971 case EOpInvariantDeclaration:
1972 // Do not do any translation
1973 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001974 case EOpPrototype:
1975 if (visit == PreVisit)
1976 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001977 size_t index = mCallDag.findIndex(node);
1978 // Skip the prototype if it is not implemented (and thus not used)
1979 if (index == CallDAG::InvalidIndex)
1980 {
1981 return false;
1982 }
1983
Olli Etuaho59f9a642015-08-06 20:38:26 +03001984 TString name = DecorateFunctionIfNeeded(node->getNameObj());
1985 out << TypeString(node->getType()) << " " << name
1986 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001987
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001988 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001989
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001990 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001991 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001992 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001993
1994 if (symbol)
1995 {
1996 out << argumentString(symbol);
1997
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001998 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001999 {
2000 out << ", ";
2001 }
2002 }
2003 else UNREACHABLE();
2004 }
2005
2006 out << ");\n";
2007
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002008 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002009 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2010 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002011 {
2012 mOutputLod0Function = true;
2013 node->traverse(this);
2014 mOutputLod0Function = false;
2015 }
2016
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002017 return false;
2018 }
2019 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002020 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002021 case EOpFunction:
2022 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002023 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002024 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025
Corentin Wallez1239ee92015-03-19 14:38:02 -07002026 size_t index = mCallDag.findIndex(node);
2027 ASSERT(index != CallDAG::InvalidIndex);
2028 mCurrentFunctionMetadata = &mASTMetadataList[index];
2029
Jamie Madill033dae62014-06-18 12:56:28 -04002030 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002031
2032 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002034 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002036 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002038 out << DecorateFunctionIfNeeded(node->getNameObj())
2039 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002040 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002041
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002042 TIntermSequence *sequence = node->getSequence();
2043 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002044
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002045 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002046 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002047 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002048
2049 if (symbol)
2050 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002051 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002052
2053 out << argumentString(symbol);
2054
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002055 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002056 {
2057 out << ", ";
2058 }
2059 }
2060 else UNREACHABLE();
2061 }
2062
Olli Etuaho4785fec2015-05-18 16:09:37 +03002063 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002064
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002065 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002066 {
2067 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002068 TIntermNode *body = (*sequence)[1];
2069 // The function body node will output braces.
2070 ASSERT(IsSequence(body));
2071 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002072 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002074 else
2075 {
2076 out << "{}\n";
2077 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002078
Corentin Wallez1239ee92015-03-19 14:38:02 -07002079 mCurrentFunctionMetadata = nullptr;
2080
2081 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2082 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002083 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002084 ASSERT(name != "main");
2085 mOutputLod0Function = true;
2086 node->traverse(this);
2087 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002088 }
2089
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002090 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002091 }
2092 break;
2093 case EOpFunctionCall:
2094 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002095 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002096
Corentin Wallez1239ee92015-03-19 14:38:02 -07002097 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002098 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002099 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002100 if (node->isArray())
2101 {
2102 UNIMPLEMENTED();
2103 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002104 size_t index = mCallDag.findIndex(node);
2105 ASSERT(index != CallDAG::InvalidIndex);
2106 lod0 &= mASTMetadataList[index].mNeedsLod0;
2107
Olli Etuaho59f9a642015-08-06 20:38:26 +03002108 out << DecorateFunctionIfNeeded(node->getNameObj()) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 }
2110 else
2111 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002112 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002113 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002114
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002115 TextureFunction textureFunction;
2116 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002117 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002118 textureFunction.method = TextureFunction::IMPLICIT;
2119 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002120 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002121
2122 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002123 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002124 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002125 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002126 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002127 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002128 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002129 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002130 }
Nicolas Capens46485082014-04-15 13:12:50 -04002131 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2132 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002133 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002134 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002135 }
Nicolas Capens46485082014-04-15 13:12:50 -04002136 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002137 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002138 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002139 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002140 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002141 else if (name == "textureSize")
2142 {
2143 textureFunction.method = TextureFunction::SIZE;
2144 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002145 else if (name == "textureOffset")
2146 {
2147 textureFunction.method = TextureFunction::IMPLICIT;
2148 textureFunction.offset = true;
2149 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002150 else if (name == "textureProjOffset")
2151 {
2152 textureFunction.method = TextureFunction::IMPLICIT;
2153 textureFunction.offset = true;
2154 textureFunction.proj = true;
2155 }
2156 else if (name == "textureLodOffset")
2157 {
2158 textureFunction.method = TextureFunction::LOD;
2159 textureFunction.offset = true;
2160 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002161 else if (name == "textureProjLodOffset")
2162 {
2163 textureFunction.method = TextureFunction::LOD;
2164 textureFunction.proj = true;
2165 textureFunction.offset = true;
2166 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002167 else if (name == "texelFetch")
2168 {
2169 textureFunction.method = TextureFunction::FETCH;
2170 }
2171 else if (name == "texelFetchOffset")
2172 {
2173 textureFunction.method = TextureFunction::FETCH;
2174 textureFunction.offset = true;
2175 }
Nicolas Capens46485082014-04-15 13:12:50 -04002176 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002177 {
2178 textureFunction.method = TextureFunction::GRAD;
2179 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002180 else if (name == "textureGradOffset")
2181 {
2182 textureFunction.method = TextureFunction::GRAD;
2183 textureFunction.offset = true;
2184 }
Nicolas Capens46485082014-04-15 13:12:50 -04002185 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002186 {
2187 textureFunction.method = TextureFunction::GRAD;
2188 textureFunction.proj = true;
2189 }
2190 else if (name == "textureProjGradOffset")
2191 {
2192 textureFunction.method = TextureFunction::GRAD;
2193 textureFunction.proj = true;
2194 textureFunction.offset = true;
2195 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002196 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002197
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002198 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002199 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002200 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2201
2202 if (textureFunction.offset)
2203 {
2204 mandatoryArgumentCount++;
2205 }
2206
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002207 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002208
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002209 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002210 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002211 if (bias)
2212 {
2213 textureFunction.method = TextureFunction::LOD0BIAS;
2214 }
2215 else
2216 {
2217 textureFunction.method = TextureFunction::LOD0;
2218 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002219 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002220 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002221 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002222 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002223 }
2224 }
2225
2226 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002227
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002228 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002229 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002230
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002231 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002232 {
2233 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2234 {
2235 out << "texture_";
2236 (*arg)->traverse(this);
2237 out << ", sampler_";
2238 }
2239
2240 (*arg)->traverse(this);
2241
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002242 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002243 {
2244 out << ", ";
2245 }
2246 }
2247
2248 out << ")";
2249
2250 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251 }
2252 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002253 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002254 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2255 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2256 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2257 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2258 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2259 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2260 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2261 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2262 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2263 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2264 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2265 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2266 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2267 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2268 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2269 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2270 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002271 case EOpConstructMat2x3: outputConstructor(visit, node->getType(), "mat2x3", node->getSequence()); break;
2272 case EOpConstructMat2x4: outputConstructor(visit, node->getType(), "mat2x4", node->getSequence()); break;
2273 case EOpConstructMat3x2: outputConstructor(visit, node->getType(), "mat3x2", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002274 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002275 case EOpConstructMat3x4: outputConstructor(visit, node->getType(), "mat3x4", node->getSequence()); break;
2276 case EOpConstructMat4x2: outputConstructor(visit, node->getType(), "mat4x2", node->getSequence()); break;
2277 case EOpConstructMat4x3: outputConstructor(visit, node->getType(), "mat4x3", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002278 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002279 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002280 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002281 if (node->getType().isArray())
2282 {
2283 UNIMPLEMENTED();
2284 }
Jamie Madill033dae62014-06-18 12:56:28 -04002285 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002286 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002287 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002288 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002289 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002290 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2291 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2292 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2293 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2294 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2295 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002296 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002297 ASSERT(node->getUseEmulatedFunction());
2298 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002299 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002300 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002301 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002303 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002304 ASSERT(node->getUseEmulatedFunction());
2305 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306 break;
2307 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2308 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2309 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
Arun Patoled94f6642015-05-18 16:25:12 +05302310 case EOpMix:
2311 {
2312 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2313 if (lastParamNode->getType().getBasicType() == EbtBool)
2314 {
2315 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2316 // so use emulated version.
2317 ASSERT(node->getUseEmulatedFunction());
2318 writeEmulatedFunctionTriplet(visit, "mix(");
2319 }
2320 else
2321 {
2322 outputTriplet(visit, "lerp(", ", ", ")");
2323 }
2324 }
2325 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2327 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2328 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2329 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2330 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002331 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002332 ASSERT(node->getUseEmulatedFunction());
2333 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002334 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2336 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002337 case EOpOuterProduct:
2338 ASSERT(node->getUseEmulatedFunction());
2339 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2340 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002342 default: UNREACHABLE();
2343 }
2344
2345 return true;
2346}
2347
Olli Etuahod81ed842015-05-12 12:46:35 +03002348void OutputHLSL::writeSelection(TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349{
Jamie Madill32aab012015-01-27 14:12:26 -05002350 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351
Olli Etuahoa6f22092015-05-08 18:31:10 +03002352 out << "if (";
2353
2354 node->getCondition()->traverse(this);
2355
2356 out << ")\n";
2357
2358 outputLineDirective(node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002359
2360 bool discard = false;
2361
2362 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002364 // The trueBlock child node will output braces.
2365 ASSERT(IsSequence(node->getTrueBlock()));
2366
Olli Etuahoa6f22092015-05-08 18:31:10 +03002367 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002368
Olli Etuahoa6f22092015-05-08 18:31:10 +03002369 // Detect true discard
2370 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2371 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002372 else
2373 {
2374 // TODO(oetuaho): Check if the semicolon inside is necessary.
2375 // It's there as a result of conservative refactoring of the output.
2376 out << "{;}\n";
2377 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002378
Olli Etuahoa6f22092015-05-08 18:31:10 +03002379 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002380
Olli Etuahoa6f22092015-05-08 18:31:10 +03002381 if (node->getFalseBlock())
2382 {
2383 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002384
Olli Etuahoa6f22092015-05-08 18:31:10 +03002385 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386
Olli Etuaho4785fec2015-05-18 16:09:37 +03002387 // Either this is "else if" or the falseBlock child node will output braces.
2388 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2389
Olli Etuahoa6f22092015-05-08 18:31:10 +03002390 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002391
Olli Etuahoa6f22092015-05-08 18:31:10 +03002392 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002393
Olli Etuahoa6f22092015-05-08 18:31:10 +03002394 // Detect false discard
2395 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2396 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002397
Olli Etuahoa6f22092015-05-08 18:31:10 +03002398 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002399 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002400 {
2401 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002402 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002403}
2404
2405bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2406{
2407 TInfoSinkBase &out = getInfoSink();
2408
2409 ASSERT(!node->usesTernaryOperator());
2410
2411 if (!mInsideFunction)
2412 {
2413 // This is part of unfolded global initialization.
2414 mDeferredGlobalInitializers.push_back(node);
2415 return false;
2416 }
2417
2418 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002419 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002420 {
2421 out << "FLATTEN ";
2422 }
2423
2424 writeSelection(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425
2426 return false;
2427}
2428
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002429bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002430{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002431 if (node->getStatementList())
2432 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002433 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002434 outputTriplet(visit, "switch (", ") ", "");
2435 // The curly braces get written when visiting the statementList aggregate
2436 }
2437 else
2438 {
2439 // No statementList, so it won't output curly braces
2440 outputTriplet(visit, "switch (", ") {", "}\n");
2441 }
2442 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002443}
2444
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002445bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002446{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002447 if (node->hasCondition())
2448 {
2449 outputTriplet(visit, "case (", "", "):\n");
2450 return true;
2451 }
2452 else
2453 {
2454 TInfoSinkBase &out = getInfoSink();
2455 out << "default:\n";
2456 return false;
2457 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002458}
2459
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2461{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002462 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463}
2464
2465bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2466{
Nicolas Capens655fe362014-04-11 13:12:34 -04002467 mNestedLoopDepth++;
2468
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002469 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002470 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002471 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002472
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002473 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002474 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002475 if (handleExcessiveLoop(node))
2476 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002477 mInsideDiscontinuousLoop = wasDiscontinuous;
2478 mNestedLoopDepth--;
2479
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002480 return false;
2481 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002482 }
2483
Jamie Madill32aab012015-01-27 14:12:26 -05002484 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485
Corentin Wallez1239ee92015-03-19 14:38:02 -07002486 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002487 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002489 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002490
Jamie Madill075edd82013-07-08 13:30:19 -04002491 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492 }
2493 else
2494 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002495 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002496
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 if (node->getInit())
2498 {
2499 node->getInit()->traverse(this);
2500 }
2501
2502 out << "; ";
2503
alokp@chromium.org52813552010-11-16 18:36:09 +00002504 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002506 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507 }
2508
2509 out << "; ";
2510
alokp@chromium.org52813552010-11-16 18:36:09 +00002511 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002513 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514 }
2515
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002516 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002517
Jamie Madill075edd82013-07-08 13:30:19 -04002518 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002519 }
2520
2521 if (node->getBody())
2522 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002523 // The loop body node will output braces.
2524 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002525 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002526 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002527 else
2528 {
2529 // TODO(oetuaho): Check if the semicolon inside is necessary.
2530 // It's there as a result of conservative refactoring of the output.
2531 out << "{;}\n";
2532 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002533
Jamie Madill075edd82013-07-08 13:30:19 -04002534 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002535
alokp@chromium.org52813552010-11-16 18:36:09 +00002536 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537 {
Jamie Madill075edd82013-07-08 13:30:19 -04002538 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 out << "while(\n";
2540
alokp@chromium.org52813552010-11-16 18:36:09 +00002541 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542
daniel@transgaming.com73536982012-03-21 20:45:49 +00002543 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 }
2545
daniel@transgaming.com73536982012-03-21 20:45:49 +00002546 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002547
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002548 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002549 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002550
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551 return false;
2552}
2553
2554bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2555{
Jamie Madill32aab012015-01-27 14:12:26 -05002556 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557
2558 switch (node->getFlowOp())
2559 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002560 case EOpKill:
2561 outputTriplet(visit, "discard;\n", "", "");
2562 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002563 case EOpBreak:
2564 if (visit == PreVisit)
2565 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002566 if (mNestedLoopDepth > 1)
2567 {
2568 mUsesNestedBreak = true;
2569 }
2570
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002571 if (mExcessiveLoopIndex)
2572 {
2573 out << "{Break";
2574 mExcessiveLoopIndex->traverse(this);
2575 out << " = true; break;}\n";
2576 }
2577 else
2578 {
2579 out << "break;\n";
2580 }
2581 }
2582 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002583 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584 case EOpReturn:
2585 if (visit == PreVisit)
2586 {
2587 if (node->getExpression())
2588 {
2589 out << "return ";
2590 }
2591 else
2592 {
2593 out << "return;\n";
2594 }
2595 }
2596 else if (visit == PostVisit)
2597 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002598 if (node->getExpression())
2599 {
2600 out << ";\n";
2601 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602 }
2603 break;
2604 default: UNREACHABLE();
2605 }
2606
2607 return true;
2608}
2609
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002610bool OutputHLSL::isSingleStatement(TIntermNode *node)
2611{
2612 TIntermAggregate *aggregate = node->getAsAggregate();
2613
2614 if (aggregate)
2615 {
2616 if (aggregate->getOp() == EOpSequence)
2617 {
2618 return false;
2619 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002620 else if (aggregate->getOp() == EOpDeclaration)
2621 {
2622 // Declaring multiple comma-separated variables must be considered multiple statements
2623 // because each individual declaration has side effects which are visible in the next.
2624 return false;
2625 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002626 else
2627 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002628 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002629 {
2630 if (!isSingleStatement(*sit))
2631 {
2632 return false;
2633 }
2634 }
2635
2636 return true;
2637 }
2638 }
2639
2640 return true;
2641}
2642
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002643// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2644// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002645bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2646{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002647 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002648 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002649
2650 // Parse loops of the form:
2651 // for(int index = initial; index [comparator] limit; index += increment)
2652 TIntermSymbol *index = NULL;
2653 TOperator comparator = EOpNull;
2654 int initial = 0;
2655 int limit = 0;
2656 int increment = 0;
2657
2658 // Parse index name and intial value
2659 if (node->getInit())
2660 {
2661 TIntermAggregate *init = node->getInit()->getAsAggregate();
2662
2663 if (init)
2664 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002665 TIntermSequence *sequence = init->getSequence();
2666 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002667
2668 if (variable && variable->getQualifier() == EvqTemporary)
2669 {
2670 TIntermBinary *assign = variable->getAsBinaryNode();
2671
2672 if (assign->getOp() == EOpInitialize)
2673 {
2674 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2675 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2676
2677 if (symbol && constant)
2678 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002679 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002680 {
2681 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002682 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002683 }
2684 }
2685 }
2686 }
2687 }
2688 }
2689
2690 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002691 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002692 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002693 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002694
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002695 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2696 {
2697 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2698
2699 if (constant)
2700 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002701 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002702 {
2703 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002704 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002705 }
2706 }
2707 }
2708 }
2709
2710 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002711 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002712 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002713 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2714 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002715
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002716 if (binaryTerminal)
2717 {
2718 TOperator op = binaryTerminal->getOp();
2719 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2720
2721 if (constant)
2722 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002723 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002724 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002725 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002726
2727 switch (op)
2728 {
2729 case EOpAddAssign: increment = value; break;
2730 case EOpSubAssign: increment = -value; break;
2731 default: UNIMPLEMENTED();
2732 }
2733 }
2734 }
2735 }
2736 else if (unaryTerminal)
2737 {
2738 TOperator op = unaryTerminal->getOp();
2739
2740 switch (op)
2741 {
2742 case EOpPostIncrement: increment = 1; break;
2743 case EOpPostDecrement: increment = -1; break;
2744 case EOpPreIncrement: increment = 1; break;
2745 case EOpPreDecrement: increment = -1; break;
2746 default: UNIMPLEMENTED();
2747 }
2748 }
2749 }
2750
2751 if (index != NULL && comparator != EOpNull && increment != 0)
2752 {
2753 if (comparator == EOpLessThanEqual)
2754 {
2755 comparator = EOpLessThan;
2756 limit += 1;
2757 }
2758
2759 if (comparator == EOpLessThan)
2760 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002761 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002762
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002763 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002764 {
2765 return false; // Not an excessive loop
2766 }
2767
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002768 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2769 mExcessiveLoopIndex = index;
2770
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002771 out << "{int ";
2772 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002773 out << ";\n"
2774 "bool Break";
2775 index->traverse(this);
2776 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002777
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002778 bool firstLoopFragment = true;
2779
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002780 while (iterations > 0)
2781 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002782 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002783
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002784 if (!firstLoopFragment)
2785 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002786 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002787 index->traverse(this);
2788 out << ") {\n";
2789 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002790
2791 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2792 {
2793 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2794 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002795
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002796 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002797 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002798
Corentin Wallez1239ee92015-03-19 14:38:02 -07002799 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002800 index->traverse(this);
2801 out << " = ";
2802 out << initial;
2803
2804 out << "; ";
2805 index->traverse(this);
2806 out << " < ";
2807 out << clampedLimit;
2808
2809 out << "; ";
2810 index->traverse(this);
2811 out << " += ";
2812 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002813 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002814
Jamie Madill075edd82013-07-08 13:30:19 -04002815 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002816 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002817
2818 if (node->getBody())
2819 {
2820 node->getBody()->traverse(this);
2821 }
2822
Jamie Madill075edd82013-07-08 13:30:19 -04002823 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002824 out << ";}\n";
2825
2826 if (!firstLoopFragment)
2827 {
2828 out << "}\n";
2829 }
2830
2831 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002832
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002833 initial += MAX_LOOP_ITERATIONS * increment;
2834 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002835 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002836
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002837 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002838
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002839 mExcessiveLoopIndex = restoreIndex;
2840
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002841 return true;
2842 }
2843 else UNIMPLEMENTED();
2844 }
2845
2846 return false; // Not handled as an excessive loop
2847}
2848
Olli Etuaho7fb49552015-03-18 17:27:44 +02002849void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002850{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002851 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002852 {
2853 out << preString;
2854 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002855 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002856 {
2857 out << inString;
2858 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002859 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002860 {
2861 out << postString;
2862 }
2863}
2864
Olli Etuaho7fb49552015-03-18 17:27:44 +02002865void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2866{
2867 outputTriplet(visit, preString, inString, postString, getInfoSink());
2868}
2869
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002870void OutputHLSL::outputLineDirective(int line)
2871{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002872 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002873 {
Jamie Madill32aab012015-01-27 14:12:26 -05002874 TInfoSinkBase &out = getInfoSink();
2875
2876 out << "\n";
2877 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002878
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002879 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002880 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002881 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002882 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002883
Jamie Madill32aab012015-01-27 14:12:26 -05002884 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002885 }
2886}
2887
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002888TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2889{
2890 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002891 const TType &type = symbol->getType();
2892 const TName &name = symbol->getName();
2893 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002894
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002895 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002896 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002897 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002898 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002899 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002900 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002901 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002902 }
2903
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002904 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2905 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002906 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + nameStr +
2907 ArrayString(type) + ", " + QualifierString(qualifier) + " " + SamplerString(type) +
2908 " sampler_" + nameStr + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002909 }
2910
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002911 return QualifierString(qualifier) + " " + TypeString(type) + " " + nameStr + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002912}
2913
2914TString OutputHLSL::initializer(const TType &type)
2915{
2916 TString string;
2917
Jamie Madill94bf7f22013-07-08 13:31:15 -04002918 size_t size = type.getObjectSize();
2919 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002920 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002921 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002922
Jamie Madill94bf7f22013-07-08 13:31:15 -04002923 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002924 {
2925 string += ", ";
2926 }
2927 }
2928
daniel@transgaming.comead23042010-04-29 03:35:36 +00002929 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002930}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002931
Daniel Bratell29190082015-02-20 16:42:54 +01002932void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002933{
Olli Etuahof40319e2015-03-10 14:33:00 +02002934 if (type.isArray())
2935 {
2936 UNIMPLEMENTED();
2937 }
Jamie Madill32aab012015-01-27 14:12:26 -05002938 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002939
2940 if (visit == PreVisit)
2941 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002942 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002943
Daniel Bratell29190082015-02-20 16:42:54 +01002944 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002945 }
2946 else if (visit == InVisit)
2947 {
2948 out << ", ";
2949 }
2950 else if (visit == PostVisit)
2951 {
2952 out << ")";
2953 }
2954}
2955
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002956const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type,
2957 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002958{
Jamie Madill32aab012015-01-27 14:12:26 -05002959 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002960
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002961 const TConstantUnion *constUnionIterated = constUnion;
2962
Jamie Madill98493dd2013-07-08 14:39:03 -04002963 const TStructure* structure = type.getStruct();
2964 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002965 {
Jamie Madill033dae62014-06-18 12:56:28 -04002966 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002967
Jamie Madill98493dd2013-07-08 14:39:03 -04002968 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002969
Jamie Madill98493dd2013-07-08 14:39:03 -04002970 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002971 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002972 const TType *fieldType = fields[i]->type();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002973 constUnionIterated = writeConstantUnion(*fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002974
Jamie Madill98493dd2013-07-08 14:39:03 -04002975 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002976 {
2977 out << ", ";
2978 }
2979 }
2980
2981 out << ")";
2982 }
2983 else
2984 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002985 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002986 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002987
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002988 if (writeType)
2989 {
Jamie Madill033dae62014-06-18 12:56:28 -04002990 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002991 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002992 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002993 if (writeType)
2994 {
2995 out << ")";
2996 }
2997 }
2998
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002999 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003000}
3001
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003002void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
3003{
3004 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
3005 outputTriplet(visit, preString.c_str(), ", ", ")");
3006}
3007
Jamie Madill37997142015-01-28 10:06:34 -05003008bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3009{
3010 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3011 expression->traverse(&searchSymbol);
3012
3013 if (searchSymbol.foundMatch())
3014 {
3015 // Type already printed
3016 out << "t" + str(mUniqueIndex) + " = ";
3017 expression->traverse(this);
3018 out << ", ";
3019 symbolNode->traverse(this);
3020 out << " = t" + str(mUniqueIndex);
3021
3022 mUniqueIndex++;
3023 return true;
3024 }
3025
3026 return false;
3027}
3028
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003029bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3030{
3031 // We support writing constant unions and constructors that only take constant unions as
3032 // parameters as HLSL literals.
3033 if (expression->getAsConstantUnion())
3034 {
3035 return true;
3036 }
3037 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3038 !expression->getAsAggregate()->isConstructor())
3039 {
3040 return false;
3041 }
3042 TIntermAggregate *constructor = expression->getAsAggregate();
3043 for (TIntermNode *&node : *constructor->getSequence())
3044 {
3045 if (!node->getAsConstantUnion())
3046 return false;
3047 }
3048 return true;
3049}
3050
3051bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3052 TIntermSymbol *symbolNode,
3053 TIntermTyped *expression)
3054{
3055 if (canWriteAsHLSLLiteral(expression))
3056 {
3057 symbolNode->traverse(this);
3058 if (expression->getType().isArray())
3059 {
3060 out << "[" << expression->getType().getArraySize() << "]";
3061 }
3062 out << " = {";
3063 if (expression->getAsConstantUnion())
3064 {
3065 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3066 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3067 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3068 }
3069 else
3070 {
3071 TIntermAggregate *constructor = expression->getAsAggregate();
3072 ASSERT(constructor != nullptr);
3073 for (TIntermNode *&node : *constructor->getSequence())
3074 {
3075 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3076 ASSERT(nodeConst);
3077 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3078 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3079 if (node != constructor->getSequence()->back())
3080 {
3081 out << ", ";
3082 }
3083 }
3084 }
3085 out << "}";
3086 return true;
3087 }
3088 return false;
3089}
3090
Jamie Madill37997142015-01-28 10:06:34 -05003091void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3092{
3093 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3094 << "\n"
3095 << "void initializeDeferredGlobals()\n"
3096 << "{\n";
3097
3098 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3099 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003100 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3101 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3102 if (binary != nullptr)
3103 {
3104 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3105 TIntermTyped *expression = binary->getRight();
3106 ASSERT(symbol);
3107 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003108
Olli Etuahod81ed842015-05-12 12:46:35 +03003109 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003110
Olli Etuahod81ed842015-05-12 12:46:35 +03003111 if (!writeSameSymbolInitializer(out, symbol, expression))
3112 {
3113 ASSERT(mInfoSinkStack.top() == &out);
3114 expression->traverse(this);
3115 }
3116 out << ";\n";
3117 }
3118 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003119 {
3120 ASSERT(mInfoSinkStack.top() == &out);
Olli Etuahod81ed842015-05-12 12:46:35 +03003121 writeSelection(selection);
Jamie Madill37997142015-01-28 10:06:34 -05003122 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003123 else
3124 {
3125 UNREACHABLE();
3126 }
Jamie Madill37997142015-01-28 10:06:34 -05003127 }
3128
3129 out << "}\n"
3130 << "\n";
3131}
3132
Jamie Madill55e79e02015-02-09 15:35:00 -05003133TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3134{
3135 const TFieldList &fields = structure.fields();
3136
3137 for (const auto &eqFunction : mStructEqualityFunctions)
3138 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003139 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003140 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003141 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003142 }
3143 }
3144
3145 const TString &structNameString = StructNameString(structure);
3146
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003147 StructEqualityFunction *function = new StructEqualityFunction();
3148 function->structure = &structure;
3149 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003150
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003151 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003152
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003153 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3154 << "{\n"
3155 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003156
3157 for (size_t i = 0; i < fields.size(); i++)
3158 {
3159 const TField *field = fields[i];
3160 const TType *fieldType = field->type();
3161
3162 const TString &fieldNameA = "a." + Decorate(field->name());
3163 const TString &fieldNameB = "b." + Decorate(field->name());
3164
3165 if (i > 0)
3166 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003167 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003168 }
3169
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003170 fnOut << "(";
3171 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3172 fnOut << fieldNameA;
3173 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3174 fnOut << fieldNameB;
3175 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3176 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003177 }
3178
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003179 fnOut << ";\n" << "}\n";
3180
3181 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003182
3183 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003184 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003185
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003186 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003187}
3188
Olli Etuaho7fb49552015-03-18 17:27:44 +02003189TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3190{
3191 for (const auto &eqFunction : mArrayEqualityFunctions)
3192 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003193 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003194 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003195 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003196 }
3197 }
3198
3199 const TString &typeName = TypeString(type);
3200
Olli Etuaho12690762015-03-31 12:55:28 +03003201 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003202 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003203
3204 TInfoSinkBase fnNameOut;
3205 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003206 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003207
3208 TType nonArrayType = type;
3209 nonArrayType.clearArrayness();
3210
3211 TInfoSinkBase fnOut;
3212
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003213 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003214 << typeName << " a[" << type.getArraySize() << "], "
3215 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003216 << "{\n"
3217 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3218 " {\n"
3219 " if (";
3220
3221 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3222 fnOut << "a[i]";
3223 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3224 fnOut << "b[i]";
3225 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3226
3227 fnOut << ") { return false; }\n"
3228 " }\n"
3229 " return true;\n"
3230 "}\n";
3231
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003232 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003233
3234 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003235 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003236
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003237 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003238}
3239
Olli Etuaho12690762015-03-31 12:55:28 +03003240TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3241{
3242 for (const auto &assignFunction : mArrayAssignmentFunctions)
3243 {
3244 if (assignFunction.type == type)
3245 {
3246 return assignFunction.functionName;
3247 }
3248 }
3249
3250 const TString &typeName = TypeString(type);
3251
3252 ArrayHelperFunction function;
3253 function.type = type;
3254
3255 TInfoSinkBase fnNameOut;
3256 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3257 function.functionName = fnNameOut.c_str();
3258
3259 TInfoSinkBase fnOut;
3260
3261 fnOut << "void " << function.functionName << "(out "
3262 << typeName << " a[" << type.getArraySize() << "], "
3263 << typeName << " b[" << type.getArraySize() << "])\n"
3264 << "{\n"
3265 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3266 " {\n"
3267 " a[i] = b[i];\n"
3268 " }\n"
3269 "}\n";
3270
3271 function.functionDefinition = fnOut.c_str();
3272
3273 mArrayAssignmentFunctions.push_back(function);
3274
3275 return function.functionName;
3276}
3277
Olli Etuaho9638c352015-04-01 14:34:52 +03003278TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3279{
3280 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3281 {
3282 if (constructIntoFunction.type == type)
3283 {
3284 return constructIntoFunction.functionName;
3285 }
3286 }
3287
3288 const TString &typeName = TypeString(type);
3289
3290 ArrayHelperFunction function;
3291 function.type = type;
3292
3293 TInfoSinkBase fnNameOut;
3294 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3295 function.functionName = fnNameOut.c_str();
3296
3297 TInfoSinkBase fnOut;
3298
3299 fnOut << "void " << function.functionName << "(out "
3300 << typeName << " a[" << type.getArraySize() << "]";
3301 for (int i = 0; i < type.getArraySize(); ++i)
3302 {
3303 fnOut << ", " << typeName << " b" << i;
3304 }
3305 fnOut << ")\n"
3306 "{\n";
3307
3308 for (int i = 0; i < type.getArraySize(); ++i)
3309 {
3310 fnOut << " a[" << i << "] = b" << i << ";\n";
3311 }
3312 fnOut << "}\n";
3313
3314 function.functionDefinition = fnOut.c_str();
3315
3316 mArrayConstructIntoFunctions.push_back(function);
3317
3318 return function.functionName;
3319}
3320
Jamie Madill2e295e22015-04-29 10:41:33 -04003321void OutputHLSL::ensureStructDefined(const TType &type)
3322{
3323 TStructure *structure = type.getStruct();
3324
3325 if (structure)
3326 {
3327 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3328 }
3329}
3330
3331
Olli Etuaho9638c352015-04-01 14:34:52 +03003332
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003333}