blob: 1f90abade034a6c6be0fb7c6fe906ec06e193842 [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/RewriteElseBlocks.h"
23#include "compiler/translator/SearchSymbol.h"
24#include "compiler/translator/StructureHLSL.h"
25#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050026#include "compiler/translator/UniformHLSL.h"
27#include "compiler/translator/UtilsHLSL.h"
28#include "compiler/translator/blocklayout.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050029#include "compiler/translator/util.h"
30
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031namespace sh
32{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000033
Nicolas Capense0ba27a2013-06-24 16:10:52 -040034TString OutputHLSL::TextureFunction::name() const
35{
36 TString name = "gl_texture";
37
Nicolas Capens6d232bb2013-07-08 15:56:38 -040038 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040039 {
40 name += "2D";
41 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040042 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040043 {
44 name += "3D";
45 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040046 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040047 {
48 name += "Cube";
49 }
50 else UNREACHABLE();
51
52 if (proj)
53 {
54 name += "Proj";
55 }
56
Nicolas Capensb1f45b72013-12-19 17:37:19 -050057 if (offset)
58 {
59 name += "Offset";
60 }
61
Nicolas Capens75fb4752013-07-10 15:14:47 -040062 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040063 {
Nicolas Capensfc014542014-02-18 14:47:13 -050064 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040065 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050066 case LOD: name += "Lod"; break;
67 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040068 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050069 case SIZE: name += "Size"; break;
70 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050071 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040072 default: UNREACHABLE();
73 }
74
75 return name + "(";
76}
77
78bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
79{
80 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040081 if (sampler > rhs.sampler) return false;
82
Nicolas Capense0ba27a2013-06-24 16:10:52 -040083 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040084 if (coords > rhs.coords) return false;
85
Nicolas Capense0ba27a2013-06-24 16:10:52 -040086 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040087 if (proj && !rhs.proj) return false;
88
89 if (!offset && rhs.offset) return true;
90 if (offset && !rhs.offset) return false;
91
Nicolas Capens75fb4752013-07-10 15:14:47 -040092 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040093 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040094
95 return false;
96}
97
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020098OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
99 const TExtensionBehavior &extensionBehavior,
100 const char *sourcePath, ShShaderOutput outputType,
101 int numRenderTargets, const std::vector<Uniform> &uniforms,
102 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400103 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200104 mShaderType(shaderType),
105 mShaderVersion(shaderVersion),
106 mExtensionBehavior(extensionBehavior),
107 mSourcePath(sourcePath),
108 mOutputType(outputType),
109 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700110 mCompileOptions(compileOptions),
111 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000113 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000114
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000115 mUsesFragColor = false;
116 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000117 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000118 mUsesFragCoord = false;
119 mUsesPointCoord = false;
120 mUsesFrontFacing = false;
121 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000122 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400123 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000124 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500125 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400126 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530127 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000128
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000129 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000130
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000131 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000132 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400133 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000134
135 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000136
Jamie Madill8daaba12014-06-13 10:04:33 -0400137 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200138 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400139
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000140 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000141 {
Arun Patole63419392015-03-13 11:51:07 +0530142 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
143 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
144 // In both cases total 3 uniform registers need to be reserved.
145 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000146 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000147
Jamie Madillf91ce812014-06-13 10:04:34 -0400148 // Reserve registers for the default uniform block and driver constants
149 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150}
151
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000152OutputHLSL::~OutputHLSL()
153{
Jamie Madill8daaba12014-06-13 10:04:33 -0400154 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400155 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200156 for (auto &eqFunction : mStructEqualityFunctions)
157 {
158 SafeDelete(eqFunction);
159 }
160 for (auto &eqFunction : mArrayEqualityFunctions)
161 {
162 SafeDelete(eqFunction);
163 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000164}
165
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200166void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000167{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200168 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400169 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000170
Jamie Madille53c98b2014-02-03 11:57:13 -0500171 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
172 // use a vertex attribute as a condition, and some related computation in the else block.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200173 if (mOutputType == SH_HLSL9_OUTPUT && mShaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500174 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200175 RewriteElseBlocks(treeRoot);
Jamie Madille53c98b2014-02-03 11:57:13 -0500176 }
177
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200178 BuiltInFunctionEmulator builtInFunctionEmulator;
179 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200180 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500181
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700182 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700183 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
184 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300185 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700186 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700187
Jamie Madill37997142015-01-28 10:06:34 -0500188 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500189 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200190 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500191 mInfoSinkStack.pop();
192
Jamie Madill37997142015-01-28 10:06:34 -0500193 mInfoSinkStack.push(&mFooter);
194 if (!mDeferredGlobalInitializers.empty())
195 {
196 writeDeferredGlobalInitializers(mFooter);
197 }
198 mInfoSinkStack.pop();
199
Jamie Madill32aab012015-01-27 14:12:26 -0500200 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200201 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500202 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000203
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200204 objSink << mHeader.c_str();
205 objSink << mBody.c_str();
206 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200207
208 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000209}
210
Jamie Madill570e04d2013-06-21 09:15:33 -0400211void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
212{
213 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
214 {
215 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
216
Jamie Madill32aab012015-01-27 14:12:26 -0500217 TInfoSinkBase structInfoSink;
218 mInfoSinkStack.push(&structInfoSink);
219
Jamie Madill570e04d2013-06-21 09:15:33 -0400220 // This will mark the necessary block elements as referenced
221 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500222
223 TString structName(structInfoSink.c_str());
224 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400225
226 mFlaggedStructOriginalNames[flaggedNode] = structName;
227
228 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
229 {
230 structName.erase(pos, 1);
231 }
232
233 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
234 }
235}
236
Jamie Madill4e1fd412014-07-10 17:50:10 -0400237const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
238{
239 return mUniformHLSL->getInterfaceBlockRegisterMap();
240}
241
Jamie Madill9fe25e92014-07-18 10:33:08 -0400242const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
243{
244 return mUniformHLSL->getUniformRegisterMap();
245}
246
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000247int OutputHLSL::vectorSize(const TType &type) const
248{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000249 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000250 int arraySize = type.isArray() ? type.getArraySize() : 1;
251
252 return elementSize * arraySize;
253}
254
Jamie Madill98493dd2013-07-08 14:39:03 -0400255TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400256{
257 TString init;
258
259 TString preIndentString;
260 TString fullIndentString;
261
262 for (int spaces = 0; spaces < (indent * 4); spaces++)
263 {
264 preIndentString += ' ';
265 }
266
267 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
268 {
269 fullIndentString += ' ';
270 }
271
272 init += preIndentString + "{\n";
273
Jamie Madill98493dd2013-07-08 14:39:03 -0400274 const TFieldList &fields = structure.fields();
275 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400276 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400278 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400279 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400280
Jamie Madill98493dd2013-07-08 14:39:03 -0400281 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400282 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400283 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400284 }
285 else
286 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400287 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400288 }
289 }
290
291 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
292
293 return init;
294}
295
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200296void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297{
Jamie Madill32aab012015-01-27 14:12:26 -0500298 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000300 TString varyings;
301 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400302 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000303
Jamie Madill829f59e2013-11-13 19:40:54 -0500304 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400305 {
306 TIntermTyped *structNode = flaggedStructIt->first;
307 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400308 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400309 const TString &originalName = mFlaggedStructOriginalNames[structNode];
310
Jamie Madill033dae62014-06-18 12:56:28 -0400311 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400312 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400313 flaggedStructs += "\n";
314 }
315
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000316 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
317 {
318 const TType &type = varying->second->getType();
319 const TString &name = varying->second->getSymbol();
320
321 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400322 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
323 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000324 }
325
326 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
327 {
328 const TType &type = attribute->second->getType();
329 const TString &name = attribute->second->getSymbol();
330
Jamie Madill033dae62014-06-18 12:56:28 -0400331 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000332 }
333
Jamie Madill8daaba12014-06-13 10:04:33 -0400334 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400335
Jamie Madillf91ce812014-06-13 10:04:34 -0400336 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
337 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
338
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200339 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500340 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200341 out << "\n// Equality functions\n\n";
342 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500343 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200344 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200345 }
346 }
Olli Etuaho12690762015-03-31 12:55:28 +0300347 if (!mArrayAssignmentFunctions.empty())
348 {
349 out << "\n// Assignment functions\n\n";
350 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
351 {
352 out << assignmentFunction.functionDefinition << "\n";
353 }
354 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300355 if (!mArrayConstructIntoFunctions.empty())
356 {
357 out << "\n// Array constructor functions\n\n";
358 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
359 {
360 out << constructIntoFunction.functionDefinition << "\n";
361 }
362 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200363
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500364 if (mUsesDiscardRewriting)
365 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400366 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500367 }
368
Nicolas Capens655fe362014-04-11 13:12:34 -0400369 if (mUsesNestedBreak)
370 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400371 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400372 }
373
Arun Patole44efa0b2015-03-04 17:11:05 +0530374 if (mRequiresIEEEStrictCompiling)
375 {
376 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
377 }
378
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400379 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
380 "#define LOOP [loop]\n"
381 "#define FLATTEN [flatten]\n"
382 "#else\n"
383 "#define LOOP\n"
384 "#define FLATTEN\n"
385 "#endif\n";
386
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200387 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200389 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
390 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000391
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000392 out << "// Varyings\n";
393 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400394 out << "\n";
395
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200396 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000397 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500398 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000399 {
Jamie Madill46131a32013-06-20 11:55:50 -0400400 const TString &variableName = outputVariableIt->first;
401 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400402
Jamie Madill033dae62014-06-18 12:56:28 -0400403 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400404 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000405 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000406 }
Jamie Madill46131a32013-06-20 11:55:50 -0400407 else
408 {
409 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
410
411 out << "static float4 gl_Color[" << numColorValues << "] =\n"
412 "{\n";
413 for (unsigned int i = 0; i < numColorValues; i++)
414 {
415 out << " float4(0, 0, 0, 0)";
416 if (i + 1 != numColorValues)
417 {
418 out << ",";
419 }
420 out << "\n";
421 }
422
423 out << "};\n";
424 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000425
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400426 if (mUsesFragDepth)
427 {
428 out << "static float gl_Depth = 0.0;\n";
429 }
430
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000431 if (mUsesFragCoord)
432 {
433 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
434 }
435
436 if (mUsesPointCoord)
437 {
438 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
439 }
440
441 if (mUsesFrontFacing)
442 {
443 out << "static bool gl_FrontFacing = false;\n";
444 }
445
446 out << "\n";
447
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000448 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000449 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000450 out << "struct gl_DepthRangeParameters\n"
451 "{\n"
452 " float near;\n"
453 " float far;\n"
454 " float diff;\n"
455 "};\n"
456 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000457 }
458
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000459 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000460 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000461 out << "cbuffer DriverConstants : register(b1)\n"
462 "{\n";
463
464 if (mUsesDepthRange)
465 {
466 out << " float3 dx_DepthRange : packoffset(c0);\n";
467 }
468
469 if (mUsesFragCoord)
470 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000471 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000472 }
473
474 if (mUsesFragCoord || mUsesFrontFacing)
475 {
476 out << " float3 dx_DepthFront : packoffset(c2);\n";
477 }
478
479 out << "};\n";
480 }
481 else
482 {
483 if (mUsesDepthRange)
484 {
485 out << "uniform float3 dx_DepthRange : register(c0);";
486 }
487
488 if (mUsesFragCoord)
489 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000490 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000491 }
492
493 if (mUsesFragCoord || mUsesFrontFacing)
494 {
495 out << "uniform float3 dx_DepthFront : register(c2);\n";
496 }
497 }
498
499 out << "\n";
500
501 if (mUsesDepthRange)
502 {
503 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
504 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000505 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000506
Jamie Madillf91ce812014-06-13 10:04:34 -0400507 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000508 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400509 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000510 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400511 out << flaggedStructs;
512 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000513 }
514
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000515 if (usingMRTExtension && mNumRenderTargets > 1)
516 {
517 out << "#define GL_USES_MRT\n";
518 }
519
520 if (mUsesFragColor)
521 {
522 out << "#define GL_USES_FRAG_COLOR\n";
523 }
524
525 if (mUsesFragData)
526 {
527 out << "#define GL_USES_FRAG_DATA\n";
528 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000530 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000532 out << "// Attributes\n";
533 out << attributes;
534 out << "\n"
535 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400536
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000537 if (mUsesPointSize)
538 {
539 out << "static float gl_PointSize = float(1);\n";
540 }
541
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000542 if (mUsesInstanceID)
543 {
544 out << "static int gl_InstanceID;";
545 }
546
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000547 out << "\n"
548 "// Varyings\n";
549 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000550 out << "\n";
551
552 if (mUsesDepthRange)
553 {
554 out << "struct gl_DepthRangeParameters\n"
555 "{\n"
556 " float near;\n"
557 " float far;\n"
558 " float diff;\n"
559 "};\n"
560 "\n";
561 }
562
563 if (mOutputType == SH_HLSL11_OUTPUT)
564 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800565 out << "cbuffer DriverConstants : register(b1)\n"
566 "{\n";
567
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000568 if (mUsesDepthRange)
569 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800570 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000571 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800572
Cooper Partine6664f02015-01-09 16:22:24 -0800573 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800574 // However, we declare it for all shaders (including Feature Level 10+).
575 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
576 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800577 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800578
579 out << "};\n"
580 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000581 }
582 else
583 {
584 if (mUsesDepthRange)
585 {
586 out << "uniform float3 dx_DepthRange : register(c0);\n";
587 }
588
Cooper Partine6664f02015-01-09 16:22:24 -0800589 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
590 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000591 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000592 }
593
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000594 if (mUsesDepthRange)
595 {
596 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
597 "\n";
598 }
599
Jamie Madillf91ce812014-06-13 10:04:34 -0400600 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000601 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400602 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000603 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400604 out << flaggedStructs;
605 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000606 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400607 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000608
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400609 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
610 {
611 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400612 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000613 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400614 switch(textureFunction->sampler)
615 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400616 case EbtSampler2D: out << "int2 "; break;
617 case EbtSampler3D: out << "int3 "; break;
618 case EbtSamplerCube: out << "int2 "; break;
619 case EbtSampler2DArray: out << "int3 "; break;
620 case EbtISampler2D: out << "int2 "; break;
621 case EbtISampler3D: out << "int3 "; break;
622 case EbtISamplerCube: out << "int2 "; break;
623 case EbtISampler2DArray: out << "int3 "; break;
624 case EbtUSampler2D: out << "int2 "; break;
625 case EbtUSampler3D: out << "int3 "; break;
626 case EbtUSamplerCube: out << "int2 "; break;
627 case EbtUSampler2DArray: out << "int3 "; break;
628 case EbtSampler2DShadow: out << "int2 "; break;
629 case EbtSamplerCubeShadow: out << "int2 "; break;
630 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400631 default: UNREACHABLE();
632 }
633 }
634 else // Sampling function
635 {
636 switch(textureFunction->sampler)
637 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400638 case EbtSampler2D: out << "float4 "; break;
639 case EbtSampler3D: out << "float4 "; break;
640 case EbtSamplerCube: out << "float4 "; break;
641 case EbtSampler2DArray: out << "float4 "; break;
642 case EbtISampler2D: out << "int4 "; break;
643 case EbtISampler3D: out << "int4 "; break;
644 case EbtISamplerCube: out << "int4 "; break;
645 case EbtISampler2DArray: out << "int4 "; break;
646 case EbtUSampler2D: out << "uint4 "; break;
647 case EbtUSampler3D: out << "uint4 "; break;
648 case EbtUSamplerCube: out << "uint4 "; break;
649 case EbtUSampler2DArray: out << "uint4 "; break;
650 case EbtSampler2DShadow: out << "float "; break;
651 case EbtSamplerCubeShadow: out << "float "; break;
652 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400653 default: UNREACHABLE();
654 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000655 }
656
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400657 // Function name
658 out << textureFunction->name();
659
660 // Argument list
661 int hlslCoords = 4;
662
663 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000664 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400665 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000666 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400667 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
668 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
669 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000670 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400671
Nicolas Capens75fb4752013-07-10 15:14:47 -0400672 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000673 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400674 case TextureFunction::IMPLICIT: break;
675 case TextureFunction::BIAS: hlslCoords = 4; break;
676 case TextureFunction::LOD: hlslCoords = 4; break;
677 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400678 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400679 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000680 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400681 }
682 else if (mOutputType == SH_HLSL11_OUTPUT)
683 {
684 switch(textureFunction->sampler)
685 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400686 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
687 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
688 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
689 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
690 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
691 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500692 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400693 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
694 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
695 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500696 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400697 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
698 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
699 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
700 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400701 default: UNREACHABLE();
702 }
703 }
704 else UNREACHABLE();
705
Nicolas Capensfc014542014-02-18 14:47:13 -0500706 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400707 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500708 switch(textureFunction->coords)
709 {
710 case 2: out << ", int2 t"; break;
711 case 3: out << ", int3 t"; break;
712 default: UNREACHABLE();
713 }
714 }
715 else // Floating-point coordinates (except textureSize)
716 {
717 switch(textureFunction->coords)
718 {
719 case 1: out << ", int lod"; break; // textureSize()
720 case 2: out << ", float2 t"; break;
721 case 3: out << ", float3 t"; break;
722 case 4: out << ", float4 t"; break;
723 default: UNREACHABLE();
724 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000725 }
726
Nicolas Capensd11d5492014-02-19 17:06:10 -0500727 if (textureFunction->method == TextureFunction::GRAD)
728 {
729 switch(textureFunction->sampler)
730 {
731 case EbtSampler2D:
732 case EbtISampler2D:
733 case EbtUSampler2D:
734 case EbtSampler2DArray:
735 case EbtISampler2DArray:
736 case EbtUSampler2DArray:
737 case EbtSampler2DShadow:
738 case EbtSampler2DArrayShadow:
739 out << ", float2 ddx, float2 ddy";
740 break;
741 case EbtSampler3D:
742 case EbtISampler3D:
743 case EbtUSampler3D:
744 case EbtSamplerCube:
745 case EbtISamplerCube:
746 case EbtUSamplerCube:
747 case EbtSamplerCubeShadow:
748 out << ", float3 ddx, float3 ddy";
749 break;
750 default: UNREACHABLE();
751 }
752 }
753
Nicolas Capens75fb4752013-07-10 15:14:47 -0400754 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000755 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400756 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400757 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400758 case TextureFunction::LOD: out << ", float lod"; break;
759 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400760 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400761 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500762 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500763 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400764 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000765 }
766
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500767 if (textureFunction->offset)
768 {
769 switch(textureFunction->sampler)
770 {
771 case EbtSampler2D: out << ", int2 offset"; break;
772 case EbtSampler3D: out << ", int3 offset"; break;
773 case EbtSampler2DArray: out << ", int2 offset"; break;
774 case EbtISampler2D: out << ", int2 offset"; break;
775 case EbtISampler3D: out << ", int3 offset"; break;
776 case EbtISampler2DArray: out << ", int2 offset"; break;
777 case EbtUSampler2D: out << ", int2 offset"; break;
778 case EbtUSampler3D: out << ", int3 offset"; break;
779 case EbtUSampler2DArray: out << ", int2 offset"; break;
780 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500781 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500782 default: UNREACHABLE();
783 }
784 }
785
Nicolas Capens84cfa122014-04-14 13:48:45 -0400786 if (textureFunction->method == TextureFunction::BIAS ||
787 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500788 {
789 out << ", float bias";
790 }
791
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400792 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400793 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400794
Nicolas Capens75fb4752013-07-10 15:14:47 -0400795 if (textureFunction->method == TextureFunction::SIZE)
796 {
797 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
798 {
799 if (IsSamplerArray(textureFunction->sampler))
800 {
801 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
802 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
803 }
804 else
805 {
806 out << " uint width; uint height; uint numberOfLevels;\n"
807 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
808 }
809 }
810 else if (IsSampler3D(textureFunction->sampler))
811 {
812 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
813 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
814 }
815 else UNREACHABLE();
816
817 switch(textureFunction->sampler)
818 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400819 case EbtSampler2D: out << " return int2(width, height);"; break;
820 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
821 case EbtSamplerCube: out << " return int2(width, height);"; break;
822 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
823 case EbtISampler2D: out << " return int2(width, height);"; break;
824 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
825 case EbtISamplerCube: out << " return int2(width, height);"; break;
826 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
827 case EbtUSampler2D: out << " return int2(width, height);"; break;
828 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
829 case EbtUSamplerCube: out << " return int2(width, height);"; break;
830 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
831 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
832 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
833 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400834 default: UNREACHABLE();
835 }
836 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400837 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400838 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500839 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
840 {
841 out << " float width; float height; float layers; float levels;\n";
842
843 out << " uint mip = 0;\n";
844
845 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
846
847 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
848 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
849 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
850 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
851
852 // FACE_POSITIVE_X = 000b
853 // FACE_NEGATIVE_X = 001b
854 // FACE_POSITIVE_Y = 010b
855 // FACE_NEGATIVE_Y = 011b
856 // FACE_POSITIVE_Z = 100b
857 // FACE_NEGATIVE_Z = 101b
858 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
859
860 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
861 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
862 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
863
864 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
865 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
866 }
867 else if (IsIntegerSampler(textureFunction->sampler) &&
868 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400869 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400870 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400871 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400872 if (IsSamplerArray(textureFunction->sampler))
873 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400874 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400875
Nicolas Capens9edebd62013-08-06 10:59:10 -0400876 if (textureFunction->method == TextureFunction::LOD0)
877 {
878 out << " uint mip = 0;\n";
879 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400880 else if (textureFunction->method == TextureFunction::LOD0BIAS)
881 {
882 out << " uint mip = bias;\n";
883 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400884 else
885 {
886 if (textureFunction->method == TextureFunction::IMPLICIT ||
887 textureFunction->method == TextureFunction::BIAS)
888 {
889 out << " x.GetDimensions(0, width, height, layers, levels);\n"
890 " float2 tSized = float2(t.x * width, t.y * height);\n"
891 " float dx = length(ddx(tSized));\n"
892 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500893 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400894
895 if (textureFunction->method == TextureFunction::BIAS)
896 {
897 out << " lod += bias;\n";
898 }
899 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500900 else if (textureFunction->method == TextureFunction::GRAD)
901 {
902 out << " x.GetDimensions(0, width, height, layers, levels);\n"
903 " float lod = log2(max(length(ddx), length(ddy)));\n";
904 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400905
906 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
907 }
908
909 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400910 }
911 else
912 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400913 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400914
Nicolas Capens9edebd62013-08-06 10:59:10 -0400915 if (textureFunction->method == TextureFunction::LOD0)
916 {
917 out << " uint mip = 0;\n";
918 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400919 else if (textureFunction->method == TextureFunction::LOD0BIAS)
920 {
921 out << " uint mip = bias;\n";
922 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400923 else
924 {
925 if (textureFunction->method == TextureFunction::IMPLICIT ||
926 textureFunction->method == TextureFunction::BIAS)
927 {
928 out << " x.GetDimensions(0, width, height, levels);\n"
929 " float2 tSized = float2(t.x * width, t.y * height);\n"
930 " float dx = length(ddx(tSized));\n"
931 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500932 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400933
934 if (textureFunction->method == TextureFunction::BIAS)
935 {
936 out << " lod += bias;\n";
937 }
938 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500939 else if (textureFunction->method == TextureFunction::LOD)
940 {
941 out << " x.GetDimensions(0, width, height, levels);\n";
942 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500943 else if (textureFunction->method == TextureFunction::GRAD)
944 {
945 out << " x.GetDimensions(0, width, height, levels);\n"
946 " float lod = log2(max(length(ddx), length(ddy)));\n";
947 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400948
949 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
950 }
951
952 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400953 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400954 }
955 else if (IsSampler3D(textureFunction->sampler))
956 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400957 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400958
Nicolas Capens9edebd62013-08-06 10:59:10 -0400959 if (textureFunction->method == TextureFunction::LOD0)
960 {
961 out << " uint mip = 0;\n";
962 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400963 else if (textureFunction->method == TextureFunction::LOD0BIAS)
964 {
965 out << " uint mip = bias;\n";
966 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400967 else
968 {
969 if (textureFunction->method == TextureFunction::IMPLICIT ||
970 textureFunction->method == TextureFunction::BIAS)
971 {
972 out << " x.GetDimensions(0, width, height, depth, levels);\n"
973 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
974 " float dx = length(ddx(tSized));\n"
975 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500976 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400977
978 if (textureFunction->method == TextureFunction::BIAS)
979 {
980 out << " lod += bias;\n";
981 }
982 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500983 else if (textureFunction->method == TextureFunction::GRAD)
984 {
985 out << " x.GetDimensions(0, width, height, depth, levels);\n"
986 " float lod = log2(max(length(ddx), length(ddy)));\n";
987 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400988
989 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
990 }
991
992 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400993 }
994 else UNREACHABLE();
995 }
996
997 out << " return ";
998
999 // HLSL intrinsic
1000 if (mOutputType == SH_HLSL9_OUTPUT)
1001 {
1002 switch(textureFunction->sampler)
1003 {
1004 case EbtSampler2D: out << "tex2D"; break;
1005 case EbtSamplerCube: out << "texCUBE"; break;
1006 default: UNREACHABLE();
1007 }
1008
Nicolas Capens75fb4752013-07-10 15:14:47 -04001009 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001010 {
1011 case TextureFunction::IMPLICIT: out << "(s, "; break;
1012 case TextureFunction::BIAS: out << "bias(s, "; break;
1013 case TextureFunction::LOD: out << "lod(s, "; break;
1014 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001015 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001016 default: UNREACHABLE();
1017 }
1018 }
1019 else if (mOutputType == SH_HLSL11_OUTPUT)
1020 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001021 if (textureFunction->method == TextureFunction::GRAD)
1022 {
1023 if (IsIntegerSampler(textureFunction->sampler))
1024 {
1025 out << "x.Load(";
1026 }
1027 else if (IsShadowSampler(textureFunction->sampler))
1028 {
1029 out << "x.SampleCmpLevelZero(s, ";
1030 }
1031 else
1032 {
1033 out << "x.SampleGrad(s, ";
1034 }
1035 }
1036 else if (IsIntegerSampler(textureFunction->sampler) ||
1037 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001038 {
1039 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001040 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001041 else if (IsShadowSampler(textureFunction->sampler))
1042 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001043 switch(textureFunction->method)
1044 {
1045 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1046 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1047 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1048 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1049 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1050 default: UNREACHABLE();
1051 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001052 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001053 else
1054 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001055 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001056 {
1057 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1058 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1059 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1060 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001061 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001062 default: UNREACHABLE();
1063 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001064 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001065 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001066 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001067
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001068 // Integer sampling requires integer addresses
1069 TString addressx = "";
1070 TString addressy = "";
1071 TString addressz = "";
1072 TString close = "";
1073
Nicolas Capensfc014542014-02-18 14:47:13 -05001074 if (IsIntegerSampler(textureFunction->sampler) ||
1075 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001076 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001077 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001078 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001079 case 2: out << "int3("; break;
1080 case 3: out << "int4("; break;
1081 default: UNREACHABLE();
1082 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001083
Nicolas Capensfc014542014-02-18 14:47:13 -05001084 // Convert from normalized floating-point to integer
1085 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001086 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001087 addressx = "int(floor(width * frac((";
1088 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001089
Nicolas Capensfc014542014-02-18 14:47:13 -05001090 if (IsSamplerArray(textureFunction->sampler))
1091 {
1092 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1093 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001094 else if (IsSamplerCube(textureFunction->sampler))
1095 {
1096 addressz = "((((";
1097 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001098 else
1099 {
1100 addressz = "int(floor(depth * frac((";
1101 }
1102
1103 close = "))))";
1104 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001105 }
1106 else
1107 {
1108 switch(hlslCoords)
1109 {
1110 case 2: out << "float2("; break;
1111 case 3: out << "float3("; break;
1112 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001113 default: UNREACHABLE();
1114 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001115 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001116
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001117 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001118
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001119 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001120 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001121 switch(textureFunction->coords)
1122 {
1123 case 3: proj = " / t.z"; break;
1124 case 4: proj = " / t.w"; break;
1125 default: UNREACHABLE();
1126 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001127 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001128
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001129 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001130
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001131 if (mOutputType == SH_HLSL9_OUTPUT)
1132 {
1133 if (hlslCoords >= 3)
1134 {
1135 if (textureFunction->coords < 3)
1136 {
1137 out << ", 0";
1138 }
1139 else
1140 {
1141 out << ", t.z" + proj;
1142 }
1143 }
1144
1145 if (hlslCoords == 4)
1146 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001147 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001148 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001149 case TextureFunction::BIAS: out << ", bias"; break;
1150 case TextureFunction::LOD: out << ", lod"; break;
1151 case TextureFunction::LOD0: out << ", 0"; break;
1152 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001153 default: UNREACHABLE();
1154 }
1155 }
1156
1157 out << "));\n";
1158 }
1159 else if (mOutputType == SH_HLSL11_OUTPUT)
1160 {
1161 if (hlslCoords >= 3)
1162 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001163 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1164 {
1165 out << ", face";
1166 }
1167 else
1168 {
1169 out << ", " + addressz + ("t.z" + proj) + close;
1170 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001171 }
1172
Nicolas Capensd11d5492014-02-19 17:06:10 -05001173 if (textureFunction->method == TextureFunction::GRAD)
1174 {
1175 if (IsIntegerSampler(textureFunction->sampler))
1176 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001177 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001178 }
1179 else if (IsShadowSampler(textureFunction->sampler))
1180 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001181 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001182 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001183 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001184 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1185 // The resulting third component of P' in the shadow forms is used as Dref
1186 out << "), t.z" << proj;
1187 }
1188 else
1189 {
1190 switch(textureFunction->coords)
1191 {
1192 case 3: out << "), t.z"; break;
1193 case 4: out << "), t.w"; break;
1194 default: UNREACHABLE();
1195 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001196 }
1197 }
1198 else
1199 {
1200 out << "), ddx, ddy";
1201 }
1202 }
1203 else if (IsIntegerSampler(textureFunction->sampler) ||
1204 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001205 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001206 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001207 }
1208 else if (IsShadowSampler(textureFunction->sampler))
1209 {
1210 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001211 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001212 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001213 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1214 // The resulting third component of P' in the shadow forms is used as Dref
1215 out << "), t.z" << proj;
1216 }
1217 else
1218 {
1219 switch(textureFunction->coords)
1220 {
1221 case 3: out << "), t.z"; break;
1222 case 4: out << "), t.w"; break;
1223 default: UNREACHABLE();
1224 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001225 }
1226 }
1227 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001228 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001229 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001230 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001231 case TextureFunction::IMPLICIT: out << ")"; break;
1232 case TextureFunction::BIAS: out << "), bias"; break;
1233 case TextureFunction::LOD: out << "), lod"; break;
1234 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001235 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001236 default: UNREACHABLE();
1237 }
1238 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001239
1240 if (textureFunction->offset)
1241 {
1242 out << ", offset";
1243 }
1244
1245 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001246 }
1247 else UNREACHABLE();
1248 }
1249
1250 out << "\n"
1251 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001252 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001253 }
1254
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001255 if (mUsesFragCoord)
1256 {
1257 out << "#define GL_USES_FRAG_COORD\n";
1258 }
1259
1260 if (mUsesPointCoord)
1261 {
1262 out << "#define GL_USES_POINT_COORD\n";
1263 }
1264
1265 if (mUsesFrontFacing)
1266 {
1267 out << "#define GL_USES_FRONT_FACING\n";
1268 }
1269
1270 if (mUsesPointSize)
1271 {
1272 out << "#define GL_USES_POINT_SIZE\n";
1273 }
1274
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001275 if (mUsesFragDepth)
1276 {
1277 out << "#define GL_USES_FRAG_DEPTH\n";
1278 }
1279
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001280 if (mUsesDepthRange)
1281 {
1282 out << "#define GL_USES_DEPTH_RANGE\n";
1283 }
1284
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001285 if (mUsesXor)
1286 {
1287 out << "bool xor(bool p, bool q)\n"
1288 "{\n"
1289 " return (p || q) && !(p && q);\n"
1290 "}\n"
1291 "\n";
1292 }
1293
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001294 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001295}
1296
1297void OutputHLSL::visitSymbol(TIntermSymbol *node)
1298{
Jamie Madill32aab012015-01-27 14:12:26 -05001299 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300
Jamie Madill570e04d2013-06-21 09:15:33 -04001301 // Handle accessing std140 structs by value
1302 if (mFlaggedStructMappedNames.count(node) > 0)
1303 {
1304 out << mFlaggedStructMappedNames[node];
1305 return;
1306 }
1307
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001308 TString name = node->getSymbol();
1309
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001310 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001311 {
1312 mUsesDepthRange = true;
1313 out << name;
1314 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001315 else
1316 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001317 TQualifier qualifier = node->getQualifier();
1318
1319 if (qualifier == EvqUniform)
1320 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001321 const TType &nodeType = node->getType();
1322 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001323
1324 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001325 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001326 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001327 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001328 else
1329 {
1330 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001331 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001332
Jamie Madill2e295e22015-04-29 10:41:33 -04001333 ensureStructDefined(nodeType);
1334
Jamie Madill033dae62014-06-18 12:56:28 -04001335 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001336 }
Jamie Madill19571812013-08-12 15:26:34 -07001337 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001338 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001339 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001340 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001341 }
Jamie Madill033dae62014-06-18 12:56:28 -04001342 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001343 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001344 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001345 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001346 }
Jamie Madill19571812013-08-12 15:26:34 -07001347 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001348 {
1349 mReferencedOutputVariables[name] = node;
1350 out << "out_" << name;
1351 }
1352 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001353 {
1354 out << "gl_Color[0]";
1355 mUsesFragColor = true;
1356 }
1357 else if (qualifier == EvqFragData)
1358 {
1359 out << "gl_Color";
1360 mUsesFragData = true;
1361 }
1362 else if (qualifier == EvqFragCoord)
1363 {
1364 mUsesFragCoord = true;
1365 out << name;
1366 }
1367 else if (qualifier == EvqPointCoord)
1368 {
1369 mUsesPointCoord = true;
1370 out << name;
1371 }
1372 else if (qualifier == EvqFrontFacing)
1373 {
1374 mUsesFrontFacing = true;
1375 out << name;
1376 }
1377 else if (qualifier == EvqPointSize)
1378 {
1379 mUsesPointSize = true;
1380 out << name;
1381 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001382 else if (qualifier == EvqInstanceID)
1383 {
1384 mUsesInstanceID = true;
1385 out << name;
1386 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001387 else if (name == "gl_FragDepthEXT")
1388 {
1389 mUsesFragDepth = true;
1390 out << "gl_Depth";
1391 }
Olli Etuaho78174db2015-04-21 16:14:00 +03001392 else if (node->isInternal())
Jamie Madille53c98b2014-02-03 11:57:13 -05001393 {
1394 out << name;
1395 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001396 else
1397 {
Jamie Madill033dae62014-06-18 12:56:28 -04001398 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001399 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001400 }
1401}
1402
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001403void OutputHLSL::visitRaw(TIntermRaw *node)
1404{
Jamie Madill32aab012015-01-27 14:12:26 -05001405 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001406}
1407
Olli Etuaho7fb49552015-03-18 17:27:44 +02001408void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1409{
1410 if (type.isScalar() && !type.isArray())
1411 {
1412 if (op == EOpEqual)
1413 {
1414 outputTriplet(visit, "(", " == ", ")", out);
1415 }
1416 else
1417 {
1418 outputTriplet(visit, "(", " != ", ")", out);
1419 }
1420 }
1421 else
1422 {
1423 if (visit == PreVisit && op == EOpNotEqual)
1424 {
1425 out << "!";
1426 }
1427
1428 if (type.isArray())
1429 {
1430 const TString &functionName = addArrayEqualityFunction(type);
1431 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1432 }
1433 else if (type.getBasicType() == EbtStruct)
1434 {
1435 const TStructure &structure = *type.getStruct();
1436 const TString &functionName = addStructEqualityFunction(structure);
1437 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1438 }
1439 else
1440 {
1441 ASSERT(type.isMatrix() || type.isVector());
1442 outputTriplet(visit, "all(", " == ", ")", out);
1443 }
1444 }
1445}
1446
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001447bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1448{
Jamie Madill32aab012015-01-27 14:12:26 -05001449 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001450
Jamie Madill570e04d2013-06-21 09:15:33 -04001451 // Handle accessing std140 structs by value
1452 if (mFlaggedStructMappedNames.count(node) > 0)
1453 {
1454 out << mFlaggedStructMappedNames[node];
1455 return false;
1456 }
1457
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001458 switch (node->getOp())
1459 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001460 case EOpAssign:
1461 if (node->getLeft()->isArray())
1462 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001463 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1464 if (rightAgg != nullptr && rightAgg->isConstructor())
1465 {
1466 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1467 out << functionName << "(";
1468 node->getLeft()->traverse(this);
1469 TIntermSequence *seq = rightAgg->getSequence();
1470 for (auto &arrayElement : *seq)
1471 {
1472 out << ", ";
1473 arrayElement->traverse(this);
1474 }
1475 out << ")";
1476 return false;
1477 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001478 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1479 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1480
1481 const TString &functionName = addArrayAssignmentFunction(node->getType());
1482 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001483 }
1484 else
1485 {
1486 outputTriplet(visit, "(", " = ", ")");
1487 }
1488 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001489 case EOpInitialize:
1490 if (visit == PreVisit)
1491 {
1492 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1493 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1494 // new variable is created before the assignment is evaluated), so we need to convert
1495 // this to "float t = x, x = t;".
1496
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001497 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001498 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001499 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001500
Jamie Madill37997142015-01-28 10:06:34 -05001501 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1502 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001503 {
Jamie Madill37997142015-01-28 10:06:34 -05001504 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001505 // after we initialize uniforms.
1506 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1507 deferredInit->setLeft(node->getLeft());
1508 deferredInit->setRight(node->getRight());
1509 deferredInit->setType(node->getType());
1510 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001511 const TString &initString = initializer(node->getType());
1512 node->setRight(new TIntermRaw(node->getType(), initString));
1513 }
1514 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1515 {
1516 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001517 return false;
1518 }
1519 }
1520 else if (visit == InVisit)
1521 {
1522 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001523 }
1524 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001525 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1526 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1527 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1528 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1529 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1530 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001531 if (visit == PreVisit)
1532 {
1533 out << "(";
1534 }
1535 else if (visit == InVisit)
1536 {
1537 out << " = mul(";
1538 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001539 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001540 }
1541 else
1542 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001543 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001544 }
1545 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001546 case EOpMatrixTimesMatrixAssign:
1547 if (visit == PreVisit)
1548 {
1549 out << "(";
1550 }
1551 else if (visit == InVisit)
1552 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001553 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001554 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001555 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001556 }
1557 else
1558 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001559 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001560 }
1561 break;
1562 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001563 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001564 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1565 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1566 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1567 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1568 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001569 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001570 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001571 const TType& leftType = node->getLeft()->getType();
1572 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001573 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001574 if (visit == PreVisit)
1575 {
1576 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1577 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001578 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001579 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001580 return false;
1581 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001582 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001583 else
1584 {
1585 outputTriplet(visit, "", "[", "]");
1586 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001587 }
1588 break;
1589 case EOpIndexIndirect:
1590 // We do not currently support indirect references to interface blocks
1591 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1592 outputTriplet(visit, "", "[", "]");
1593 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001594 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001595 if (visit == InVisit)
1596 {
1597 const TStructure* structure = node->getLeft()->getType().getStruct();
1598 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1599 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001600 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001601
1602 return false;
1603 }
1604 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001605 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001606 if (visit == InVisit)
1607 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001608 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1609 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1610 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001611 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001612
1613 return false;
1614 }
1615 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001616 case EOpVectorSwizzle:
1617 if (visit == InVisit)
1618 {
1619 out << ".";
1620
1621 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1622
1623 if (swizzle)
1624 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001625 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001627 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001628 {
1629 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1630
1631 if (element)
1632 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001633 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634
1635 switch (i)
1636 {
1637 case 0: out << "x"; break;
1638 case 1: out << "y"; break;
1639 case 2: out << "z"; break;
1640 case 3: out << "w"; break;
1641 default: UNREACHABLE();
1642 }
1643 }
1644 else UNREACHABLE();
1645 }
1646 }
1647 else UNREACHABLE();
1648
1649 return false; // Fully processed
1650 }
1651 break;
1652 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1653 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1654 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1655 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001656 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001657 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1658 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1659 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1660 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1661 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001662 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001663 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001664 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001665 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1667 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1668 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1669 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1670 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001671 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001672 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1673 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001674 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001675 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001676 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1677 ASSERT(!node->getRight()->hasSideEffects());
1678 outputTriplet(visit, "(", " || ", ")");
1679 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001680 case EOpLogicalXor:
1681 mUsesXor = true;
1682 outputTriplet(visit, "xor(", ", ", ")");
1683 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001684 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001685 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1686 ASSERT(!node->getRight()->hasSideEffects());
1687 outputTriplet(visit, "(", " && ", ")");
1688 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689 default: UNREACHABLE();
1690 }
1691
1692 return true;
1693}
1694
1695bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1696{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 switch (node->getOp())
1698 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001699 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001700 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001701 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1702 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001703 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001704 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1705 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1706 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1707 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001708 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1709 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1710 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1711 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1712 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1713 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1714 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1715 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001716 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1717 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1718 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1719 case EOpAsinh:
1720 ASSERT(node->getUseEmulatedFunction());
1721 writeEmulatedFunctionTriplet(visit, "asinh(");
1722 break;
1723 case EOpAcosh:
1724 ASSERT(node->getUseEmulatedFunction());
1725 writeEmulatedFunctionTriplet(visit, "acosh(");
1726 break;
1727 case EOpAtanh:
1728 ASSERT(node->getUseEmulatedFunction());
1729 writeEmulatedFunctionTriplet(visit, "atanh(");
1730 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001731 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1732 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1733 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1734 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1735 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1736 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1737 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1738 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1739 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001740 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1741 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1742 case EOpRoundEven:
1743 ASSERT(node->getUseEmulatedFunction());
1744 writeEmulatedFunctionTriplet(visit, "roundEven(");
1745 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001746 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1747 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301748 case EOpIsNan:
1749 outputTriplet(visit, "isnan(", "", ")");
1750 mRequiresIEEEStrictCompiling = true;
1751 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301752 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001753 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1754 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1755 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1756 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001757 case EOpPackSnorm2x16:
1758 ASSERT(node->getUseEmulatedFunction());
1759 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1760 break;
1761 case EOpPackUnorm2x16:
1762 ASSERT(node->getUseEmulatedFunction());
1763 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1764 break;
1765 case EOpPackHalf2x16:
1766 ASSERT(node->getUseEmulatedFunction());
1767 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1768 break;
1769 case EOpUnpackSnorm2x16:
1770 ASSERT(node->getUseEmulatedFunction());
1771 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1772 break;
1773 case EOpUnpackUnorm2x16:
1774 ASSERT(node->getUseEmulatedFunction());
1775 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1776 break;
1777 case EOpUnpackHalf2x16:
1778 ASSERT(node->getUseEmulatedFunction());
1779 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1780 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001781 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1782 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001783 case EOpDFdx:
1784 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1785 {
1786 outputTriplet(visit, "(", "", ", 0.0)");
1787 }
1788 else
1789 {
1790 outputTriplet(visit, "ddx(", "", ")");
1791 }
1792 break;
1793 case EOpDFdy:
1794 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1795 {
1796 outputTriplet(visit, "(", "", ", 0.0)");
1797 }
1798 else
1799 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001800 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001801 }
1802 break;
1803 case EOpFwidth:
1804 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1805 {
1806 outputTriplet(visit, "(", "", ", 0.0)");
1807 }
1808 else
1809 {
1810 outputTriplet(visit, "fwidth(", "", ")");
1811 }
1812 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001813 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1814 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001815 case EOpInverse:
1816 ASSERT(node->getUseEmulatedFunction());
1817 writeEmulatedFunctionTriplet(visit, "inverse(");
1818 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001819
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001820 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1821 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822 default: UNREACHABLE();
1823 }
1824
1825 return true;
1826}
1827
1828bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1829{
Jamie Madill32aab012015-01-27 14:12:26 -05001830 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832 switch (node->getOp())
1833 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001834 case EOpSequence:
1835 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001836 if (mInsideFunction)
1837 {
Jamie Madill075edd82013-07-08 13:30:19 -04001838 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001839 out << "{\n";
1840 }
1841
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001842 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001843 {
Jamie Madill075edd82013-07-08 13:30:19 -04001844 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001845
Olli Etuahoa6f22092015-05-08 18:31:10 +03001846 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001847
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001848 // Don't output ; after case labels, they're terminated by :
1849 // This is needed especially since outputting a ; after a case statement would turn empty
1850 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001851 // Also no need to output ; after selection (if) statements. This is done just for code clarity.
1852 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
1853 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
1854 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001855 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001856 }
1857
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001858 if (mInsideFunction)
1859 {
Jamie Madill075edd82013-07-08 13:30:19 -04001860 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001861 out << "}\n";
1862 }
1863
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001864 return false;
1865 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866 case EOpDeclaration:
1867 if (visit == PreVisit)
1868 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001869 TIntermSequence *sequence = node->getSequence();
1870 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001871 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001872
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001873 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001875 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001876
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001877 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001879 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001881 out << "static ";
1882 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001883
Olli Etuahoa6f22092015-05-08 18:31:10 +03001884 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001885
Olli Etuahoa6f22092015-05-08 18:31:10 +03001886 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001887
Olli Etuahoa6f22092015-05-08 18:31:10 +03001888 if (symbol)
1889 {
1890 symbol->traverse(this);
1891 out << ArrayString(symbol->getType());
1892 out << " = " + initializer(symbol->getType());
1893 }
1894 else
1895 {
1896 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001899 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1900 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001901 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001902 }
1903 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904 }
Jamie Madill033dae62014-06-18 12:56:28 -04001905 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001906 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001907 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001908 {
1909 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1910
1911 if (symbol)
1912 {
1913 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1914 mReferencedVaryings[symbol->getSymbol()] = symbol;
1915 }
1916 else
1917 {
1918 (*sit)->traverse(this);
1919 }
1920 }
1921 }
1922
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923 return false;
1924 }
1925 else if (visit == InVisit)
1926 {
1927 out << ", ";
1928 }
1929 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001930 case EOpInvariantDeclaration:
1931 // Do not do any translation
1932 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001933 case EOpPrototype:
1934 if (visit == PreVisit)
1935 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001936 size_t index = mCallDag.findIndex(node);
1937 // Skip the prototype if it is not implemented (and thus not used)
1938 if (index == CallDAG::InvalidIndex)
1939 {
1940 return false;
1941 }
1942
Olli Etuaho76acee82014-11-04 13:44:03 +02001943 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001944
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001945 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001946
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001947 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001948 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001949 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001950
1951 if (symbol)
1952 {
1953 out << argumentString(symbol);
1954
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001955 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001956 {
1957 out << ", ";
1958 }
1959 }
1960 else UNREACHABLE();
1961 }
1962
1963 out << ");\n";
1964
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001965 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001966 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1967 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001968 {
1969 mOutputLod0Function = true;
1970 node->traverse(this);
1971 mOutputLod0Function = false;
1972 }
1973
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001974 return false;
1975 }
1976 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001977 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978 case EOpFunction:
1979 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001980 ASSERT(mCurrentFunctionMetadata == nullptr);
alokp@chromium.org43884872010-03-30 00:08:52 +00001981 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982
Corentin Wallez1239ee92015-03-19 14:38:02 -07001983 size_t index = mCallDag.findIndex(node);
1984 ASSERT(index != CallDAG::InvalidIndex);
1985 mCurrentFunctionMetadata = &mASTMetadataList[index];
1986
Jamie Madill033dae62014-06-18 12:56:28 -04001987 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001988
1989 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001991 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001993 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994 {
Jamie Madill033dae62014-06-18 12:56:28 -04001995 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001996 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001997
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001998 TIntermSequence *sequence = node->getSequence();
1999 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002000
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002001 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002002 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002003 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002004
2005 if (symbol)
2006 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002007 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002008
2009 out << argumentString(symbol);
2010
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002011 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002012 {
2013 out << ", ";
2014 }
2015 }
2016 else UNREACHABLE();
2017 }
2018
2019 out << ")\n"
2020 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002021
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002022 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002023 {
2024 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002025 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002026 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002028
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002029 out << "}\n";
2030
Corentin Wallez1239ee92015-03-19 14:38:02 -07002031 mCurrentFunctionMetadata = nullptr;
2032
2033 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2034 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002035 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002036 ASSERT(name != "main");
2037 mOutputLod0Function = true;
2038 node->traverse(this);
2039 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002040 }
2041
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002042 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002043 }
2044 break;
2045 case EOpFunctionCall:
2046 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002047 TString name = TFunction::unmangleName(node->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002048 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002049
Corentin Wallez1239ee92015-03-19 14:38:02 -07002050 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002051 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002053 if (node->isArray())
2054 {
2055 UNIMPLEMENTED();
2056 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002057 size_t index = mCallDag.findIndex(node);
2058 ASSERT(index != CallDAG::InvalidIndex);
2059 lod0 &= mASTMetadataList[index].mNeedsLod0;
2060
Jamie Madill033dae62014-06-18 12:56:28 -04002061 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 }
2063 else
2064 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002065 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002066
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002067 TextureFunction textureFunction;
2068 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002069 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002070 textureFunction.method = TextureFunction::IMPLICIT;
2071 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002072 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002073
2074 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002075 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002076 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002077 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002078 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002079 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002080 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002081 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002082 }
Nicolas Capens46485082014-04-15 13:12:50 -04002083 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2084 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002085 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002086 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002087 }
Nicolas Capens46485082014-04-15 13:12:50 -04002088 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002089 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002090 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002091 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002092 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002093 else if (name == "textureSize")
2094 {
2095 textureFunction.method = TextureFunction::SIZE;
2096 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002097 else if (name == "textureOffset")
2098 {
2099 textureFunction.method = TextureFunction::IMPLICIT;
2100 textureFunction.offset = true;
2101 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002102 else if (name == "textureProjOffset")
2103 {
2104 textureFunction.method = TextureFunction::IMPLICIT;
2105 textureFunction.offset = true;
2106 textureFunction.proj = true;
2107 }
2108 else if (name == "textureLodOffset")
2109 {
2110 textureFunction.method = TextureFunction::LOD;
2111 textureFunction.offset = true;
2112 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002113 else if (name == "textureProjLodOffset")
2114 {
2115 textureFunction.method = TextureFunction::LOD;
2116 textureFunction.proj = true;
2117 textureFunction.offset = true;
2118 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002119 else if (name == "texelFetch")
2120 {
2121 textureFunction.method = TextureFunction::FETCH;
2122 }
2123 else if (name == "texelFetchOffset")
2124 {
2125 textureFunction.method = TextureFunction::FETCH;
2126 textureFunction.offset = true;
2127 }
Nicolas Capens46485082014-04-15 13:12:50 -04002128 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002129 {
2130 textureFunction.method = TextureFunction::GRAD;
2131 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002132 else if (name == "textureGradOffset")
2133 {
2134 textureFunction.method = TextureFunction::GRAD;
2135 textureFunction.offset = true;
2136 }
Nicolas Capens46485082014-04-15 13:12:50 -04002137 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002138 {
2139 textureFunction.method = TextureFunction::GRAD;
2140 textureFunction.proj = true;
2141 }
2142 else if (name == "textureProjGradOffset")
2143 {
2144 textureFunction.method = TextureFunction::GRAD;
2145 textureFunction.proj = true;
2146 textureFunction.offset = true;
2147 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002148 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002149
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002150 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002151 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002152 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2153
2154 if (textureFunction.offset)
2155 {
2156 mandatoryArgumentCount++;
2157 }
2158
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002159 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002160
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002161 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002162 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002163 if (bias)
2164 {
2165 textureFunction.method = TextureFunction::LOD0BIAS;
2166 }
2167 else
2168 {
2169 textureFunction.method = TextureFunction::LOD0;
2170 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002171 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002172 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002173 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002174 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002175 }
2176 }
2177
2178 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002179
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002180 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002182
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002183 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002184 {
2185 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2186 {
2187 out << "texture_";
2188 (*arg)->traverse(this);
2189 out << ", sampler_";
2190 }
2191
2192 (*arg)->traverse(this);
2193
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002194 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002195 {
2196 out << ", ";
2197 }
2198 }
2199
2200 out << ")";
2201
2202 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002203 }
2204 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002205 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002206 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2207 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2208 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2209 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2210 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2211 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2212 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2213 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2214 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2215 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2216 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2217 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2218 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2219 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2220 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2221 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2222 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2223 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2224 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002225 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002226 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002227 if (node->getType().isArray())
2228 {
2229 UNIMPLEMENTED();
2230 }
Jamie Madill033dae62014-06-18 12:56:28 -04002231 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002232 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002233 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002234 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002235 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002236 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2237 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2238 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2239 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2240 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2241 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002242 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002243 ASSERT(node->getUseEmulatedFunction());
2244 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002245 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002246 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002247 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002249 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002250 ASSERT(node->getUseEmulatedFunction());
2251 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 break;
2253 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2254 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2255 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
Arun Patoled94f6642015-05-18 16:25:12 +05302256 case EOpMix:
2257 {
2258 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2259 if (lastParamNode->getType().getBasicType() == EbtBool)
2260 {
2261 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2262 // so use emulated version.
2263 ASSERT(node->getUseEmulatedFunction());
2264 writeEmulatedFunctionTriplet(visit, "mix(");
2265 }
2266 else
2267 {
2268 outputTriplet(visit, "lerp(", ", ", ")");
2269 }
2270 }
2271 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2273 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2274 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2275 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2276 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002277 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002278 ASSERT(node->getUseEmulatedFunction());
2279 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002280 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2282 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002283 case EOpOuterProduct:
2284 ASSERT(node->getUseEmulatedFunction());
2285 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2286 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 default: UNREACHABLE();
2289 }
2290
2291 return true;
2292}
2293
Olli Etuahod81ed842015-05-12 12:46:35 +03002294void OutputHLSL::writeSelection(TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295{
Jamie Madill32aab012015-01-27 14:12:26 -05002296 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297
Olli Etuahoa6f22092015-05-08 18:31:10 +03002298 out << "if (";
2299
2300 node->getCondition()->traverse(this);
2301
2302 out << ")\n";
2303
2304 outputLineDirective(node->getLine().first_line);
2305 out << "{\n";
2306
2307 bool discard = false;
2308
2309 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002311 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002312
Olli Etuahoa6f22092015-05-08 18:31:10 +03002313 // Detect true discard
2314 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2315 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002316
Olli Etuahoa6f22092015-05-08 18:31:10 +03002317 outputLineDirective(node->getLine().first_line);
2318 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002319
Olli Etuahoa6f22092015-05-08 18:31:10 +03002320 if (node->getFalseBlock())
2321 {
2322 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002323
Olli Etuahoa6f22092015-05-08 18:31:10 +03002324 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002325 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326
Olli Etuahoa6f22092015-05-08 18:31:10 +03002327 outputLineDirective(node->getFalseBlock()->getLine().first_line);
2328 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002329
Olli Etuahoa6f22092015-05-08 18:31:10 +03002330 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002331 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002332
Olli Etuahoa6f22092015-05-08 18:31:10 +03002333 // Detect false discard
2334 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2335 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002336
Olli Etuahoa6f22092015-05-08 18:31:10 +03002337 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002338 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002339 {
2340 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002341 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002342}
2343
2344bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2345{
2346 TInfoSinkBase &out = getInfoSink();
2347
2348 ASSERT(!node->usesTernaryOperator());
2349
2350 if (!mInsideFunction)
2351 {
2352 // This is part of unfolded global initialization.
2353 mDeferredGlobalInitializers.push_back(node);
2354 return false;
2355 }
2356
2357 // D3D errors when there is a gradient operation in a loop in an unflattened if.
2358 if (mShaderType == GL_FRAGMENT_SHADER &&
2359 mCurrentFunctionMetadata->hasDiscontinuousLoop(node) &&
2360 mCurrentFunctionMetadata->hasGradientInCallGraph(node))
2361 {
2362 out << "FLATTEN ";
2363 }
2364
2365 writeSelection(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366
2367 return false;
2368}
2369
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002370bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002371{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002372 if (node->getStatementList())
2373 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002374 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002375 outputTriplet(visit, "switch (", ") ", "");
2376 // The curly braces get written when visiting the statementList aggregate
2377 }
2378 else
2379 {
2380 // No statementList, so it won't output curly braces
2381 outputTriplet(visit, "switch (", ") {", "}\n");
2382 }
2383 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002384}
2385
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002386bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002387{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002388 if (node->hasCondition())
2389 {
2390 outputTriplet(visit, "case (", "", "):\n");
2391 return true;
2392 }
2393 else
2394 {
2395 TInfoSinkBase &out = getInfoSink();
2396 out << "default:\n";
2397 return false;
2398 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002399}
2400
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002401void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2402{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002403 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002404}
2405
2406bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2407{
Nicolas Capens655fe362014-04-11 13:12:34 -04002408 mNestedLoopDepth++;
2409
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002410 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002411 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002412 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002413
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002414 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002415 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002416 if (handleExcessiveLoop(node))
2417 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002418 mInsideDiscontinuousLoop = wasDiscontinuous;
2419 mNestedLoopDepth--;
2420
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002421 return false;
2422 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002423 }
2424
Jamie Madill32aab012015-01-27 14:12:26 -05002425 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426
Corentin Wallez1239ee92015-03-19 14:38:02 -07002427 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002428 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002430 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002431
Jamie Madill075edd82013-07-08 13:30:19 -04002432 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002433 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 }
2435 else
2436 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002437 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002438
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439 if (node->getInit())
2440 {
2441 node->getInit()->traverse(this);
2442 }
2443
2444 out << "; ";
2445
alokp@chromium.org52813552010-11-16 18:36:09 +00002446 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002448 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 }
2450
2451 out << "; ";
2452
alokp@chromium.org52813552010-11-16 18:36:09 +00002453 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002455 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 }
2457
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002458 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002459
Jamie Madill075edd82013-07-08 13:30:19 -04002460 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002461 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 }
2463
2464 if (node->getBody())
2465 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002466 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 }
2468
Jamie Madill075edd82013-07-08 13:30:19 -04002469 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002470 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471
alokp@chromium.org52813552010-11-16 18:36:09 +00002472 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 {
Jamie Madill075edd82013-07-08 13:30:19 -04002474 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002475 out << "while(\n";
2476
alokp@chromium.org52813552010-11-16 18:36:09 +00002477 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478
daniel@transgaming.com73536982012-03-21 20:45:49 +00002479 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 }
2481
daniel@transgaming.com73536982012-03-21 20:45:49 +00002482 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002484 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002485 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002486
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487 return false;
2488}
2489
2490bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2491{
Jamie Madill32aab012015-01-27 14:12:26 -05002492 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493
2494 switch (node->getFlowOp())
2495 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002496 case EOpKill:
2497 outputTriplet(visit, "discard;\n", "", "");
2498 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002499 case EOpBreak:
2500 if (visit == PreVisit)
2501 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002502 if (mNestedLoopDepth > 1)
2503 {
2504 mUsesNestedBreak = true;
2505 }
2506
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002507 if (mExcessiveLoopIndex)
2508 {
2509 out << "{Break";
2510 mExcessiveLoopIndex->traverse(this);
2511 out << " = true; break;}\n";
2512 }
2513 else
2514 {
2515 out << "break;\n";
2516 }
2517 }
2518 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002519 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002520 case EOpReturn:
2521 if (visit == PreVisit)
2522 {
2523 if (node->getExpression())
2524 {
2525 out << "return ";
2526 }
2527 else
2528 {
2529 out << "return;\n";
2530 }
2531 }
2532 else if (visit == PostVisit)
2533 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002534 if (node->getExpression())
2535 {
2536 out << ";\n";
2537 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538 }
2539 break;
2540 default: UNREACHABLE();
2541 }
2542
2543 return true;
2544}
2545
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002546bool OutputHLSL::isSingleStatement(TIntermNode *node)
2547{
2548 TIntermAggregate *aggregate = node->getAsAggregate();
2549
2550 if (aggregate)
2551 {
2552 if (aggregate->getOp() == EOpSequence)
2553 {
2554 return false;
2555 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002556 else if (aggregate->getOp() == EOpDeclaration)
2557 {
2558 // Declaring multiple comma-separated variables must be considered multiple statements
2559 // because each individual declaration has side effects which are visible in the next.
2560 return false;
2561 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002562 else
2563 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002564 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002565 {
2566 if (!isSingleStatement(*sit))
2567 {
2568 return false;
2569 }
2570 }
2571
2572 return true;
2573 }
2574 }
2575
2576 return true;
2577}
2578
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002579// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2580// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002581bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2582{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002583 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002584 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002585
2586 // Parse loops of the form:
2587 // for(int index = initial; index [comparator] limit; index += increment)
2588 TIntermSymbol *index = NULL;
2589 TOperator comparator = EOpNull;
2590 int initial = 0;
2591 int limit = 0;
2592 int increment = 0;
2593
2594 // Parse index name and intial value
2595 if (node->getInit())
2596 {
2597 TIntermAggregate *init = node->getInit()->getAsAggregate();
2598
2599 if (init)
2600 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002601 TIntermSequence *sequence = init->getSequence();
2602 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002603
2604 if (variable && variable->getQualifier() == EvqTemporary)
2605 {
2606 TIntermBinary *assign = variable->getAsBinaryNode();
2607
2608 if (assign->getOp() == EOpInitialize)
2609 {
2610 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2611 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2612
2613 if (symbol && constant)
2614 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002615 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002616 {
2617 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002618 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002619 }
2620 }
2621 }
2622 }
2623 }
2624 }
2625
2626 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002627 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002628 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002629 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002630
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002631 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2632 {
2633 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2634
2635 if (constant)
2636 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002637 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002638 {
2639 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002640 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002641 }
2642 }
2643 }
2644 }
2645
2646 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002647 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002648 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002649 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2650 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002651
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002652 if (binaryTerminal)
2653 {
2654 TOperator op = binaryTerminal->getOp();
2655 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2656
2657 if (constant)
2658 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002659 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002660 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002661 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002662
2663 switch (op)
2664 {
2665 case EOpAddAssign: increment = value; break;
2666 case EOpSubAssign: increment = -value; break;
2667 default: UNIMPLEMENTED();
2668 }
2669 }
2670 }
2671 }
2672 else if (unaryTerminal)
2673 {
2674 TOperator op = unaryTerminal->getOp();
2675
2676 switch (op)
2677 {
2678 case EOpPostIncrement: increment = 1; break;
2679 case EOpPostDecrement: increment = -1; break;
2680 case EOpPreIncrement: increment = 1; break;
2681 case EOpPreDecrement: increment = -1; break;
2682 default: UNIMPLEMENTED();
2683 }
2684 }
2685 }
2686
2687 if (index != NULL && comparator != EOpNull && increment != 0)
2688 {
2689 if (comparator == EOpLessThanEqual)
2690 {
2691 comparator = EOpLessThan;
2692 limit += 1;
2693 }
2694
2695 if (comparator == EOpLessThan)
2696 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002697 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002698
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002699 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002700 {
2701 return false; // Not an excessive loop
2702 }
2703
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002704 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2705 mExcessiveLoopIndex = index;
2706
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002707 out << "{int ";
2708 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002709 out << ";\n"
2710 "bool Break";
2711 index->traverse(this);
2712 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002713
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002714 bool firstLoopFragment = true;
2715
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002716 while (iterations > 0)
2717 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002718 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002719
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002720 if (!firstLoopFragment)
2721 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002722 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002723 index->traverse(this);
2724 out << ") {\n";
2725 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002726
2727 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2728 {
2729 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2730 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002731
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002732 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002733 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002734
Corentin Wallez1239ee92015-03-19 14:38:02 -07002735 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002736 index->traverse(this);
2737 out << " = ";
2738 out << initial;
2739
2740 out << "; ";
2741 index->traverse(this);
2742 out << " < ";
2743 out << clampedLimit;
2744
2745 out << "; ";
2746 index->traverse(this);
2747 out << " += ";
2748 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002749 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002750
Jamie Madill075edd82013-07-08 13:30:19 -04002751 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002752 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002753
2754 if (node->getBody())
2755 {
2756 node->getBody()->traverse(this);
2757 }
2758
Jamie Madill075edd82013-07-08 13:30:19 -04002759 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002760 out << ";}\n";
2761
2762 if (!firstLoopFragment)
2763 {
2764 out << "}\n";
2765 }
2766
2767 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002768
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002769 initial += MAX_LOOP_ITERATIONS * increment;
2770 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002771 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002772
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002773 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002774
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002775 mExcessiveLoopIndex = restoreIndex;
2776
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002777 return true;
2778 }
2779 else UNIMPLEMENTED();
2780 }
2781
2782 return false; // Not handled as an excessive loop
2783}
2784
Olli Etuaho7fb49552015-03-18 17:27:44 +02002785void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002787 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002788 {
2789 out << preString;
2790 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002791 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002792 {
2793 out << inString;
2794 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002795 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796 {
2797 out << postString;
2798 }
2799}
2800
Olli Etuaho7fb49552015-03-18 17:27:44 +02002801void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2802{
2803 outputTriplet(visit, preString, inString, postString, getInfoSink());
2804}
2805
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002806void OutputHLSL::outputLineDirective(int line)
2807{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002808 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002809 {
Jamie Madill32aab012015-01-27 14:12:26 -05002810 TInfoSinkBase &out = getInfoSink();
2811
2812 out << "\n";
2813 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002814
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002815 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002816 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002817 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002818 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002819
Jamie Madill32aab012015-01-27 14:12:26 -05002820 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002821 }
2822}
2823
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002824TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2825{
2826 TQualifier qualifier = symbol->getQualifier();
2827 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002828 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002829
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002830 if (name.empty()) // HLSL demands named arguments, also for prototypes
2831 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002832 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002833 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002834 else if (!symbol->isInternal())
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002835 {
Jamie Madill033dae62014-06-18 12:56:28 -04002836 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002837 }
2838
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002839 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2840 {
Jamie Madill033dae62014-06-18 12:56:28 -04002841 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002842 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002843 }
2844
Jamie Madill033dae62014-06-18 12:56:28 -04002845 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002846}
2847
2848TString OutputHLSL::initializer(const TType &type)
2849{
2850 TString string;
2851
Jamie Madill94bf7f22013-07-08 13:31:15 -04002852 size_t size = type.getObjectSize();
2853 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002854 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002855 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002856
Jamie Madill94bf7f22013-07-08 13:31:15 -04002857 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002858 {
2859 string += ", ";
2860 }
2861 }
2862
daniel@transgaming.comead23042010-04-29 03:35:36 +00002863 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002864}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002865
Daniel Bratell29190082015-02-20 16:42:54 +01002866void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002867{
Olli Etuahof40319e2015-03-10 14:33:00 +02002868 if (type.isArray())
2869 {
2870 UNIMPLEMENTED();
2871 }
Jamie Madill32aab012015-01-27 14:12:26 -05002872 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002873
2874 if (visit == PreVisit)
2875 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002876 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002877
Daniel Bratell29190082015-02-20 16:42:54 +01002878 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002879 }
2880 else if (visit == InVisit)
2881 {
2882 out << ", ";
2883 }
2884 else if (visit == PostVisit)
2885 {
2886 out << ")";
2887 }
2888}
2889
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002890const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const TConstantUnion *constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002891{
Jamie Madill32aab012015-01-27 14:12:26 -05002892 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002893
Jamie Madill98493dd2013-07-08 14:39:03 -04002894 const TStructure* structure = type.getStruct();
2895 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002896 {
Jamie Madill033dae62014-06-18 12:56:28 -04002897 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002898
Jamie Madill98493dd2013-07-08 14:39:03 -04002899 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002900
Jamie Madill98493dd2013-07-08 14:39:03 -04002901 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002902 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002903 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002904 constUnion = writeConstantUnion(*fieldType, constUnion);
2905
Jamie Madill98493dd2013-07-08 14:39:03 -04002906 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002907 {
2908 out << ", ";
2909 }
2910 }
2911
2912 out << ")";
2913 }
2914 else
2915 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002916 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002917 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002918
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002919 if (writeType)
2920 {
Jamie Madill033dae62014-06-18 12:56:28 -04002921 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002922 }
2923
Jamie Madill94bf7f22013-07-08 13:31:15 -04002924 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002925 {
2926 switch (constUnion->getType())
2927 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002928 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002929 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002930 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002931 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002932 default: UNREACHABLE();
2933 }
2934
2935 if (i != size - 1)
2936 {
2937 out << ", ";
2938 }
2939 }
2940
2941 if (writeType)
2942 {
2943 out << ")";
2944 }
2945 }
2946
2947 return constUnion;
2948}
2949
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002950void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2951{
2952 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2953 outputTriplet(visit, preString.c_str(), ", ", ")");
2954}
2955
Jamie Madill37997142015-01-28 10:06:34 -05002956bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2957{
2958 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2959 expression->traverse(&searchSymbol);
2960
2961 if (searchSymbol.foundMatch())
2962 {
2963 // Type already printed
2964 out << "t" + str(mUniqueIndex) + " = ";
2965 expression->traverse(this);
2966 out << ", ";
2967 symbolNode->traverse(this);
2968 out << " = t" + str(mUniqueIndex);
2969
2970 mUniqueIndex++;
2971 return true;
2972 }
2973
2974 return false;
2975}
2976
2977void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2978{
2979 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2980 << "\n"
2981 << "void initializeDeferredGlobals()\n"
2982 << "{\n";
2983
2984 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2985 {
Olli Etuahod81ed842015-05-12 12:46:35 +03002986 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
2987 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
2988 if (binary != nullptr)
2989 {
2990 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
2991 TIntermTyped *expression = binary->getRight();
2992 ASSERT(symbol);
2993 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05002994
Olli Etuahod81ed842015-05-12 12:46:35 +03002995 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05002996
Olli Etuahod81ed842015-05-12 12:46:35 +03002997 if (!writeSameSymbolInitializer(out, symbol, expression))
2998 {
2999 ASSERT(mInfoSinkStack.top() == &out);
3000 expression->traverse(this);
3001 }
3002 out << ";\n";
3003 }
3004 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003005 {
3006 ASSERT(mInfoSinkStack.top() == &out);
Olli Etuahod81ed842015-05-12 12:46:35 +03003007 writeSelection(selection);
Jamie Madill37997142015-01-28 10:06:34 -05003008 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003009 else
3010 {
3011 UNREACHABLE();
3012 }
Jamie Madill37997142015-01-28 10:06:34 -05003013 }
3014
3015 out << "}\n"
3016 << "\n";
3017}
3018
Jamie Madill55e79e02015-02-09 15:35:00 -05003019TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3020{
3021 const TFieldList &fields = structure.fields();
3022
3023 for (const auto &eqFunction : mStructEqualityFunctions)
3024 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003025 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003026 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003027 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003028 }
3029 }
3030
3031 const TString &structNameString = StructNameString(structure);
3032
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003033 StructEqualityFunction *function = new StructEqualityFunction();
3034 function->structure = &structure;
3035 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003036
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003037 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003038
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003039 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3040 << "{\n"
3041 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003042
3043 for (size_t i = 0; i < fields.size(); i++)
3044 {
3045 const TField *field = fields[i];
3046 const TType *fieldType = field->type();
3047
3048 const TString &fieldNameA = "a." + Decorate(field->name());
3049 const TString &fieldNameB = "b." + Decorate(field->name());
3050
3051 if (i > 0)
3052 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003053 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003054 }
3055
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003056 fnOut << "(";
3057 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3058 fnOut << fieldNameA;
3059 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3060 fnOut << fieldNameB;
3061 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3062 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003063 }
3064
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003065 fnOut << ";\n" << "}\n";
3066
3067 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003068
3069 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003070 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003071
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003072 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003073}
3074
Olli Etuaho7fb49552015-03-18 17:27:44 +02003075TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3076{
3077 for (const auto &eqFunction : mArrayEqualityFunctions)
3078 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003079 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003080 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003081 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003082 }
3083 }
3084
3085 const TString &typeName = TypeString(type);
3086
Olli Etuaho12690762015-03-31 12:55:28 +03003087 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003088 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003089
3090 TInfoSinkBase fnNameOut;
3091 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003092 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003093
3094 TType nonArrayType = type;
3095 nonArrayType.clearArrayness();
3096
3097 TInfoSinkBase fnOut;
3098
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003099 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003100 << typeName << " a[" << type.getArraySize() << "], "
3101 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003102 << "{\n"
3103 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3104 " {\n"
3105 " if (";
3106
3107 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3108 fnOut << "a[i]";
3109 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3110 fnOut << "b[i]";
3111 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3112
3113 fnOut << ") { return false; }\n"
3114 " }\n"
3115 " return true;\n"
3116 "}\n";
3117
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003118 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003119
3120 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003121 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003122
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003123 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003124}
3125
Olli Etuaho12690762015-03-31 12:55:28 +03003126TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3127{
3128 for (const auto &assignFunction : mArrayAssignmentFunctions)
3129 {
3130 if (assignFunction.type == type)
3131 {
3132 return assignFunction.functionName;
3133 }
3134 }
3135
3136 const TString &typeName = TypeString(type);
3137
3138 ArrayHelperFunction function;
3139 function.type = type;
3140
3141 TInfoSinkBase fnNameOut;
3142 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3143 function.functionName = fnNameOut.c_str();
3144
3145 TInfoSinkBase fnOut;
3146
3147 fnOut << "void " << function.functionName << "(out "
3148 << typeName << " a[" << type.getArraySize() << "], "
3149 << typeName << " b[" << type.getArraySize() << "])\n"
3150 << "{\n"
3151 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3152 " {\n"
3153 " a[i] = b[i];\n"
3154 " }\n"
3155 "}\n";
3156
3157 function.functionDefinition = fnOut.c_str();
3158
3159 mArrayAssignmentFunctions.push_back(function);
3160
3161 return function.functionName;
3162}
3163
Olli Etuaho9638c352015-04-01 14:34:52 +03003164TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3165{
3166 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3167 {
3168 if (constructIntoFunction.type == type)
3169 {
3170 return constructIntoFunction.functionName;
3171 }
3172 }
3173
3174 const TString &typeName = TypeString(type);
3175
3176 ArrayHelperFunction function;
3177 function.type = type;
3178
3179 TInfoSinkBase fnNameOut;
3180 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3181 function.functionName = fnNameOut.c_str();
3182
3183 TInfoSinkBase fnOut;
3184
3185 fnOut << "void " << function.functionName << "(out "
3186 << typeName << " a[" << type.getArraySize() << "]";
3187 for (int i = 0; i < type.getArraySize(); ++i)
3188 {
3189 fnOut << ", " << typeName << " b" << i;
3190 }
3191 fnOut << ")\n"
3192 "{\n";
3193
3194 for (int i = 0; i < type.getArraySize(); ++i)
3195 {
3196 fnOut << " a[" << i << "] = b" << i << ";\n";
3197 }
3198 fnOut << "}\n";
3199
3200 function.functionDefinition = fnOut.c_str();
3201
3202 mArrayConstructIntoFunctions.push_back(function);
3203
3204 return function.functionName;
3205}
3206
Jamie Madill2e295e22015-04-29 10:41:33 -04003207void OutputHLSL::ensureStructDefined(const TType &type)
3208{
3209 TStructure *structure = type.getStruct();
3210
3211 if (structure)
3212 {
3213 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3214 }
3215}
3216
3217
Olli Etuaho9638c352015-04-01 14:34:52 +03003218
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003219}