blob: a2309a9cf479ac8c189d7130e375035ca56345d3 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030014#include "common/debug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020016#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/FlagStd140Structs.h"
19#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/SearchSymbol.h"
23#include "compiler/translator/StructureHLSL.h"
24#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050025#include "compiler/translator/UniformHLSL.h"
26#include "compiler/translator/UtilsHLSL.h"
27#include "compiler/translator/blocklayout.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050028#include "compiler/translator/util.h"
29
Olli Etuaho4785fec2015-05-18 16:09:37 +030030namespace
31{
32
33bool IsSequence(TIntermNode *node)
34{
35 return node->getAsAggregate() != nullptr && node->getAsAggregate()->getOp() == EOpSequence;
36}
37
Olli Etuaho18b9deb2015-11-05 12:14:50 +020038void WriteSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
39{
40 ASSERT(constUnion != nullptr);
41 switch (constUnion->getType())
42 {
43 case EbtFloat:
44 out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst()));
45 break;
46 case EbtInt:
47 out << constUnion->getIConst();
48 break;
49 case EbtUInt:
50 out << constUnion->getUConst();
51 break;
52 case EbtBool:
53 out << constUnion->getBConst();
54 break;
55 default:
56 UNREACHABLE();
57 }
58}
59
60const TConstantUnion *WriteConstantUnionArray(TInfoSinkBase &out,
61 const TConstantUnion *const constUnion,
62 const size_t size)
63{
64 const TConstantUnion *constUnionIterated = constUnion;
65 for (size_t i = 0; i < size; i++, constUnionIterated++)
66 {
67 WriteSingleConstant(out, constUnionIterated);
68
69 if (i != size - 1)
70 {
71 out << ", ";
72 }
73 }
74 return constUnionIterated;
75}
76
Olli Etuaho4785fec2015-05-18 16:09:37 +030077} // namespace
78
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079namespace sh
80{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000081
Nicolas Capense0ba27a2013-06-24 16:10:52 -040082TString OutputHLSL::TextureFunction::name() const
83{
84 TString name = "gl_texture";
85
Nicolas Capens6d232bb2013-07-08 15:56:38 -040086 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040087 {
88 name += "2D";
89 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040090 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040091 {
92 name += "3D";
93 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040094 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040095 {
96 name += "Cube";
97 }
98 else UNREACHABLE();
99
100 if (proj)
101 {
102 name += "Proj";
103 }
104
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500105 if (offset)
106 {
107 name += "Offset";
108 }
109
Nicolas Capens75fb4752013-07-10 15:14:47 -0400110 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400111 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500112 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400113 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500114 case LOD: name += "Lod"; break;
115 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400116 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500117 case SIZE: name += "Size"; break;
118 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500119 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400120 default: UNREACHABLE();
121 }
122
123 return name + "(";
124}
125
126bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
127{
128 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400129 if (sampler > rhs.sampler) return false;
130
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400131 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400132 if (coords > rhs.coords) return false;
133
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400134 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400135 if (proj && !rhs.proj) return false;
136
137 if (!offset && rhs.offset) return true;
138 if (offset && !rhs.offset) return false;
139
Nicolas Capens75fb4752013-07-10 15:14:47 -0400140 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400141 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400142
143 return false;
144}
145
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200146OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
147 const TExtensionBehavior &extensionBehavior,
148 const char *sourcePath, ShShaderOutput outputType,
149 int numRenderTargets, const std::vector<Uniform> &uniforms,
150 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400151 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200152 mShaderType(shaderType),
153 mShaderVersion(shaderVersion),
154 mExtensionBehavior(extensionBehavior),
155 mSourcePath(sourcePath),
156 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700157 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000158 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700159 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000161 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000162
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000163 mUsesFragColor = false;
164 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000165 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000166 mUsesFragCoord = false;
167 mUsesPointCoord = false;
168 mUsesFrontFacing = false;
169 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000170 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400171 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000172 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500173 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400174 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530175 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000176
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000177 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000178
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000179 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000180 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400181 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000182
183 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000184
Jamie Madill8daaba12014-06-13 10:04:33 -0400185 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200186 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400187
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000188 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000189 {
Arun Patole63419392015-03-13 11:51:07 +0530190 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
191 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
192 // In both cases total 3 uniform registers need to be reserved.
193 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000194 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000195
Jamie Madillf91ce812014-06-13 10:04:34 -0400196 // Reserve registers for the default uniform block and driver constants
197 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000198}
199
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000200OutputHLSL::~OutputHLSL()
201{
Jamie Madill8daaba12014-06-13 10:04:33 -0400202 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400203 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200204 for (auto &eqFunction : mStructEqualityFunctions)
205 {
206 SafeDelete(eqFunction);
207 }
208 for (auto &eqFunction : mArrayEqualityFunctions)
209 {
210 SafeDelete(eqFunction);
211 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000212}
213
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200214void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000215{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200216 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400217 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000218
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200219 BuiltInFunctionEmulator builtInFunctionEmulator;
220 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200221 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500222
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700223 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700224 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
225 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300226 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700227 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700228
Jamie Madill37997142015-01-28 10:06:34 -0500229 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500230 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200231 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500232 mInfoSinkStack.pop();
233
Jamie Madill37997142015-01-28 10:06:34 -0500234 mInfoSinkStack.push(&mFooter);
235 if (!mDeferredGlobalInitializers.empty())
236 {
237 writeDeferredGlobalInitializers(mFooter);
238 }
239 mInfoSinkStack.pop();
240
Jamie Madill32aab012015-01-27 14:12:26 -0500241 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200242 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500243 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000244
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200245 objSink << mHeader.c_str();
246 objSink << mBody.c_str();
247 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200248
249 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000250}
251
Jamie Madill570e04d2013-06-21 09:15:33 -0400252void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
253{
254 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
255 {
256 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
257
Jamie Madill32aab012015-01-27 14:12:26 -0500258 TInfoSinkBase structInfoSink;
259 mInfoSinkStack.push(&structInfoSink);
260
Jamie Madill570e04d2013-06-21 09:15:33 -0400261 // This will mark the necessary block elements as referenced
262 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500263
264 TString structName(structInfoSink.c_str());
265 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400266
267 mFlaggedStructOriginalNames[flaggedNode] = structName;
268
269 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
270 {
271 structName.erase(pos, 1);
272 }
273
274 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
275 }
276}
277
Jamie Madill4e1fd412014-07-10 17:50:10 -0400278const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
279{
280 return mUniformHLSL->getInterfaceBlockRegisterMap();
281}
282
Jamie Madill9fe25e92014-07-18 10:33:08 -0400283const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
284{
285 return mUniformHLSL->getUniformRegisterMap();
286}
287
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000288int OutputHLSL::vectorSize(const TType &type) const
289{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000290 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000291 int arraySize = type.isArray() ? type.getArraySize() : 1;
292
293 return elementSize * arraySize;
294}
295
Jamie Madill98493dd2013-07-08 14:39:03 -0400296TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400297{
298 TString init;
299
300 TString preIndentString;
301 TString fullIndentString;
302
303 for (int spaces = 0; spaces < (indent * 4); spaces++)
304 {
305 preIndentString += ' ';
306 }
307
308 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
309 {
310 fullIndentString += ' ';
311 }
312
313 init += preIndentString + "{\n";
314
Jamie Madill98493dd2013-07-08 14:39:03 -0400315 const TFieldList &fields = structure.fields();
316 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400317 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400318 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400319 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400320 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400321
Jamie Madill98493dd2013-07-08 14:39:03 -0400322 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400323 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400324 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400325 }
326 else
327 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400328 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400329 }
330 }
331
332 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
333
334 return init;
335}
336
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200337void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338{
Jamie Madill32aab012015-01-27 14:12:26 -0500339 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000341 TString varyings;
342 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400343 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000344
Jamie Madill829f59e2013-11-13 19:40:54 -0500345 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400346 {
347 TIntermTyped *structNode = flaggedStructIt->first;
348 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400349 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400350 const TString &originalName = mFlaggedStructOriginalNames[structNode];
351
Jamie Madill033dae62014-06-18 12:56:28 -0400352 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400353 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400354 flaggedStructs += "\n";
355 }
356
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000357 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
358 {
359 const TType &type = varying->second->getType();
360 const TString &name = varying->second->getSymbol();
361
362 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400363 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
364 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000365 }
366
367 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
368 {
369 const TType &type = attribute->second->getType();
370 const TString &name = attribute->second->getSymbol();
371
Jamie Madill033dae62014-06-18 12:56:28 -0400372 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000373 }
374
Jamie Madill8daaba12014-06-13 10:04:33 -0400375 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400376
Jamie Madillf91ce812014-06-13 10:04:34 -0400377 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
378 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
379
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200380 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500381 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200382 out << "\n// Equality functions\n\n";
383 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500384 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200385 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200386 }
387 }
Olli Etuaho12690762015-03-31 12:55:28 +0300388 if (!mArrayAssignmentFunctions.empty())
389 {
390 out << "\n// Assignment functions\n\n";
391 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
392 {
393 out << assignmentFunction.functionDefinition << "\n";
394 }
395 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300396 if (!mArrayConstructIntoFunctions.empty())
397 {
398 out << "\n// Array constructor functions\n\n";
399 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
400 {
401 out << constructIntoFunction.functionDefinition << "\n";
402 }
403 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200404
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500405 if (mUsesDiscardRewriting)
406 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400407 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500408 }
409
Nicolas Capens655fe362014-04-11 13:12:34 -0400410 if (mUsesNestedBreak)
411 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400412 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400413 }
414
Arun Patole44efa0b2015-03-04 17:11:05 +0530415 if (mRequiresIEEEStrictCompiling)
416 {
417 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
418 }
419
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400420 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
421 "#define LOOP [loop]\n"
422 "#define FLATTEN [flatten]\n"
423 "#else\n"
424 "#define LOOP\n"
425 "#define FLATTEN\n"
426 "#endif\n";
427
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200428 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200430 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
431 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000432
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000433 out << "// Varyings\n";
434 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400435 out << "\n";
436
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200437 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000438 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500439 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000440 {
Jamie Madill46131a32013-06-20 11:55:50 -0400441 const TString &variableName = outputVariableIt->first;
442 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400443
Jamie Madill033dae62014-06-18 12:56:28 -0400444 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400445 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000446 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000447 }
Jamie Madill46131a32013-06-20 11:55:50 -0400448 else
449 {
450 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
451
452 out << "static float4 gl_Color[" << numColorValues << "] =\n"
453 "{\n";
454 for (unsigned int i = 0; i < numColorValues; i++)
455 {
456 out << " float4(0, 0, 0, 0)";
457 if (i + 1 != numColorValues)
458 {
459 out << ",";
460 }
461 out << "\n";
462 }
463
464 out << "};\n";
465 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000466
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400467 if (mUsesFragDepth)
468 {
469 out << "static float gl_Depth = 0.0;\n";
470 }
471
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000472 if (mUsesFragCoord)
473 {
474 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
475 }
476
477 if (mUsesPointCoord)
478 {
479 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
480 }
481
482 if (mUsesFrontFacing)
483 {
484 out << "static bool gl_FrontFacing = false;\n";
485 }
486
487 out << "\n";
488
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000489 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000490 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000491 out << "struct gl_DepthRangeParameters\n"
492 "{\n"
493 " float near;\n"
494 " float far;\n"
495 " float diff;\n"
496 "};\n"
497 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000498 }
499
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000500 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000501 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000502 out << "cbuffer DriverConstants : register(b1)\n"
503 "{\n";
504
505 if (mUsesDepthRange)
506 {
507 out << " float3 dx_DepthRange : packoffset(c0);\n";
508 }
509
510 if (mUsesFragCoord)
511 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000512 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000513 }
514
515 if (mUsesFragCoord || mUsesFrontFacing)
516 {
517 out << " float3 dx_DepthFront : packoffset(c2);\n";
518 }
519
520 out << "};\n";
521 }
522 else
523 {
524 if (mUsesDepthRange)
525 {
526 out << "uniform float3 dx_DepthRange : register(c0);";
527 }
528
529 if (mUsesFragCoord)
530 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000531 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000532 }
533
534 if (mUsesFragCoord || mUsesFrontFacing)
535 {
536 out << "uniform float3 dx_DepthFront : register(c2);\n";
537 }
538 }
539
540 out << "\n";
541
542 if (mUsesDepthRange)
543 {
544 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
545 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000546 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000547
Jamie Madillf91ce812014-06-13 10:04:34 -0400548 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000549 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400550 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000551 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400552 out << flaggedStructs;
553 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000554 }
555
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000556 if (usingMRTExtension && mNumRenderTargets > 1)
557 {
558 out << "#define GL_USES_MRT\n";
559 }
560
561 if (mUsesFragColor)
562 {
563 out << "#define GL_USES_FRAG_COLOR\n";
564 }
565
566 if (mUsesFragData)
567 {
568 out << "#define GL_USES_FRAG_DATA\n";
569 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000571 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000573 out << "// Attributes\n";
574 out << attributes;
575 out << "\n"
576 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400577
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000578 if (mUsesPointSize)
579 {
580 out << "static float gl_PointSize = float(1);\n";
581 }
582
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000583 if (mUsesInstanceID)
584 {
585 out << "static int gl_InstanceID;";
586 }
587
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000588 out << "\n"
589 "// Varyings\n";
590 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000591 out << "\n";
592
593 if (mUsesDepthRange)
594 {
595 out << "struct gl_DepthRangeParameters\n"
596 "{\n"
597 " float near;\n"
598 " float far;\n"
599 " float diff;\n"
600 "};\n"
601 "\n";
602 }
603
604 if (mOutputType == SH_HLSL11_OUTPUT)
605 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800606 out << "cbuffer DriverConstants : register(b1)\n"
607 "{\n";
608
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000609 if (mUsesDepthRange)
610 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800611 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000612 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800613
Cooper Partine6664f02015-01-09 16:22:24 -0800614 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800615 // However, we declare it for all shaders (including Feature Level 10+).
616 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
617 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800618 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800619
620 out << "};\n"
621 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000622 }
623 else
624 {
625 if (mUsesDepthRange)
626 {
627 out << "uniform float3 dx_DepthRange : register(c0);\n";
628 }
629
Cooper Partine6664f02015-01-09 16:22:24 -0800630 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
631 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000632 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000633 }
634
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000635 if (mUsesDepthRange)
636 {
637 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
638 "\n";
639 }
640
Jamie Madillf91ce812014-06-13 10:04:34 -0400641 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000642 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400643 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000644 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400645 out << flaggedStructs;
646 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000647 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400648 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000649
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400650 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
651 {
652 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400653 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000654 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400655 switch(textureFunction->sampler)
656 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400657 case EbtSampler2D: out << "int2 "; break;
658 case EbtSampler3D: out << "int3 "; break;
659 case EbtSamplerCube: out << "int2 "; break;
660 case EbtSampler2DArray: out << "int3 "; break;
661 case EbtISampler2D: out << "int2 "; break;
662 case EbtISampler3D: out << "int3 "; break;
663 case EbtISamplerCube: out << "int2 "; break;
664 case EbtISampler2DArray: out << "int3 "; break;
665 case EbtUSampler2D: out << "int2 "; break;
666 case EbtUSampler3D: out << "int3 "; break;
667 case EbtUSamplerCube: out << "int2 "; break;
668 case EbtUSampler2DArray: out << "int3 "; break;
669 case EbtSampler2DShadow: out << "int2 "; break;
670 case EbtSamplerCubeShadow: out << "int2 "; break;
671 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400672 default: UNREACHABLE();
673 }
674 }
675 else // Sampling function
676 {
677 switch(textureFunction->sampler)
678 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400679 case EbtSampler2D: out << "float4 "; break;
680 case EbtSampler3D: out << "float4 "; break;
681 case EbtSamplerCube: out << "float4 "; break;
682 case EbtSampler2DArray: out << "float4 "; break;
683 case EbtISampler2D: out << "int4 "; break;
684 case EbtISampler3D: out << "int4 "; break;
685 case EbtISamplerCube: out << "int4 "; break;
686 case EbtISampler2DArray: out << "int4 "; break;
687 case EbtUSampler2D: out << "uint4 "; break;
688 case EbtUSampler3D: out << "uint4 "; break;
689 case EbtUSamplerCube: out << "uint4 "; break;
690 case EbtUSampler2DArray: out << "uint4 "; break;
691 case EbtSampler2DShadow: out << "float "; break;
692 case EbtSamplerCubeShadow: out << "float "; break;
693 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400694 default: UNREACHABLE();
695 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000696 }
697
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400698 // Function name
699 out << textureFunction->name();
700
701 // Argument list
702 int hlslCoords = 4;
703
704 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000705 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400706 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000707 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400708 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
709 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
710 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000711 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400712
Nicolas Capens75fb4752013-07-10 15:14:47 -0400713 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000714 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400715 case TextureFunction::IMPLICIT: break;
716 case TextureFunction::BIAS: hlslCoords = 4; break;
717 case TextureFunction::LOD: hlslCoords = 4; break;
718 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400719 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400720 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000721 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400722 }
723 else if (mOutputType == SH_HLSL11_OUTPUT)
724 {
725 switch(textureFunction->sampler)
726 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400727 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
728 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
729 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
730 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
731 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
732 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500733 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400734 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
735 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
736 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500737 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400738 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
739 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
740 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
741 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400742 default: UNREACHABLE();
743 }
744 }
745 else UNREACHABLE();
746
Nicolas Capensfc014542014-02-18 14:47:13 -0500747 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400748 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500749 switch(textureFunction->coords)
750 {
751 case 2: out << ", int2 t"; break;
752 case 3: out << ", int3 t"; break;
753 default: UNREACHABLE();
754 }
755 }
756 else // Floating-point coordinates (except textureSize)
757 {
758 switch(textureFunction->coords)
759 {
760 case 1: out << ", int lod"; break; // textureSize()
761 case 2: out << ", float2 t"; break;
762 case 3: out << ", float3 t"; break;
763 case 4: out << ", float4 t"; break;
764 default: UNREACHABLE();
765 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000766 }
767
Nicolas Capensd11d5492014-02-19 17:06:10 -0500768 if (textureFunction->method == TextureFunction::GRAD)
769 {
770 switch(textureFunction->sampler)
771 {
772 case EbtSampler2D:
773 case EbtISampler2D:
774 case EbtUSampler2D:
775 case EbtSampler2DArray:
776 case EbtISampler2DArray:
777 case EbtUSampler2DArray:
778 case EbtSampler2DShadow:
779 case EbtSampler2DArrayShadow:
780 out << ", float2 ddx, float2 ddy";
781 break;
782 case EbtSampler3D:
783 case EbtISampler3D:
784 case EbtUSampler3D:
785 case EbtSamplerCube:
786 case EbtISamplerCube:
787 case EbtUSamplerCube:
788 case EbtSamplerCubeShadow:
789 out << ", float3 ddx, float3 ddy";
790 break;
791 default: UNREACHABLE();
792 }
793 }
794
Nicolas Capens75fb4752013-07-10 15:14:47 -0400795 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000796 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400797 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400798 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400799 case TextureFunction::LOD: out << ", float lod"; break;
800 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400801 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400802 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500803 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500804 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400805 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000806 }
807
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500808 if (textureFunction->offset)
809 {
810 switch(textureFunction->sampler)
811 {
812 case EbtSampler2D: out << ", int2 offset"; break;
813 case EbtSampler3D: out << ", int3 offset"; break;
814 case EbtSampler2DArray: out << ", int2 offset"; break;
815 case EbtISampler2D: out << ", int2 offset"; break;
816 case EbtISampler3D: out << ", int3 offset"; break;
817 case EbtISampler2DArray: out << ", int2 offset"; break;
818 case EbtUSampler2D: out << ", int2 offset"; break;
819 case EbtUSampler3D: out << ", int3 offset"; break;
820 case EbtUSampler2DArray: out << ", int2 offset"; break;
821 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500822 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500823 default: UNREACHABLE();
824 }
825 }
826
Nicolas Capens84cfa122014-04-14 13:48:45 -0400827 if (textureFunction->method == TextureFunction::BIAS ||
828 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500829 {
830 out << ", float bias";
831 }
832
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400833 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400834 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400835
Nicolas Capens75fb4752013-07-10 15:14:47 -0400836 if (textureFunction->method == TextureFunction::SIZE)
837 {
838 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
839 {
840 if (IsSamplerArray(textureFunction->sampler))
841 {
842 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
843 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
844 }
845 else
846 {
847 out << " uint width; uint height; uint numberOfLevels;\n"
848 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
849 }
850 }
851 else if (IsSampler3D(textureFunction->sampler))
852 {
853 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
854 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
855 }
856 else UNREACHABLE();
857
858 switch(textureFunction->sampler)
859 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400860 case EbtSampler2D: out << " return int2(width, height);"; break;
861 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
862 case EbtSamplerCube: out << " return int2(width, height);"; break;
863 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
864 case EbtISampler2D: out << " return int2(width, height);"; break;
865 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
866 case EbtISamplerCube: out << " return int2(width, height);"; break;
867 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
868 case EbtUSampler2D: out << " return int2(width, height);"; break;
869 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
870 case EbtUSamplerCube: out << " return int2(width, height);"; break;
871 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
872 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
873 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
874 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400875 default: UNREACHABLE();
876 }
877 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400878 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400879 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500880 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
881 {
882 out << " float width; float height; float layers; float levels;\n";
883
884 out << " uint mip = 0;\n";
885
886 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
887
888 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
889 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
890 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
891 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
892
893 // FACE_POSITIVE_X = 000b
894 // FACE_NEGATIVE_X = 001b
895 // FACE_POSITIVE_Y = 010b
896 // FACE_NEGATIVE_Y = 011b
897 // FACE_POSITIVE_Z = 100b
898 // FACE_NEGATIVE_Z = 101b
899 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
900
901 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
902 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
903 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
904
905 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
906 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500907
908 // Mip level computation.
909 if (textureFunction->method == TextureFunction::IMPLICIT)
910 {
911 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
912 " float2 dx = ddx(tSized);\n"
913 " float2 dy = ddy(tSized);\n"
914 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n"
915 " mip = uint(min(max(round(lod), 0), levels - 1));\n"
916 " x.GetDimensions(mip, width, height, layers, levels);\n";
917 }
Nicolas Capens0027fa92014-02-20 14:26:42 -0500918 }
919 else if (IsIntegerSampler(textureFunction->sampler) &&
920 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400921 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400922 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400923 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400924 if (IsSamplerArray(textureFunction->sampler))
925 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400926 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400927
Nicolas Capens9edebd62013-08-06 10:59:10 -0400928 if (textureFunction->method == TextureFunction::LOD0)
929 {
930 out << " uint mip = 0;\n";
931 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400932 else if (textureFunction->method == TextureFunction::LOD0BIAS)
933 {
934 out << " uint mip = bias;\n";
935 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400936 else
937 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200938
939 out << " x.GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400940 if (textureFunction->method == TextureFunction::IMPLICIT ||
941 textureFunction->method == TextureFunction::BIAS)
942 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200943 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400944 " float dx = length(ddx(tSized));\n"
945 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500946 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400947
948 if (textureFunction->method == TextureFunction::BIAS)
949 {
950 out << " lod += bias;\n";
951 }
952 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500953 else if (textureFunction->method == TextureFunction::GRAD)
954 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200955 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500956 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400957
958 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
959 }
960
961 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400962 }
963 else
964 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400965 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400966
Nicolas Capens9edebd62013-08-06 10:59:10 -0400967 if (textureFunction->method == TextureFunction::LOD0)
968 {
969 out << " uint mip = 0;\n";
970 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400971 else if (textureFunction->method == TextureFunction::LOD0BIAS)
972 {
973 out << " uint mip = bias;\n";
974 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400975 else
976 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200977 out << " x.GetDimensions(0, width, height, levels);\n";
978
Nicolas Capens9edebd62013-08-06 10:59:10 -0400979 if (textureFunction->method == TextureFunction::IMPLICIT ||
980 textureFunction->method == TextureFunction::BIAS)
981 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200982 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400983 " float dx = length(ddx(tSized));\n"
984 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500985 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400986
987 if (textureFunction->method == TextureFunction::BIAS)
988 {
989 out << " lod += bias;\n";
990 }
991 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500992 else if (textureFunction->method == TextureFunction::GRAD)
993 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200994 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500995 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400996
997 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
998 }
999
1000 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001001 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001002 }
1003 else if (IsSampler3D(textureFunction->sampler))
1004 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001005 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001006
Nicolas Capens9edebd62013-08-06 10:59:10 -04001007 if (textureFunction->method == TextureFunction::LOD0)
1008 {
1009 out << " uint mip = 0;\n";
1010 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001011 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1012 {
1013 out << " uint mip = bias;\n";
1014 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001015 else
1016 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001017 out << " x.GetDimensions(0, width, height, depth, levels);\n";
1018
Nicolas Capens9edebd62013-08-06 10:59:10 -04001019 if (textureFunction->method == TextureFunction::IMPLICIT ||
1020 textureFunction->method == TextureFunction::BIAS)
1021 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001022 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1023 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001024 " float dx = length(ddx(tSized));\n"
1025 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001026 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001027
1028 if (textureFunction->method == TextureFunction::BIAS)
1029 {
1030 out << " lod += bias;\n";
1031 }
1032 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001033 else if (textureFunction->method == TextureFunction::GRAD)
1034 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001035 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001036 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001037
1038 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1039 }
1040
1041 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001042 }
1043 else UNREACHABLE();
1044 }
1045
1046 out << " return ";
1047
1048 // HLSL intrinsic
1049 if (mOutputType == SH_HLSL9_OUTPUT)
1050 {
1051 switch(textureFunction->sampler)
1052 {
1053 case EbtSampler2D: out << "tex2D"; break;
1054 case EbtSamplerCube: out << "texCUBE"; break;
1055 default: UNREACHABLE();
1056 }
1057
Nicolas Capens75fb4752013-07-10 15:14:47 -04001058 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001059 {
1060 case TextureFunction::IMPLICIT: out << "(s, "; break;
1061 case TextureFunction::BIAS: out << "bias(s, "; break;
1062 case TextureFunction::LOD: out << "lod(s, "; break;
1063 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001064 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001065 default: UNREACHABLE();
1066 }
1067 }
1068 else if (mOutputType == SH_HLSL11_OUTPUT)
1069 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001070 if (textureFunction->method == TextureFunction::GRAD)
1071 {
1072 if (IsIntegerSampler(textureFunction->sampler))
1073 {
1074 out << "x.Load(";
1075 }
1076 else if (IsShadowSampler(textureFunction->sampler))
1077 {
1078 out << "x.SampleCmpLevelZero(s, ";
1079 }
1080 else
1081 {
1082 out << "x.SampleGrad(s, ";
1083 }
1084 }
1085 else if (IsIntegerSampler(textureFunction->sampler) ||
1086 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001087 {
1088 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001089 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001090 else if (IsShadowSampler(textureFunction->sampler))
1091 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001092 switch(textureFunction->method)
1093 {
1094 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1095 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1096 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1097 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1098 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1099 default: UNREACHABLE();
1100 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001101 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001102 else
1103 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001104 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001105 {
1106 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1107 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1108 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1109 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001110 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001111 default: UNREACHABLE();
1112 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001113 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001114 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001115 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001116
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001117 // Integer sampling requires integer addresses
1118 TString addressx = "";
1119 TString addressy = "";
1120 TString addressz = "";
Jamie Madill7a6a1ff2015-12-07 16:32:58 -05001121 TString closex = "";
1122 TString closey = "";
1123 TString closez = "";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001124
Nicolas Capensfc014542014-02-18 14:47:13 -05001125 if (IsIntegerSampler(textureFunction->sampler) ||
1126 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001127 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001128 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001129 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001130 case 2: out << "int3("; break;
1131 case 3: out << "int4("; break;
1132 default: UNREACHABLE();
1133 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001134
Nicolas Capensfc014542014-02-18 14:47:13 -05001135 // Convert from normalized floating-point to integer
1136 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001137 {
Jamie Madill7a6a1ff2015-12-07 16:32:58 -05001138 // We hard-code the clamp wrap mode for integer textures.
1139 // TODO(jmadill): Figure out how to integer texture wrap modes.
1140 addressx = "int(clamp(round((width *";
1141 addressy = "int(clamp(round((height * ";
1142 closex = ") - 0.5f), 0.0f, width - 1.0f))";
1143 closey = ") - 0.5f), 0.0f, height - 1.0f))";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001144
Nicolas Capensfc014542014-02-18 14:47:13 -05001145 if (IsSamplerArray(textureFunction->sampler))
1146 {
1147 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
Jamie Madill7a6a1ff2015-12-07 16:32:58 -05001148 closez = "))))";
Nicolas Capensfc014542014-02-18 14:47:13 -05001149 }
Jamie Madill7a6a1ff2015-12-07 16:32:58 -05001150 else if (IsSampler3D(textureFunction->sampler))
Nicolas Capens0027fa92014-02-20 14:26:42 -05001151 {
Jamie Madill7a6a1ff2015-12-07 16:32:58 -05001152 addressz = "int(clamp(round((depth * ";
1153 closez = ") - 0.5f), 0.0f, depth - 1.0f))";
Nicolas Capens0027fa92014-02-20 14:26:42 -05001154 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001155 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001156 }
1157 else
1158 {
1159 switch(hlslCoords)
1160 {
1161 case 2: out << "float2("; break;
1162 case 3: out << "float3("; break;
1163 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001164 default: UNREACHABLE();
1165 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001166 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001167
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001168 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001169
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001170 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001171 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001172 switch(textureFunction->coords)
1173 {
1174 case 3: proj = " / t.z"; break;
1175 case 4: proj = " / t.w"; break;
1176 default: UNREACHABLE();
1177 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001178 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001179
Jamie Madill7a6a1ff2015-12-07 16:32:58 -05001180 out << addressx + ("t.x" + proj) + closex + ", " + addressy + ("t.y" + proj) + closey;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001181
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001182 if (mOutputType == SH_HLSL9_OUTPUT)
1183 {
1184 if (hlslCoords >= 3)
1185 {
1186 if (textureFunction->coords < 3)
1187 {
1188 out << ", 0";
1189 }
1190 else
1191 {
1192 out << ", t.z" + proj;
1193 }
1194 }
1195
1196 if (hlslCoords == 4)
1197 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001198 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001199 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001200 case TextureFunction::BIAS: out << ", bias"; break;
1201 case TextureFunction::LOD: out << ", lod"; break;
1202 case TextureFunction::LOD0: out << ", 0"; break;
1203 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001204 default: UNREACHABLE();
1205 }
1206 }
1207
1208 out << "));\n";
1209 }
1210 else if (mOutputType == SH_HLSL11_OUTPUT)
1211 {
1212 if (hlslCoords >= 3)
1213 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001214 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1215 {
1216 out << ", face";
1217 }
1218 else
1219 {
Jamie Madill7a6a1ff2015-12-07 16:32:58 -05001220 out << ", " + addressz + ("t.z" + proj) + closez;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001221 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001222 }
1223
Nicolas Capensd11d5492014-02-19 17:06:10 -05001224 if (textureFunction->method == TextureFunction::GRAD)
1225 {
1226 if (IsIntegerSampler(textureFunction->sampler))
1227 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001228 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001229 }
1230 else if (IsShadowSampler(textureFunction->sampler))
1231 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001232 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001233 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001234 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001235 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1236 // The resulting third component of P' in the shadow forms is used as Dref
1237 out << "), t.z" << proj;
1238 }
1239 else
1240 {
1241 switch(textureFunction->coords)
1242 {
1243 case 3: out << "), t.z"; break;
1244 case 4: out << "), t.w"; break;
1245 default: UNREACHABLE();
1246 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001247 }
1248 }
1249 else
1250 {
1251 out << "), ddx, ddy";
1252 }
1253 }
1254 else if (IsIntegerSampler(textureFunction->sampler) ||
1255 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001256 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001257 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001258 }
1259 else if (IsShadowSampler(textureFunction->sampler))
1260 {
1261 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001262 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001263 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001264 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1265 // The resulting third component of P' in the shadow forms is used as Dref
1266 out << "), t.z" << proj;
1267 }
1268 else
1269 {
1270 switch(textureFunction->coords)
1271 {
1272 case 3: out << "), t.z"; break;
1273 case 4: out << "), t.w"; break;
1274 default: UNREACHABLE();
1275 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001276 }
1277 }
1278 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001279 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001280 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001281 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001282 case TextureFunction::IMPLICIT: out << ")"; break;
1283 case TextureFunction::BIAS: out << "), bias"; break;
1284 case TextureFunction::LOD: out << "), lod"; break;
1285 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001286 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001287 default: UNREACHABLE();
1288 }
1289 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001290
1291 if (textureFunction->offset)
1292 {
1293 out << ", offset";
1294 }
1295
1296 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001297 }
1298 else UNREACHABLE();
1299 }
1300
1301 out << "\n"
1302 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001303 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001304 }
1305
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001306 if (mUsesFragCoord)
1307 {
1308 out << "#define GL_USES_FRAG_COORD\n";
1309 }
1310
1311 if (mUsesPointCoord)
1312 {
1313 out << "#define GL_USES_POINT_COORD\n";
1314 }
1315
1316 if (mUsesFrontFacing)
1317 {
1318 out << "#define GL_USES_FRONT_FACING\n";
1319 }
1320
1321 if (mUsesPointSize)
1322 {
1323 out << "#define GL_USES_POINT_SIZE\n";
1324 }
1325
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001326 if (mUsesFragDepth)
1327 {
1328 out << "#define GL_USES_FRAG_DEPTH\n";
1329 }
1330
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001331 if (mUsesDepthRange)
1332 {
1333 out << "#define GL_USES_DEPTH_RANGE\n";
1334 }
1335
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001336 if (mUsesXor)
1337 {
1338 out << "bool xor(bool p, bool q)\n"
1339 "{\n"
1340 " return (p || q) && !(p && q);\n"
1341 "}\n"
1342 "\n";
1343 }
1344
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001345 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001346}
1347
1348void OutputHLSL::visitSymbol(TIntermSymbol *node)
1349{
Jamie Madill32aab012015-01-27 14:12:26 -05001350 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001351
Jamie Madill570e04d2013-06-21 09:15:33 -04001352 // Handle accessing std140 structs by value
1353 if (mFlaggedStructMappedNames.count(node) > 0)
1354 {
1355 out << mFlaggedStructMappedNames[node];
1356 return;
1357 }
1358
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001359 TString name = node->getSymbol();
1360
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001361 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001362 {
1363 mUsesDepthRange = true;
1364 out << name;
1365 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001366 else
1367 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001368 TQualifier qualifier = node->getQualifier();
1369
1370 if (qualifier == EvqUniform)
1371 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001372 const TType &nodeType = node->getType();
1373 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001374
1375 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001376 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001377 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001378 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001379 else
1380 {
1381 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001382 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001383
Jamie Madill2e295e22015-04-29 10:41:33 -04001384 ensureStructDefined(nodeType);
1385
Jamie Madill033dae62014-06-18 12:56:28 -04001386 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001387 }
Jamie Madill19571812013-08-12 15:26:34 -07001388 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001389 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001390 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001391 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001392 }
Jamie Madill033dae62014-06-18 12:56:28 -04001393 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001394 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001395 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001396 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001397 }
Jamie Madill19571812013-08-12 15:26:34 -07001398 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001399 {
1400 mReferencedOutputVariables[name] = node;
1401 out << "out_" << name;
1402 }
1403 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001404 {
1405 out << "gl_Color[0]";
1406 mUsesFragColor = true;
1407 }
1408 else if (qualifier == EvqFragData)
1409 {
1410 out << "gl_Color";
1411 mUsesFragData = true;
1412 }
1413 else if (qualifier == EvqFragCoord)
1414 {
1415 mUsesFragCoord = true;
1416 out << name;
1417 }
1418 else if (qualifier == EvqPointCoord)
1419 {
1420 mUsesPointCoord = true;
1421 out << name;
1422 }
1423 else if (qualifier == EvqFrontFacing)
1424 {
1425 mUsesFrontFacing = true;
1426 out << name;
1427 }
1428 else if (qualifier == EvqPointSize)
1429 {
1430 mUsesPointSize = true;
1431 out << name;
1432 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001433 else if (qualifier == EvqInstanceID)
1434 {
1435 mUsesInstanceID = true;
1436 out << name;
1437 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001438 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001439 {
1440 mUsesFragDepth = true;
1441 out << "gl_Depth";
1442 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001443 else
1444 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001445 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001446 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001447 }
1448}
1449
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001450void OutputHLSL::visitRaw(TIntermRaw *node)
1451{
Jamie Madill32aab012015-01-27 14:12:26 -05001452 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001453}
1454
Olli Etuaho7fb49552015-03-18 17:27:44 +02001455void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1456{
1457 if (type.isScalar() && !type.isArray())
1458 {
1459 if (op == EOpEqual)
1460 {
1461 outputTriplet(visit, "(", " == ", ")", out);
1462 }
1463 else
1464 {
1465 outputTriplet(visit, "(", " != ", ")", out);
1466 }
1467 }
1468 else
1469 {
1470 if (visit == PreVisit && op == EOpNotEqual)
1471 {
1472 out << "!";
1473 }
1474
1475 if (type.isArray())
1476 {
1477 const TString &functionName = addArrayEqualityFunction(type);
1478 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1479 }
1480 else if (type.getBasicType() == EbtStruct)
1481 {
1482 const TStructure &structure = *type.getStruct();
1483 const TString &functionName = addStructEqualityFunction(structure);
1484 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1485 }
1486 else
1487 {
1488 ASSERT(type.isMatrix() || type.isVector());
1489 outputTriplet(visit, "all(", " == ", ")", out);
1490 }
1491 }
1492}
1493
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001494bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1495{
Jamie Madill32aab012015-01-27 14:12:26 -05001496 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001497
Jamie Madill570e04d2013-06-21 09:15:33 -04001498 // Handle accessing std140 structs by value
1499 if (mFlaggedStructMappedNames.count(node) > 0)
1500 {
1501 out << mFlaggedStructMappedNames[node];
1502 return false;
1503 }
1504
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001505 switch (node->getOp())
1506 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001507 case EOpAssign:
1508 if (node->getLeft()->isArray())
1509 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001510 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1511 if (rightAgg != nullptr && rightAgg->isConstructor())
1512 {
1513 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1514 out << functionName << "(";
1515 node->getLeft()->traverse(this);
1516 TIntermSequence *seq = rightAgg->getSequence();
1517 for (auto &arrayElement : *seq)
1518 {
1519 out << ", ";
1520 arrayElement->traverse(this);
1521 }
1522 out << ")";
1523 return false;
1524 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001525 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1526 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1527
1528 const TString &functionName = addArrayAssignmentFunction(node->getType());
1529 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001530 }
1531 else
1532 {
1533 outputTriplet(visit, "(", " = ", ")");
1534 }
1535 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001536 case EOpInitialize:
1537 if (visit == PreVisit)
1538 {
1539 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1540 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1541 // new variable is created before the assignment is evaluated), so we need to convert
1542 // this to "float t = x, x = t;".
1543
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001544 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001545 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001546 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001547
Jamie Madill37997142015-01-28 10:06:34 -05001548 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1549 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001550 {
Jamie Madill37997142015-01-28 10:06:34 -05001551 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001552 // after we initialize uniforms.
1553 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1554 deferredInit->setLeft(node->getLeft());
1555 deferredInit->setRight(node->getRight());
1556 deferredInit->setType(node->getType());
1557 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001558 const TString &initString = initializer(node->getType());
1559 node->setRight(new TIntermRaw(node->getType(), initString));
1560 }
1561 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1562 {
1563 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001564 return false;
1565 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001566 else if (writeConstantInitialization(out, symbolNode, expression))
1567 {
1568 return false;
1569 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001570 }
1571 else if (visit == InVisit)
1572 {
1573 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001574 }
1575 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001576 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1577 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1578 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1579 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1580 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1581 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001582 if (visit == PreVisit)
1583 {
1584 out << "(";
1585 }
1586 else if (visit == InVisit)
1587 {
1588 out << " = mul(";
1589 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001590 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001591 }
1592 else
1593 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001594 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001595 }
1596 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001597 case EOpMatrixTimesMatrixAssign:
1598 if (visit == PreVisit)
1599 {
1600 out << "(";
1601 }
1602 else if (visit == InVisit)
1603 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001604 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001605 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001606 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001607 }
1608 else
1609 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001610 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001611 }
1612 break;
1613 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001614 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001615 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1616 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1617 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1618 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1619 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001620 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001621 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001622 const TType& leftType = node->getLeft()->getType();
1623 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001624 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001625 if (visit == PreVisit)
1626 {
1627 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1628 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001629 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001630 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001631 return false;
1632 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001633 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001634 else
1635 {
1636 outputTriplet(visit, "", "[", "]");
1637 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001638 }
1639 break;
1640 case EOpIndexIndirect:
1641 // We do not currently support indirect references to interface blocks
1642 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1643 outputTriplet(visit, "", "[", "]");
1644 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001645 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001646 if (visit == InVisit)
1647 {
1648 const TStructure* structure = node->getLeft()->getType().getStruct();
1649 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1650 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001651 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001652
1653 return false;
1654 }
1655 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001656 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001657 if (visit == InVisit)
1658 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001659 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1660 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1661 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001662 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001663
1664 return false;
1665 }
1666 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 case EOpVectorSwizzle:
1668 if (visit == InVisit)
1669 {
1670 out << ".";
1671
1672 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1673
1674 if (swizzle)
1675 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001676 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001678 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001679 {
1680 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1681
1682 if (element)
1683 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001684 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685
1686 switch (i)
1687 {
1688 case 0: out << "x"; break;
1689 case 1: out << "y"; break;
1690 case 2: out << "z"; break;
1691 case 3: out << "w"; break;
1692 default: UNREACHABLE();
1693 }
1694 }
1695 else UNREACHABLE();
1696 }
1697 }
1698 else UNREACHABLE();
1699
1700 return false; // Fully processed
1701 }
1702 break;
1703 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1704 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1705 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1706 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001707 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001708 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1709 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1710 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1711 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1712 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001713 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001714 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001715 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001716 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1718 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1719 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1720 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1721 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001722 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001723 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1724 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001725 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001726 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001727 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1728 ASSERT(!node->getRight()->hasSideEffects());
1729 outputTriplet(visit, "(", " || ", ")");
1730 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001731 case EOpLogicalXor:
1732 mUsesXor = true;
1733 outputTriplet(visit, "xor(", ", ", ")");
1734 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001735 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001736 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1737 ASSERT(!node->getRight()->hasSideEffects());
1738 outputTriplet(visit, "(", " && ", ")");
1739 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740 default: UNREACHABLE();
1741 }
1742
1743 return true;
1744}
1745
1746bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1747{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748 switch (node->getOp())
1749 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001750 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001751 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001752 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1753 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001754 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001755 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1756 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1757 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1758 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001759 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1760 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1761 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1762 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1763 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1764 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1765 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1766 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001767 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1768 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1769 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1770 case EOpAsinh:
1771 ASSERT(node->getUseEmulatedFunction());
1772 writeEmulatedFunctionTriplet(visit, "asinh(");
1773 break;
1774 case EOpAcosh:
1775 ASSERT(node->getUseEmulatedFunction());
1776 writeEmulatedFunctionTriplet(visit, "acosh(");
1777 break;
1778 case EOpAtanh:
1779 ASSERT(node->getUseEmulatedFunction());
1780 writeEmulatedFunctionTriplet(visit, "atanh(");
1781 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001782 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1783 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1784 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1785 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1786 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1787 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1788 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1789 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1790 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001791 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1792 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1793 case EOpRoundEven:
1794 ASSERT(node->getUseEmulatedFunction());
1795 writeEmulatedFunctionTriplet(visit, "roundEven(");
1796 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001797 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1798 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301799 case EOpIsNan:
1800 outputTriplet(visit, "isnan(", "", ")");
1801 mRequiresIEEEStrictCompiling = true;
1802 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301803 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001804 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1805 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1806 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1807 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001808 case EOpPackSnorm2x16:
1809 ASSERT(node->getUseEmulatedFunction());
1810 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1811 break;
1812 case EOpPackUnorm2x16:
1813 ASSERT(node->getUseEmulatedFunction());
1814 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1815 break;
1816 case EOpPackHalf2x16:
1817 ASSERT(node->getUseEmulatedFunction());
1818 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1819 break;
1820 case EOpUnpackSnorm2x16:
1821 ASSERT(node->getUseEmulatedFunction());
1822 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1823 break;
1824 case EOpUnpackUnorm2x16:
1825 ASSERT(node->getUseEmulatedFunction());
1826 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1827 break;
1828 case EOpUnpackHalf2x16:
1829 ASSERT(node->getUseEmulatedFunction());
1830 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1831 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001832 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1833 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001834 case EOpDFdx:
1835 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1836 {
1837 outputTriplet(visit, "(", "", ", 0.0)");
1838 }
1839 else
1840 {
1841 outputTriplet(visit, "ddx(", "", ")");
1842 }
1843 break;
1844 case EOpDFdy:
1845 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1846 {
1847 outputTriplet(visit, "(", "", ", 0.0)");
1848 }
1849 else
1850 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001851 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001852 }
1853 break;
1854 case EOpFwidth:
1855 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1856 {
1857 outputTriplet(visit, "(", "", ", 0.0)");
1858 }
1859 else
1860 {
1861 outputTriplet(visit, "fwidth(", "", ")");
1862 }
1863 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001864 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1865 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001866 case EOpInverse:
1867 ASSERT(node->getUseEmulatedFunction());
1868 writeEmulatedFunctionTriplet(visit, "inverse(");
1869 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001870
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001871 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1872 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873 default: UNREACHABLE();
1874 }
1875
1876 return true;
1877}
1878
1879bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1880{
Jamie Madill32aab012015-01-27 14:12:26 -05001881 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001882
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883 switch (node->getOp())
1884 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001885 case EOpSequence:
1886 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001887 if (mInsideFunction)
1888 {
Jamie Madill075edd82013-07-08 13:30:19 -04001889 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001890 out << "{\n";
1891 }
1892
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001893 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001894 {
Jamie Madill075edd82013-07-08 13:30:19 -04001895 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001896
Olli Etuahoa6f22092015-05-08 18:31:10 +03001897 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001898
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001899 // Don't output ; after case labels, they're terminated by :
1900 // This is needed especially since outputting a ; after a case statement would turn empty
1901 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03001902 // Also no need to output ; after selection (if) statements or sequences. This is done just
1903 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001904 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
1905 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03001906 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001907 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001908 }
1909
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001910 if (mInsideFunction)
1911 {
Jamie Madill075edd82013-07-08 13:30:19 -04001912 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001913 out << "}\n";
1914 }
1915
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001916 return false;
1917 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 case EOpDeclaration:
1919 if (visit == PreVisit)
1920 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001921 TIntermSequence *sequence = node->getSequence();
1922 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001923 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001925 if (variable &&
1926 (variable->getQualifier() == EvqTemporary ||
1927 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001928 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001929 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001930
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001931 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001932 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001933 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001934 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001935 out << "static ";
1936 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001937
Olli Etuahoa6f22092015-05-08 18:31:10 +03001938 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001939
Olli Etuahoa6f22092015-05-08 18:31:10 +03001940 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001941
Olli Etuahoa6f22092015-05-08 18:31:10 +03001942 if (symbol)
1943 {
1944 symbol->traverse(this);
1945 out << ArrayString(symbol->getType());
1946 out << " = " + initializer(symbol->getType());
1947 }
1948 else
1949 {
1950 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001952 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001953 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1954 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001955 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001956 }
1957 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001958 }
Jamie Madill033dae62014-06-18 12:56:28 -04001959 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001960 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001961 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001962 {
1963 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1964
1965 if (symbol)
1966 {
1967 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1968 mReferencedVaryings[symbol->getSymbol()] = symbol;
1969 }
1970 else
1971 {
1972 (*sit)->traverse(this);
1973 }
1974 }
1975 }
1976
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001977 return false;
1978 }
1979 else if (visit == InVisit)
1980 {
1981 out << ", ";
1982 }
1983 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001984 case EOpInvariantDeclaration:
1985 // Do not do any translation
1986 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001987 case EOpPrototype:
1988 if (visit == PreVisit)
1989 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001990 size_t index = mCallDag.findIndex(node);
1991 // Skip the prototype if it is not implemented (and thus not used)
1992 if (index == CallDAG::InvalidIndex)
1993 {
1994 return false;
1995 }
1996
Olli Etuaho59f9a642015-08-06 20:38:26 +03001997 TString name = DecorateFunctionIfNeeded(node->getNameObj());
1998 out << TypeString(node->getType()) << " " << name
1999 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002000
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002001 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002002
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002003 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002004 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002005 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002006
2007 if (symbol)
2008 {
2009 out << argumentString(symbol);
2010
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002011 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002012 {
2013 out << ", ";
2014 }
2015 }
2016 else UNREACHABLE();
2017 }
2018
2019 out << ");\n";
2020
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002021 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002022 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2023 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002024 {
2025 mOutputLod0Function = true;
2026 node->traverse(this);
2027 mOutputLod0Function = false;
2028 }
2029
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002030 return false;
2031 }
2032 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002033 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034 case EOpFunction:
2035 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002036 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002037 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038
Corentin Wallez1239ee92015-03-19 14:38:02 -07002039 size_t index = mCallDag.findIndex(node);
2040 ASSERT(index != CallDAG::InvalidIndex);
2041 mCurrentFunctionMetadata = &mASTMetadataList[index];
2042
Jamie Madill033dae62014-06-18 12:56:28 -04002043 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002044
2045 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002047 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002048 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002049 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002051 out << DecorateFunctionIfNeeded(node->getNameObj())
2052 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002053 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002054
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002055 TIntermSequence *sequence = node->getSequence();
2056 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002057
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002058 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002059 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002060 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002061
2062 if (symbol)
2063 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002064 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002065
2066 out << argumentString(symbol);
2067
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002068 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002069 {
2070 out << ", ";
2071 }
2072 }
2073 else UNREACHABLE();
2074 }
2075
Olli Etuaho4785fec2015-05-18 16:09:37 +03002076 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002077
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002078 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002079 {
2080 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002081 TIntermNode *body = (*sequence)[1];
2082 // The function body node will output braces.
2083 ASSERT(IsSequence(body));
2084 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002085 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002087 else
2088 {
2089 out << "{}\n";
2090 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002091
Corentin Wallez1239ee92015-03-19 14:38:02 -07002092 mCurrentFunctionMetadata = nullptr;
2093
2094 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2095 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002096 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002097 ASSERT(name != "main");
2098 mOutputLod0Function = true;
2099 node->traverse(this);
2100 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002101 }
2102
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002103 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 }
2105 break;
2106 case EOpFunctionCall:
2107 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002108 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002109
Corentin Wallez1239ee92015-03-19 14:38:02 -07002110 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002111 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002113 if (node->isArray())
2114 {
2115 UNIMPLEMENTED();
2116 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002117 size_t index = mCallDag.findIndex(node);
2118 ASSERT(index != CallDAG::InvalidIndex);
2119 lod0 &= mASTMetadataList[index].mNeedsLod0;
2120
Olli Etuaho59f9a642015-08-06 20:38:26 +03002121 out << DecorateFunctionIfNeeded(node->getNameObj()) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122 }
2123 else
2124 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002125 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002126 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002127
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002128 TextureFunction textureFunction;
2129 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002130 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002131 textureFunction.method = TextureFunction::IMPLICIT;
2132 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002133 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002134
2135 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002136 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002137 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002138 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002139 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002140 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002141 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002142 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002143 }
Nicolas Capens46485082014-04-15 13:12:50 -04002144 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2145 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002146 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002147 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002148 }
Nicolas Capens46485082014-04-15 13:12:50 -04002149 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002150 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002151 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002152 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002153 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002154 else if (name == "textureSize")
2155 {
2156 textureFunction.method = TextureFunction::SIZE;
2157 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002158 else if (name == "textureOffset")
2159 {
2160 textureFunction.method = TextureFunction::IMPLICIT;
2161 textureFunction.offset = true;
2162 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002163 else if (name == "textureProjOffset")
2164 {
2165 textureFunction.method = TextureFunction::IMPLICIT;
2166 textureFunction.offset = true;
2167 textureFunction.proj = true;
2168 }
2169 else if (name == "textureLodOffset")
2170 {
2171 textureFunction.method = TextureFunction::LOD;
2172 textureFunction.offset = true;
2173 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002174 else if (name == "textureProjLodOffset")
2175 {
2176 textureFunction.method = TextureFunction::LOD;
2177 textureFunction.proj = true;
2178 textureFunction.offset = true;
2179 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002180 else if (name == "texelFetch")
2181 {
2182 textureFunction.method = TextureFunction::FETCH;
2183 }
2184 else if (name == "texelFetchOffset")
2185 {
2186 textureFunction.method = TextureFunction::FETCH;
2187 textureFunction.offset = true;
2188 }
Nicolas Capens46485082014-04-15 13:12:50 -04002189 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002190 {
2191 textureFunction.method = TextureFunction::GRAD;
2192 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002193 else if (name == "textureGradOffset")
2194 {
2195 textureFunction.method = TextureFunction::GRAD;
2196 textureFunction.offset = true;
2197 }
Nicolas Capens46485082014-04-15 13:12:50 -04002198 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002199 {
2200 textureFunction.method = TextureFunction::GRAD;
2201 textureFunction.proj = true;
2202 }
2203 else if (name == "textureProjGradOffset")
2204 {
2205 textureFunction.method = TextureFunction::GRAD;
2206 textureFunction.proj = true;
2207 textureFunction.offset = true;
2208 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002209 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002210
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002211 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002212 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002213 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2214
2215 if (textureFunction.offset)
2216 {
2217 mandatoryArgumentCount++;
2218 }
2219
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002220 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002221
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002222 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002223 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002224 if (bias)
2225 {
2226 textureFunction.method = TextureFunction::LOD0BIAS;
2227 }
2228 else
2229 {
2230 textureFunction.method = TextureFunction::LOD0;
2231 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002232 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002233 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002234 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002235 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002236 }
2237 }
2238
2239 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002240
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002241 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002242 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002243
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002244 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002245 {
2246 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2247 {
2248 out << "texture_";
2249 (*arg)->traverse(this);
2250 out << ", sampler_";
2251 }
2252
2253 (*arg)->traverse(this);
2254
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002255 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002256 {
2257 out << ", ";
2258 }
2259 }
2260
2261 out << ")";
2262
2263 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 }
2265 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002266 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002267 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2268 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2269 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2270 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2271 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2272 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2273 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2274 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2275 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2276 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2277 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2278 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2279 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2280 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2281 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2282 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2283 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002284 case EOpConstructMat2x3: outputConstructor(visit, node->getType(), "mat2x3", node->getSequence()); break;
2285 case EOpConstructMat2x4: outputConstructor(visit, node->getType(), "mat2x4", node->getSequence()); break;
2286 case EOpConstructMat3x2: outputConstructor(visit, node->getType(), "mat3x2", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002287 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002288 case EOpConstructMat3x4: outputConstructor(visit, node->getType(), "mat3x4", node->getSequence()); break;
2289 case EOpConstructMat4x2: outputConstructor(visit, node->getType(), "mat4x2", node->getSequence()); break;
2290 case EOpConstructMat4x3: outputConstructor(visit, node->getType(), "mat4x3", node->getSequence()); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002291 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002292 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002293 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002294 if (node->getType().isArray())
2295 {
2296 UNIMPLEMENTED();
2297 }
Jamie Madill033dae62014-06-18 12:56:28 -04002298 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002299 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002300 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002301 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002302 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002303 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2304 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2305 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2306 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2307 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2308 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002309 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002310 ASSERT(node->getUseEmulatedFunction());
2311 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002312 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002313 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002314 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002316 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002317 ASSERT(node->getUseEmulatedFunction());
2318 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 break;
2320 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2321 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2322 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
Arun Patoled94f6642015-05-18 16:25:12 +05302323 case EOpMix:
2324 {
2325 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2326 if (lastParamNode->getType().getBasicType() == EbtBool)
2327 {
2328 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2329 // so use emulated version.
2330 ASSERT(node->getUseEmulatedFunction());
2331 writeEmulatedFunctionTriplet(visit, "mix(");
2332 }
2333 else
2334 {
2335 outputTriplet(visit, "lerp(", ", ", ")");
2336 }
2337 }
2338 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2340 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2341 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2342 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2343 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002344 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002345 ASSERT(node->getUseEmulatedFunction());
2346 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002347 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2349 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002350 case EOpOuterProduct:
2351 ASSERT(node->getUseEmulatedFunction());
2352 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2353 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002354 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 default: UNREACHABLE();
2356 }
2357
2358 return true;
2359}
2360
Olli Etuahod81ed842015-05-12 12:46:35 +03002361void OutputHLSL::writeSelection(TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362{
Jamie Madill32aab012015-01-27 14:12:26 -05002363 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002364
Olli Etuahoa6f22092015-05-08 18:31:10 +03002365 out << "if (";
2366
2367 node->getCondition()->traverse(this);
2368
2369 out << ")\n";
2370
2371 outputLineDirective(node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002372
2373 bool discard = false;
2374
2375 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002377 // The trueBlock child node will output braces.
2378 ASSERT(IsSequence(node->getTrueBlock()));
2379
Olli Etuahoa6f22092015-05-08 18:31:10 +03002380 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002381
Olli Etuahoa6f22092015-05-08 18:31:10 +03002382 // Detect true discard
2383 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2384 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002385 else
2386 {
2387 // TODO(oetuaho): Check if the semicolon inside is necessary.
2388 // It's there as a result of conservative refactoring of the output.
2389 out << "{;}\n";
2390 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002391
Olli Etuahoa6f22092015-05-08 18:31:10 +03002392 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002393
Olli Etuahoa6f22092015-05-08 18:31:10 +03002394 if (node->getFalseBlock())
2395 {
2396 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002397
Olli Etuahoa6f22092015-05-08 18:31:10 +03002398 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399
Olli Etuaho4785fec2015-05-18 16:09:37 +03002400 // Either this is "else if" or the falseBlock child node will output braces.
2401 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2402
Olli Etuahoa6f22092015-05-08 18:31:10 +03002403 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002404
Olli Etuahoa6f22092015-05-08 18:31:10 +03002405 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002406
Olli Etuahoa6f22092015-05-08 18:31:10 +03002407 // Detect false discard
2408 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2409 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002410
Olli Etuahoa6f22092015-05-08 18:31:10 +03002411 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002412 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002413 {
2414 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002415 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002416}
2417
2418bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2419{
2420 TInfoSinkBase &out = getInfoSink();
2421
2422 ASSERT(!node->usesTernaryOperator());
2423
2424 if (!mInsideFunction)
2425 {
2426 // This is part of unfolded global initialization.
2427 mDeferredGlobalInitializers.push_back(node);
2428 return false;
2429 }
2430
2431 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002432 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002433 {
2434 out << "FLATTEN ";
2435 }
2436
2437 writeSelection(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438
2439 return false;
2440}
2441
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002442bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002443{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002444 if (node->getStatementList())
2445 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002446 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002447 outputTriplet(visit, "switch (", ") ", "");
2448 // The curly braces get written when visiting the statementList aggregate
2449 }
2450 else
2451 {
2452 // No statementList, so it won't output curly braces
2453 outputTriplet(visit, "switch (", ") {", "}\n");
2454 }
2455 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002456}
2457
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002458bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002459{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002460 if (node->hasCondition())
2461 {
2462 outputTriplet(visit, "case (", "", "):\n");
2463 return true;
2464 }
2465 else
2466 {
2467 TInfoSinkBase &out = getInfoSink();
2468 out << "default:\n";
2469 return false;
2470 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002471}
2472
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2474{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002475 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476}
2477
2478bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2479{
Nicolas Capens655fe362014-04-11 13:12:34 -04002480 mNestedLoopDepth++;
2481
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002482 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002483 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002484 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002485
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002486 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002487 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002488 if (handleExcessiveLoop(node))
2489 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002490 mInsideDiscontinuousLoop = wasDiscontinuous;
2491 mNestedLoopDepth--;
2492
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002493 return false;
2494 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002495 }
2496
Jamie Madill32aab012015-01-27 14:12:26 -05002497 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002498
Corentin Wallez1239ee92015-03-19 14:38:02 -07002499 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002500 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002502 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002503
Jamie Madill075edd82013-07-08 13:30:19 -04002504 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 }
2506 else
2507 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002508 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002509
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002510 if (node->getInit())
2511 {
2512 node->getInit()->traverse(this);
2513 }
2514
2515 out << "; ";
2516
alokp@chromium.org52813552010-11-16 18:36:09 +00002517 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002518 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002519 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002520 }
2521
2522 out << "; ";
2523
alokp@chromium.org52813552010-11-16 18:36:09 +00002524 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002525 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002526 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002527 }
2528
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002529 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002530
Jamie Madill075edd82013-07-08 13:30:19 -04002531 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532 }
2533
2534 if (node->getBody())
2535 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002536 // The loop body node will output braces.
2537 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002538 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002540 else
2541 {
2542 // TODO(oetuaho): Check if the semicolon inside is necessary.
2543 // It's there as a result of conservative refactoring of the output.
2544 out << "{;}\n";
2545 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546
Jamie Madill075edd82013-07-08 13:30:19 -04002547 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548
alokp@chromium.org52813552010-11-16 18:36:09 +00002549 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550 {
Jamie Madill075edd82013-07-08 13:30:19 -04002551 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552 out << "while(\n";
2553
alokp@chromium.org52813552010-11-16 18:36:09 +00002554 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555
daniel@transgaming.com73536982012-03-21 20:45:49 +00002556 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557 }
2558
daniel@transgaming.com73536982012-03-21 20:45:49 +00002559 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002560
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002561 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002562 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002563
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564 return false;
2565}
2566
2567bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2568{
Jamie Madill32aab012015-01-27 14:12:26 -05002569 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570
2571 switch (node->getFlowOp())
2572 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002573 case EOpKill:
2574 outputTriplet(visit, "discard;\n", "", "");
2575 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002576 case EOpBreak:
2577 if (visit == PreVisit)
2578 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002579 if (mNestedLoopDepth > 1)
2580 {
2581 mUsesNestedBreak = true;
2582 }
2583
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002584 if (mExcessiveLoopIndex)
2585 {
2586 out << "{Break";
2587 mExcessiveLoopIndex->traverse(this);
2588 out << " = true; break;}\n";
2589 }
2590 else
2591 {
2592 out << "break;\n";
2593 }
2594 }
2595 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002596 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002597 case EOpReturn:
2598 if (visit == PreVisit)
2599 {
2600 if (node->getExpression())
2601 {
2602 out << "return ";
2603 }
2604 else
2605 {
2606 out << "return;\n";
2607 }
2608 }
2609 else if (visit == PostVisit)
2610 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002611 if (node->getExpression())
2612 {
2613 out << ";\n";
2614 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002615 }
2616 break;
2617 default: UNREACHABLE();
2618 }
2619
2620 return true;
2621}
2622
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002623bool OutputHLSL::isSingleStatement(TIntermNode *node)
2624{
2625 TIntermAggregate *aggregate = node->getAsAggregate();
2626
2627 if (aggregate)
2628 {
2629 if (aggregate->getOp() == EOpSequence)
2630 {
2631 return false;
2632 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002633 else if (aggregate->getOp() == EOpDeclaration)
2634 {
2635 // Declaring multiple comma-separated variables must be considered multiple statements
2636 // because each individual declaration has side effects which are visible in the next.
2637 return false;
2638 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002639 else
2640 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002641 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002642 {
2643 if (!isSingleStatement(*sit))
2644 {
2645 return false;
2646 }
2647 }
2648
2649 return true;
2650 }
2651 }
2652
2653 return true;
2654}
2655
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002656// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2657// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002658bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2659{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002660 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002661 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002662
2663 // Parse loops of the form:
2664 // for(int index = initial; index [comparator] limit; index += increment)
2665 TIntermSymbol *index = NULL;
2666 TOperator comparator = EOpNull;
2667 int initial = 0;
2668 int limit = 0;
2669 int increment = 0;
2670
2671 // Parse index name and intial value
2672 if (node->getInit())
2673 {
2674 TIntermAggregate *init = node->getInit()->getAsAggregate();
2675
2676 if (init)
2677 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002678 TIntermSequence *sequence = init->getSequence();
2679 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002680
2681 if (variable && variable->getQualifier() == EvqTemporary)
2682 {
2683 TIntermBinary *assign = variable->getAsBinaryNode();
2684
2685 if (assign->getOp() == EOpInitialize)
2686 {
2687 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2688 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2689
2690 if (symbol && constant)
2691 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002692 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002693 {
2694 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002695 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002696 }
2697 }
2698 }
2699 }
2700 }
2701 }
2702
2703 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002704 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002705 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002706 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002707
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002708 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2709 {
2710 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2711
2712 if (constant)
2713 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002714 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002715 {
2716 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002717 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002718 }
2719 }
2720 }
2721 }
2722
2723 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002724 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002725 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002726 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2727 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002728
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002729 if (binaryTerminal)
2730 {
2731 TOperator op = binaryTerminal->getOp();
2732 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2733
2734 if (constant)
2735 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002736 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002737 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002738 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002739
2740 switch (op)
2741 {
2742 case EOpAddAssign: increment = value; break;
2743 case EOpSubAssign: increment = -value; break;
2744 default: UNIMPLEMENTED();
2745 }
2746 }
2747 }
2748 }
2749 else if (unaryTerminal)
2750 {
2751 TOperator op = unaryTerminal->getOp();
2752
2753 switch (op)
2754 {
2755 case EOpPostIncrement: increment = 1; break;
2756 case EOpPostDecrement: increment = -1; break;
2757 case EOpPreIncrement: increment = 1; break;
2758 case EOpPreDecrement: increment = -1; break;
2759 default: UNIMPLEMENTED();
2760 }
2761 }
2762 }
2763
2764 if (index != NULL && comparator != EOpNull && increment != 0)
2765 {
2766 if (comparator == EOpLessThanEqual)
2767 {
2768 comparator = EOpLessThan;
2769 limit += 1;
2770 }
2771
2772 if (comparator == EOpLessThan)
2773 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002774 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002775
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002776 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002777 {
2778 return false; // Not an excessive loop
2779 }
2780
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002781 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2782 mExcessiveLoopIndex = index;
2783
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002784 out << "{int ";
2785 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002786 out << ";\n"
2787 "bool Break";
2788 index->traverse(this);
2789 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002790
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002791 bool firstLoopFragment = true;
2792
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002793 while (iterations > 0)
2794 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002795 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002796
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002797 if (!firstLoopFragment)
2798 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002799 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002800 index->traverse(this);
2801 out << ") {\n";
2802 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002803
2804 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2805 {
2806 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2807 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002808
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002809 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002810 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002811
Corentin Wallez1239ee92015-03-19 14:38:02 -07002812 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002813 index->traverse(this);
2814 out << " = ";
2815 out << initial;
2816
2817 out << "; ";
2818 index->traverse(this);
2819 out << " < ";
2820 out << clampedLimit;
2821
2822 out << "; ";
2823 index->traverse(this);
2824 out << " += ";
2825 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002826 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002827
Jamie Madill075edd82013-07-08 13:30:19 -04002828 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002829 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002830
2831 if (node->getBody())
2832 {
2833 node->getBody()->traverse(this);
2834 }
2835
Jamie Madill075edd82013-07-08 13:30:19 -04002836 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002837 out << ";}\n";
2838
2839 if (!firstLoopFragment)
2840 {
2841 out << "}\n";
2842 }
2843
2844 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002845
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002846 initial += MAX_LOOP_ITERATIONS * increment;
2847 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002848 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002849
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002850 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002851
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002852 mExcessiveLoopIndex = restoreIndex;
2853
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002854 return true;
2855 }
2856 else UNIMPLEMENTED();
2857 }
2858
2859 return false; // Not handled as an excessive loop
2860}
2861
Olli Etuaho7fb49552015-03-18 17:27:44 +02002862void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002863{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002864 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002865 {
2866 out << preString;
2867 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002868 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869 {
2870 out << inString;
2871 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002872 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873 {
2874 out << postString;
2875 }
2876}
2877
Olli Etuaho7fb49552015-03-18 17:27:44 +02002878void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2879{
2880 outputTriplet(visit, preString, inString, postString, getInfoSink());
2881}
2882
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002883void OutputHLSL::outputLineDirective(int line)
2884{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002885 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002886 {
Jamie Madill32aab012015-01-27 14:12:26 -05002887 TInfoSinkBase &out = getInfoSink();
2888
2889 out << "\n";
2890 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002891
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002892 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002893 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002894 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002895 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002896
Jamie Madill32aab012015-01-27 14:12:26 -05002897 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002898 }
2899}
2900
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002901TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2902{
2903 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002904 const TType &type = symbol->getType();
2905 const TName &name = symbol->getName();
2906 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002907
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002908 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002909 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002910 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002911 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002912 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002913 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002914 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002915 }
2916
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002917 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2918 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002919 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + nameStr +
2920 ArrayString(type) + ", " + QualifierString(qualifier) + " " + SamplerString(type) +
2921 " sampler_" + nameStr + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002922 }
2923
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002924 return QualifierString(qualifier) + " " + TypeString(type) + " " + nameStr + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002925}
2926
2927TString OutputHLSL::initializer(const TType &type)
2928{
2929 TString string;
2930
Jamie Madill94bf7f22013-07-08 13:31:15 -04002931 size_t size = type.getObjectSize();
2932 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002933 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002934 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002935
Jamie Madill94bf7f22013-07-08 13:31:15 -04002936 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002937 {
2938 string += ", ";
2939 }
2940 }
2941
daniel@transgaming.comead23042010-04-29 03:35:36 +00002942 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002943}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002944
Daniel Bratell29190082015-02-20 16:42:54 +01002945void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002946{
Olli Etuahof40319e2015-03-10 14:33:00 +02002947 if (type.isArray())
2948 {
2949 UNIMPLEMENTED();
2950 }
Jamie Madill32aab012015-01-27 14:12:26 -05002951 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002952
2953 if (visit == PreVisit)
2954 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002955 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002956
Daniel Bratell29190082015-02-20 16:42:54 +01002957 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002958 }
2959 else if (visit == InVisit)
2960 {
2961 out << ", ";
2962 }
2963 else if (visit == PostVisit)
2964 {
2965 out << ")";
2966 }
2967}
2968
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002969const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type,
2970 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002971{
Jamie Madill32aab012015-01-27 14:12:26 -05002972 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002973
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002974 const TConstantUnion *constUnionIterated = constUnion;
2975
Jamie Madill98493dd2013-07-08 14:39:03 -04002976 const TStructure* structure = type.getStruct();
2977 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002978 {
Jamie Madill033dae62014-06-18 12:56:28 -04002979 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002980
Jamie Madill98493dd2013-07-08 14:39:03 -04002981 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002982
Jamie Madill98493dd2013-07-08 14:39:03 -04002983 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002984 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002985 const TType *fieldType = fields[i]->type();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002986 constUnionIterated = writeConstantUnion(*fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002987
Jamie Madill98493dd2013-07-08 14:39:03 -04002988 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002989 {
2990 out << ", ";
2991 }
2992 }
2993
2994 out << ")";
2995 }
2996 else
2997 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002998 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002999 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04003000
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003001 if (writeType)
3002 {
Jamie Madill033dae62014-06-18 12:56:28 -04003003 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003004 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003005 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003006 if (writeType)
3007 {
3008 out << ")";
3009 }
3010 }
3011
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003012 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003013}
3014
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003015void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
3016{
3017 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
3018 outputTriplet(visit, preString.c_str(), ", ", ")");
3019}
3020
Jamie Madill37997142015-01-28 10:06:34 -05003021bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3022{
3023 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3024 expression->traverse(&searchSymbol);
3025
3026 if (searchSymbol.foundMatch())
3027 {
3028 // Type already printed
3029 out << "t" + str(mUniqueIndex) + " = ";
3030 expression->traverse(this);
3031 out << ", ";
3032 symbolNode->traverse(this);
3033 out << " = t" + str(mUniqueIndex);
3034
3035 mUniqueIndex++;
3036 return true;
3037 }
3038
3039 return false;
3040}
3041
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003042bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3043{
3044 // We support writing constant unions and constructors that only take constant unions as
3045 // parameters as HLSL literals.
3046 if (expression->getAsConstantUnion())
3047 {
3048 return true;
3049 }
3050 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3051 !expression->getAsAggregate()->isConstructor())
3052 {
3053 return false;
3054 }
3055 TIntermAggregate *constructor = expression->getAsAggregate();
3056 for (TIntermNode *&node : *constructor->getSequence())
3057 {
3058 if (!node->getAsConstantUnion())
3059 return false;
3060 }
3061 return true;
3062}
3063
3064bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3065 TIntermSymbol *symbolNode,
3066 TIntermTyped *expression)
3067{
3068 if (canWriteAsHLSLLiteral(expression))
3069 {
3070 symbolNode->traverse(this);
3071 if (expression->getType().isArray())
3072 {
3073 out << "[" << expression->getType().getArraySize() << "]";
3074 }
3075 out << " = {";
3076 if (expression->getAsConstantUnion())
3077 {
3078 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3079 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3080 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3081 }
3082 else
3083 {
3084 TIntermAggregate *constructor = expression->getAsAggregate();
3085 ASSERT(constructor != nullptr);
3086 for (TIntermNode *&node : *constructor->getSequence())
3087 {
3088 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3089 ASSERT(nodeConst);
3090 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3091 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3092 if (node != constructor->getSequence()->back())
3093 {
3094 out << ", ";
3095 }
3096 }
3097 }
3098 out << "}";
3099 return true;
3100 }
3101 return false;
3102}
3103
Jamie Madill37997142015-01-28 10:06:34 -05003104void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3105{
3106 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3107 << "\n"
3108 << "void initializeDeferredGlobals()\n"
3109 << "{\n";
3110
3111 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3112 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003113 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3114 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3115 if (binary != nullptr)
3116 {
3117 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3118 TIntermTyped *expression = binary->getRight();
3119 ASSERT(symbol);
3120 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003121
Olli Etuahod81ed842015-05-12 12:46:35 +03003122 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003123
Olli Etuahod81ed842015-05-12 12:46:35 +03003124 if (!writeSameSymbolInitializer(out, symbol, expression))
3125 {
3126 ASSERT(mInfoSinkStack.top() == &out);
3127 expression->traverse(this);
3128 }
3129 out << ";\n";
3130 }
3131 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003132 {
3133 ASSERT(mInfoSinkStack.top() == &out);
Olli Etuahod81ed842015-05-12 12:46:35 +03003134 writeSelection(selection);
Jamie Madill37997142015-01-28 10:06:34 -05003135 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003136 else
3137 {
3138 UNREACHABLE();
3139 }
Jamie Madill37997142015-01-28 10:06:34 -05003140 }
3141
3142 out << "}\n"
3143 << "\n";
3144}
3145
Jamie Madill55e79e02015-02-09 15:35:00 -05003146TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3147{
3148 const TFieldList &fields = structure.fields();
3149
3150 for (const auto &eqFunction : mStructEqualityFunctions)
3151 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003152 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003153 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003154 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003155 }
3156 }
3157
3158 const TString &structNameString = StructNameString(structure);
3159
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003160 StructEqualityFunction *function = new StructEqualityFunction();
3161 function->structure = &structure;
3162 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003163
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003164 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003165
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003166 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3167 << "{\n"
3168 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003169
3170 for (size_t i = 0; i < fields.size(); i++)
3171 {
3172 const TField *field = fields[i];
3173 const TType *fieldType = field->type();
3174
3175 const TString &fieldNameA = "a." + Decorate(field->name());
3176 const TString &fieldNameB = "b." + Decorate(field->name());
3177
3178 if (i > 0)
3179 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003180 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003181 }
3182
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003183 fnOut << "(";
3184 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3185 fnOut << fieldNameA;
3186 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3187 fnOut << fieldNameB;
3188 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3189 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003190 }
3191
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003192 fnOut << ";\n" << "}\n";
3193
3194 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003195
3196 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003197 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003198
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003199 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003200}
3201
Olli Etuaho7fb49552015-03-18 17:27:44 +02003202TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3203{
3204 for (const auto &eqFunction : mArrayEqualityFunctions)
3205 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003206 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003207 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003208 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003209 }
3210 }
3211
3212 const TString &typeName = TypeString(type);
3213
Olli Etuaho12690762015-03-31 12:55:28 +03003214 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003215 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003216
3217 TInfoSinkBase fnNameOut;
3218 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003219 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003220
3221 TType nonArrayType = type;
3222 nonArrayType.clearArrayness();
3223
3224 TInfoSinkBase fnOut;
3225
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003226 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003227 << typeName << " a[" << type.getArraySize() << "], "
3228 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003229 << "{\n"
3230 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3231 " {\n"
3232 " if (";
3233
3234 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3235 fnOut << "a[i]";
3236 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3237 fnOut << "b[i]";
3238 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3239
3240 fnOut << ") { return false; }\n"
3241 " }\n"
3242 " return true;\n"
3243 "}\n";
3244
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003245 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003246
3247 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003248 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003249
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003250 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003251}
3252
Olli Etuaho12690762015-03-31 12:55:28 +03003253TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3254{
3255 for (const auto &assignFunction : mArrayAssignmentFunctions)
3256 {
3257 if (assignFunction.type == type)
3258 {
3259 return assignFunction.functionName;
3260 }
3261 }
3262
3263 const TString &typeName = TypeString(type);
3264
3265 ArrayHelperFunction function;
3266 function.type = type;
3267
3268 TInfoSinkBase fnNameOut;
3269 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3270 function.functionName = fnNameOut.c_str();
3271
3272 TInfoSinkBase fnOut;
3273
3274 fnOut << "void " << function.functionName << "(out "
3275 << typeName << " a[" << type.getArraySize() << "], "
3276 << typeName << " b[" << type.getArraySize() << "])\n"
3277 << "{\n"
3278 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3279 " {\n"
3280 " a[i] = b[i];\n"
3281 " }\n"
3282 "}\n";
3283
3284 function.functionDefinition = fnOut.c_str();
3285
3286 mArrayAssignmentFunctions.push_back(function);
3287
3288 return function.functionName;
3289}
3290
Olli Etuaho9638c352015-04-01 14:34:52 +03003291TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3292{
3293 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3294 {
3295 if (constructIntoFunction.type == type)
3296 {
3297 return constructIntoFunction.functionName;
3298 }
3299 }
3300
3301 const TString &typeName = TypeString(type);
3302
3303 ArrayHelperFunction function;
3304 function.type = type;
3305
3306 TInfoSinkBase fnNameOut;
3307 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3308 function.functionName = fnNameOut.c_str();
3309
3310 TInfoSinkBase fnOut;
3311
3312 fnOut << "void " << function.functionName << "(out "
3313 << typeName << " a[" << type.getArraySize() << "]";
3314 for (int i = 0; i < type.getArraySize(); ++i)
3315 {
3316 fnOut << ", " << typeName << " b" << i;
3317 }
3318 fnOut << ")\n"
3319 "{\n";
3320
3321 for (int i = 0; i < type.getArraySize(); ++i)
3322 {
3323 fnOut << " a[" << i << "] = b" << i << ";\n";
3324 }
3325 fnOut << "}\n";
3326
3327 function.functionDefinition = fnOut.c_str();
3328
3329 mArrayConstructIntoFunctions.push_back(function);
3330
3331 return function.functionName;
3332}
3333
Jamie Madill2e295e22015-04-29 10:41:33 -04003334void OutputHLSL::ensureStructDefined(const TType &type)
3335{
3336 TStructure *structure = type.getStruct();
3337
3338 if (structure)
3339 {
3340 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3341 }
3342}
3343
3344
Olli Etuaho9638c352015-04-01 14:34:52 +03003345
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003346}