blob: c3240f521936b7ffe6e646cfcf2cdfdeb46fbefc [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"
14#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020015#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050016#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
17#include "compiler/translator/DetectDiscontinuity.h"
18#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"
26#include "compiler/translator/UnfoldShortCircuit.h"
27#include "compiler/translator/UniformHLSL.h"
28#include "compiler/translator/UtilsHLSL.h"
29#include "compiler/translator/blocklayout.h"
30#include "compiler/translator/compilerdebug.h"
31#include "compiler/translator/util.h"
32
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033namespace sh
34{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000035
Nicolas Capense0ba27a2013-06-24 16:10:52 -040036TString OutputHLSL::TextureFunction::name() const
37{
38 TString name = "gl_texture";
39
Nicolas Capens6d232bb2013-07-08 15:56:38 -040040 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040041 {
42 name += "2D";
43 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040044 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040045 {
46 name += "3D";
47 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040048 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040049 {
50 name += "Cube";
51 }
52 else UNREACHABLE();
53
54 if (proj)
55 {
56 name += "Proj";
57 }
58
Nicolas Capensb1f45b72013-12-19 17:37:19 -050059 if (offset)
60 {
61 name += "Offset";
62 }
63
Nicolas Capens75fb4752013-07-10 15:14:47 -040064 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040065 {
Nicolas Capensfc014542014-02-18 14:47:13 -050066 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040067 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050068 case LOD: name += "Lod"; break;
69 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040070 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050071 case SIZE: name += "Size"; break;
72 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050073 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040074 default: UNREACHABLE();
75 }
76
77 return name + "(";
78}
79
80bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
81{
82 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040083 if (sampler > rhs.sampler) return false;
84
Nicolas Capense0ba27a2013-06-24 16:10:52 -040085 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040086 if (coords > rhs.coords) return false;
87
Nicolas Capense0ba27a2013-06-24 16:10:52 -040088 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040089 if (proj && !rhs.proj) return false;
90
91 if (!offset && rhs.offset) return true;
92 if (offset && !rhs.offset) return false;
93
Nicolas Capens75fb4752013-07-10 15:14:47 -040094 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040095 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040096
97 return false;
98}
99
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200100OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
101 const TExtensionBehavior &extensionBehavior,
102 const char *sourcePath, ShShaderOutput outputType,
103 int numRenderTargets, const std::vector<Uniform> &uniforms,
104 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400105 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200106 mShaderType(shaderType),
107 mShaderVersion(shaderVersion),
108 mExtensionBehavior(extensionBehavior),
109 mSourcePath(sourcePath),
110 mOutputType(outputType),
111 mNumRenderTargets(numRenderTargets),
112 mCompileOptions(compileOptions)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200114 mUnfoldShortCircuit = new UnfoldShortCircuit(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000115 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000116
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000117 mUsesFragColor = false;
118 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000119 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000120 mUsesFragCoord = false;
121 mUsesPointCoord = false;
122 mUsesFrontFacing = false;
123 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000124 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400125 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000126 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500127 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400128 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530129 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000130
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000131 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000132
133 mContainsLoopDiscontinuity = false;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800134 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000135 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000136 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400137 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000138
139 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000140
Jamie Madill8daaba12014-06-13 10:04:33 -0400141 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200142 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400143
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000144 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000145 {
Arun Patole63419392015-03-13 11:51:07 +0530146 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
147 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
148 // In both cases total 3 uniform registers need to be reserved.
149 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000150 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000151
Jamie Madillf91ce812014-06-13 10:04:34 -0400152 // Reserve registers for the default uniform block and driver constants
153 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154}
155
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000156OutputHLSL::~OutputHLSL()
157{
Jamie Madill8daaba12014-06-13 10:04:33 -0400158 SafeDelete(mUnfoldShortCircuit);
159 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400160 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200161 for (auto &eqFunction : mStructEqualityFunctions)
162 {
163 SafeDelete(eqFunction);
164 }
165 for (auto &eqFunction : mArrayEqualityFunctions)
166 {
167 SafeDelete(eqFunction);
168 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000169}
170
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200171void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000172{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200173 mContainsLoopDiscontinuity = mShaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(treeRoot);
174 mContainsAnyLoop = containsAnyLoop(treeRoot);
175 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400176 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000177
Jamie Madille53c98b2014-02-03 11:57:13 -0500178 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
179 // use a vertex attribute as a condition, and some related computation in the else block.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200180 if (mOutputType == SH_HLSL9_OUTPUT && mShaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500181 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200182 RewriteElseBlocks(treeRoot);
Jamie Madille53c98b2014-02-03 11:57:13 -0500183 }
184
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200185 BuiltInFunctionEmulator builtInFunctionEmulator;
186 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200187 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500188
Jamie Madill37997142015-01-28 10:06:34 -0500189 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500190 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200191 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500192 mInfoSinkStack.pop();
193
Jamie Madill37997142015-01-28 10:06:34 -0500194 mInfoSinkStack.push(&mFooter);
195 if (!mDeferredGlobalInitializers.empty())
196 {
197 writeDeferredGlobalInitializers(mFooter);
198 }
199 mInfoSinkStack.pop();
200
Jamie Madill32aab012015-01-27 14:12:26 -0500201 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200202 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500203 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000204
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200205 objSink << mHeader.c_str();
206 objSink << mBody.c_str();
207 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200208
209 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000210}
211
Jamie Madill570e04d2013-06-21 09:15:33 -0400212void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
213{
214 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
215 {
216 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
217
Jamie Madill32aab012015-01-27 14:12:26 -0500218 TInfoSinkBase structInfoSink;
219 mInfoSinkStack.push(&structInfoSink);
220
Jamie Madill570e04d2013-06-21 09:15:33 -0400221 // This will mark the necessary block elements as referenced
222 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500223
224 TString structName(structInfoSink.c_str());
225 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400226
227 mFlaggedStructOriginalNames[flaggedNode] = structName;
228
229 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
230 {
231 structName.erase(pos, 1);
232 }
233
234 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
235 }
236}
237
Jamie Madill4e1fd412014-07-10 17:50:10 -0400238const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
239{
240 return mUniformHLSL->getInterfaceBlockRegisterMap();
241}
242
Jamie Madill9fe25e92014-07-18 10:33:08 -0400243const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
244{
245 return mUniformHLSL->getUniformRegisterMap();
246}
247
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000248int OutputHLSL::vectorSize(const TType &type) const
249{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000250 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000251 int arraySize = type.isArray() ? type.getArraySize() : 1;
252
253 return elementSize * arraySize;
254}
255
Jamie Madill98493dd2013-07-08 14:39:03 -0400256TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400257{
258 TString init;
259
260 TString preIndentString;
261 TString fullIndentString;
262
263 for (int spaces = 0; spaces < (indent * 4); spaces++)
264 {
265 preIndentString += ' ';
266 }
267
268 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
269 {
270 fullIndentString += ' ';
271 }
272
273 init += preIndentString + "{\n";
274
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 const TFieldList &fields = structure.fields();
276 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400277 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400278 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400279 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400280 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400281
Jamie Madill98493dd2013-07-08 14:39:03 -0400282 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400283 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400284 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400285 }
286 else
287 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400288 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400289 }
290 }
291
292 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
293
294 return init;
295}
296
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200297void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298{
Jamie Madill32aab012015-01-27 14:12:26 -0500299 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000301 TString varyings;
302 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400303 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000304
Jamie Madill829f59e2013-11-13 19:40:54 -0500305 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400306 {
307 TIntermTyped *structNode = flaggedStructIt->first;
308 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400309 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400310 const TString &originalName = mFlaggedStructOriginalNames[structNode];
311
Jamie Madill033dae62014-06-18 12:56:28 -0400312 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400313 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400314 flaggedStructs += "\n";
315 }
316
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000317 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
318 {
319 const TType &type = varying->second->getType();
320 const TString &name = varying->second->getSymbol();
321
322 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400323 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
324 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000325 }
326
327 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
328 {
329 const TType &type = attribute->second->getType();
330 const TString &name = attribute->second->getSymbol();
331
Jamie Madill033dae62014-06-18 12:56:28 -0400332 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000333 }
334
Jamie Madill8daaba12014-06-13 10:04:33 -0400335 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400336
Jamie Madillf91ce812014-06-13 10:04:34 -0400337 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
338 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
339
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200340 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500341 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200342 out << "\n// Equality functions\n\n";
343 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500344 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200345 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200346 }
347 }
Olli Etuaho12690762015-03-31 12:55:28 +0300348 if (!mArrayAssignmentFunctions.empty())
349 {
350 out << "\n// Assignment functions\n\n";
351 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
352 {
353 out << assignmentFunction.functionDefinition << "\n";
354 }
355 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200356
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500357 if (mUsesDiscardRewriting)
358 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400359 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500360 }
361
Nicolas Capens655fe362014-04-11 13:12:34 -0400362 if (mUsesNestedBreak)
363 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400364 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400365 }
366
Arun Patole44efa0b2015-03-04 17:11:05 +0530367 if (mRequiresIEEEStrictCompiling)
368 {
369 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
370 }
371
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400372 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
373 "#define LOOP [loop]\n"
374 "#define FLATTEN [flatten]\n"
375 "#else\n"
376 "#define LOOP\n"
377 "#define FLATTEN\n"
378 "#endif\n";
379
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200380 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200382 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
383 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000384
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000385 out << "// Varyings\n";
386 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400387 out << "\n";
388
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200389 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000390 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500391 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000392 {
Jamie Madill46131a32013-06-20 11:55:50 -0400393 const TString &variableName = outputVariableIt->first;
394 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400395
Jamie Madill033dae62014-06-18 12:56:28 -0400396 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400397 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000398 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000399 }
Jamie Madill46131a32013-06-20 11:55:50 -0400400 else
401 {
402 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
403
404 out << "static float4 gl_Color[" << numColorValues << "] =\n"
405 "{\n";
406 for (unsigned int i = 0; i < numColorValues; i++)
407 {
408 out << " float4(0, 0, 0, 0)";
409 if (i + 1 != numColorValues)
410 {
411 out << ",";
412 }
413 out << "\n";
414 }
415
416 out << "};\n";
417 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000418
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400419 if (mUsesFragDepth)
420 {
421 out << "static float gl_Depth = 0.0;\n";
422 }
423
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000424 if (mUsesFragCoord)
425 {
426 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
427 }
428
429 if (mUsesPointCoord)
430 {
431 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
432 }
433
434 if (mUsesFrontFacing)
435 {
436 out << "static bool gl_FrontFacing = false;\n";
437 }
438
439 out << "\n";
440
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000441 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000442 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000443 out << "struct gl_DepthRangeParameters\n"
444 "{\n"
445 " float near;\n"
446 " float far;\n"
447 " float diff;\n"
448 "};\n"
449 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000450 }
451
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000452 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000453 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000454 out << "cbuffer DriverConstants : register(b1)\n"
455 "{\n";
456
457 if (mUsesDepthRange)
458 {
459 out << " float3 dx_DepthRange : packoffset(c0);\n";
460 }
461
462 if (mUsesFragCoord)
463 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000464 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000465 }
466
467 if (mUsesFragCoord || mUsesFrontFacing)
468 {
469 out << " float3 dx_DepthFront : packoffset(c2);\n";
470 }
471
472 out << "};\n";
473 }
474 else
475 {
476 if (mUsesDepthRange)
477 {
478 out << "uniform float3 dx_DepthRange : register(c0);";
479 }
480
481 if (mUsesFragCoord)
482 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000483 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000484 }
485
486 if (mUsesFragCoord || mUsesFrontFacing)
487 {
488 out << "uniform float3 dx_DepthFront : register(c2);\n";
489 }
490 }
491
492 out << "\n";
493
494 if (mUsesDepthRange)
495 {
496 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
497 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000498 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000499
Jamie Madillf91ce812014-06-13 10:04:34 -0400500 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000501 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400502 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000503 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400504 out << flaggedStructs;
505 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000506 }
507
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000508 if (usingMRTExtension && mNumRenderTargets > 1)
509 {
510 out << "#define GL_USES_MRT\n";
511 }
512
513 if (mUsesFragColor)
514 {
515 out << "#define GL_USES_FRAG_COLOR\n";
516 }
517
518 if (mUsesFragData)
519 {
520 out << "#define GL_USES_FRAG_DATA\n";
521 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000523 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000524 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000525 out << "// Attributes\n";
526 out << attributes;
527 out << "\n"
528 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400529
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000530 if (mUsesPointSize)
531 {
532 out << "static float gl_PointSize = float(1);\n";
533 }
534
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000535 if (mUsesInstanceID)
536 {
537 out << "static int gl_InstanceID;";
538 }
539
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000540 out << "\n"
541 "// Varyings\n";
542 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000543 out << "\n";
544
545 if (mUsesDepthRange)
546 {
547 out << "struct gl_DepthRangeParameters\n"
548 "{\n"
549 " float near;\n"
550 " float far;\n"
551 " float diff;\n"
552 "};\n"
553 "\n";
554 }
555
556 if (mOutputType == SH_HLSL11_OUTPUT)
557 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800558 out << "cbuffer DriverConstants : register(b1)\n"
559 "{\n";
560
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000561 if (mUsesDepthRange)
562 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800563 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000564 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800565
Cooper Partine6664f02015-01-09 16:22:24 -0800566 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800567 // However, we declare it for all shaders (including Feature Level 10+).
568 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
569 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800570 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800571
572 out << "};\n"
573 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000574 }
575 else
576 {
577 if (mUsesDepthRange)
578 {
579 out << "uniform float3 dx_DepthRange : register(c0);\n";
580 }
581
Cooper Partine6664f02015-01-09 16:22:24 -0800582 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
583 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000584 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000585 }
586
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000587 if (mUsesDepthRange)
588 {
589 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
590 "\n";
591 }
592
Jamie Madillf91ce812014-06-13 10:04:34 -0400593 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000594 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400595 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000596 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400597 out << flaggedStructs;
598 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000599 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400600 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000601
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400602 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
603 {
604 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400605 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000606 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400607 switch(textureFunction->sampler)
608 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400609 case EbtSampler2D: out << "int2 "; break;
610 case EbtSampler3D: out << "int3 "; break;
611 case EbtSamplerCube: out << "int2 "; break;
612 case EbtSampler2DArray: out << "int3 "; break;
613 case EbtISampler2D: out << "int2 "; break;
614 case EbtISampler3D: out << "int3 "; break;
615 case EbtISamplerCube: out << "int2 "; break;
616 case EbtISampler2DArray: out << "int3 "; break;
617 case EbtUSampler2D: out << "int2 "; break;
618 case EbtUSampler3D: out << "int3 "; break;
619 case EbtUSamplerCube: out << "int2 "; break;
620 case EbtUSampler2DArray: out << "int3 "; break;
621 case EbtSampler2DShadow: out << "int2 "; break;
622 case EbtSamplerCubeShadow: out << "int2 "; break;
623 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400624 default: UNREACHABLE();
625 }
626 }
627 else // Sampling function
628 {
629 switch(textureFunction->sampler)
630 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400631 case EbtSampler2D: out << "float4 "; break;
632 case EbtSampler3D: out << "float4 "; break;
633 case EbtSamplerCube: out << "float4 "; break;
634 case EbtSampler2DArray: out << "float4 "; break;
635 case EbtISampler2D: out << "int4 "; break;
636 case EbtISampler3D: out << "int4 "; break;
637 case EbtISamplerCube: out << "int4 "; break;
638 case EbtISampler2DArray: out << "int4 "; break;
639 case EbtUSampler2D: out << "uint4 "; break;
640 case EbtUSampler3D: out << "uint4 "; break;
641 case EbtUSamplerCube: out << "uint4 "; break;
642 case EbtUSampler2DArray: out << "uint4 "; break;
643 case EbtSampler2DShadow: out << "float "; break;
644 case EbtSamplerCubeShadow: out << "float "; break;
645 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400646 default: UNREACHABLE();
647 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000648 }
649
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400650 // Function name
651 out << textureFunction->name();
652
653 // Argument list
654 int hlslCoords = 4;
655
656 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000657 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400658 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000659 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400660 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
661 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
662 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000663 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400664
Nicolas Capens75fb4752013-07-10 15:14:47 -0400665 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000666 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400667 case TextureFunction::IMPLICIT: break;
668 case TextureFunction::BIAS: hlslCoords = 4; break;
669 case TextureFunction::LOD: hlslCoords = 4; break;
670 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400671 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400672 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000673 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400674 }
675 else if (mOutputType == SH_HLSL11_OUTPUT)
676 {
677 switch(textureFunction->sampler)
678 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400679 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
680 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
681 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
682 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
683 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
684 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500685 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400686 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
687 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
688 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500689 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400690 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
691 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
692 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
693 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400694 default: UNREACHABLE();
695 }
696 }
697 else UNREACHABLE();
698
Nicolas Capensfc014542014-02-18 14:47:13 -0500699 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400700 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500701 switch(textureFunction->coords)
702 {
703 case 2: out << ", int2 t"; break;
704 case 3: out << ", int3 t"; break;
705 default: UNREACHABLE();
706 }
707 }
708 else // Floating-point coordinates (except textureSize)
709 {
710 switch(textureFunction->coords)
711 {
712 case 1: out << ", int lod"; break; // textureSize()
713 case 2: out << ", float2 t"; break;
714 case 3: out << ", float3 t"; break;
715 case 4: out << ", float4 t"; break;
716 default: UNREACHABLE();
717 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000718 }
719
Nicolas Capensd11d5492014-02-19 17:06:10 -0500720 if (textureFunction->method == TextureFunction::GRAD)
721 {
722 switch(textureFunction->sampler)
723 {
724 case EbtSampler2D:
725 case EbtISampler2D:
726 case EbtUSampler2D:
727 case EbtSampler2DArray:
728 case EbtISampler2DArray:
729 case EbtUSampler2DArray:
730 case EbtSampler2DShadow:
731 case EbtSampler2DArrayShadow:
732 out << ", float2 ddx, float2 ddy";
733 break;
734 case EbtSampler3D:
735 case EbtISampler3D:
736 case EbtUSampler3D:
737 case EbtSamplerCube:
738 case EbtISamplerCube:
739 case EbtUSamplerCube:
740 case EbtSamplerCubeShadow:
741 out << ", float3 ddx, float3 ddy";
742 break;
743 default: UNREACHABLE();
744 }
745 }
746
Nicolas Capens75fb4752013-07-10 15:14:47 -0400747 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000748 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400749 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400750 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400751 case TextureFunction::LOD: out << ", float lod"; break;
752 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400753 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400754 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500755 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500756 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400757 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000758 }
759
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500760 if (textureFunction->offset)
761 {
762 switch(textureFunction->sampler)
763 {
764 case EbtSampler2D: out << ", int2 offset"; break;
765 case EbtSampler3D: out << ", int3 offset"; break;
766 case EbtSampler2DArray: out << ", int2 offset"; break;
767 case EbtISampler2D: out << ", int2 offset"; break;
768 case EbtISampler3D: out << ", int3 offset"; break;
769 case EbtISampler2DArray: out << ", int2 offset"; break;
770 case EbtUSampler2D: out << ", int2 offset"; break;
771 case EbtUSampler3D: out << ", int3 offset"; break;
772 case EbtUSampler2DArray: out << ", int2 offset"; break;
773 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500774 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500775 default: UNREACHABLE();
776 }
777 }
778
Nicolas Capens84cfa122014-04-14 13:48:45 -0400779 if (textureFunction->method == TextureFunction::BIAS ||
780 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500781 {
782 out << ", float bias";
783 }
784
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400785 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400786 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400787
Nicolas Capens75fb4752013-07-10 15:14:47 -0400788 if (textureFunction->method == TextureFunction::SIZE)
789 {
790 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
791 {
792 if (IsSamplerArray(textureFunction->sampler))
793 {
794 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
795 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
796 }
797 else
798 {
799 out << " uint width; uint height; uint numberOfLevels;\n"
800 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
801 }
802 }
803 else if (IsSampler3D(textureFunction->sampler))
804 {
805 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
806 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
807 }
808 else UNREACHABLE();
809
810 switch(textureFunction->sampler)
811 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400812 case EbtSampler2D: out << " return int2(width, height);"; break;
813 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
814 case EbtSamplerCube: out << " return int2(width, height);"; break;
815 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
816 case EbtISampler2D: out << " return int2(width, height);"; break;
817 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
818 case EbtISamplerCube: out << " return int2(width, height);"; break;
819 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
820 case EbtUSampler2D: out << " return int2(width, height);"; break;
821 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
822 case EbtUSamplerCube: out << " return int2(width, height);"; break;
823 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
824 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
825 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
826 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400827 default: UNREACHABLE();
828 }
829 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400830 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400831 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500832 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
833 {
834 out << " float width; float height; float layers; float levels;\n";
835
836 out << " uint mip = 0;\n";
837
838 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
839
840 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
841 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
842 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
843 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
844
845 // FACE_POSITIVE_X = 000b
846 // FACE_NEGATIVE_X = 001b
847 // FACE_POSITIVE_Y = 010b
848 // FACE_NEGATIVE_Y = 011b
849 // FACE_POSITIVE_Z = 100b
850 // FACE_NEGATIVE_Z = 101b
851 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
852
853 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
854 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
855 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
856
857 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
858 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
859 }
860 else if (IsIntegerSampler(textureFunction->sampler) &&
861 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400862 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400863 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400864 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400865 if (IsSamplerArray(textureFunction->sampler))
866 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400867 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400868
Nicolas Capens9edebd62013-08-06 10:59:10 -0400869 if (textureFunction->method == TextureFunction::LOD0)
870 {
871 out << " uint mip = 0;\n";
872 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400873 else if (textureFunction->method == TextureFunction::LOD0BIAS)
874 {
875 out << " uint mip = bias;\n";
876 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400877 else
878 {
879 if (textureFunction->method == TextureFunction::IMPLICIT ||
880 textureFunction->method == TextureFunction::BIAS)
881 {
882 out << " x.GetDimensions(0, width, height, layers, levels);\n"
883 " float2 tSized = float2(t.x * width, t.y * height);\n"
884 " float dx = length(ddx(tSized));\n"
885 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500886 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400887
888 if (textureFunction->method == TextureFunction::BIAS)
889 {
890 out << " lod += bias;\n";
891 }
892 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500893 else if (textureFunction->method == TextureFunction::GRAD)
894 {
895 out << " x.GetDimensions(0, width, height, layers, levels);\n"
896 " float lod = log2(max(length(ddx), length(ddy)));\n";
897 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400898
899 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
900 }
901
902 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400903 }
904 else
905 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400906 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400907
Nicolas Capens9edebd62013-08-06 10:59:10 -0400908 if (textureFunction->method == TextureFunction::LOD0)
909 {
910 out << " uint mip = 0;\n";
911 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400912 else if (textureFunction->method == TextureFunction::LOD0BIAS)
913 {
914 out << " uint mip = bias;\n";
915 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400916 else
917 {
918 if (textureFunction->method == TextureFunction::IMPLICIT ||
919 textureFunction->method == TextureFunction::BIAS)
920 {
921 out << " x.GetDimensions(0, width, height, levels);\n"
922 " float2 tSized = float2(t.x * width, t.y * height);\n"
923 " float dx = length(ddx(tSized));\n"
924 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500925 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400926
927 if (textureFunction->method == TextureFunction::BIAS)
928 {
929 out << " lod += bias;\n";
930 }
931 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500932 else if (textureFunction->method == TextureFunction::LOD)
933 {
934 out << " x.GetDimensions(0, width, height, levels);\n";
935 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500936 else if (textureFunction->method == TextureFunction::GRAD)
937 {
938 out << " x.GetDimensions(0, width, height, levels);\n"
939 " float lod = log2(max(length(ddx), length(ddy)));\n";
940 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400941
942 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
943 }
944
945 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400946 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400947 }
948 else if (IsSampler3D(textureFunction->sampler))
949 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400950 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400951
Nicolas Capens9edebd62013-08-06 10:59:10 -0400952 if (textureFunction->method == TextureFunction::LOD0)
953 {
954 out << " uint mip = 0;\n";
955 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400956 else if (textureFunction->method == TextureFunction::LOD0BIAS)
957 {
958 out << " uint mip = bias;\n";
959 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400960 else
961 {
962 if (textureFunction->method == TextureFunction::IMPLICIT ||
963 textureFunction->method == TextureFunction::BIAS)
964 {
965 out << " x.GetDimensions(0, width, height, depth, levels);\n"
966 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
967 " float dx = length(ddx(tSized));\n"
968 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500969 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400970
971 if (textureFunction->method == TextureFunction::BIAS)
972 {
973 out << " lod += bias;\n";
974 }
975 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500976 else if (textureFunction->method == TextureFunction::GRAD)
977 {
978 out << " x.GetDimensions(0, width, height, depth, levels);\n"
979 " float lod = log2(max(length(ddx), length(ddy)));\n";
980 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400981
982 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
983 }
984
985 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400986 }
987 else UNREACHABLE();
988 }
989
990 out << " return ";
991
992 // HLSL intrinsic
993 if (mOutputType == SH_HLSL9_OUTPUT)
994 {
995 switch(textureFunction->sampler)
996 {
997 case EbtSampler2D: out << "tex2D"; break;
998 case EbtSamplerCube: out << "texCUBE"; break;
999 default: UNREACHABLE();
1000 }
1001
Nicolas Capens75fb4752013-07-10 15:14:47 -04001002 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001003 {
1004 case TextureFunction::IMPLICIT: out << "(s, "; break;
1005 case TextureFunction::BIAS: out << "bias(s, "; break;
1006 case TextureFunction::LOD: out << "lod(s, "; break;
1007 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001008 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001009 default: UNREACHABLE();
1010 }
1011 }
1012 else if (mOutputType == SH_HLSL11_OUTPUT)
1013 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001014 if (textureFunction->method == TextureFunction::GRAD)
1015 {
1016 if (IsIntegerSampler(textureFunction->sampler))
1017 {
1018 out << "x.Load(";
1019 }
1020 else if (IsShadowSampler(textureFunction->sampler))
1021 {
1022 out << "x.SampleCmpLevelZero(s, ";
1023 }
1024 else
1025 {
1026 out << "x.SampleGrad(s, ";
1027 }
1028 }
1029 else if (IsIntegerSampler(textureFunction->sampler) ||
1030 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001031 {
1032 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001033 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001034 else if (IsShadowSampler(textureFunction->sampler))
1035 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001036 switch(textureFunction->method)
1037 {
1038 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1039 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1040 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1041 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1042 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1043 default: UNREACHABLE();
1044 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001045 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001046 else
1047 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001048 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001049 {
1050 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1051 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1052 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1053 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001054 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001055 default: UNREACHABLE();
1056 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001057 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001058 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001059 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001060
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001061 // Integer sampling requires integer addresses
1062 TString addressx = "";
1063 TString addressy = "";
1064 TString addressz = "";
1065 TString close = "";
1066
Nicolas Capensfc014542014-02-18 14:47:13 -05001067 if (IsIntegerSampler(textureFunction->sampler) ||
1068 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001069 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001070 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001071 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001072 case 2: out << "int3("; break;
1073 case 3: out << "int4("; break;
1074 default: UNREACHABLE();
1075 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001076
Nicolas Capensfc014542014-02-18 14:47:13 -05001077 // Convert from normalized floating-point to integer
1078 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001079 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001080 addressx = "int(floor(width * frac((";
1081 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001082
Nicolas Capensfc014542014-02-18 14:47:13 -05001083 if (IsSamplerArray(textureFunction->sampler))
1084 {
1085 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1086 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001087 else if (IsSamplerCube(textureFunction->sampler))
1088 {
1089 addressz = "((((";
1090 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001091 else
1092 {
1093 addressz = "int(floor(depth * frac((";
1094 }
1095
1096 close = "))))";
1097 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001098 }
1099 else
1100 {
1101 switch(hlslCoords)
1102 {
1103 case 2: out << "float2("; break;
1104 case 3: out << "float3("; break;
1105 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001106 default: UNREACHABLE();
1107 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001108 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001109
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001110 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001111
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001112 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001113 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001114 switch(textureFunction->coords)
1115 {
1116 case 3: proj = " / t.z"; break;
1117 case 4: proj = " / t.w"; break;
1118 default: UNREACHABLE();
1119 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001120 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001121
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001122 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001123
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001124 if (mOutputType == SH_HLSL9_OUTPUT)
1125 {
1126 if (hlslCoords >= 3)
1127 {
1128 if (textureFunction->coords < 3)
1129 {
1130 out << ", 0";
1131 }
1132 else
1133 {
1134 out << ", t.z" + proj;
1135 }
1136 }
1137
1138 if (hlslCoords == 4)
1139 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001140 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001141 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001142 case TextureFunction::BIAS: out << ", bias"; break;
1143 case TextureFunction::LOD: out << ", lod"; break;
1144 case TextureFunction::LOD0: out << ", 0"; break;
1145 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001146 default: UNREACHABLE();
1147 }
1148 }
1149
1150 out << "));\n";
1151 }
1152 else if (mOutputType == SH_HLSL11_OUTPUT)
1153 {
1154 if (hlslCoords >= 3)
1155 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001156 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1157 {
1158 out << ", face";
1159 }
1160 else
1161 {
1162 out << ", " + addressz + ("t.z" + proj) + close;
1163 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001164 }
1165
Nicolas Capensd11d5492014-02-19 17:06:10 -05001166 if (textureFunction->method == TextureFunction::GRAD)
1167 {
1168 if (IsIntegerSampler(textureFunction->sampler))
1169 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001170 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001171 }
1172 else if (IsShadowSampler(textureFunction->sampler))
1173 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001174 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001175 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001176 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001177 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1178 // The resulting third component of P' in the shadow forms is used as Dref
1179 out << "), t.z" << proj;
1180 }
1181 else
1182 {
1183 switch(textureFunction->coords)
1184 {
1185 case 3: out << "), t.z"; break;
1186 case 4: out << "), t.w"; break;
1187 default: UNREACHABLE();
1188 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001189 }
1190 }
1191 else
1192 {
1193 out << "), ddx, ddy";
1194 }
1195 }
1196 else if (IsIntegerSampler(textureFunction->sampler) ||
1197 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001198 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001199 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001200 }
1201 else if (IsShadowSampler(textureFunction->sampler))
1202 {
1203 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001204 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001205 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001206 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1207 // The resulting third component of P' in the shadow forms is used as Dref
1208 out << "), t.z" << proj;
1209 }
1210 else
1211 {
1212 switch(textureFunction->coords)
1213 {
1214 case 3: out << "), t.z"; break;
1215 case 4: out << "), t.w"; break;
1216 default: UNREACHABLE();
1217 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001218 }
1219 }
1220 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001221 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001222 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001223 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001224 case TextureFunction::IMPLICIT: out << ")"; break;
1225 case TextureFunction::BIAS: out << "), bias"; break;
1226 case TextureFunction::LOD: out << "), lod"; break;
1227 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001228 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001229 default: UNREACHABLE();
1230 }
1231 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001232
1233 if (textureFunction->offset)
1234 {
1235 out << ", offset";
1236 }
1237
1238 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001239 }
1240 else UNREACHABLE();
1241 }
1242
1243 out << "\n"
1244 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001245 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001246 }
1247
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001248 if (mUsesFragCoord)
1249 {
1250 out << "#define GL_USES_FRAG_COORD\n";
1251 }
1252
1253 if (mUsesPointCoord)
1254 {
1255 out << "#define GL_USES_POINT_COORD\n";
1256 }
1257
1258 if (mUsesFrontFacing)
1259 {
1260 out << "#define GL_USES_FRONT_FACING\n";
1261 }
1262
1263 if (mUsesPointSize)
1264 {
1265 out << "#define GL_USES_POINT_SIZE\n";
1266 }
1267
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001268 if (mUsesFragDepth)
1269 {
1270 out << "#define GL_USES_FRAG_DEPTH\n";
1271 }
1272
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001273 if (mUsesDepthRange)
1274 {
1275 out << "#define GL_USES_DEPTH_RANGE\n";
1276 }
1277
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001278 if (mUsesXor)
1279 {
1280 out << "bool xor(bool p, bool q)\n"
1281 "{\n"
1282 " return (p || q) && !(p && q);\n"
1283 "}\n"
1284 "\n";
1285 }
1286
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001287 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001288}
1289
1290void OutputHLSL::visitSymbol(TIntermSymbol *node)
1291{
Jamie Madill32aab012015-01-27 14:12:26 -05001292 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001293
Jamie Madill570e04d2013-06-21 09:15:33 -04001294 // Handle accessing std140 structs by value
1295 if (mFlaggedStructMappedNames.count(node) > 0)
1296 {
1297 out << mFlaggedStructMappedNames[node];
1298 return;
1299 }
1300
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001301 TString name = node->getSymbol();
1302
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001303 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001304 {
1305 mUsesDepthRange = true;
1306 out << name;
1307 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001308 else
1309 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001310 TQualifier qualifier = node->getQualifier();
1311
1312 if (qualifier == EvqUniform)
1313 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001314 const TType& nodeType = node->getType();
1315 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1316
1317 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001318 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001319 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001320 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001321 else
1322 {
1323 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001324 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001325
Jamie Madill033dae62014-06-18 12:56:28 -04001326 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001327 }
Jamie Madill19571812013-08-12 15:26:34 -07001328 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001329 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001330 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001331 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001332 }
Jamie Madill033dae62014-06-18 12:56:28 -04001333 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001334 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001335 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001336 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001337 }
Jamie Madill19571812013-08-12 15:26:34 -07001338 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001339 {
1340 mReferencedOutputVariables[name] = node;
1341 out << "out_" << name;
1342 }
1343 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001344 {
1345 out << "gl_Color[0]";
1346 mUsesFragColor = true;
1347 }
1348 else if (qualifier == EvqFragData)
1349 {
1350 out << "gl_Color";
1351 mUsesFragData = true;
1352 }
1353 else if (qualifier == EvqFragCoord)
1354 {
1355 mUsesFragCoord = true;
1356 out << name;
1357 }
1358 else if (qualifier == EvqPointCoord)
1359 {
1360 mUsesPointCoord = true;
1361 out << name;
1362 }
1363 else if (qualifier == EvqFrontFacing)
1364 {
1365 mUsesFrontFacing = true;
1366 out << name;
1367 }
1368 else if (qualifier == EvqPointSize)
1369 {
1370 mUsesPointSize = true;
1371 out << name;
1372 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001373 else if (qualifier == EvqInstanceID)
1374 {
1375 mUsesInstanceID = true;
1376 out << name;
1377 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001378 else if (name == "gl_FragDepthEXT")
1379 {
1380 mUsesFragDepth = true;
1381 out << "gl_Depth";
1382 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001383 else if (qualifier == EvqInternal)
1384 {
1385 out << name;
1386 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001387 else
1388 {
Jamie Madill033dae62014-06-18 12:56:28 -04001389 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001391 }
1392}
1393
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001394void OutputHLSL::visitRaw(TIntermRaw *node)
1395{
Jamie Madill32aab012015-01-27 14:12:26 -05001396 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001397}
1398
Olli Etuaho7fb49552015-03-18 17:27:44 +02001399void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1400{
1401 if (type.isScalar() && !type.isArray())
1402 {
1403 if (op == EOpEqual)
1404 {
1405 outputTriplet(visit, "(", " == ", ")", out);
1406 }
1407 else
1408 {
1409 outputTriplet(visit, "(", " != ", ")", out);
1410 }
1411 }
1412 else
1413 {
1414 if (visit == PreVisit && op == EOpNotEqual)
1415 {
1416 out << "!";
1417 }
1418
1419 if (type.isArray())
1420 {
1421 const TString &functionName = addArrayEqualityFunction(type);
1422 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1423 }
1424 else if (type.getBasicType() == EbtStruct)
1425 {
1426 const TStructure &structure = *type.getStruct();
1427 const TString &functionName = addStructEqualityFunction(structure);
1428 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1429 }
1430 else
1431 {
1432 ASSERT(type.isMatrix() || type.isVector());
1433 outputTriplet(visit, "all(", " == ", ")", out);
1434 }
1435 }
1436}
1437
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001438bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1439{
Jamie Madill32aab012015-01-27 14:12:26 -05001440 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001441
Jamie Madill570e04d2013-06-21 09:15:33 -04001442 // Handle accessing std140 structs by value
1443 if (mFlaggedStructMappedNames.count(node) > 0)
1444 {
1445 out << mFlaggedStructMappedNames[node];
1446 return false;
1447 }
1448
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001449 switch (node->getOp())
1450 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001451 case EOpAssign:
1452 if (node->getLeft()->isArray())
1453 {
Olli Etuaho12690762015-03-31 12:55:28 +03001454 const TString &functionName = addArrayAssignmentFunction(node->getType());
1455 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001456 }
1457 else
1458 {
1459 outputTriplet(visit, "(", " = ", ")");
1460 }
1461 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001462 case EOpInitialize:
1463 if (visit == PreVisit)
1464 {
1465 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1466 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1467 // new variable is created before the assignment is evaluated), so we need to convert
1468 // this to "float t = x, x = t;".
1469
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001470 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001471 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001472 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001473
Jamie Madill37997142015-01-28 10:06:34 -05001474 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1475 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001476 {
Jamie Madill37997142015-01-28 10:06:34 -05001477 // For variables which are not constant, defer their real initialization until
1478 // after we initialize other globals: uniforms, attributes and varyings.
1479 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1480 const TString &initString = initializer(node->getType());
1481 node->setRight(new TIntermRaw(node->getType(), initString));
1482 }
1483 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1484 {
1485 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001486 return false;
1487 }
1488 }
1489 else if (visit == InVisit)
1490 {
1491 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001492 }
1493 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001494 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1495 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1496 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1497 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1498 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1499 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001500 if (visit == PreVisit)
1501 {
1502 out << "(";
1503 }
1504 else if (visit == InVisit)
1505 {
1506 out << " = mul(";
1507 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001508 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001509 }
1510 else
1511 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001512 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001513 }
1514 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001515 case EOpMatrixTimesMatrixAssign:
1516 if (visit == PreVisit)
1517 {
1518 out << "(";
1519 }
1520 else if (visit == InVisit)
1521 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001522 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001523 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001524 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001525 }
1526 else
1527 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001528 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001529 }
1530 break;
1531 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001532 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001533 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1534 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1535 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1536 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1537 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001538 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001539 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001540 const TType& leftType = node->getLeft()->getType();
1541 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001542 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001543 if (visit == PreVisit)
1544 {
1545 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1546 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001547 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001548 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001549 return false;
1550 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001551 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001552 else
1553 {
1554 outputTriplet(visit, "", "[", "]");
1555 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001556 }
1557 break;
1558 case EOpIndexIndirect:
1559 // We do not currently support indirect references to interface blocks
1560 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1561 outputTriplet(visit, "", "[", "]");
1562 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001563 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001564 if (visit == InVisit)
1565 {
1566 const TStructure* structure = node->getLeft()->getType().getStruct();
1567 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1568 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001569 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001570
1571 return false;
1572 }
1573 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001574 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001575 if (visit == InVisit)
1576 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001577 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1578 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1579 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001580 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001581
1582 return false;
1583 }
1584 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001585 case EOpVectorSwizzle:
1586 if (visit == InVisit)
1587 {
1588 out << ".";
1589
1590 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1591
1592 if (swizzle)
1593 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001594 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001595
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001596 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597 {
1598 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1599
1600 if (element)
1601 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001602 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001603
1604 switch (i)
1605 {
1606 case 0: out << "x"; break;
1607 case 1: out << "y"; break;
1608 case 2: out << "z"; break;
1609 case 3: out << "w"; break;
1610 default: UNREACHABLE();
1611 }
1612 }
1613 else UNREACHABLE();
1614 }
1615 }
1616 else UNREACHABLE();
1617
1618 return false; // Fully processed
1619 }
1620 break;
1621 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1622 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1623 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1624 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001625 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001626 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1627 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1628 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1629 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1630 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001631 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001632 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001633 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001634 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001635 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1636 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1637 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1638 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1639 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001640 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001641 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1642 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001643 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001644 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001645 if (node->getRight()->hasSideEffects())
1646 {
1647 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1648 return false;
1649 }
1650 else
1651 {
1652 outputTriplet(visit, "(", " || ", ")");
1653 return true;
1654 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001655 case EOpLogicalXor:
1656 mUsesXor = true;
1657 outputTriplet(visit, "xor(", ", ", ")");
1658 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001659 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001660 if (node->getRight()->hasSideEffects())
1661 {
1662 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1663 return false;
1664 }
1665 else
1666 {
1667 outputTriplet(visit, "(", " && ", ")");
1668 return true;
1669 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670 default: UNREACHABLE();
1671 }
1672
1673 return true;
1674}
1675
1676bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1677{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001678 switch (node->getOp())
1679 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001680 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001681 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001682 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1683 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001684 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001685 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1686 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1687 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1688 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001689 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1690 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1691 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1692 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1693 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1694 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1695 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1696 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001697 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1698 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1699 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1700 case EOpAsinh:
1701 ASSERT(node->getUseEmulatedFunction());
1702 writeEmulatedFunctionTriplet(visit, "asinh(");
1703 break;
1704 case EOpAcosh:
1705 ASSERT(node->getUseEmulatedFunction());
1706 writeEmulatedFunctionTriplet(visit, "acosh(");
1707 break;
1708 case EOpAtanh:
1709 ASSERT(node->getUseEmulatedFunction());
1710 writeEmulatedFunctionTriplet(visit, "atanh(");
1711 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001712 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1713 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1714 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1715 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1716 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1717 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1718 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1719 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1720 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001721 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1722 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1723 case EOpRoundEven:
1724 ASSERT(node->getUseEmulatedFunction());
1725 writeEmulatedFunctionTriplet(visit, "roundEven(");
1726 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001727 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1728 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301729 case EOpIsNan:
1730 outputTriplet(visit, "isnan(", "", ")");
1731 mRequiresIEEEStrictCompiling = true;
1732 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301733 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001734 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1735 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1736 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1737 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001738 case EOpPackSnorm2x16:
1739 ASSERT(node->getUseEmulatedFunction());
1740 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1741 break;
1742 case EOpPackUnorm2x16:
1743 ASSERT(node->getUseEmulatedFunction());
1744 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1745 break;
1746 case EOpPackHalf2x16:
1747 ASSERT(node->getUseEmulatedFunction());
1748 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1749 break;
1750 case EOpUnpackSnorm2x16:
1751 ASSERT(node->getUseEmulatedFunction());
1752 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1753 break;
1754 case EOpUnpackUnorm2x16:
1755 ASSERT(node->getUseEmulatedFunction());
1756 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1757 break;
1758 case EOpUnpackHalf2x16:
1759 ASSERT(node->getUseEmulatedFunction());
1760 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1761 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001762 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1763 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001764 case EOpDFdx:
1765 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1766 {
1767 outputTriplet(visit, "(", "", ", 0.0)");
1768 }
1769 else
1770 {
1771 outputTriplet(visit, "ddx(", "", ")");
1772 }
1773 break;
1774 case EOpDFdy:
1775 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1776 {
1777 outputTriplet(visit, "(", "", ", 0.0)");
1778 }
1779 else
1780 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001781 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001782 }
1783 break;
1784 case EOpFwidth:
1785 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1786 {
1787 outputTriplet(visit, "(", "", ", 0.0)");
1788 }
1789 else
1790 {
1791 outputTriplet(visit, "fwidth(", "", ")");
1792 }
1793 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001794 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1795 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001796 case EOpInverse:
1797 ASSERT(node->getUseEmulatedFunction());
1798 writeEmulatedFunctionTriplet(visit, "inverse(");
1799 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001800
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001801 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1802 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803 default: UNREACHABLE();
1804 }
1805
1806 return true;
1807}
1808
1809bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1810{
Jamie Madill32aab012015-01-27 14:12:26 -05001811 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813 switch (node->getOp())
1814 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001815 case EOpSequence:
1816 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001817 if (mInsideFunction)
1818 {
Jamie Madill075edd82013-07-08 13:30:19 -04001819 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001820 out << "{\n";
1821 }
1822
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001823 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001824 {
Jamie Madill075edd82013-07-08 13:30:19 -04001825 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001826
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001827 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001828
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001829 // Don't output ; after case labels, they're terminated by :
1830 // This is needed especially since outputting a ; after a case statement would turn empty
1831 // case statements into non-empty case statements, disallowing fall-through from them.
1832 if ((*sit)->getAsCaseNode() == nullptr)
1833 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001834 }
1835
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001836 if (mInsideFunction)
1837 {
Jamie Madill075edd82013-07-08 13:30:19 -04001838 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001839 out << "}\n";
1840 }
1841
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001842 return false;
1843 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001844 case EOpDeclaration:
1845 if (visit == PreVisit)
1846 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001847 TIntermSequence *sequence = node->getSequence();
1848 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001849
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001850 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001851 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001852 TStructure *structure = variable->getType().getStruct();
1853
1854 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001855 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001856 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001857 }
1858
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001859 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860 {
Jamie Madill37997142015-01-28 10:06:34 -05001861 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862 {
Jamie Madill37997142015-01-28 10:06:34 -05001863 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001864 {
Jamie Madill37997142015-01-28 10:06:34 -05001865 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001866 }
1867
Nicolas Capensd974db42014-10-07 10:50:19 -04001868 if (!mInsideFunction)
1869 {
1870 out << "static ";
1871 }
1872
1873 out << TypeString(variable->getType()) + " ";
1874
Jamie Madill37997142015-01-28 10:06:34 -05001875 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001876
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001877 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001879 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001880 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001881 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001882 }
1883 else
1884 {
Jamie Madill37997142015-01-28 10:06:34 -05001885 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001886 }
1887
Jamie Madill37997142015-01-28 10:06:34 -05001888 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001889 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001890 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891 }
1892 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001894 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1895 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001896 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001897 }
1898 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001899 }
Jamie Madill033dae62014-06-18 12:56:28 -04001900 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001901 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001902 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001903 {
1904 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1905
1906 if (symbol)
1907 {
1908 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1909 mReferencedVaryings[symbol->getSymbol()] = symbol;
1910 }
1911 else
1912 {
1913 (*sit)->traverse(this);
1914 }
1915 }
1916 }
1917
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 return false;
1919 }
1920 else if (visit == InVisit)
1921 {
1922 out << ", ";
1923 }
1924 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001925 case EOpInvariantDeclaration:
1926 // Do not do any translation
1927 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001928 case EOpPrototype:
1929 if (visit == PreVisit)
1930 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001931 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001932
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001933 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001934
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001935 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001936 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001937 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001938
1939 if (symbol)
1940 {
1941 out << argumentString(symbol);
1942
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001943 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001944 {
1945 out << ", ";
1946 }
1947 }
1948 else UNREACHABLE();
1949 }
1950
1951 out << ");\n";
1952
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001953 // Also prototype the Lod0 variant if needed
1954 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1955 {
1956 mOutputLod0Function = true;
1957 node->traverse(this);
1958 mOutputLod0Function = false;
1959 }
1960
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001961 return false;
1962 }
1963 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001964 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965 case EOpFunction:
1966 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001967 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968
Jamie Madill033dae62014-06-18 12:56:28 -04001969 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001970
1971 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001972 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001973 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001974 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001975 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001976 {
Jamie Madill033dae62014-06-18 12:56:28 -04001977 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001978 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001979
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001980 TIntermSequence *sequence = node->getSequence();
1981 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001982
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001983 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001984 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001985 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001986
1987 if (symbol)
1988 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001989 TStructure *structure = symbol->getType().getStruct();
1990
1991 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001992 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001993 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001994 }
1995
1996 out << argumentString(symbol);
1997
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001998 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001999 {
2000 out << ", ";
2001 }
2002 }
2003 else UNREACHABLE();
2004 }
2005
2006 out << ")\n"
2007 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002008
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002009 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002010 {
2011 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002012 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002013 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002014 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002015
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002016 out << "}\n";
2017
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002018 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2019 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002020 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002021 {
2022 mOutputLod0Function = true;
2023 node->traverse(this);
2024 mOutputLod0Function = false;
2025 }
2026 }
2027
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002028 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029 }
2030 break;
2031 case EOpFunctionCall:
2032 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002033 TString name = TFunction::unmangleName(node->getName());
2034 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002035 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002036
2037 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038 {
Jamie Madill033dae62014-06-18 12:56:28 -04002039 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040 }
2041 else
2042 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002043 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002044
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002045 TextureFunction textureFunction;
2046 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002047 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002048 textureFunction.method = TextureFunction::IMPLICIT;
2049 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002050 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002051
2052 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002053 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002054 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002055 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002056 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002057 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002058 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002059 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002060 }
Nicolas Capens46485082014-04-15 13:12:50 -04002061 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2062 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002063 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002064 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002065 }
Nicolas Capens46485082014-04-15 13:12:50 -04002066 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002067 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002068 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002069 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002070 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002071 else if (name == "textureSize")
2072 {
2073 textureFunction.method = TextureFunction::SIZE;
2074 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002075 else if (name == "textureOffset")
2076 {
2077 textureFunction.method = TextureFunction::IMPLICIT;
2078 textureFunction.offset = true;
2079 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002080 else if (name == "textureProjOffset")
2081 {
2082 textureFunction.method = TextureFunction::IMPLICIT;
2083 textureFunction.offset = true;
2084 textureFunction.proj = true;
2085 }
2086 else if (name == "textureLodOffset")
2087 {
2088 textureFunction.method = TextureFunction::LOD;
2089 textureFunction.offset = true;
2090 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002091 else if (name == "textureProjLodOffset")
2092 {
2093 textureFunction.method = TextureFunction::LOD;
2094 textureFunction.proj = true;
2095 textureFunction.offset = true;
2096 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002097 else if (name == "texelFetch")
2098 {
2099 textureFunction.method = TextureFunction::FETCH;
2100 }
2101 else if (name == "texelFetchOffset")
2102 {
2103 textureFunction.method = TextureFunction::FETCH;
2104 textureFunction.offset = true;
2105 }
Nicolas Capens46485082014-04-15 13:12:50 -04002106 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002107 {
2108 textureFunction.method = TextureFunction::GRAD;
2109 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002110 else if (name == "textureGradOffset")
2111 {
2112 textureFunction.method = TextureFunction::GRAD;
2113 textureFunction.offset = true;
2114 }
Nicolas Capens46485082014-04-15 13:12:50 -04002115 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002116 {
2117 textureFunction.method = TextureFunction::GRAD;
2118 textureFunction.proj = true;
2119 }
2120 else if (name == "textureProjGradOffset")
2121 {
2122 textureFunction.method = TextureFunction::GRAD;
2123 textureFunction.proj = true;
2124 textureFunction.offset = true;
2125 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002126 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002127
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002128 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002129 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002130 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2131
2132 if (textureFunction.offset)
2133 {
2134 mandatoryArgumentCount++;
2135 }
2136
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002137 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002138
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002139 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002140 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002141 if (bias)
2142 {
2143 textureFunction.method = TextureFunction::LOD0BIAS;
2144 }
2145 else
2146 {
2147 textureFunction.method = TextureFunction::LOD0;
2148 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002149 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002150 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002151 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002152 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002153 }
2154 }
2155
2156 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002157
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002158 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002159 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002160
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002161 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002162 {
2163 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2164 {
2165 out << "texture_";
2166 (*arg)->traverse(this);
2167 out << ", sampler_";
2168 }
2169
2170 (*arg)->traverse(this);
2171
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002172 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002173 {
2174 out << ", ";
2175 }
2176 }
2177
2178 out << ")";
2179
2180 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 }
2182 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002183 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002184 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2185 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2186 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2187 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2188 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2189 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2190 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2191 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2192 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2193 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2194 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2195 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2196 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2197 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2198 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2199 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2200 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2201 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2202 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002203 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002204 {
Jamie Madill033dae62014-06-18 12:56:28 -04002205 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002206 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002207 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002208 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002209 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002210 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2211 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2212 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2213 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2214 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2215 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002216 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002217 ASSERT(node->getUseEmulatedFunction());
2218 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002219 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002220 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002221 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002222 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002223 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002224 ASSERT(node->getUseEmulatedFunction());
2225 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002226 break;
2227 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2228 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2229 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2230 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2231 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2232 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2233 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2234 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2235 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002236 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002237 ASSERT(node->getUseEmulatedFunction());
2238 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002239 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2241 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002242 case EOpOuterProduct:
2243 ASSERT(node->getUseEmulatedFunction());
2244 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2245 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002246 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002247 default: UNREACHABLE();
2248 }
2249
2250 return true;
2251}
2252
2253bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2254{
Jamie Madill32aab012015-01-27 14:12:26 -05002255 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002256
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002257 if (node->usesTernaryOperator())
2258 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002259 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002260 }
2261 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002263 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002264
Corentin Wallez80bacde2014-11-10 12:07:37 -08002265 // D3D errors when there is a gradient operation in a loop in an unflattened if
2266 // however flattening all the ifs in branch heavy shaders made D3D error too.
2267 // As a temporary workaround we flatten the ifs only if there is at least a loop
2268 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002269 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002270 {
2271 out << "FLATTEN ";
2272 }
2273
2274 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002275
2276 node->getCondition()->traverse(this);
2277
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002278 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002279
Jamie Madill075edd82013-07-08 13:30:19 -04002280 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002281 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002283 bool discard = false;
2284
daniel@transgaming.combb885322010-04-15 20:45:24 +00002285 if (node->getTrueBlock())
2286 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002287 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002288
2289 // Detect true discard
2290 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002291 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292
Jamie Madill075edd82013-07-08 13:30:19 -04002293 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002294 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002295
2296 if (node->getFalseBlock())
2297 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002298 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002299
Jamie Madill075edd82013-07-08 13:30:19 -04002300 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002301 out << "{\n";
2302
Jamie Madill075edd82013-07-08 13:30:19 -04002303 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002304 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002305
Jamie Madill075edd82013-07-08 13:30:19 -04002306 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002307 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002308
2309 // Detect false discard
2310 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2311 }
2312
2313 // ANGLE issue 486: Detect problematic conditional discard
2314 if (discard && FindSideEffectRewriting::search(node))
2315 {
2316 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002317 }
2318 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319
2320 return false;
2321}
2322
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002323bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002324{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002325 if (node->getStatementList())
2326 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002327 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002328 outputTriplet(visit, "switch (", ") ", "");
2329 // The curly braces get written when visiting the statementList aggregate
2330 }
2331 else
2332 {
2333 // No statementList, so it won't output curly braces
2334 outputTriplet(visit, "switch (", ") {", "}\n");
2335 }
2336 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002337}
2338
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002339bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002340{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002341 if (node->hasCondition())
2342 {
2343 outputTriplet(visit, "case (", "", "):\n");
2344 return true;
2345 }
2346 else
2347 {
2348 TInfoSinkBase &out = getInfoSink();
2349 out << "default:\n";
2350 return false;
2351 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002352}
2353
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002354void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2355{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002356 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357}
2358
2359bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2360{
Nicolas Capens655fe362014-04-11 13:12:34 -04002361 mNestedLoopDepth++;
2362
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002363 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2364
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002365 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002366 {
2367 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2368 }
2369
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002370 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002371 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002372 if (handleExcessiveLoop(node))
2373 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002374 mInsideDiscontinuousLoop = wasDiscontinuous;
2375 mNestedLoopDepth--;
2376
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002377 return false;
2378 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002379 }
2380
Jamie Madill32aab012015-01-27 14:12:26 -05002381 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002382
alokp@chromium.org52813552010-11-16 18:36:09 +00002383 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002384 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002385 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002386
Jamie Madill075edd82013-07-08 13:30:19 -04002387 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002388 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 }
2390 else
2391 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002392 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002393
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394 if (node->getInit())
2395 {
2396 node->getInit()->traverse(this);
2397 }
2398
2399 out << "; ";
2400
alokp@chromium.org52813552010-11-16 18:36:09 +00002401 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002403 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002404 }
2405
2406 out << "; ";
2407
alokp@chromium.org52813552010-11-16 18:36:09 +00002408 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002410 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411 }
2412
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002413 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002414
Jamie Madill075edd82013-07-08 13:30:19 -04002415 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002416 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417 }
2418
2419 if (node->getBody())
2420 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002421 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 }
2423
Jamie Madill075edd82013-07-08 13:30:19 -04002424 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002425 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426
alokp@chromium.org52813552010-11-16 18:36:09 +00002427 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 {
Jamie Madill075edd82013-07-08 13:30:19 -04002429 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 out << "while(\n";
2431
alokp@chromium.org52813552010-11-16 18:36:09 +00002432 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433
daniel@transgaming.com73536982012-03-21 20:45:49 +00002434 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 }
2436
daniel@transgaming.com73536982012-03-21 20:45:49 +00002437 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002439 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002440 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002441
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442 return false;
2443}
2444
2445bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2446{
Jamie Madill32aab012015-01-27 14:12:26 -05002447 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448
2449 switch (node->getFlowOp())
2450 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002451 case EOpKill:
2452 outputTriplet(visit, "discard;\n", "", "");
2453 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002454 case EOpBreak:
2455 if (visit == PreVisit)
2456 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002457 if (mNestedLoopDepth > 1)
2458 {
2459 mUsesNestedBreak = true;
2460 }
2461
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002462 if (mExcessiveLoopIndex)
2463 {
2464 out << "{Break";
2465 mExcessiveLoopIndex->traverse(this);
2466 out << " = true; break;}\n";
2467 }
2468 else
2469 {
2470 out << "break;\n";
2471 }
2472 }
2473 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002474 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002475 case EOpReturn:
2476 if (visit == PreVisit)
2477 {
2478 if (node->getExpression())
2479 {
2480 out << "return ";
2481 }
2482 else
2483 {
2484 out << "return;\n";
2485 }
2486 }
2487 else if (visit == PostVisit)
2488 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002489 if (node->getExpression())
2490 {
2491 out << ";\n";
2492 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493 }
2494 break;
2495 default: UNREACHABLE();
2496 }
2497
2498 return true;
2499}
2500
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002501void OutputHLSL::traverseStatements(TIntermNode *node)
2502{
2503 if (isSingleStatement(node))
2504 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002505 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002506 }
2507
2508 node->traverse(this);
2509}
2510
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002511bool OutputHLSL::isSingleStatement(TIntermNode *node)
2512{
2513 TIntermAggregate *aggregate = node->getAsAggregate();
2514
2515 if (aggregate)
2516 {
2517 if (aggregate->getOp() == EOpSequence)
2518 {
2519 return false;
2520 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002521 else if (aggregate->getOp() == EOpDeclaration)
2522 {
2523 // Declaring multiple comma-separated variables must be considered multiple statements
2524 // because each individual declaration has side effects which are visible in the next.
2525 return false;
2526 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002527 else
2528 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002529 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002530 {
2531 if (!isSingleStatement(*sit))
2532 {
2533 return false;
2534 }
2535 }
2536
2537 return true;
2538 }
2539 }
2540
2541 return true;
2542}
2543
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002544// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2545// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002546bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2547{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002548 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002549 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002550
2551 // Parse loops of the form:
2552 // for(int index = initial; index [comparator] limit; index += increment)
2553 TIntermSymbol *index = NULL;
2554 TOperator comparator = EOpNull;
2555 int initial = 0;
2556 int limit = 0;
2557 int increment = 0;
2558
2559 // Parse index name and intial value
2560 if (node->getInit())
2561 {
2562 TIntermAggregate *init = node->getInit()->getAsAggregate();
2563
2564 if (init)
2565 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002566 TIntermSequence *sequence = init->getSequence();
2567 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002568
2569 if (variable && variable->getQualifier() == EvqTemporary)
2570 {
2571 TIntermBinary *assign = variable->getAsBinaryNode();
2572
2573 if (assign->getOp() == EOpInitialize)
2574 {
2575 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2576 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2577
2578 if (symbol && constant)
2579 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002580 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002581 {
2582 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002583 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002584 }
2585 }
2586 }
2587 }
2588 }
2589 }
2590
2591 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002592 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002593 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002594 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002595
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002596 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2597 {
2598 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2599
2600 if (constant)
2601 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002602 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002603 {
2604 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002605 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002606 }
2607 }
2608 }
2609 }
2610
2611 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002612 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002613 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002614 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2615 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002616
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002617 if (binaryTerminal)
2618 {
2619 TOperator op = binaryTerminal->getOp();
2620 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2621
2622 if (constant)
2623 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002624 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002625 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002626 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002627
2628 switch (op)
2629 {
2630 case EOpAddAssign: increment = value; break;
2631 case EOpSubAssign: increment = -value; break;
2632 default: UNIMPLEMENTED();
2633 }
2634 }
2635 }
2636 }
2637 else if (unaryTerminal)
2638 {
2639 TOperator op = unaryTerminal->getOp();
2640
2641 switch (op)
2642 {
2643 case EOpPostIncrement: increment = 1; break;
2644 case EOpPostDecrement: increment = -1; break;
2645 case EOpPreIncrement: increment = 1; break;
2646 case EOpPreDecrement: increment = -1; break;
2647 default: UNIMPLEMENTED();
2648 }
2649 }
2650 }
2651
2652 if (index != NULL && comparator != EOpNull && increment != 0)
2653 {
2654 if (comparator == EOpLessThanEqual)
2655 {
2656 comparator = EOpLessThan;
2657 limit += 1;
2658 }
2659
2660 if (comparator == EOpLessThan)
2661 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002662 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002663
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002664 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002665 {
2666 return false; // Not an excessive loop
2667 }
2668
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002669 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2670 mExcessiveLoopIndex = index;
2671
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002672 out << "{int ";
2673 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002674 out << ";\n"
2675 "bool Break";
2676 index->traverse(this);
2677 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002678
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002679 bool firstLoopFragment = true;
2680
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002681 while (iterations > 0)
2682 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002683 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002684
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002685 if (!firstLoopFragment)
2686 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002687 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002688 index->traverse(this);
2689 out << ") {\n";
2690 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002691
2692 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2693 {
2694 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2695 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002696
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002697 // for(int index = initial; index < clampedLimit; index += increment)
2698
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002699 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002700 index->traverse(this);
2701 out << " = ";
2702 out << initial;
2703
2704 out << "; ";
2705 index->traverse(this);
2706 out << " < ";
2707 out << clampedLimit;
2708
2709 out << "; ";
2710 index->traverse(this);
2711 out << " += ";
2712 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002713 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002714
Jamie Madill075edd82013-07-08 13:30:19 -04002715 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002716 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002717
2718 if (node->getBody())
2719 {
2720 node->getBody()->traverse(this);
2721 }
2722
Jamie Madill075edd82013-07-08 13:30:19 -04002723 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002724 out << ";}\n";
2725
2726 if (!firstLoopFragment)
2727 {
2728 out << "}\n";
2729 }
2730
2731 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002732
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002733 initial += MAX_LOOP_ITERATIONS * increment;
2734 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002735 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002736
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002737 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002738
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002739 mExcessiveLoopIndex = restoreIndex;
2740
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002741 return true;
2742 }
2743 else UNIMPLEMENTED();
2744 }
2745
2746 return false; // Not handled as an excessive loop
2747}
2748
Olli Etuaho7fb49552015-03-18 17:27:44 +02002749void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002750{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002751 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002752 {
2753 out << preString;
2754 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002755 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002756 {
2757 out << inString;
2758 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002759 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760 {
2761 out << postString;
2762 }
2763}
2764
Olli Etuaho7fb49552015-03-18 17:27:44 +02002765void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2766{
2767 outputTriplet(visit, preString, inString, postString, getInfoSink());
2768}
2769
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002770void OutputHLSL::outputLineDirective(int line)
2771{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002772 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002773 {
Jamie Madill32aab012015-01-27 14:12:26 -05002774 TInfoSinkBase &out = getInfoSink();
2775
2776 out << "\n";
2777 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002778
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002779 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002780 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002781 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002782 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002783
Jamie Madill32aab012015-01-27 14:12:26 -05002784 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002785 }
2786}
2787
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002788TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2789{
2790 TQualifier qualifier = symbol->getQualifier();
2791 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002792 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002793
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002794 if (name.empty()) // HLSL demands named arguments, also for prototypes
2795 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002796 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002797 }
2798 else
2799 {
Jamie Madill033dae62014-06-18 12:56:28 -04002800 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002801 }
2802
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002803 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2804 {
Jamie Madill033dae62014-06-18 12:56:28 -04002805 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002806 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002807 }
2808
Jamie Madill033dae62014-06-18 12:56:28 -04002809 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810}
2811
2812TString OutputHLSL::initializer(const TType &type)
2813{
2814 TString string;
2815
Jamie Madill94bf7f22013-07-08 13:31:15 -04002816 size_t size = type.getObjectSize();
2817 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002819 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002820
Jamie Madill94bf7f22013-07-08 13:31:15 -04002821 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002822 {
2823 string += ", ";
2824 }
2825 }
2826
daniel@transgaming.comead23042010-04-29 03:35:36 +00002827 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002828}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002829
Daniel Bratell29190082015-02-20 16:42:54 +01002830void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002831{
Jamie Madill32aab012015-01-27 14:12:26 -05002832 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002833
2834 if (visit == PreVisit)
2835 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002836 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002837
Daniel Bratell29190082015-02-20 16:42:54 +01002838 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002839 }
2840 else if (visit == InVisit)
2841 {
2842 out << ", ";
2843 }
2844 else if (visit == PostVisit)
2845 {
2846 out << ")";
2847 }
2848}
2849
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002850const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2851{
Jamie Madill32aab012015-01-27 14:12:26 -05002852 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002853
Jamie Madill98493dd2013-07-08 14:39:03 -04002854 const TStructure* structure = type.getStruct();
2855 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002856 {
Jamie Madill033dae62014-06-18 12:56:28 -04002857 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002858
Jamie Madill98493dd2013-07-08 14:39:03 -04002859 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002860
Jamie Madill98493dd2013-07-08 14:39:03 -04002861 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002862 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002863 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002864 constUnion = writeConstantUnion(*fieldType, constUnion);
2865
Jamie Madill98493dd2013-07-08 14:39:03 -04002866 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002867 {
2868 out << ", ";
2869 }
2870 }
2871
2872 out << ")";
2873 }
2874 else
2875 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002876 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002877 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002878
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002879 if (writeType)
2880 {
Jamie Madill033dae62014-06-18 12:56:28 -04002881 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002882 }
2883
Jamie Madill94bf7f22013-07-08 13:31:15 -04002884 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002885 {
2886 switch (constUnion->getType())
2887 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002888 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002889 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002890 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002891 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002892 default: UNREACHABLE();
2893 }
2894
2895 if (i != size - 1)
2896 {
2897 out << ", ";
2898 }
2899 }
2900
2901 if (writeType)
2902 {
2903 out << ")";
2904 }
2905 }
2906
2907 return constUnion;
2908}
2909
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002910void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2911{
2912 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2913 outputTriplet(visit, preString.c_str(), ", ", ")");
2914}
2915
Jamie Madill37997142015-01-28 10:06:34 -05002916bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2917{
2918 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2919 expression->traverse(&searchSymbol);
2920
2921 if (searchSymbol.foundMatch())
2922 {
2923 // Type already printed
2924 out << "t" + str(mUniqueIndex) + " = ";
2925 expression->traverse(this);
2926 out << ", ";
2927 symbolNode->traverse(this);
2928 out << " = t" + str(mUniqueIndex);
2929
2930 mUniqueIndex++;
2931 return true;
2932 }
2933
2934 return false;
2935}
2936
2937void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2938{
2939 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2940 << "\n"
2941 << "void initializeDeferredGlobals()\n"
2942 << "{\n";
2943
2944 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2945 {
2946 TIntermSymbol *symbol = deferredGlobal.first;
2947 TIntermTyped *expression = deferredGlobal.second;
2948 ASSERT(symbol);
2949 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2950
2951 out << " " << Decorate(symbol->getSymbol()) << " = ";
2952
2953 if (!writeSameSymbolInitializer(out, symbol, expression))
2954 {
2955 ASSERT(mInfoSinkStack.top() == &out);
2956 expression->traverse(this);
2957 }
2958
2959 out << ";\n";
2960 }
2961
2962 out << "}\n"
2963 << "\n";
2964}
2965
Jamie Madill55e79e02015-02-09 15:35:00 -05002966TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2967{
2968 const TFieldList &fields = structure.fields();
2969
2970 for (const auto &eqFunction : mStructEqualityFunctions)
2971 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002972 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002973 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002974 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002975 }
2976 }
2977
2978 const TString &structNameString = StructNameString(structure);
2979
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002980 StructEqualityFunction *function = new StructEqualityFunction();
2981 function->structure = &structure;
2982 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002983
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002984 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002985
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002986 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2987 << "{\n"
2988 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002989
2990 for (size_t i = 0; i < fields.size(); i++)
2991 {
2992 const TField *field = fields[i];
2993 const TType *fieldType = field->type();
2994
2995 const TString &fieldNameA = "a." + Decorate(field->name());
2996 const TString &fieldNameB = "b." + Decorate(field->name());
2997
2998 if (i > 0)
2999 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003000 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003001 }
3002
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003003 fnOut << "(";
3004 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3005 fnOut << fieldNameA;
3006 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3007 fnOut << fieldNameB;
3008 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3009 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003010 }
3011
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003012 fnOut << ";\n" << "}\n";
3013
3014 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003015
3016 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003017 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003018
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003019 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003020}
3021
Olli Etuaho7fb49552015-03-18 17:27:44 +02003022TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3023{
3024 for (const auto &eqFunction : mArrayEqualityFunctions)
3025 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003026 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003027 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003028 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003029 }
3030 }
3031
3032 const TString &typeName = TypeString(type);
3033
Olli Etuaho12690762015-03-31 12:55:28 +03003034 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003035 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003036
3037 TInfoSinkBase fnNameOut;
3038 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003039 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003040
3041 TType nonArrayType = type;
3042 nonArrayType.clearArrayness();
3043
3044 TInfoSinkBase fnOut;
3045
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003046 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003047 << typeName << " a[" << type.getArraySize() << "], "
3048 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003049 << "{\n"
3050 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3051 " {\n"
3052 " if (";
3053
3054 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3055 fnOut << "a[i]";
3056 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3057 fnOut << "b[i]";
3058 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3059
3060 fnOut << ") { return false; }\n"
3061 " }\n"
3062 " return true;\n"
3063 "}\n";
3064
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003065 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003066
3067 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003068 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003069
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003070 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003071}
3072
Olli Etuaho12690762015-03-31 12:55:28 +03003073TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3074{
3075 for (const auto &assignFunction : mArrayAssignmentFunctions)
3076 {
3077 if (assignFunction.type == type)
3078 {
3079 return assignFunction.functionName;
3080 }
3081 }
3082
3083 const TString &typeName = TypeString(type);
3084
3085 ArrayHelperFunction function;
3086 function.type = type;
3087
3088 TInfoSinkBase fnNameOut;
3089 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3090 function.functionName = fnNameOut.c_str();
3091
3092 TInfoSinkBase fnOut;
3093
3094 fnOut << "void " << function.functionName << "(out "
3095 << typeName << " a[" << type.getArraySize() << "], "
3096 << typeName << " b[" << type.getArraySize() << "])\n"
3097 << "{\n"
3098 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3099 " {\n"
3100 " a[i] = b[i];\n"
3101 " }\n"
3102 "}\n";
3103
3104 function.functionDefinition = fnOut.c_str();
3105
3106 mArrayAssignmentFunctions.push_back(function);
3107
3108 return function.functionName;
3109}
3110
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003111}