blob: d52f3a82653421782ce9cd939a75fb4ab64fab34 [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 Etuaho9638c352015-04-01 14:34:52 +0300356 if (!mArrayConstructIntoFunctions.empty())
357 {
358 out << "\n// Array constructor functions\n\n";
359 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
360 {
361 out << constructIntoFunction.functionDefinition << "\n";
362 }
363 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200364
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500365 if (mUsesDiscardRewriting)
366 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400367 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500368 }
369
Nicolas Capens655fe362014-04-11 13:12:34 -0400370 if (mUsesNestedBreak)
371 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400372 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400373 }
374
Arun Patole44efa0b2015-03-04 17:11:05 +0530375 if (mRequiresIEEEStrictCompiling)
376 {
377 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
378 }
379
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400380 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
381 "#define LOOP [loop]\n"
382 "#define FLATTEN [flatten]\n"
383 "#else\n"
384 "#define LOOP\n"
385 "#define FLATTEN\n"
386 "#endif\n";
387
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200388 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000389 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200390 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
391 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000392
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000393 out << "// Varyings\n";
394 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400395 out << "\n";
396
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200397 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000398 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500399 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000400 {
Jamie Madill46131a32013-06-20 11:55:50 -0400401 const TString &variableName = outputVariableIt->first;
402 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400403
Jamie Madill033dae62014-06-18 12:56:28 -0400404 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400405 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000406 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000407 }
Jamie Madill46131a32013-06-20 11:55:50 -0400408 else
409 {
410 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
411
412 out << "static float4 gl_Color[" << numColorValues << "] =\n"
413 "{\n";
414 for (unsigned int i = 0; i < numColorValues; i++)
415 {
416 out << " float4(0, 0, 0, 0)";
417 if (i + 1 != numColorValues)
418 {
419 out << ",";
420 }
421 out << "\n";
422 }
423
424 out << "};\n";
425 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000426
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400427 if (mUsesFragDepth)
428 {
429 out << "static float gl_Depth = 0.0;\n";
430 }
431
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000432 if (mUsesFragCoord)
433 {
434 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
435 }
436
437 if (mUsesPointCoord)
438 {
439 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
440 }
441
442 if (mUsesFrontFacing)
443 {
444 out << "static bool gl_FrontFacing = false;\n";
445 }
446
447 out << "\n";
448
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000449 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000450 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000451 out << "struct gl_DepthRangeParameters\n"
452 "{\n"
453 " float near;\n"
454 " float far;\n"
455 " float diff;\n"
456 "};\n"
457 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000458 }
459
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000460 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000461 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000462 out << "cbuffer DriverConstants : register(b1)\n"
463 "{\n";
464
465 if (mUsesDepthRange)
466 {
467 out << " float3 dx_DepthRange : packoffset(c0);\n";
468 }
469
470 if (mUsesFragCoord)
471 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000472 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000473 }
474
475 if (mUsesFragCoord || mUsesFrontFacing)
476 {
477 out << " float3 dx_DepthFront : packoffset(c2);\n";
478 }
479
480 out << "};\n";
481 }
482 else
483 {
484 if (mUsesDepthRange)
485 {
486 out << "uniform float3 dx_DepthRange : register(c0);";
487 }
488
489 if (mUsesFragCoord)
490 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000491 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000492 }
493
494 if (mUsesFragCoord || mUsesFrontFacing)
495 {
496 out << "uniform float3 dx_DepthFront : register(c2);\n";
497 }
498 }
499
500 out << "\n";
501
502 if (mUsesDepthRange)
503 {
504 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
505 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000506 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000507
Jamie Madillf91ce812014-06-13 10:04:34 -0400508 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000509 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400510 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000511 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400512 out << flaggedStructs;
513 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000514 }
515
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000516 if (usingMRTExtension && mNumRenderTargets > 1)
517 {
518 out << "#define GL_USES_MRT\n";
519 }
520
521 if (mUsesFragColor)
522 {
523 out << "#define GL_USES_FRAG_COLOR\n";
524 }
525
526 if (mUsesFragData)
527 {
528 out << "#define GL_USES_FRAG_DATA\n";
529 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000530 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000531 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000533 out << "// Attributes\n";
534 out << attributes;
535 out << "\n"
536 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400537
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000538 if (mUsesPointSize)
539 {
540 out << "static float gl_PointSize = float(1);\n";
541 }
542
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000543 if (mUsesInstanceID)
544 {
545 out << "static int gl_InstanceID;";
546 }
547
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000548 out << "\n"
549 "// Varyings\n";
550 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000551 out << "\n";
552
553 if (mUsesDepthRange)
554 {
555 out << "struct gl_DepthRangeParameters\n"
556 "{\n"
557 " float near;\n"
558 " float far;\n"
559 " float diff;\n"
560 "};\n"
561 "\n";
562 }
563
564 if (mOutputType == SH_HLSL11_OUTPUT)
565 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800566 out << "cbuffer DriverConstants : register(b1)\n"
567 "{\n";
568
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000569 if (mUsesDepthRange)
570 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800571 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000572 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800573
Cooper Partine6664f02015-01-09 16:22:24 -0800574 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800575 // However, we declare it for all shaders (including Feature Level 10+).
576 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
577 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800578 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800579
580 out << "};\n"
581 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000582 }
583 else
584 {
585 if (mUsesDepthRange)
586 {
587 out << "uniform float3 dx_DepthRange : register(c0);\n";
588 }
589
Cooper Partine6664f02015-01-09 16:22:24 -0800590 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
591 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000592 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000593 }
594
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000595 if (mUsesDepthRange)
596 {
597 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
598 "\n";
599 }
600
Jamie Madillf91ce812014-06-13 10:04:34 -0400601 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000602 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400603 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000604 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400605 out << flaggedStructs;
606 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000607 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400608 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000609
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400610 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
611 {
612 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400613 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000614 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400615 switch(textureFunction->sampler)
616 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400617 case EbtSampler2D: out << "int2 "; break;
618 case EbtSampler3D: out << "int3 "; break;
619 case EbtSamplerCube: out << "int2 "; break;
620 case EbtSampler2DArray: out << "int3 "; break;
621 case EbtISampler2D: out << "int2 "; break;
622 case EbtISampler3D: out << "int3 "; break;
623 case EbtISamplerCube: out << "int2 "; break;
624 case EbtISampler2DArray: out << "int3 "; break;
625 case EbtUSampler2D: out << "int2 "; break;
626 case EbtUSampler3D: out << "int3 "; break;
627 case EbtUSamplerCube: out << "int2 "; break;
628 case EbtUSampler2DArray: out << "int3 "; break;
629 case EbtSampler2DShadow: out << "int2 "; break;
630 case EbtSamplerCubeShadow: out << "int2 "; break;
631 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400632 default: UNREACHABLE();
633 }
634 }
635 else // Sampling function
636 {
637 switch(textureFunction->sampler)
638 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400639 case EbtSampler2D: out << "float4 "; break;
640 case EbtSampler3D: out << "float4 "; break;
641 case EbtSamplerCube: out << "float4 "; break;
642 case EbtSampler2DArray: out << "float4 "; break;
643 case EbtISampler2D: out << "int4 "; break;
644 case EbtISampler3D: out << "int4 "; break;
645 case EbtISamplerCube: out << "int4 "; break;
646 case EbtISampler2DArray: out << "int4 "; break;
647 case EbtUSampler2D: out << "uint4 "; break;
648 case EbtUSampler3D: out << "uint4 "; break;
649 case EbtUSamplerCube: out << "uint4 "; break;
650 case EbtUSampler2DArray: out << "uint4 "; break;
651 case EbtSampler2DShadow: out << "float "; break;
652 case EbtSamplerCubeShadow: out << "float "; break;
653 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400654 default: UNREACHABLE();
655 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000656 }
657
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400658 // Function name
659 out << textureFunction->name();
660
661 // Argument list
662 int hlslCoords = 4;
663
664 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000665 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400666 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000667 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400668 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
669 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
670 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000671 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400672
Nicolas Capens75fb4752013-07-10 15:14:47 -0400673 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000674 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400675 case TextureFunction::IMPLICIT: break;
676 case TextureFunction::BIAS: hlslCoords = 4; break;
677 case TextureFunction::LOD: hlslCoords = 4; break;
678 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400679 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400680 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000681 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400682 }
683 else if (mOutputType == SH_HLSL11_OUTPUT)
684 {
685 switch(textureFunction->sampler)
686 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400687 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
688 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
689 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
690 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
691 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
692 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500693 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400694 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
695 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
696 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500697 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400698 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
699 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
700 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
701 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400702 default: UNREACHABLE();
703 }
704 }
705 else UNREACHABLE();
706
Nicolas Capensfc014542014-02-18 14:47:13 -0500707 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400708 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500709 switch(textureFunction->coords)
710 {
711 case 2: out << ", int2 t"; break;
712 case 3: out << ", int3 t"; break;
713 default: UNREACHABLE();
714 }
715 }
716 else // Floating-point coordinates (except textureSize)
717 {
718 switch(textureFunction->coords)
719 {
720 case 1: out << ", int lod"; break; // textureSize()
721 case 2: out << ", float2 t"; break;
722 case 3: out << ", float3 t"; break;
723 case 4: out << ", float4 t"; break;
724 default: UNREACHABLE();
725 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000726 }
727
Nicolas Capensd11d5492014-02-19 17:06:10 -0500728 if (textureFunction->method == TextureFunction::GRAD)
729 {
730 switch(textureFunction->sampler)
731 {
732 case EbtSampler2D:
733 case EbtISampler2D:
734 case EbtUSampler2D:
735 case EbtSampler2DArray:
736 case EbtISampler2DArray:
737 case EbtUSampler2DArray:
738 case EbtSampler2DShadow:
739 case EbtSampler2DArrayShadow:
740 out << ", float2 ddx, float2 ddy";
741 break;
742 case EbtSampler3D:
743 case EbtISampler3D:
744 case EbtUSampler3D:
745 case EbtSamplerCube:
746 case EbtISamplerCube:
747 case EbtUSamplerCube:
748 case EbtSamplerCubeShadow:
749 out << ", float3 ddx, float3 ddy";
750 break;
751 default: UNREACHABLE();
752 }
753 }
754
Nicolas Capens75fb4752013-07-10 15:14:47 -0400755 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000756 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400757 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400758 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400759 case TextureFunction::LOD: out << ", float lod"; break;
760 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400761 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400762 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500763 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500764 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400765 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000766 }
767
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500768 if (textureFunction->offset)
769 {
770 switch(textureFunction->sampler)
771 {
772 case EbtSampler2D: out << ", int2 offset"; break;
773 case EbtSampler3D: out << ", int3 offset"; break;
774 case EbtSampler2DArray: out << ", int2 offset"; break;
775 case EbtISampler2D: out << ", int2 offset"; break;
776 case EbtISampler3D: out << ", int3 offset"; break;
777 case EbtISampler2DArray: out << ", int2 offset"; break;
778 case EbtUSampler2D: out << ", int2 offset"; break;
779 case EbtUSampler3D: out << ", int3 offset"; break;
780 case EbtUSampler2DArray: out << ", int2 offset"; break;
781 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500782 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500783 default: UNREACHABLE();
784 }
785 }
786
Nicolas Capens84cfa122014-04-14 13:48:45 -0400787 if (textureFunction->method == TextureFunction::BIAS ||
788 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500789 {
790 out << ", float bias";
791 }
792
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400793 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400794 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400795
Nicolas Capens75fb4752013-07-10 15:14:47 -0400796 if (textureFunction->method == TextureFunction::SIZE)
797 {
798 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
799 {
800 if (IsSamplerArray(textureFunction->sampler))
801 {
802 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
803 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
804 }
805 else
806 {
807 out << " uint width; uint height; uint numberOfLevels;\n"
808 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
809 }
810 }
811 else if (IsSampler3D(textureFunction->sampler))
812 {
813 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
814 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
815 }
816 else UNREACHABLE();
817
818 switch(textureFunction->sampler)
819 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400820 case EbtSampler2D: out << " return int2(width, height);"; break;
821 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
822 case EbtSamplerCube: out << " return int2(width, height);"; break;
823 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
824 case EbtISampler2D: out << " return int2(width, height);"; break;
825 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
826 case EbtISamplerCube: out << " return int2(width, height);"; break;
827 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
828 case EbtUSampler2D: out << " return int2(width, height);"; break;
829 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
830 case EbtUSamplerCube: out << " return int2(width, height);"; break;
831 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
832 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
833 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
834 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400835 default: UNREACHABLE();
836 }
837 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400838 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400839 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500840 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
841 {
842 out << " float width; float height; float layers; float levels;\n";
843
844 out << " uint mip = 0;\n";
845
846 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
847
848 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
849 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
850 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
851 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
852
853 // FACE_POSITIVE_X = 000b
854 // FACE_NEGATIVE_X = 001b
855 // FACE_POSITIVE_Y = 010b
856 // FACE_NEGATIVE_Y = 011b
857 // FACE_POSITIVE_Z = 100b
858 // FACE_NEGATIVE_Z = 101b
859 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
860
861 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
862 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
863 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
864
865 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
866 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
867 }
868 else if (IsIntegerSampler(textureFunction->sampler) &&
869 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400870 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400871 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400872 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400873 if (IsSamplerArray(textureFunction->sampler))
874 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400875 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400876
Nicolas Capens9edebd62013-08-06 10:59:10 -0400877 if (textureFunction->method == TextureFunction::LOD0)
878 {
879 out << " uint mip = 0;\n";
880 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400881 else if (textureFunction->method == TextureFunction::LOD0BIAS)
882 {
883 out << " uint mip = bias;\n";
884 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400885 else
886 {
887 if (textureFunction->method == TextureFunction::IMPLICIT ||
888 textureFunction->method == TextureFunction::BIAS)
889 {
890 out << " x.GetDimensions(0, width, height, layers, levels);\n"
891 " float2 tSized = float2(t.x * width, t.y * height);\n"
892 " float dx = length(ddx(tSized));\n"
893 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500894 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400895
896 if (textureFunction->method == TextureFunction::BIAS)
897 {
898 out << " lod += bias;\n";
899 }
900 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500901 else if (textureFunction->method == TextureFunction::GRAD)
902 {
903 out << " x.GetDimensions(0, width, height, layers, levels);\n"
904 " float lod = log2(max(length(ddx), length(ddy)));\n";
905 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400906
907 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
908 }
909
910 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400911 }
912 else
913 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400914 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400915
Nicolas Capens9edebd62013-08-06 10:59:10 -0400916 if (textureFunction->method == TextureFunction::LOD0)
917 {
918 out << " uint mip = 0;\n";
919 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400920 else if (textureFunction->method == TextureFunction::LOD0BIAS)
921 {
922 out << " uint mip = bias;\n";
923 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400924 else
925 {
926 if (textureFunction->method == TextureFunction::IMPLICIT ||
927 textureFunction->method == TextureFunction::BIAS)
928 {
929 out << " x.GetDimensions(0, width, height, levels);\n"
930 " float2 tSized = float2(t.x * width, t.y * height);\n"
931 " float dx = length(ddx(tSized));\n"
932 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500933 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400934
935 if (textureFunction->method == TextureFunction::BIAS)
936 {
937 out << " lod += bias;\n";
938 }
939 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500940 else if (textureFunction->method == TextureFunction::LOD)
941 {
942 out << " x.GetDimensions(0, width, height, levels);\n";
943 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500944 else if (textureFunction->method == TextureFunction::GRAD)
945 {
946 out << " x.GetDimensions(0, width, height, levels);\n"
947 " float lod = log2(max(length(ddx), length(ddy)));\n";
948 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400949
950 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
951 }
952
953 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400954 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400955 }
956 else if (IsSampler3D(textureFunction->sampler))
957 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400958 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400959
Nicolas Capens9edebd62013-08-06 10:59:10 -0400960 if (textureFunction->method == TextureFunction::LOD0)
961 {
962 out << " uint mip = 0;\n";
963 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400964 else if (textureFunction->method == TextureFunction::LOD0BIAS)
965 {
966 out << " uint mip = bias;\n";
967 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400968 else
969 {
970 if (textureFunction->method == TextureFunction::IMPLICIT ||
971 textureFunction->method == TextureFunction::BIAS)
972 {
973 out << " x.GetDimensions(0, width, height, depth, levels);\n"
974 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
975 " float dx = length(ddx(tSized));\n"
976 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500977 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400978
979 if (textureFunction->method == TextureFunction::BIAS)
980 {
981 out << " lod += bias;\n";
982 }
983 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500984 else if (textureFunction->method == TextureFunction::GRAD)
985 {
986 out << " x.GetDimensions(0, width, height, depth, levels);\n"
987 " float lod = log2(max(length(ddx), length(ddy)));\n";
988 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400989
990 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
991 }
992
993 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400994 }
995 else UNREACHABLE();
996 }
997
998 out << " return ";
999
1000 // HLSL intrinsic
1001 if (mOutputType == SH_HLSL9_OUTPUT)
1002 {
1003 switch(textureFunction->sampler)
1004 {
1005 case EbtSampler2D: out << "tex2D"; break;
1006 case EbtSamplerCube: out << "texCUBE"; break;
1007 default: UNREACHABLE();
1008 }
1009
Nicolas Capens75fb4752013-07-10 15:14:47 -04001010 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001011 {
1012 case TextureFunction::IMPLICIT: out << "(s, "; break;
1013 case TextureFunction::BIAS: out << "bias(s, "; break;
1014 case TextureFunction::LOD: out << "lod(s, "; break;
1015 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001016 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001017 default: UNREACHABLE();
1018 }
1019 }
1020 else if (mOutputType == SH_HLSL11_OUTPUT)
1021 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001022 if (textureFunction->method == TextureFunction::GRAD)
1023 {
1024 if (IsIntegerSampler(textureFunction->sampler))
1025 {
1026 out << "x.Load(";
1027 }
1028 else if (IsShadowSampler(textureFunction->sampler))
1029 {
1030 out << "x.SampleCmpLevelZero(s, ";
1031 }
1032 else
1033 {
1034 out << "x.SampleGrad(s, ";
1035 }
1036 }
1037 else if (IsIntegerSampler(textureFunction->sampler) ||
1038 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001039 {
1040 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001041 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001042 else if (IsShadowSampler(textureFunction->sampler))
1043 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001044 switch(textureFunction->method)
1045 {
1046 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1047 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1048 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1049 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1050 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1051 default: UNREACHABLE();
1052 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001053 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001054 else
1055 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001056 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001057 {
1058 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1059 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1060 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1061 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001062 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001063 default: UNREACHABLE();
1064 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001065 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001066 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001067 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001068
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001069 // Integer sampling requires integer addresses
1070 TString addressx = "";
1071 TString addressy = "";
1072 TString addressz = "";
1073 TString close = "";
1074
Nicolas Capensfc014542014-02-18 14:47:13 -05001075 if (IsIntegerSampler(textureFunction->sampler) ||
1076 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001077 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001078 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001079 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001080 case 2: out << "int3("; break;
1081 case 3: out << "int4("; break;
1082 default: UNREACHABLE();
1083 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001084
Nicolas Capensfc014542014-02-18 14:47:13 -05001085 // Convert from normalized floating-point to integer
1086 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001087 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001088 addressx = "int(floor(width * frac((";
1089 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001090
Nicolas Capensfc014542014-02-18 14:47:13 -05001091 if (IsSamplerArray(textureFunction->sampler))
1092 {
1093 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1094 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001095 else if (IsSamplerCube(textureFunction->sampler))
1096 {
1097 addressz = "((((";
1098 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001099 else
1100 {
1101 addressz = "int(floor(depth * frac((";
1102 }
1103
1104 close = "))))";
1105 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001106 }
1107 else
1108 {
1109 switch(hlslCoords)
1110 {
1111 case 2: out << "float2("; break;
1112 case 3: out << "float3("; break;
1113 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001114 default: UNREACHABLE();
1115 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001116 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001117
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001118 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001119
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001120 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001121 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001122 switch(textureFunction->coords)
1123 {
1124 case 3: proj = " / t.z"; break;
1125 case 4: proj = " / t.w"; break;
1126 default: UNREACHABLE();
1127 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001128 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001129
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001130 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001131
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001132 if (mOutputType == SH_HLSL9_OUTPUT)
1133 {
1134 if (hlslCoords >= 3)
1135 {
1136 if (textureFunction->coords < 3)
1137 {
1138 out << ", 0";
1139 }
1140 else
1141 {
1142 out << ", t.z" + proj;
1143 }
1144 }
1145
1146 if (hlslCoords == 4)
1147 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001148 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001149 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001150 case TextureFunction::BIAS: out << ", bias"; break;
1151 case TextureFunction::LOD: out << ", lod"; break;
1152 case TextureFunction::LOD0: out << ", 0"; break;
1153 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001154 default: UNREACHABLE();
1155 }
1156 }
1157
1158 out << "));\n";
1159 }
1160 else if (mOutputType == SH_HLSL11_OUTPUT)
1161 {
1162 if (hlslCoords >= 3)
1163 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001164 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1165 {
1166 out << ", face";
1167 }
1168 else
1169 {
1170 out << ", " + addressz + ("t.z" + proj) + close;
1171 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001172 }
1173
Nicolas Capensd11d5492014-02-19 17:06:10 -05001174 if (textureFunction->method == TextureFunction::GRAD)
1175 {
1176 if (IsIntegerSampler(textureFunction->sampler))
1177 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001178 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001179 }
1180 else if (IsShadowSampler(textureFunction->sampler))
1181 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001182 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001183 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001184 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001185 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1186 // The resulting third component of P' in the shadow forms is used as Dref
1187 out << "), t.z" << proj;
1188 }
1189 else
1190 {
1191 switch(textureFunction->coords)
1192 {
1193 case 3: out << "), t.z"; break;
1194 case 4: out << "), t.w"; break;
1195 default: UNREACHABLE();
1196 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001197 }
1198 }
1199 else
1200 {
1201 out << "), ddx, ddy";
1202 }
1203 }
1204 else if (IsIntegerSampler(textureFunction->sampler) ||
1205 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001206 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001207 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001208 }
1209 else if (IsShadowSampler(textureFunction->sampler))
1210 {
1211 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001212 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001213 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001214 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1215 // The resulting third component of P' in the shadow forms is used as Dref
1216 out << "), t.z" << proj;
1217 }
1218 else
1219 {
1220 switch(textureFunction->coords)
1221 {
1222 case 3: out << "), t.z"; break;
1223 case 4: out << "), t.w"; break;
1224 default: UNREACHABLE();
1225 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001226 }
1227 }
1228 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001229 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001230 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001231 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001232 case TextureFunction::IMPLICIT: out << ")"; break;
1233 case TextureFunction::BIAS: out << "), bias"; break;
1234 case TextureFunction::LOD: out << "), lod"; break;
1235 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001236 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001237 default: UNREACHABLE();
1238 }
1239 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001240
1241 if (textureFunction->offset)
1242 {
1243 out << ", offset";
1244 }
1245
1246 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001247 }
1248 else UNREACHABLE();
1249 }
1250
1251 out << "\n"
1252 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001253 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001254 }
1255
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001256 if (mUsesFragCoord)
1257 {
1258 out << "#define GL_USES_FRAG_COORD\n";
1259 }
1260
1261 if (mUsesPointCoord)
1262 {
1263 out << "#define GL_USES_POINT_COORD\n";
1264 }
1265
1266 if (mUsesFrontFacing)
1267 {
1268 out << "#define GL_USES_FRONT_FACING\n";
1269 }
1270
1271 if (mUsesPointSize)
1272 {
1273 out << "#define GL_USES_POINT_SIZE\n";
1274 }
1275
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001276 if (mUsesFragDepth)
1277 {
1278 out << "#define GL_USES_FRAG_DEPTH\n";
1279 }
1280
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001281 if (mUsesDepthRange)
1282 {
1283 out << "#define GL_USES_DEPTH_RANGE\n";
1284 }
1285
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001286 if (mUsesXor)
1287 {
1288 out << "bool xor(bool p, bool q)\n"
1289 "{\n"
1290 " return (p || q) && !(p && q);\n"
1291 "}\n"
1292 "\n";
1293 }
1294
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001295 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001296}
1297
1298void OutputHLSL::visitSymbol(TIntermSymbol *node)
1299{
Jamie Madill32aab012015-01-27 14:12:26 -05001300 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001301
Jamie Madill570e04d2013-06-21 09:15:33 -04001302 // Handle accessing std140 structs by value
1303 if (mFlaggedStructMappedNames.count(node) > 0)
1304 {
1305 out << mFlaggedStructMappedNames[node];
1306 return;
1307 }
1308
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001309 TString name = node->getSymbol();
1310
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001311 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001312 {
1313 mUsesDepthRange = true;
1314 out << name;
1315 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001316 else
1317 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001318 TQualifier qualifier = node->getQualifier();
1319
1320 if (qualifier == EvqUniform)
1321 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001322 const TType& nodeType = node->getType();
1323 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1324
1325 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001326 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001327 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001328 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001329 else
1330 {
1331 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001332 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001333
Jamie Madill033dae62014-06-18 12:56:28 -04001334 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001335 }
Jamie Madill19571812013-08-12 15:26:34 -07001336 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001337 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001338 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001339 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001340 }
Jamie Madill033dae62014-06-18 12:56:28 -04001341 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001342 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001343 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001344 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001345 }
Jamie Madill19571812013-08-12 15:26:34 -07001346 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001347 {
1348 mReferencedOutputVariables[name] = node;
1349 out << "out_" << name;
1350 }
1351 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001352 {
1353 out << "gl_Color[0]";
1354 mUsesFragColor = true;
1355 }
1356 else if (qualifier == EvqFragData)
1357 {
1358 out << "gl_Color";
1359 mUsesFragData = true;
1360 }
1361 else if (qualifier == EvqFragCoord)
1362 {
1363 mUsesFragCoord = true;
1364 out << name;
1365 }
1366 else if (qualifier == EvqPointCoord)
1367 {
1368 mUsesPointCoord = true;
1369 out << name;
1370 }
1371 else if (qualifier == EvqFrontFacing)
1372 {
1373 mUsesFrontFacing = true;
1374 out << name;
1375 }
1376 else if (qualifier == EvqPointSize)
1377 {
1378 mUsesPointSize = true;
1379 out << name;
1380 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001381 else if (qualifier == EvqInstanceID)
1382 {
1383 mUsesInstanceID = true;
1384 out << name;
1385 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001386 else if (name == "gl_FragDepthEXT")
1387 {
1388 mUsesFragDepth = true;
1389 out << "gl_Depth";
1390 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001391 else if (qualifier == EvqInternal)
1392 {
1393 out << name;
1394 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001395 else
1396 {
Jamie Madill033dae62014-06-18 12:56:28 -04001397 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001398 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001399 }
1400}
1401
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001402void OutputHLSL::visitRaw(TIntermRaw *node)
1403{
Jamie Madill32aab012015-01-27 14:12:26 -05001404 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001405}
1406
Olli Etuaho7fb49552015-03-18 17:27:44 +02001407void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1408{
1409 if (type.isScalar() && !type.isArray())
1410 {
1411 if (op == EOpEqual)
1412 {
1413 outputTriplet(visit, "(", " == ", ")", out);
1414 }
1415 else
1416 {
1417 outputTriplet(visit, "(", " != ", ")", out);
1418 }
1419 }
1420 else
1421 {
1422 if (visit == PreVisit && op == EOpNotEqual)
1423 {
1424 out << "!";
1425 }
1426
1427 if (type.isArray())
1428 {
1429 const TString &functionName = addArrayEqualityFunction(type);
1430 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1431 }
1432 else if (type.getBasicType() == EbtStruct)
1433 {
1434 const TStructure &structure = *type.getStruct();
1435 const TString &functionName = addStructEqualityFunction(structure);
1436 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1437 }
1438 else
1439 {
1440 ASSERT(type.isMatrix() || type.isVector());
1441 outputTriplet(visit, "all(", " == ", ")", out);
1442 }
1443 }
1444}
1445
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001446bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1447{
Jamie Madill32aab012015-01-27 14:12:26 -05001448 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001449
Jamie Madill570e04d2013-06-21 09:15:33 -04001450 // Handle accessing std140 structs by value
1451 if (mFlaggedStructMappedNames.count(node) > 0)
1452 {
1453 out << mFlaggedStructMappedNames[node];
1454 return false;
1455 }
1456
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001457 switch (node->getOp())
1458 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001459 case EOpAssign:
1460 if (node->getLeft()->isArray())
1461 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001462 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1463 if (rightAgg != nullptr && rightAgg->isConstructor())
1464 {
1465 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1466 out << functionName << "(";
1467 node->getLeft()->traverse(this);
1468 TIntermSequence *seq = rightAgg->getSequence();
1469 for (auto &arrayElement : *seq)
1470 {
1471 out << ", ";
1472 arrayElement->traverse(this);
1473 }
1474 out << ")";
1475 return false;
1476 }
1477 else
1478 {
1479 const TString &functionName = addArrayAssignmentFunction(node->getType());
1480 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
1481 }
Olli Etuahoe79904c2015-03-18 16:56:42 +02001482 }
1483 else
1484 {
1485 outputTriplet(visit, "(", " = ", ")");
1486 }
1487 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001488 case EOpInitialize:
1489 if (visit == PreVisit)
1490 {
1491 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1492 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1493 // new variable is created before the assignment is evaluated), so we need to convert
1494 // this to "float t = x, x = t;".
1495
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001496 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001497 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001498 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001499
Jamie Madill37997142015-01-28 10:06:34 -05001500 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1501 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001502 {
Jamie Madill37997142015-01-28 10:06:34 -05001503 // For variables which are not constant, defer their real initialization until
1504 // after we initialize other globals: uniforms, attributes and varyings.
1505 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1506 const TString &initString = initializer(node->getType());
1507 node->setRight(new TIntermRaw(node->getType(), initString));
1508 }
1509 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1510 {
1511 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001512 return false;
1513 }
1514 }
1515 else if (visit == InVisit)
1516 {
1517 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001518 }
1519 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001520 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1521 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1522 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1523 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1524 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1525 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001526 if (visit == PreVisit)
1527 {
1528 out << "(";
1529 }
1530 else if (visit == InVisit)
1531 {
1532 out << " = mul(";
1533 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001534 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001535 }
1536 else
1537 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001538 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001539 }
1540 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001541 case EOpMatrixTimesMatrixAssign:
1542 if (visit == PreVisit)
1543 {
1544 out << "(";
1545 }
1546 else if (visit == InVisit)
1547 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001548 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001549 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001550 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001551 }
1552 else
1553 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001554 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001555 }
1556 break;
1557 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001558 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001559 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1560 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1561 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1562 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1563 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001564 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001565 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001566 const TType& leftType = node->getLeft()->getType();
1567 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001568 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001569 if (visit == PreVisit)
1570 {
1571 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1572 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001573 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001574 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001575 return false;
1576 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001577 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001578 else
1579 {
1580 outputTriplet(visit, "", "[", "]");
1581 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001582 }
1583 break;
1584 case EOpIndexIndirect:
1585 // We do not currently support indirect references to interface blocks
1586 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1587 outputTriplet(visit, "", "[", "]");
1588 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001589 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001590 if (visit == InVisit)
1591 {
1592 const TStructure* structure = node->getLeft()->getType().getStruct();
1593 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1594 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001595 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001596
1597 return false;
1598 }
1599 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001600 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001601 if (visit == InVisit)
1602 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001603 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1604 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1605 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001606 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001607
1608 return false;
1609 }
1610 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611 case EOpVectorSwizzle:
1612 if (visit == InVisit)
1613 {
1614 out << ".";
1615
1616 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1617
1618 if (swizzle)
1619 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001620 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001621
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001622 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001623 {
1624 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1625
1626 if (element)
1627 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001628 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001629
1630 switch (i)
1631 {
1632 case 0: out << "x"; break;
1633 case 1: out << "y"; break;
1634 case 2: out << "z"; break;
1635 case 3: out << "w"; break;
1636 default: UNREACHABLE();
1637 }
1638 }
1639 else UNREACHABLE();
1640 }
1641 }
1642 else UNREACHABLE();
1643
1644 return false; // Fully processed
1645 }
1646 break;
1647 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1648 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1649 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1650 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001651 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001652 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1653 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1654 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1655 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1656 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001657 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001658 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001659 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001660 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001661 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1662 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1663 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1664 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1665 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001666 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001667 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1668 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001669 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001670 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001671 if (node->getRight()->hasSideEffects())
1672 {
1673 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1674 return false;
1675 }
1676 else
1677 {
1678 outputTriplet(visit, "(", " || ", ")");
1679 return true;
1680 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001681 case EOpLogicalXor:
1682 mUsesXor = true;
1683 outputTriplet(visit, "xor(", ", ", ")");
1684 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001685 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001686 if (node->getRight()->hasSideEffects())
1687 {
1688 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1689 return false;
1690 }
1691 else
1692 {
1693 outputTriplet(visit, "(", " && ", ")");
1694 return true;
1695 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696 default: UNREACHABLE();
1697 }
1698
1699 return true;
1700}
1701
1702bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1703{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 switch (node->getOp())
1705 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001706 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001707 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001708 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1709 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001710 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001711 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1712 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1713 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1714 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001715 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1716 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1717 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1718 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1719 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1720 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1721 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1722 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001723 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1724 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1725 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1726 case EOpAsinh:
1727 ASSERT(node->getUseEmulatedFunction());
1728 writeEmulatedFunctionTriplet(visit, "asinh(");
1729 break;
1730 case EOpAcosh:
1731 ASSERT(node->getUseEmulatedFunction());
1732 writeEmulatedFunctionTriplet(visit, "acosh(");
1733 break;
1734 case EOpAtanh:
1735 ASSERT(node->getUseEmulatedFunction());
1736 writeEmulatedFunctionTriplet(visit, "atanh(");
1737 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001738 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1739 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1740 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1741 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1742 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1743 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1744 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1745 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1746 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001747 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1748 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1749 case EOpRoundEven:
1750 ASSERT(node->getUseEmulatedFunction());
1751 writeEmulatedFunctionTriplet(visit, "roundEven(");
1752 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001753 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1754 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301755 case EOpIsNan:
1756 outputTriplet(visit, "isnan(", "", ")");
1757 mRequiresIEEEStrictCompiling = true;
1758 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301759 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001760 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1761 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1762 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1763 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001764 case EOpPackSnorm2x16:
1765 ASSERT(node->getUseEmulatedFunction());
1766 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1767 break;
1768 case EOpPackUnorm2x16:
1769 ASSERT(node->getUseEmulatedFunction());
1770 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1771 break;
1772 case EOpPackHalf2x16:
1773 ASSERT(node->getUseEmulatedFunction());
1774 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1775 break;
1776 case EOpUnpackSnorm2x16:
1777 ASSERT(node->getUseEmulatedFunction());
1778 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1779 break;
1780 case EOpUnpackUnorm2x16:
1781 ASSERT(node->getUseEmulatedFunction());
1782 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1783 break;
1784 case EOpUnpackHalf2x16:
1785 ASSERT(node->getUseEmulatedFunction());
1786 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1787 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001788 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1789 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001790 case EOpDFdx:
1791 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1792 {
1793 outputTriplet(visit, "(", "", ", 0.0)");
1794 }
1795 else
1796 {
1797 outputTriplet(visit, "ddx(", "", ")");
1798 }
1799 break;
1800 case EOpDFdy:
1801 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1802 {
1803 outputTriplet(visit, "(", "", ", 0.0)");
1804 }
1805 else
1806 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001807 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001808 }
1809 break;
1810 case EOpFwidth:
1811 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1812 {
1813 outputTriplet(visit, "(", "", ", 0.0)");
1814 }
1815 else
1816 {
1817 outputTriplet(visit, "fwidth(", "", ")");
1818 }
1819 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001820 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1821 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001822 case EOpInverse:
1823 ASSERT(node->getUseEmulatedFunction());
1824 writeEmulatedFunctionTriplet(visit, "inverse(");
1825 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001826
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001827 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1828 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829 default: UNREACHABLE();
1830 }
1831
1832 return true;
1833}
1834
1835bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1836{
Jamie Madill32aab012015-01-27 14:12:26 -05001837 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839 switch (node->getOp())
1840 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001841 case EOpSequence:
1842 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001843 if (mInsideFunction)
1844 {
Jamie Madill075edd82013-07-08 13:30:19 -04001845 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001846 out << "{\n";
1847 }
1848
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001849 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001850 {
Jamie Madill075edd82013-07-08 13:30:19 -04001851 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001852
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001853 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001854
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001855 // Don't output ; after case labels, they're terminated by :
1856 // This is needed especially since outputting a ; after a case statement would turn empty
1857 // case statements into non-empty case statements, disallowing fall-through from them.
1858 if ((*sit)->getAsCaseNode() == nullptr)
1859 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001860 }
1861
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001862 if (mInsideFunction)
1863 {
Jamie Madill075edd82013-07-08 13:30:19 -04001864 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001865 out << "}\n";
1866 }
1867
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001868 return false;
1869 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 case EOpDeclaration:
1871 if (visit == PreVisit)
1872 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001873 TIntermSequence *sequence = node->getSequence();
1874 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001876 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001878 TStructure *structure = variable->getType().getStruct();
1879
1880 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001881 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001882 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001883 }
1884
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001885 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001886 {
Jamie Madill37997142015-01-28 10:06:34 -05001887 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888 {
Jamie Madill37997142015-01-28 10:06:34 -05001889 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001890 {
Jamie Madill37997142015-01-28 10:06:34 -05001891 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001892 }
1893
Nicolas Capensd974db42014-10-07 10:50:19 -04001894 if (!mInsideFunction)
1895 {
1896 out << "static ";
1897 }
1898
1899 out << TypeString(variable->getType()) + " ";
1900
Jamie Madill37997142015-01-28 10:06:34 -05001901 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001902
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001903 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001905 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001906 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001907 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001908 }
1909 else
1910 {
Jamie Madill37997142015-01-28 10:06:34 -05001911 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001912 }
1913
Jamie Madill37997142015-01-28 10:06:34 -05001914 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001915 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001916 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917 }
1918 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001920 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1921 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001922 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001923 }
1924 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925 }
Jamie Madill033dae62014-06-18 12:56:28 -04001926 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001927 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001928 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001929 {
1930 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1931
1932 if (symbol)
1933 {
1934 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1935 mReferencedVaryings[symbol->getSymbol()] = symbol;
1936 }
1937 else
1938 {
1939 (*sit)->traverse(this);
1940 }
1941 }
1942 }
1943
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944 return false;
1945 }
1946 else if (visit == InVisit)
1947 {
1948 out << ", ";
1949 }
1950 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001951 case EOpInvariantDeclaration:
1952 // Do not do any translation
1953 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001954 case EOpPrototype:
1955 if (visit == PreVisit)
1956 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001957 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001958
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001959 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001960
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001961 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001962 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001963 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001964
1965 if (symbol)
1966 {
1967 out << argumentString(symbol);
1968
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001969 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001970 {
1971 out << ", ";
1972 }
1973 }
1974 else UNREACHABLE();
1975 }
1976
1977 out << ");\n";
1978
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001979 // Also prototype the Lod0 variant if needed
1980 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1981 {
1982 mOutputLod0Function = true;
1983 node->traverse(this);
1984 mOutputLod0Function = false;
1985 }
1986
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001987 return false;
1988 }
1989 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001990 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991 case EOpFunction:
1992 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001993 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994
Jamie Madill033dae62014-06-18 12:56:28 -04001995 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001996
1997 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001999 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002001 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002002 {
Jamie Madill033dae62014-06-18 12:56:28 -04002003 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002004 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002005
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002006 TIntermSequence *sequence = node->getSequence();
2007 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002008
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002009 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002010 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002011 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002012
2013 if (symbol)
2014 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002015 TStructure *structure = symbol->getType().getStruct();
2016
2017 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002018 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002019 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002020 }
2021
2022 out << argumentString(symbol);
2023
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002024 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002025 {
2026 out << ", ";
2027 }
2028 }
2029 else UNREACHABLE();
2030 }
2031
2032 out << ")\n"
2033 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002034
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002035 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002036 {
2037 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002038 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002039 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002041
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002042 out << "}\n";
2043
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002044 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2045 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002046 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002047 {
2048 mOutputLod0Function = true;
2049 node->traverse(this);
2050 mOutputLod0Function = false;
2051 }
2052 }
2053
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002054 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055 }
2056 break;
2057 case EOpFunctionCall:
2058 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002059 TString name = TFunction::unmangleName(node->getName());
2060 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002061 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002062
2063 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064 {
Jamie Madill033dae62014-06-18 12:56:28 -04002065 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066 }
2067 else
2068 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002069 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002070
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002071 TextureFunction textureFunction;
2072 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002073 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002074 textureFunction.method = TextureFunction::IMPLICIT;
2075 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002076 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002077
2078 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002079 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002080 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002081 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002082 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002083 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002084 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002085 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002086 }
Nicolas Capens46485082014-04-15 13:12:50 -04002087 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2088 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002089 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002090 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002091 }
Nicolas Capens46485082014-04-15 13:12:50 -04002092 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002093 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002094 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002095 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002096 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002097 else if (name == "textureSize")
2098 {
2099 textureFunction.method = TextureFunction::SIZE;
2100 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002101 else if (name == "textureOffset")
2102 {
2103 textureFunction.method = TextureFunction::IMPLICIT;
2104 textureFunction.offset = true;
2105 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002106 else if (name == "textureProjOffset")
2107 {
2108 textureFunction.method = TextureFunction::IMPLICIT;
2109 textureFunction.offset = true;
2110 textureFunction.proj = true;
2111 }
2112 else if (name == "textureLodOffset")
2113 {
2114 textureFunction.method = TextureFunction::LOD;
2115 textureFunction.offset = true;
2116 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002117 else if (name == "textureProjLodOffset")
2118 {
2119 textureFunction.method = TextureFunction::LOD;
2120 textureFunction.proj = true;
2121 textureFunction.offset = true;
2122 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002123 else if (name == "texelFetch")
2124 {
2125 textureFunction.method = TextureFunction::FETCH;
2126 }
2127 else if (name == "texelFetchOffset")
2128 {
2129 textureFunction.method = TextureFunction::FETCH;
2130 textureFunction.offset = true;
2131 }
Nicolas Capens46485082014-04-15 13:12:50 -04002132 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002133 {
2134 textureFunction.method = TextureFunction::GRAD;
2135 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002136 else if (name == "textureGradOffset")
2137 {
2138 textureFunction.method = TextureFunction::GRAD;
2139 textureFunction.offset = true;
2140 }
Nicolas Capens46485082014-04-15 13:12:50 -04002141 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002142 {
2143 textureFunction.method = TextureFunction::GRAD;
2144 textureFunction.proj = true;
2145 }
2146 else if (name == "textureProjGradOffset")
2147 {
2148 textureFunction.method = TextureFunction::GRAD;
2149 textureFunction.proj = true;
2150 textureFunction.offset = true;
2151 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002152 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002153
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002154 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002155 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002156 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2157
2158 if (textureFunction.offset)
2159 {
2160 mandatoryArgumentCount++;
2161 }
2162
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002163 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002164
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002165 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002166 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002167 if (bias)
2168 {
2169 textureFunction.method = TextureFunction::LOD0BIAS;
2170 }
2171 else
2172 {
2173 textureFunction.method = TextureFunction::LOD0;
2174 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002175 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002176 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002177 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002178 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002179 }
2180 }
2181
2182 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002183
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002184 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002186
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002187 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002188 {
2189 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2190 {
2191 out << "texture_";
2192 (*arg)->traverse(this);
2193 out << ", sampler_";
2194 }
2195
2196 (*arg)->traverse(this);
2197
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002198 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002199 {
2200 out << ", ";
2201 }
2202 }
2203
2204 out << ")";
2205
2206 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002207 }
2208 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002209 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002210 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2211 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2212 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2213 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2214 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2215 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2216 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2217 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2218 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2219 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2220 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2221 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2222 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2223 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2224 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2225 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2226 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2227 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2228 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002229 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002230 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002231 if (node->getType().isArray())
2232 {
2233 UNIMPLEMENTED();
2234 }
Jamie Madill033dae62014-06-18 12:56:28 -04002235 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002236 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002237 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002238 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002239 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002240 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2241 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2242 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2243 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2244 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2245 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002246 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002247 ASSERT(node->getUseEmulatedFunction());
2248 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002249 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002250 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002251 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002253 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002254 ASSERT(node->getUseEmulatedFunction());
2255 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002256 break;
2257 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2258 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2259 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2260 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2261 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2262 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2263 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2264 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2265 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002266 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002267 ASSERT(node->getUseEmulatedFunction());
2268 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002269 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002270 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2271 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002272 case EOpOuterProduct:
2273 ASSERT(node->getUseEmulatedFunction());
2274 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2275 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002277 default: UNREACHABLE();
2278 }
2279
2280 return true;
2281}
2282
2283bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2284{
Jamie Madill32aab012015-01-27 14:12:26 -05002285 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002287 if (node->usesTernaryOperator())
2288 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002289 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002290 }
2291 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002293 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002294
Corentin Wallez80bacde2014-11-10 12:07:37 -08002295 // D3D errors when there is a gradient operation in a loop in an unflattened if
2296 // however flattening all the ifs in branch heavy shaders made D3D error too.
2297 // As a temporary workaround we flatten the ifs only if there is at least a loop
2298 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002299 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002300 {
2301 out << "FLATTEN ";
2302 }
2303
2304 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002305
2306 node->getCondition()->traverse(this);
2307
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002308 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002309
Jamie Madill075edd82013-07-08 13:30:19 -04002310 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002311 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002313 bool discard = false;
2314
daniel@transgaming.combb885322010-04-15 20:45:24 +00002315 if (node->getTrueBlock())
2316 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002317 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002318
2319 // Detect true discard
2320 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002321 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322
Jamie Madill075edd82013-07-08 13:30:19 -04002323 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002324 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002325
2326 if (node->getFalseBlock())
2327 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002328 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002329
Jamie Madill075edd82013-07-08 13:30:19 -04002330 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002331 out << "{\n";
2332
Jamie Madill075edd82013-07-08 13:30:19 -04002333 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002334 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002335
Jamie Madill075edd82013-07-08 13:30:19 -04002336 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002337 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002338
2339 // Detect false discard
2340 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2341 }
2342
2343 // ANGLE issue 486: Detect problematic conditional discard
2344 if (discard && FindSideEffectRewriting::search(node))
2345 {
2346 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002347 }
2348 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349
2350 return false;
2351}
2352
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002353bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002354{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002355 if (node->getStatementList())
2356 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002357 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002358 outputTriplet(visit, "switch (", ") ", "");
2359 // The curly braces get written when visiting the statementList aggregate
2360 }
2361 else
2362 {
2363 // No statementList, so it won't output curly braces
2364 outputTriplet(visit, "switch (", ") {", "}\n");
2365 }
2366 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002367}
2368
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002369bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002370{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002371 if (node->hasCondition())
2372 {
2373 outputTriplet(visit, "case (", "", "):\n");
2374 return true;
2375 }
2376 else
2377 {
2378 TInfoSinkBase &out = getInfoSink();
2379 out << "default:\n";
2380 return false;
2381 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002382}
2383
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002384void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2385{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002386 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387}
2388
2389bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2390{
Nicolas Capens655fe362014-04-11 13:12:34 -04002391 mNestedLoopDepth++;
2392
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002393 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2394
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002395 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002396 {
2397 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2398 }
2399
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002400 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002401 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002402 if (handleExcessiveLoop(node))
2403 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002404 mInsideDiscontinuousLoop = wasDiscontinuous;
2405 mNestedLoopDepth--;
2406
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002407 return false;
2408 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002409 }
2410
Jamie Madill32aab012015-01-27 14:12:26 -05002411 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002412
alokp@chromium.org52813552010-11-16 18:36:09 +00002413 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002415 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002416
Jamie Madill075edd82013-07-08 13:30:19 -04002417 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002418 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419 }
2420 else
2421 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002422 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002423
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424 if (node->getInit())
2425 {
2426 node->getInit()->traverse(this);
2427 }
2428
2429 out << "; ";
2430
alokp@chromium.org52813552010-11-16 18:36:09 +00002431 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002433 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 }
2435
2436 out << "; ";
2437
alokp@chromium.org52813552010-11-16 18:36:09 +00002438 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002440 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 }
2442
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002443 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002444
Jamie Madill075edd82013-07-08 13:30:19 -04002445 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002446 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447 }
2448
2449 if (node->getBody())
2450 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002451 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 }
2453
Jamie Madill075edd82013-07-08 13:30:19 -04002454 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002455 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456
alokp@chromium.org52813552010-11-16 18:36:09 +00002457 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458 {
Jamie Madill075edd82013-07-08 13:30:19 -04002459 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460 out << "while(\n";
2461
alokp@chromium.org52813552010-11-16 18:36:09 +00002462 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463
daniel@transgaming.com73536982012-03-21 20:45:49 +00002464 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 }
2466
daniel@transgaming.com73536982012-03-21 20:45:49 +00002467 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002469 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002470 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002471
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002472 return false;
2473}
2474
2475bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2476{
Jamie Madill32aab012015-01-27 14:12:26 -05002477 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478
2479 switch (node->getFlowOp())
2480 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002481 case EOpKill:
2482 outputTriplet(visit, "discard;\n", "", "");
2483 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002484 case EOpBreak:
2485 if (visit == PreVisit)
2486 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002487 if (mNestedLoopDepth > 1)
2488 {
2489 mUsesNestedBreak = true;
2490 }
2491
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002492 if (mExcessiveLoopIndex)
2493 {
2494 out << "{Break";
2495 mExcessiveLoopIndex->traverse(this);
2496 out << " = true; break;}\n";
2497 }
2498 else
2499 {
2500 out << "break;\n";
2501 }
2502 }
2503 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002504 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 case EOpReturn:
2506 if (visit == PreVisit)
2507 {
2508 if (node->getExpression())
2509 {
2510 out << "return ";
2511 }
2512 else
2513 {
2514 out << "return;\n";
2515 }
2516 }
2517 else if (visit == PostVisit)
2518 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002519 if (node->getExpression())
2520 {
2521 out << ";\n";
2522 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523 }
2524 break;
2525 default: UNREACHABLE();
2526 }
2527
2528 return true;
2529}
2530
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002531void OutputHLSL::traverseStatements(TIntermNode *node)
2532{
2533 if (isSingleStatement(node))
2534 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002535 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002536 }
2537
2538 node->traverse(this);
2539}
2540
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002541bool OutputHLSL::isSingleStatement(TIntermNode *node)
2542{
2543 TIntermAggregate *aggregate = node->getAsAggregate();
2544
2545 if (aggregate)
2546 {
2547 if (aggregate->getOp() == EOpSequence)
2548 {
2549 return false;
2550 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002551 else if (aggregate->getOp() == EOpDeclaration)
2552 {
2553 // Declaring multiple comma-separated variables must be considered multiple statements
2554 // because each individual declaration has side effects which are visible in the next.
2555 return false;
2556 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002557 else
2558 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002559 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002560 {
2561 if (!isSingleStatement(*sit))
2562 {
2563 return false;
2564 }
2565 }
2566
2567 return true;
2568 }
2569 }
2570
2571 return true;
2572}
2573
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002574// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2575// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002576bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2577{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002578 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002579 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002580
2581 // Parse loops of the form:
2582 // for(int index = initial; index [comparator] limit; index += increment)
2583 TIntermSymbol *index = NULL;
2584 TOperator comparator = EOpNull;
2585 int initial = 0;
2586 int limit = 0;
2587 int increment = 0;
2588
2589 // Parse index name and intial value
2590 if (node->getInit())
2591 {
2592 TIntermAggregate *init = node->getInit()->getAsAggregate();
2593
2594 if (init)
2595 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002596 TIntermSequence *sequence = init->getSequence();
2597 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002598
2599 if (variable && variable->getQualifier() == EvqTemporary)
2600 {
2601 TIntermBinary *assign = variable->getAsBinaryNode();
2602
2603 if (assign->getOp() == EOpInitialize)
2604 {
2605 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2606 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2607
2608 if (symbol && constant)
2609 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002610 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002611 {
2612 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002613 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002614 }
2615 }
2616 }
2617 }
2618 }
2619 }
2620
2621 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002622 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002623 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002624 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002625
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002626 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2627 {
2628 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2629
2630 if (constant)
2631 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002632 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002633 {
2634 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002635 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002636 }
2637 }
2638 }
2639 }
2640
2641 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002642 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002643 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002644 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2645 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002646
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002647 if (binaryTerminal)
2648 {
2649 TOperator op = binaryTerminal->getOp();
2650 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2651
2652 if (constant)
2653 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002654 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002655 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002656 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002657
2658 switch (op)
2659 {
2660 case EOpAddAssign: increment = value; break;
2661 case EOpSubAssign: increment = -value; break;
2662 default: UNIMPLEMENTED();
2663 }
2664 }
2665 }
2666 }
2667 else if (unaryTerminal)
2668 {
2669 TOperator op = unaryTerminal->getOp();
2670
2671 switch (op)
2672 {
2673 case EOpPostIncrement: increment = 1; break;
2674 case EOpPostDecrement: increment = -1; break;
2675 case EOpPreIncrement: increment = 1; break;
2676 case EOpPreDecrement: increment = -1; break;
2677 default: UNIMPLEMENTED();
2678 }
2679 }
2680 }
2681
2682 if (index != NULL && comparator != EOpNull && increment != 0)
2683 {
2684 if (comparator == EOpLessThanEqual)
2685 {
2686 comparator = EOpLessThan;
2687 limit += 1;
2688 }
2689
2690 if (comparator == EOpLessThan)
2691 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002692 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002693
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002694 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002695 {
2696 return false; // Not an excessive loop
2697 }
2698
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002699 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2700 mExcessiveLoopIndex = index;
2701
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002702 out << "{int ";
2703 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002704 out << ";\n"
2705 "bool Break";
2706 index->traverse(this);
2707 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002708
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002709 bool firstLoopFragment = true;
2710
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002711 while (iterations > 0)
2712 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002713 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002714
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002715 if (!firstLoopFragment)
2716 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002717 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002718 index->traverse(this);
2719 out << ") {\n";
2720 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002721
2722 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2723 {
2724 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2725 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002726
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002727 // for(int index = initial; index < clampedLimit; index += increment)
2728
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002729 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002730 index->traverse(this);
2731 out << " = ";
2732 out << initial;
2733
2734 out << "; ";
2735 index->traverse(this);
2736 out << " < ";
2737 out << clampedLimit;
2738
2739 out << "; ";
2740 index->traverse(this);
2741 out << " += ";
2742 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002743 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002744
Jamie Madill075edd82013-07-08 13:30:19 -04002745 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002746 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002747
2748 if (node->getBody())
2749 {
2750 node->getBody()->traverse(this);
2751 }
2752
Jamie Madill075edd82013-07-08 13:30:19 -04002753 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002754 out << ";}\n";
2755
2756 if (!firstLoopFragment)
2757 {
2758 out << "}\n";
2759 }
2760
2761 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002762
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002763 initial += MAX_LOOP_ITERATIONS * increment;
2764 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002765 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002766
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002767 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002768
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002769 mExcessiveLoopIndex = restoreIndex;
2770
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002771 return true;
2772 }
2773 else UNIMPLEMENTED();
2774 }
2775
2776 return false; // Not handled as an excessive loop
2777}
2778
Olli Etuaho7fb49552015-03-18 17:27:44 +02002779void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002780{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002781 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002782 {
2783 out << preString;
2784 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002785 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786 {
2787 out << inString;
2788 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002789 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002790 {
2791 out << postString;
2792 }
2793}
2794
Olli Etuaho7fb49552015-03-18 17:27:44 +02002795void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2796{
2797 outputTriplet(visit, preString, inString, postString, getInfoSink());
2798}
2799
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002800void OutputHLSL::outputLineDirective(int line)
2801{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002802 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002803 {
Jamie Madill32aab012015-01-27 14:12:26 -05002804 TInfoSinkBase &out = getInfoSink();
2805
2806 out << "\n";
2807 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002808
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002809 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002810 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002811 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002812 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002813
Jamie Madill32aab012015-01-27 14:12:26 -05002814 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002815 }
2816}
2817
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002818TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2819{
2820 TQualifier qualifier = symbol->getQualifier();
2821 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002822 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002823
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002824 if (name.empty()) // HLSL demands named arguments, also for prototypes
2825 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002826 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002827 }
2828 else
2829 {
Jamie Madill033dae62014-06-18 12:56:28 -04002830 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002831 }
2832
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002833 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2834 {
Jamie Madill033dae62014-06-18 12:56:28 -04002835 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002836 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002837 }
2838
Jamie Madill033dae62014-06-18 12:56:28 -04002839 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002840}
2841
2842TString OutputHLSL::initializer(const TType &type)
2843{
2844 TString string;
2845
Jamie Madill94bf7f22013-07-08 13:31:15 -04002846 size_t size = type.getObjectSize();
2847 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002848 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002849 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002850
Jamie Madill94bf7f22013-07-08 13:31:15 -04002851 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002852 {
2853 string += ", ";
2854 }
2855 }
2856
daniel@transgaming.comead23042010-04-29 03:35:36 +00002857 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002858}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002859
Daniel Bratell29190082015-02-20 16:42:54 +01002860void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002861{
Olli Etuahof40319e2015-03-10 14:33:00 +02002862 if (type.isArray())
2863 {
2864 UNIMPLEMENTED();
2865 }
Jamie Madill32aab012015-01-27 14:12:26 -05002866 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002867
2868 if (visit == PreVisit)
2869 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002870 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002871
Daniel Bratell29190082015-02-20 16:42:54 +01002872 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002873 }
2874 else if (visit == InVisit)
2875 {
2876 out << ", ";
2877 }
2878 else if (visit == PostVisit)
2879 {
2880 out << ")";
2881 }
2882}
2883
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002884const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2885{
Jamie Madill32aab012015-01-27 14:12:26 -05002886 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002887
Jamie Madill98493dd2013-07-08 14:39:03 -04002888 const TStructure* structure = type.getStruct();
2889 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002890 {
Jamie Madill033dae62014-06-18 12:56:28 -04002891 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002892
Jamie Madill98493dd2013-07-08 14:39:03 -04002893 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002894
Jamie Madill98493dd2013-07-08 14:39:03 -04002895 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002896 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002897 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002898 constUnion = writeConstantUnion(*fieldType, constUnion);
2899
Jamie Madill98493dd2013-07-08 14:39:03 -04002900 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002901 {
2902 out << ", ";
2903 }
2904 }
2905
2906 out << ")";
2907 }
2908 else
2909 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002910 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002911 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002912
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002913 if (writeType)
2914 {
Jamie Madill033dae62014-06-18 12:56:28 -04002915 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002916 }
2917
Jamie Madill94bf7f22013-07-08 13:31:15 -04002918 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002919 {
2920 switch (constUnion->getType())
2921 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002922 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002923 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002924 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002925 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002926 default: UNREACHABLE();
2927 }
2928
2929 if (i != size - 1)
2930 {
2931 out << ", ";
2932 }
2933 }
2934
2935 if (writeType)
2936 {
2937 out << ")";
2938 }
2939 }
2940
2941 return constUnion;
2942}
2943
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002944void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2945{
2946 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2947 outputTriplet(visit, preString.c_str(), ", ", ")");
2948}
2949
Jamie Madill37997142015-01-28 10:06:34 -05002950bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2951{
2952 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2953 expression->traverse(&searchSymbol);
2954
2955 if (searchSymbol.foundMatch())
2956 {
2957 // Type already printed
2958 out << "t" + str(mUniqueIndex) + " = ";
2959 expression->traverse(this);
2960 out << ", ";
2961 symbolNode->traverse(this);
2962 out << " = t" + str(mUniqueIndex);
2963
2964 mUniqueIndex++;
2965 return true;
2966 }
2967
2968 return false;
2969}
2970
2971void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2972{
2973 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2974 << "\n"
2975 << "void initializeDeferredGlobals()\n"
2976 << "{\n";
2977
2978 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2979 {
2980 TIntermSymbol *symbol = deferredGlobal.first;
2981 TIntermTyped *expression = deferredGlobal.second;
2982 ASSERT(symbol);
2983 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2984
2985 out << " " << Decorate(symbol->getSymbol()) << " = ";
2986
2987 if (!writeSameSymbolInitializer(out, symbol, expression))
2988 {
2989 ASSERT(mInfoSinkStack.top() == &out);
2990 expression->traverse(this);
2991 }
2992
2993 out << ";\n";
2994 }
2995
2996 out << "}\n"
2997 << "\n";
2998}
2999
Jamie Madill55e79e02015-02-09 15:35:00 -05003000TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3001{
3002 const TFieldList &fields = structure.fields();
3003
3004 for (const auto &eqFunction : mStructEqualityFunctions)
3005 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003006 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003007 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003008 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003009 }
3010 }
3011
3012 const TString &structNameString = StructNameString(structure);
3013
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003014 StructEqualityFunction *function = new StructEqualityFunction();
3015 function->structure = &structure;
3016 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003017
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003018 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003019
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003020 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3021 << "{\n"
3022 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003023
3024 for (size_t i = 0; i < fields.size(); i++)
3025 {
3026 const TField *field = fields[i];
3027 const TType *fieldType = field->type();
3028
3029 const TString &fieldNameA = "a." + Decorate(field->name());
3030 const TString &fieldNameB = "b." + Decorate(field->name());
3031
3032 if (i > 0)
3033 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003034 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003035 }
3036
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003037 fnOut << "(";
3038 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3039 fnOut << fieldNameA;
3040 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3041 fnOut << fieldNameB;
3042 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3043 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003044 }
3045
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003046 fnOut << ";\n" << "}\n";
3047
3048 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003049
3050 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003051 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003052
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003053 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003054}
3055
Olli Etuaho7fb49552015-03-18 17:27:44 +02003056TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3057{
3058 for (const auto &eqFunction : mArrayEqualityFunctions)
3059 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003060 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003061 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003062 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003063 }
3064 }
3065
3066 const TString &typeName = TypeString(type);
3067
Olli Etuaho12690762015-03-31 12:55:28 +03003068 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003069 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003070
3071 TInfoSinkBase fnNameOut;
3072 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003073 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003074
3075 TType nonArrayType = type;
3076 nonArrayType.clearArrayness();
3077
3078 TInfoSinkBase fnOut;
3079
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003080 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003081 << typeName << " a[" << type.getArraySize() << "], "
3082 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003083 << "{\n"
3084 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3085 " {\n"
3086 " if (";
3087
3088 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3089 fnOut << "a[i]";
3090 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3091 fnOut << "b[i]";
3092 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3093
3094 fnOut << ") { return false; }\n"
3095 " }\n"
3096 " return true;\n"
3097 "}\n";
3098
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003099 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003100
3101 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003102 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003103
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003104 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003105}
3106
Olli Etuaho12690762015-03-31 12:55:28 +03003107TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3108{
3109 for (const auto &assignFunction : mArrayAssignmentFunctions)
3110 {
3111 if (assignFunction.type == type)
3112 {
3113 return assignFunction.functionName;
3114 }
3115 }
3116
3117 const TString &typeName = TypeString(type);
3118
3119 ArrayHelperFunction function;
3120 function.type = type;
3121
3122 TInfoSinkBase fnNameOut;
3123 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3124 function.functionName = fnNameOut.c_str();
3125
3126 TInfoSinkBase fnOut;
3127
3128 fnOut << "void " << function.functionName << "(out "
3129 << typeName << " a[" << type.getArraySize() << "], "
3130 << typeName << " b[" << type.getArraySize() << "])\n"
3131 << "{\n"
3132 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3133 " {\n"
3134 " a[i] = b[i];\n"
3135 " }\n"
3136 "}\n";
3137
3138 function.functionDefinition = fnOut.c_str();
3139
3140 mArrayAssignmentFunctions.push_back(function);
3141
3142 return function.functionName;
3143}
3144
Olli Etuaho9638c352015-04-01 14:34:52 +03003145TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3146{
3147 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3148 {
3149 if (constructIntoFunction.type == type)
3150 {
3151 return constructIntoFunction.functionName;
3152 }
3153 }
3154
3155 const TString &typeName = TypeString(type);
3156
3157 ArrayHelperFunction function;
3158 function.type = type;
3159
3160 TInfoSinkBase fnNameOut;
3161 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3162 function.functionName = fnNameOut.c_str();
3163
3164 TInfoSinkBase fnOut;
3165
3166 fnOut << "void " << function.functionName << "(out "
3167 << typeName << " a[" << type.getArraySize() << "]";
3168 for (int i = 0; i < type.getArraySize(); ++i)
3169 {
3170 fnOut << ", " << typeName << " b" << i;
3171 }
3172 fnOut << ")\n"
3173 "{\n";
3174
3175 for (int i = 0; i < type.getArraySize(); ++i)
3176 {
3177 fnOut << " a[" << i << "] = b" << i << ";\n";
3178 }
3179 fnOut << "}\n";
3180
3181 function.functionDefinition = fnOut.c_str();
3182
3183 mArrayConstructIntoFunctions.push_back(function);
3184
3185 return function.functionName;
3186}
3187
3188
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003189}