blob: 903f6e3e0b18c017dda09d6c4379d0cae06a323a [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
1505 // after we initialize other globals: uniforms, attributes and varyings.
1506 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1507 const TString &initString = initializer(node->getType());
1508 node->setRight(new TIntermRaw(node->getType(), initString));
1509 }
1510 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1511 {
1512 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001513 return false;
1514 }
1515 }
1516 else if (visit == InVisit)
1517 {
1518 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001519 }
1520 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001521 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1522 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1523 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1524 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1525 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1526 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001527 if (visit == PreVisit)
1528 {
1529 out << "(";
1530 }
1531 else if (visit == InVisit)
1532 {
1533 out << " = mul(";
1534 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001535 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001536 }
1537 else
1538 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001539 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001540 }
1541 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001542 case EOpMatrixTimesMatrixAssign:
1543 if (visit == PreVisit)
1544 {
1545 out << "(";
1546 }
1547 else if (visit == InVisit)
1548 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001549 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001550 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001551 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001552 }
1553 else
1554 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001555 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001556 }
1557 break;
1558 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001559 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001560 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1561 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1562 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1563 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1564 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001565 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001566 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001567 const TType& leftType = node->getLeft()->getType();
1568 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001569 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001570 if (visit == PreVisit)
1571 {
1572 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1573 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001574 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001575 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001576 return false;
1577 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001578 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001579 else
1580 {
1581 outputTriplet(visit, "", "[", "]");
1582 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001583 }
1584 break;
1585 case EOpIndexIndirect:
1586 // We do not currently support indirect references to interface blocks
1587 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1588 outputTriplet(visit, "", "[", "]");
1589 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001590 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001591 if (visit == InVisit)
1592 {
1593 const TStructure* structure = node->getLeft()->getType().getStruct();
1594 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1595 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001596 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001597
1598 return false;
1599 }
1600 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001601 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001602 if (visit == InVisit)
1603 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001604 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1605 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1606 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001607 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001608
1609 return false;
1610 }
1611 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001612 case EOpVectorSwizzle:
1613 if (visit == InVisit)
1614 {
1615 out << ".";
1616
1617 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1618
1619 if (swizzle)
1620 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001621 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001622
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001623 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001624 {
1625 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1626
1627 if (element)
1628 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001629 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001630
1631 switch (i)
1632 {
1633 case 0: out << "x"; break;
1634 case 1: out << "y"; break;
1635 case 2: out << "z"; break;
1636 case 3: out << "w"; break;
1637 default: UNREACHABLE();
1638 }
1639 }
1640 else UNREACHABLE();
1641 }
1642 }
1643 else UNREACHABLE();
1644
1645 return false; // Fully processed
1646 }
1647 break;
1648 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1649 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1650 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1651 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001652 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001653 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1654 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1655 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1656 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1657 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001658 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001659 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001660 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001661 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001662 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1663 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1664 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1665 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1666 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001667 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001668 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1669 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001670 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001671 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001672 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1673 ASSERT(!node->getRight()->hasSideEffects());
1674 outputTriplet(visit, "(", " || ", ")");
1675 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001676 case EOpLogicalXor:
1677 mUsesXor = true;
1678 outputTriplet(visit, "xor(", ", ", ")");
1679 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001680 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001681 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1682 ASSERT(!node->getRight()->hasSideEffects());
1683 outputTriplet(visit, "(", " && ", ")");
1684 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685 default: UNREACHABLE();
1686 }
1687
1688 return true;
1689}
1690
1691bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1692{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693 switch (node->getOp())
1694 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001695 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001696 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001697 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1698 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001699 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001700 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1701 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1702 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1703 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001704 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1705 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1706 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1707 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1708 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1709 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1710 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1711 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001712 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1713 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1714 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1715 case EOpAsinh:
1716 ASSERT(node->getUseEmulatedFunction());
1717 writeEmulatedFunctionTriplet(visit, "asinh(");
1718 break;
1719 case EOpAcosh:
1720 ASSERT(node->getUseEmulatedFunction());
1721 writeEmulatedFunctionTriplet(visit, "acosh(");
1722 break;
1723 case EOpAtanh:
1724 ASSERT(node->getUseEmulatedFunction());
1725 writeEmulatedFunctionTriplet(visit, "atanh(");
1726 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001727 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1728 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1729 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1730 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1731 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1732 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1733 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1734 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1735 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001736 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1737 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1738 case EOpRoundEven:
1739 ASSERT(node->getUseEmulatedFunction());
1740 writeEmulatedFunctionTriplet(visit, "roundEven(");
1741 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001742 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1743 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301744 case EOpIsNan:
1745 outputTriplet(visit, "isnan(", "", ")");
1746 mRequiresIEEEStrictCompiling = true;
1747 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301748 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001749 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1750 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1751 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1752 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001753 case EOpPackSnorm2x16:
1754 ASSERT(node->getUseEmulatedFunction());
1755 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1756 break;
1757 case EOpPackUnorm2x16:
1758 ASSERT(node->getUseEmulatedFunction());
1759 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1760 break;
1761 case EOpPackHalf2x16:
1762 ASSERT(node->getUseEmulatedFunction());
1763 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1764 break;
1765 case EOpUnpackSnorm2x16:
1766 ASSERT(node->getUseEmulatedFunction());
1767 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1768 break;
1769 case EOpUnpackUnorm2x16:
1770 ASSERT(node->getUseEmulatedFunction());
1771 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1772 break;
1773 case EOpUnpackHalf2x16:
1774 ASSERT(node->getUseEmulatedFunction());
1775 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1776 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001777 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1778 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001779 case EOpDFdx:
1780 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1781 {
1782 outputTriplet(visit, "(", "", ", 0.0)");
1783 }
1784 else
1785 {
1786 outputTriplet(visit, "ddx(", "", ")");
1787 }
1788 break;
1789 case EOpDFdy:
1790 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1791 {
1792 outputTriplet(visit, "(", "", ", 0.0)");
1793 }
1794 else
1795 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001796 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001797 }
1798 break;
1799 case EOpFwidth:
1800 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1801 {
1802 outputTriplet(visit, "(", "", ", 0.0)");
1803 }
1804 else
1805 {
1806 outputTriplet(visit, "fwidth(", "", ")");
1807 }
1808 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001809 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1810 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001811 case EOpInverse:
1812 ASSERT(node->getUseEmulatedFunction());
1813 writeEmulatedFunctionTriplet(visit, "inverse(");
1814 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001815
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001816 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1817 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001818 default: UNREACHABLE();
1819 }
1820
1821 return true;
1822}
1823
1824bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1825{
Jamie Madill32aab012015-01-27 14:12:26 -05001826 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828 switch (node->getOp())
1829 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001830 case EOpSequence:
1831 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001832 if (mInsideFunction)
1833 {
Jamie Madill075edd82013-07-08 13:30:19 -04001834 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001835 out << "{\n";
1836 }
1837
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001838 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001839 {
Jamie Madill075edd82013-07-08 13:30:19 -04001840 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001841
Olli Etuahoa6f22092015-05-08 18:31:10 +03001842 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001843
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001844 // Don't output ; after case labels, they're terminated by :
1845 // This is needed especially since outputting a ; after a case statement would turn empty
1846 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001847 // Also no need to output ; after selection (if) statements. This is done just for code clarity.
1848 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
1849 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
1850 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001851 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001852 }
1853
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001854 if (mInsideFunction)
1855 {
Jamie Madill075edd82013-07-08 13:30:19 -04001856 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001857 out << "}\n";
1858 }
1859
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001860 return false;
1861 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862 case EOpDeclaration:
1863 if (visit == PreVisit)
1864 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001865 TIntermSequence *sequence = node->getSequence();
1866 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001867 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001868
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001869 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001871 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001872
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001873 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001875 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001876 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001877 out << "static ";
1878 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001879
Olli Etuahoa6f22092015-05-08 18:31:10 +03001880 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001881
Olli Etuahoa6f22092015-05-08 18:31:10 +03001882 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001883
Olli Etuahoa6f22092015-05-08 18:31:10 +03001884 if (symbol)
1885 {
1886 symbol->traverse(this);
1887 out << ArrayString(symbol->getType());
1888 out << " = " + initializer(symbol->getType());
1889 }
1890 else
1891 {
1892 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001895 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1896 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001897 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001898 }
1899 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900 }
Jamie Madill033dae62014-06-18 12:56:28 -04001901 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001902 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001903 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001904 {
1905 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1906
1907 if (symbol)
1908 {
1909 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1910 mReferencedVaryings[symbol->getSymbol()] = symbol;
1911 }
1912 else
1913 {
1914 (*sit)->traverse(this);
1915 }
1916 }
1917 }
1918
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919 return false;
1920 }
1921 else if (visit == InVisit)
1922 {
1923 out << ", ";
1924 }
1925 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001926 case EOpInvariantDeclaration:
1927 // Do not do any translation
1928 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001929 case EOpPrototype:
1930 if (visit == PreVisit)
1931 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001932 size_t index = mCallDag.findIndex(node);
1933 // Skip the prototype if it is not implemented (and thus not used)
1934 if (index == CallDAG::InvalidIndex)
1935 {
1936 return false;
1937 }
1938
Olli Etuaho76acee82014-11-04 13:44:03 +02001939 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001940
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001941 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001942
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001943 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001944 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001945 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001946
1947 if (symbol)
1948 {
1949 out << argumentString(symbol);
1950
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001951 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001952 {
1953 out << ", ";
1954 }
1955 }
1956 else UNREACHABLE();
1957 }
1958
1959 out << ");\n";
1960
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001961 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001962 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1963 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001964 {
1965 mOutputLod0Function = true;
1966 node->traverse(this);
1967 mOutputLod0Function = false;
1968 }
1969
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001970 return false;
1971 }
1972 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001973 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001974 case EOpFunction:
1975 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001976 ASSERT(mCurrentFunctionMetadata == nullptr);
alokp@chromium.org43884872010-03-30 00:08:52 +00001977 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978
Corentin Wallez1239ee92015-03-19 14:38:02 -07001979 size_t index = mCallDag.findIndex(node);
1980 ASSERT(index != CallDAG::InvalidIndex);
1981 mCurrentFunctionMetadata = &mASTMetadataList[index];
1982
Jamie Madill033dae62014-06-18 12:56:28 -04001983 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001984
1985 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001986 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001987 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001988 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001989 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990 {
Jamie Madill033dae62014-06-18 12:56:28 -04001991 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001992 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001993
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001994 TIntermSequence *sequence = node->getSequence();
1995 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001996
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001997 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001998 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001999 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002000
2001 if (symbol)
2002 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002003 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002004
2005 out << argumentString(symbol);
2006
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002007 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002008 {
2009 out << ", ";
2010 }
2011 }
2012 else UNREACHABLE();
2013 }
2014
2015 out << ")\n"
2016 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002017
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002018 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002019 {
2020 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002021 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002022 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002024
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002025 out << "}\n";
2026
Corentin Wallez1239ee92015-03-19 14:38:02 -07002027 mCurrentFunctionMetadata = nullptr;
2028
2029 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2030 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002031 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002032 ASSERT(name != "main");
2033 mOutputLod0Function = true;
2034 node->traverse(this);
2035 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002036 }
2037
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002038 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002039 }
2040 break;
2041 case EOpFunctionCall:
2042 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002043 TString name = TFunction::unmangleName(node->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002044 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002045
Corentin Wallez1239ee92015-03-19 14:38:02 -07002046 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002047 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002048 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002049 if (node->isArray())
2050 {
2051 UNIMPLEMENTED();
2052 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002053 size_t index = mCallDag.findIndex(node);
2054 ASSERT(index != CallDAG::InvalidIndex);
2055 lod0 &= mASTMetadataList[index].mNeedsLod0;
2056
Jamie Madill033dae62014-06-18 12:56:28 -04002057 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002058 }
2059 else
2060 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002061 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002062
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002063 TextureFunction textureFunction;
2064 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002065 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002066 textureFunction.method = TextureFunction::IMPLICIT;
2067 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002068 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002069
2070 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002071 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002072 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002073 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002074 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002075 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002076 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002077 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002078 }
Nicolas Capens46485082014-04-15 13:12:50 -04002079 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2080 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002081 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002082 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002083 }
Nicolas Capens46485082014-04-15 13:12:50 -04002084 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002085 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002086 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002087 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002088 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002089 else if (name == "textureSize")
2090 {
2091 textureFunction.method = TextureFunction::SIZE;
2092 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002093 else if (name == "textureOffset")
2094 {
2095 textureFunction.method = TextureFunction::IMPLICIT;
2096 textureFunction.offset = true;
2097 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002098 else if (name == "textureProjOffset")
2099 {
2100 textureFunction.method = TextureFunction::IMPLICIT;
2101 textureFunction.offset = true;
2102 textureFunction.proj = true;
2103 }
2104 else if (name == "textureLodOffset")
2105 {
2106 textureFunction.method = TextureFunction::LOD;
2107 textureFunction.offset = true;
2108 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002109 else if (name == "textureProjLodOffset")
2110 {
2111 textureFunction.method = TextureFunction::LOD;
2112 textureFunction.proj = true;
2113 textureFunction.offset = true;
2114 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002115 else if (name == "texelFetch")
2116 {
2117 textureFunction.method = TextureFunction::FETCH;
2118 }
2119 else if (name == "texelFetchOffset")
2120 {
2121 textureFunction.method = TextureFunction::FETCH;
2122 textureFunction.offset = true;
2123 }
Nicolas Capens46485082014-04-15 13:12:50 -04002124 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002125 {
2126 textureFunction.method = TextureFunction::GRAD;
2127 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002128 else if (name == "textureGradOffset")
2129 {
2130 textureFunction.method = TextureFunction::GRAD;
2131 textureFunction.offset = true;
2132 }
Nicolas Capens46485082014-04-15 13:12:50 -04002133 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002134 {
2135 textureFunction.method = TextureFunction::GRAD;
2136 textureFunction.proj = true;
2137 }
2138 else if (name == "textureProjGradOffset")
2139 {
2140 textureFunction.method = TextureFunction::GRAD;
2141 textureFunction.proj = true;
2142 textureFunction.offset = true;
2143 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002144 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002145
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002146 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002147 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002148 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2149
2150 if (textureFunction.offset)
2151 {
2152 mandatoryArgumentCount++;
2153 }
2154
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002155 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002156
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002157 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002158 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002159 if (bias)
2160 {
2161 textureFunction.method = TextureFunction::LOD0BIAS;
2162 }
2163 else
2164 {
2165 textureFunction.method = TextureFunction::LOD0;
2166 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002167 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002168 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002169 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002170 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002171 }
2172 }
2173
2174 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002175
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002176 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002178
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002179 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002180 {
2181 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2182 {
2183 out << "texture_";
2184 (*arg)->traverse(this);
2185 out << ", sampler_";
2186 }
2187
2188 (*arg)->traverse(this);
2189
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002190 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002191 {
2192 out << ", ";
2193 }
2194 }
2195
2196 out << ")";
2197
2198 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199 }
2200 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002201 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002202 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2203 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2204 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2205 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2206 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2207 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2208 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2209 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2210 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2211 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2212 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2213 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2214 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2215 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2216 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2217 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2218 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2219 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2220 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002221 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002222 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002223 if (node->getType().isArray())
2224 {
2225 UNIMPLEMENTED();
2226 }
Jamie Madill033dae62014-06-18 12:56:28 -04002227 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002228 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002229 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002230 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002231 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002232 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2233 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2234 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2235 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2236 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2237 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002238 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002239 ASSERT(node->getUseEmulatedFunction());
2240 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002241 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002242 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002243 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002245 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002246 ASSERT(node->getUseEmulatedFunction());
2247 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248 break;
2249 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2250 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2251 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2252 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2253 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2254 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2255 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2256 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2257 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002258 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002259 ASSERT(node->getUseEmulatedFunction());
2260 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002261 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2263 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002264 case EOpOuterProduct:
2265 ASSERT(node->getUseEmulatedFunction());
2266 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2267 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002268 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 default: UNREACHABLE();
2270 }
2271
2272 return true;
2273}
2274
2275bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2276{
Jamie Madill32aab012015-01-27 14:12:26 -05002277 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278
Olli Etuahoa6f22092015-05-08 18:31:10 +03002279 ASSERT(!node->usesTernaryOperator());
2280
2281 // D3D errors when there is a gradient operation in a loop in an unflattened if.
2282 // We check for null mCurrentFunctionMetadata to prevent crashing in the case that the translator has generated if
2283 // statements in the global scope when unfolding global initializers. This is a bug that should be addressed by
2284 // moving the unfolded global initializers into a function.
2285 if (mShaderType == GL_FRAGMENT_SHADER
2286 && mCurrentFunctionMetadata != nullptr
2287 && mCurrentFunctionMetadata->hasDiscontinuousLoop(node)
2288 && mCurrentFunctionMetadata->hasGradientInCallGraph(node))
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002289 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002290 out << "FLATTEN ";
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002291 }
Olli Etuahoa6f22092015-05-08 18:31:10 +03002292
2293 out << "if (";
2294
2295 node->getCondition()->traverse(this);
2296
2297 out << ")\n";
2298
2299 outputLineDirective(node->getLine().first_line);
2300 out << "{\n";
2301
2302 bool discard = false;
2303
2304 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002306 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002307
Olli Etuahoa6f22092015-05-08 18:31:10 +03002308 // Detect true discard
2309 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2310 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002311
Olli Etuahoa6f22092015-05-08 18:31:10 +03002312 outputLineDirective(node->getLine().first_line);
2313 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002314
Olli Etuahoa6f22092015-05-08 18:31:10 +03002315 if (node->getFalseBlock())
2316 {
2317 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002318
Olli Etuahoa6f22092015-05-08 18:31:10 +03002319 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002320 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321
Olli Etuahoa6f22092015-05-08 18:31:10 +03002322 outputLineDirective(node->getFalseBlock()->getLine().first_line);
2323 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002324
Olli Etuahoa6f22092015-05-08 18:31:10 +03002325 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002326 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002327
Olli Etuahoa6f22092015-05-08 18:31:10 +03002328 // Detect false discard
2329 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2330 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002331
Olli Etuahoa6f22092015-05-08 18:31:10 +03002332 // ANGLE issue 486: Detect problematic conditional discard
2333 if (discard && FindSideEffectRewriting::search(node))
2334 {
2335 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002336 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337
2338 return false;
2339}
2340
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002341bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002342{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002343 if (node->getStatementList())
2344 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002345 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002346 outputTriplet(visit, "switch (", ") ", "");
2347 // The curly braces get written when visiting the statementList aggregate
2348 }
2349 else
2350 {
2351 // No statementList, so it won't output curly braces
2352 outputTriplet(visit, "switch (", ") {", "}\n");
2353 }
2354 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002355}
2356
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002357bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002358{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002359 if (node->hasCondition())
2360 {
2361 outputTriplet(visit, "case (", "", "):\n");
2362 return true;
2363 }
2364 else
2365 {
2366 TInfoSinkBase &out = getInfoSink();
2367 out << "default:\n";
2368 return false;
2369 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002370}
2371
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2373{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002374 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375}
2376
2377bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2378{
Nicolas Capens655fe362014-04-11 13:12:34 -04002379 mNestedLoopDepth++;
2380
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002381 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002382 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002383 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002384
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002385 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002386 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002387 if (handleExcessiveLoop(node))
2388 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002389 mInsideDiscontinuousLoop = wasDiscontinuous;
2390 mNestedLoopDepth--;
2391
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002392 return false;
2393 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002394 }
2395
Jamie Madill32aab012015-01-27 14:12:26 -05002396 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002397
Corentin Wallez1239ee92015-03-19 14:38:02 -07002398 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002399 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002401 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002402
Jamie Madill075edd82013-07-08 13:30:19 -04002403 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002404 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 }
2406 else
2407 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002408 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002409
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410 if (node->getInit())
2411 {
2412 node->getInit()->traverse(this);
2413 }
2414
2415 out << "; ";
2416
alokp@chromium.org52813552010-11-16 18:36:09 +00002417 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002419 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420 }
2421
2422 out << "; ";
2423
alokp@chromium.org52813552010-11-16 18:36:09 +00002424 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002426 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 }
2428
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002429 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002430
Jamie Madill075edd82013-07-08 13:30:19 -04002431 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002432 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 }
2434
2435 if (node->getBody())
2436 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002437 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438 }
2439
Jamie Madill075edd82013-07-08 13:30:19 -04002440 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002441 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442
alokp@chromium.org52813552010-11-16 18:36:09 +00002443 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 {
Jamie Madill075edd82013-07-08 13:30:19 -04002445 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446 out << "while(\n";
2447
alokp@chromium.org52813552010-11-16 18:36:09 +00002448 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449
daniel@transgaming.com73536982012-03-21 20:45:49 +00002450 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002451 }
2452
daniel@transgaming.com73536982012-03-21 20:45:49 +00002453 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002455 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002456 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002457
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458 return false;
2459}
2460
2461bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2462{
Jamie Madill32aab012015-01-27 14:12:26 -05002463 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002464
2465 switch (node->getFlowOp())
2466 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002467 case EOpKill:
2468 outputTriplet(visit, "discard;\n", "", "");
2469 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002470 case EOpBreak:
2471 if (visit == PreVisit)
2472 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002473 if (mNestedLoopDepth > 1)
2474 {
2475 mUsesNestedBreak = true;
2476 }
2477
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002478 if (mExcessiveLoopIndex)
2479 {
2480 out << "{Break";
2481 mExcessiveLoopIndex->traverse(this);
2482 out << " = true; break;}\n";
2483 }
2484 else
2485 {
2486 out << "break;\n";
2487 }
2488 }
2489 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002490 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491 case EOpReturn:
2492 if (visit == PreVisit)
2493 {
2494 if (node->getExpression())
2495 {
2496 out << "return ";
2497 }
2498 else
2499 {
2500 out << "return;\n";
2501 }
2502 }
2503 else if (visit == PostVisit)
2504 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002505 if (node->getExpression())
2506 {
2507 out << ";\n";
2508 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002509 }
2510 break;
2511 default: UNREACHABLE();
2512 }
2513
2514 return true;
2515}
2516
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002517bool OutputHLSL::isSingleStatement(TIntermNode *node)
2518{
2519 TIntermAggregate *aggregate = node->getAsAggregate();
2520
2521 if (aggregate)
2522 {
2523 if (aggregate->getOp() == EOpSequence)
2524 {
2525 return false;
2526 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002527 else if (aggregate->getOp() == EOpDeclaration)
2528 {
2529 // Declaring multiple comma-separated variables must be considered multiple statements
2530 // because each individual declaration has side effects which are visible in the next.
2531 return false;
2532 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002533 else
2534 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002535 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002536 {
2537 if (!isSingleStatement(*sit))
2538 {
2539 return false;
2540 }
2541 }
2542
2543 return true;
2544 }
2545 }
2546
2547 return true;
2548}
2549
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002550// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2551// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002552bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2553{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002554 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002555 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002556
2557 // Parse loops of the form:
2558 // for(int index = initial; index [comparator] limit; index += increment)
2559 TIntermSymbol *index = NULL;
2560 TOperator comparator = EOpNull;
2561 int initial = 0;
2562 int limit = 0;
2563 int increment = 0;
2564
2565 // Parse index name and intial value
2566 if (node->getInit())
2567 {
2568 TIntermAggregate *init = node->getInit()->getAsAggregate();
2569
2570 if (init)
2571 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002572 TIntermSequence *sequence = init->getSequence();
2573 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002574
2575 if (variable && variable->getQualifier() == EvqTemporary)
2576 {
2577 TIntermBinary *assign = variable->getAsBinaryNode();
2578
2579 if (assign->getOp() == EOpInitialize)
2580 {
2581 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2582 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2583
2584 if (symbol && constant)
2585 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002586 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002587 {
2588 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002589 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002590 }
2591 }
2592 }
2593 }
2594 }
2595 }
2596
2597 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002598 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002599 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002600 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002601
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002602 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2603 {
2604 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2605
2606 if (constant)
2607 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002608 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002609 {
2610 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002611 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002612 }
2613 }
2614 }
2615 }
2616
2617 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002618 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002619 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002620 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2621 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002622
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002623 if (binaryTerminal)
2624 {
2625 TOperator op = binaryTerminal->getOp();
2626 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2627
2628 if (constant)
2629 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002630 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002631 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002632 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002633
2634 switch (op)
2635 {
2636 case EOpAddAssign: increment = value; break;
2637 case EOpSubAssign: increment = -value; break;
2638 default: UNIMPLEMENTED();
2639 }
2640 }
2641 }
2642 }
2643 else if (unaryTerminal)
2644 {
2645 TOperator op = unaryTerminal->getOp();
2646
2647 switch (op)
2648 {
2649 case EOpPostIncrement: increment = 1; break;
2650 case EOpPostDecrement: increment = -1; break;
2651 case EOpPreIncrement: increment = 1; break;
2652 case EOpPreDecrement: increment = -1; break;
2653 default: UNIMPLEMENTED();
2654 }
2655 }
2656 }
2657
2658 if (index != NULL && comparator != EOpNull && increment != 0)
2659 {
2660 if (comparator == EOpLessThanEqual)
2661 {
2662 comparator = EOpLessThan;
2663 limit += 1;
2664 }
2665
2666 if (comparator == EOpLessThan)
2667 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002668 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002669
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002670 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002671 {
2672 return false; // Not an excessive loop
2673 }
2674
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002675 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2676 mExcessiveLoopIndex = index;
2677
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002678 out << "{int ";
2679 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002680 out << ";\n"
2681 "bool Break";
2682 index->traverse(this);
2683 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002684
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002685 bool firstLoopFragment = true;
2686
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002687 while (iterations > 0)
2688 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002689 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002690
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002691 if (!firstLoopFragment)
2692 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002693 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002694 index->traverse(this);
2695 out << ") {\n";
2696 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002697
2698 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2699 {
2700 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2701 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002702
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002703 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002704 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002705
Corentin Wallez1239ee92015-03-19 14:38:02 -07002706 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002707 index->traverse(this);
2708 out << " = ";
2709 out << initial;
2710
2711 out << "; ";
2712 index->traverse(this);
2713 out << " < ";
2714 out << clampedLimit;
2715
2716 out << "; ";
2717 index->traverse(this);
2718 out << " += ";
2719 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002720 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002721
Jamie Madill075edd82013-07-08 13:30:19 -04002722 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002723 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002724
2725 if (node->getBody())
2726 {
2727 node->getBody()->traverse(this);
2728 }
2729
Jamie Madill075edd82013-07-08 13:30:19 -04002730 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002731 out << ";}\n";
2732
2733 if (!firstLoopFragment)
2734 {
2735 out << "}\n";
2736 }
2737
2738 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002739
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002740 initial += MAX_LOOP_ITERATIONS * increment;
2741 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002742 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002743
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002744 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002745
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002746 mExcessiveLoopIndex = restoreIndex;
2747
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002748 return true;
2749 }
2750 else UNIMPLEMENTED();
2751 }
2752
2753 return false; // Not handled as an excessive loop
2754}
2755
Olli Etuaho7fb49552015-03-18 17:27:44 +02002756void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002757{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002758 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002759 {
2760 out << preString;
2761 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002762 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002763 {
2764 out << inString;
2765 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002766 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002767 {
2768 out << postString;
2769 }
2770}
2771
Olli Etuaho7fb49552015-03-18 17:27:44 +02002772void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2773{
2774 outputTriplet(visit, preString, inString, postString, getInfoSink());
2775}
2776
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002777void OutputHLSL::outputLineDirective(int line)
2778{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002779 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002780 {
Jamie Madill32aab012015-01-27 14:12:26 -05002781 TInfoSinkBase &out = getInfoSink();
2782
2783 out << "\n";
2784 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002785
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002786 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002787 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002788 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002789 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002790
Jamie Madill32aab012015-01-27 14:12:26 -05002791 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002792 }
2793}
2794
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002795TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2796{
2797 TQualifier qualifier = symbol->getQualifier();
2798 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002799 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002800
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002801 if (name.empty()) // HLSL demands named arguments, also for prototypes
2802 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002803 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002804 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002805 else if (!symbol->isInternal())
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002806 {
Jamie Madill033dae62014-06-18 12:56:28 -04002807 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002808 }
2809
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002810 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2811 {
Jamie Madill033dae62014-06-18 12:56:28 -04002812 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002813 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002814 }
2815
Jamie Madill033dae62014-06-18 12:56:28 -04002816 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002817}
2818
2819TString OutputHLSL::initializer(const TType &type)
2820{
2821 TString string;
2822
Jamie Madill94bf7f22013-07-08 13:31:15 -04002823 size_t size = type.getObjectSize();
2824 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002825 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002826 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002827
Jamie Madill94bf7f22013-07-08 13:31:15 -04002828 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002829 {
2830 string += ", ";
2831 }
2832 }
2833
daniel@transgaming.comead23042010-04-29 03:35:36 +00002834 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002835}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002836
Daniel Bratell29190082015-02-20 16:42:54 +01002837void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002838{
Olli Etuahof40319e2015-03-10 14:33:00 +02002839 if (type.isArray())
2840 {
2841 UNIMPLEMENTED();
2842 }
Jamie Madill32aab012015-01-27 14:12:26 -05002843 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002844
2845 if (visit == PreVisit)
2846 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002847 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002848
Daniel Bratell29190082015-02-20 16:42:54 +01002849 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002850 }
2851 else if (visit == InVisit)
2852 {
2853 out << ", ";
2854 }
2855 else if (visit == PostVisit)
2856 {
2857 out << ")";
2858 }
2859}
2860
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002861const TConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const TConstantUnion *constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002862{
Jamie Madill32aab012015-01-27 14:12:26 -05002863 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002864
Jamie Madill98493dd2013-07-08 14:39:03 -04002865 const TStructure* structure = type.getStruct();
2866 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002867 {
Jamie Madill033dae62014-06-18 12:56:28 -04002868 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002869
Jamie Madill98493dd2013-07-08 14:39:03 -04002870 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002871
Jamie Madill98493dd2013-07-08 14:39:03 -04002872 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002873 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002874 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002875 constUnion = writeConstantUnion(*fieldType, constUnion);
2876
Jamie Madill98493dd2013-07-08 14:39:03 -04002877 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002878 {
2879 out << ", ";
2880 }
2881 }
2882
2883 out << ")";
2884 }
2885 else
2886 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002887 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002888 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002889
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002890 if (writeType)
2891 {
Jamie Madill033dae62014-06-18 12:56:28 -04002892 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002893 }
2894
Jamie Madill94bf7f22013-07-08 13:31:15 -04002895 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002896 {
2897 switch (constUnion->getType())
2898 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002899 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002900 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002901 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002902 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002903 default: UNREACHABLE();
2904 }
2905
2906 if (i != size - 1)
2907 {
2908 out << ", ";
2909 }
2910 }
2911
2912 if (writeType)
2913 {
2914 out << ")";
2915 }
2916 }
2917
2918 return constUnion;
2919}
2920
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002921void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2922{
2923 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2924 outputTriplet(visit, preString.c_str(), ", ", ")");
2925}
2926
Jamie Madill37997142015-01-28 10:06:34 -05002927bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2928{
2929 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2930 expression->traverse(&searchSymbol);
2931
2932 if (searchSymbol.foundMatch())
2933 {
2934 // Type already printed
2935 out << "t" + str(mUniqueIndex) + " = ";
2936 expression->traverse(this);
2937 out << ", ";
2938 symbolNode->traverse(this);
2939 out << " = t" + str(mUniqueIndex);
2940
2941 mUniqueIndex++;
2942 return true;
2943 }
2944
2945 return false;
2946}
2947
2948void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2949{
2950 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2951 << "\n"
2952 << "void initializeDeferredGlobals()\n"
2953 << "{\n";
2954
2955 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2956 {
2957 TIntermSymbol *symbol = deferredGlobal.first;
2958 TIntermTyped *expression = deferredGlobal.second;
2959 ASSERT(symbol);
2960 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2961
2962 out << " " << Decorate(symbol->getSymbol()) << " = ";
2963
2964 if (!writeSameSymbolInitializer(out, symbol, expression))
2965 {
2966 ASSERT(mInfoSinkStack.top() == &out);
2967 expression->traverse(this);
2968 }
2969
2970 out << ";\n";
2971 }
2972
2973 out << "}\n"
2974 << "\n";
2975}
2976
Jamie Madill55e79e02015-02-09 15:35:00 -05002977TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2978{
2979 const TFieldList &fields = structure.fields();
2980
2981 for (const auto &eqFunction : mStructEqualityFunctions)
2982 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002983 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002984 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002985 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002986 }
2987 }
2988
2989 const TString &structNameString = StructNameString(structure);
2990
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002991 StructEqualityFunction *function = new StructEqualityFunction();
2992 function->structure = &structure;
2993 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002994
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002995 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002996
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002997 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2998 << "{\n"
2999 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003000
3001 for (size_t i = 0; i < fields.size(); i++)
3002 {
3003 const TField *field = fields[i];
3004 const TType *fieldType = field->type();
3005
3006 const TString &fieldNameA = "a." + Decorate(field->name());
3007 const TString &fieldNameB = "b." + Decorate(field->name());
3008
3009 if (i > 0)
3010 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003011 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003012 }
3013
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003014 fnOut << "(";
3015 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3016 fnOut << fieldNameA;
3017 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3018 fnOut << fieldNameB;
3019 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3020 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003021 }
3022
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003023 fnOut << ";\n" << "}\n";
3024
3025 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003026
3027 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003028 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003029
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003030 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003031}
3032
Olli Etuaho7fb49552015-03-18 17:27:44 +02003033TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3034{
3035 for (const auto &eqFunction : mArrayEqualityFunctions)
3036 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003037 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003038 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003039 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003040 }
3041 }
3042
3043 const TString &typeName = TypeString(type);
3044
Olli Etuaho12690762015-03-31 12:55:28 +03003045 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003046 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003047
3048 TInfoSinkBase fnNameOut;
3049 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003050 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003051
3052 TType nonArrayType = type;
3053 nonArrayType.clearArrayness();
3054
3055 TInfoSinkBase fnOut;
3056
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003057 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003058 << typeName << " a[" << type.getArraySize() << "], "
3059 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003060 << "{\n"
3061 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3062 " {\n"
3063 " if (";
3064
3065 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3066 fnOut << "a[i]";
3067 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3068 fnOut << "b[i]";
3069 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3070
3071 fnOut << ") { return false; }\n"
3072 " }\n"
3073 " return true;\n"
3074 "}\n";
3075
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003076 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003077
3078 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003079 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003080
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003081 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003082}
3083
Olli Etuaho12690762015-03-31 12:55:28 +03003084TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3085{
3086 for (const auto &assignFunction : mArrayAssignmentFunctions)
3087 {
3088 if (assignFunction.type == type)
3089 {
3090 return assignFunction.functionName;
3091 }
3092 }
3093
3094 const TString &typeName = TypeString(type);
3095
3096 ArrayHelperFunction function;
3097 function.type = type;
3098
3099 TInfoSinkBase fnNameOut;
3100 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3101 function.functionName = fnNameOut.c_str();
3102
3103 TInfoSinkBase fnOut;
3104
3105 fnOut << "void " << function.functionName << "(out "
3106 << typeName << " a[" << type.getArraySize() << "], "
3107 << typeName << " b[" << type.getArraySize() << "])\n"
3108 << "{\n"
3109 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3110 " {\n"
3111 " a[i] = b[i];\n"
3112 " }\n"
3113 "}\n";
3114
3115 function.functionDefinition = fnOut.c_str();
3116
3117 mArrayAssignmentFunctions.push_back(function);
3118
3119 return function.functionName;
3120}
3121
Olli Etuaho9638c352015-04-01 14:34:52 +03003122TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3123{
3124 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3125 {
3126 if (constructIntoFunction.type == type)
3127 {
3128 return constructIntoFunction.functionName;
3129 }
3130 }
3131
3132 const TString &typeName = TypeString(type);
3133
3134 ArrayHelperFunction function;
3135 function.type = type;
3136
3137 TInfoSinkBase fnNameOut;
3138 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3139 function.functionName = fnNameOut.c_str();
3140
3141 TInfoSinkBase fnOut;
3142
3143 fnOut << "void " << function.functionName << "(out "
3144 << typeName << " a[" << type.getArraySize() << "]";
3145 for (int i = 0; i < type.getArraySize(); ++i)
3146 {
3147 fnOut << ", " << typeName << " b" << i;
3148 }
3149 fnOut << ")\n"
3150 "{\n";
3151
3152 for (int i = 0; i < type.getArraySize(); ++i)
3153 {
3154 fnOut << " a[" << i << "] = b" << i << ";\n";
3155 }
3156 fnOut << "}\n";
3157
3158 function.functionDefinition = fnOut.c_str();
3159
3160 mArrayConstructIntoFunctions.push_back(function);
3161
3162 return function.functionName;
3163}
3164
Jamie Madill2e295e22015-04-29 10:41:33 -04003165void OutputHLSL::ensureStructDefined(const TType &type)
3166{
3167 TStructure *structure = type.getStruct();
3168
3169 if (structure)
3170 {
3171 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3172 }
3173}
3174
3175
Olli Etuaho9638c352015-04-01 14:34:52 +03003176
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003177}