blob: a7004a686564e5535171dd82077d3257c6168fab [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 }
348
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500349 if (mUsesDiscardRewriting)
350 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400351 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500352 }
353
Nicolas Capens655fe362014-04-11 13:12:34 -0400354 if (mUsesNestedBreak)
355 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400356 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400357 }
358
Arun Patole44efa0b2015-03-04 17:11:05 +0530359 if (mRequiresIEEEStrictCompiling)
360 {
361 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
362 }
363
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400364 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
365 "#define LOOP [loop]\n"
366 "#define FLATTEN [flatten]\n"
367 "#else\n"
368 "#define LOOP\n"
369 "#define FLATTEN\n"
370 "#endif\n";
371
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200372 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200374 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
375 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000376
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000377 out << "// Varyings\n";
378 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400379 out << "\n";
380
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200381 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000382 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500383 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000384 {
Jamie Madill46131a32013-06-20 11:55:50 -0400385 const TString &variableName = outputVariableIt->first;
386 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400387
Jamie Madill033dae62014-06-18 12:56:28 -0400388 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400389 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000390 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000391 }
Jamie Madill46131a32013-06-20 11:55:50 -0400392 else
393 {
394 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
395
396 out << "static float4 gl_Color[" << numColorValues << "] =\n"
397 "{\n";
398 for (unsigned int i = 0; i < numColorValues; i++)
399 {
400 out << " float4(0, 0, 0, 0)";
401 if (i + 1 != numColorValues)
402 {
403 out << ",";
404 }
405 out << "\n";
406 }
407
408 out << "};\n";
409 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000410
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400411 if (mUsesFragDepth)
412 {
413 out << "static float gl_Depth = 0.0;\n";
414 }
415
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000416 if (mUsesFragCoord)
417 {
418 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
419 }
420
421 if (mUsesPointCoord)
422 {
423 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
424 }
425
426 if (mUsesFrontFacing)
427 {
428 out << "static bool gl_FrontFacing = false;\n";
429 }
430
431 out << "\n";
432
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000433 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000434 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000435 out << "struct gl_DepthRangeParameters\n"
436 "{\n"
437 " float near;\n"
438 " float far;\n"
439 " float diff;\n"
440 "};\n"
441 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000442 }
443
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000444 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000445 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000446 out << "cbuffer DriverConstants : register(b1)\n"
447 "{\n";
448
449 if (mUsesDepthRange)
450 {
451 out << " float3 dx_DepthRange : packoffset(c0);\n";
452 }
453
454 if (mUsesFragCoord)
455 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000456 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000457 }
458
459 if (mUsesFragCoord || mUsesFrontFacing)
460 {
461 out << " float3 dx_DepthFront : packoffset(c2);\n";
462 }
463
464 out << "};\n";
465 }
466 else
467 {
468 if (mUsesDepthRange)
469 {
470 out << "uniform float3 dx_DepthRange : register(c0);";
471 }
472
473 if (mUsesFragCoord)
474 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000475 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000476 }
477
478 if (mUsesFragCoord || mUsesFrontFacing)
479 {
480 out << "uniform float3 dx_DepthFront : register(c2);\n";
481 }
482 }
483
484 out << "\n";
485
486 if (mUsesDepthRange)
487 {
488 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
489 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000490 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000491
Jamie Madillf91ce812014-06-13 10:04:34 -0400492 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000493 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400494 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000495 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400496 out << flaggedStructs;
497 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000498 }
499
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000500 if (usingMRTExtension && mNumRenderTargets > 1)
501 {
502 out << "#define GL_USES_MRT\n";
503 }
504
505 if (mUsesFragColor)
506 {
507 out << "#define GL_USES_FRAG_COLOR\n";
508 }
509
510 if (mUsesFragData)
511 {
512 out << "#define GL_USES_FRAG_DATA\n";
513 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000515 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000517 out << "// Attributes\n";
518 out << attributes;
519 out << "\n"
520 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400521
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000522 if (mUsesPointSize)
523 {
524 out << "static float gl_PointSize = float(1);\n";
525 }
526
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000527 if (mUsesInstanceID)
528 {
529 out << "static int gl_InstanceID;";
530 }
531
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000532 out << "\n"
533 "// Varyings\n";
534 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000535 out << "\n";
536
537 if (mUsesDepthRange)
538 {
539 out << "struct gl_DepthRangeParameters\n"
540 "{\n"
541 " float near;\n"
542 " float far;\n"
543 " float diff;\n"
544 "};\n"
545 "\n";
546 }
547
548 if (mOutputType == SH_HLSL11_OUTPUT)
549 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800550 out << "cbuffer DriverConstants : register(b1)\n"
551 "{\n";
552
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000553 if (mUsesDepthRange)
554 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800555 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000556 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800557
Cooper Partine6664f02015-01-09 16:22:24 -0800558 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800559 // However, we declare it for all shaders (including Feature Level 10+).
560 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
561 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800562 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800563
564 out << "};\n"
565 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000566 }
567 else
568 {
569 if (mUsesDepthRange)
570 {
571 out << "uniform float3 dx_DepthRange : register(c0);\n";
572 }
573
Cooper Partine6664f02015-01-09 16:22:24 -0800574 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
575 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000576 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000577 }
578
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000579 if (mUsesDepthRange)
580 {
581 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
582 "\n";
583 }
584
Jamie Madillf91ce812014-06-13 10:04:34 -0400585 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000586 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400587 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000588 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400589 out << flaggedStructs;
590 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000591 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400592 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000593
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400594 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
595 {
596 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400597 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000598 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400599 switch(textureFunction->sampler)
600 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400601 case EbtSampler2D: out << "int2 "; break;
602 case EbtSampler3D: out << "int3 "; break;
603 case EbtSamplerCube: out << "int2 "; break;
604 case EbtSampler2DArray: out << "int3 "; break;
605 case EbtISampler2D: out << "int2 "; break;
606 case EbtISampler3D: out << "int3 "; break;
607 case EbtISamplerCube: out << "int2 "; break;
608 case EbtISampler2DArray: out << "int3 "; break;
609 case EbtUSampler2D: out << "int2 "; break;
610 case EbtUSampler3D: out << "int3 "; break;
611 case EbtUSamplerCube: out << "int2 "; break;
612 case EbtUSampler2DArray: out << "int3 "; break;
613 case EbtSampler2DShadow: out << "int2 "; break;
614 case EbtSamplerCubeShadow: out << "int2 "; break;
615 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400616 default: UNREACHABLE();
617 }
618 }
619 else // Sampling function
620 {
621 switch(textureFunction->sampler)
622 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400623 case EbtSampler2D: out << "float4 "; break;
624 case EbtSampler3D: out << "float4 "; break;
625 case EbtSamplerCube: out << "float4 "; break;
626 case EbtSampler2DArray: out << "float4 "; break;
627 case EbtISampler2D: out << "int4 "; break;
628 case EbtISampler3D: out << "int4 "; break;
629 case EbtISamplerCube: out << "int4 "; break;
630 case EbtISampler2DArray: out << "int4 "; break;
631 case EbtUSampler2D: out << "uint4 "; break;
632 case EbtUSampler3D: out << "uint4 "; break;
633 case EbtUSamplerCube: out << "uint4 "; break;
634 case EbtUSampler2DArray: out << "uint4 "; break;
635 case EbtSampler2DShadow: out << "float "; break;
636 case EbtSamplerCubeShadow: out << "float "; break;
637 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400638 default: UNREACHABLE();
639 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000640 }
641
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400642 // Function name
643 out << textureFunction->name();
644
645 // Argument list
646 int hlslCoords = 4;
647
648 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000649 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400650 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000651 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400652 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
653 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
654 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000655 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400656
Nicolas Capens75fb4752013-07-10 15:14:47 -0400657 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000658 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400659 case TextureFunction::IMPLICIT: break;
660 case TextureFunction::BIAS: hlslCoords = 4; break;
661 case TextureFunction::LOD: hlslCoords = 4; break;
662 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400663 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400664 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000665 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400666 }
667 else if (mOutputType == SH_HLSL11_OUTPUT)
668 {
669 switch(textureFunction->sampler)
670 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400671 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
672 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
673 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
674 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
675 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
676 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500677 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400678 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
679 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
680 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500681 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400682 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
683 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
684 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
685 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400686 default: UNREACHABLE();
687 }
688 }
689 else UNREACHABLE();
690
Nicolas Capensfc014542014-02-18 14:47:13 -0500691 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400692 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500693 switch(textureFunction->coords)
694 {
695 case 2: out << ", int2 t"; break;
696 case 3: out << ", int3 t"; break;
697 default: UNREACHABLE();
698 }
699 }
700 else // Floating-point coordinates (except textureSize)
701 {
702 switch(textureFunction->coords)
703 {
704 case 1: out << ", int lod"; break; // textureSize()
705 case 2: out << ", float2 t"; break;
706 case 3: out << ", float3 t"; break;
707 case 4: out << ", float4 t"; break;
708 default: UNREACHABLE();
709 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000710 }
711
Nicolas Capensd11d5492014-02-19 17:06:10 -0500712 if (textureFunction->method == TextureFunction::GRAD)
713 {
714 switch(textureFunction->sampler)
715 {
716 case EbtSampler2D:
717 case EbtISampler2D:
718 case EbtUSampler2D:
719 case EbtSampler2DArray:
720 case EbtISampler2DArray:
721 case EbtUSampler2DArray:
722 case EbtSampler2DShadow:
723 case EbtSampler2DArrayShadow:
724 out << ", float2 ddx, float2 ddy";
725 break;
726 case EbtSampler3D:
727 case EbtISampler3D:
728 case EbtUSampler3D:
729 case EbtSamplerCube:
730 case EbtISamplerCube:
731 case EbtUSamplerCube:
732 case EbtSamplerCubeShadow:
733 out << ", float3 ddx, float3 ddy";
734 break;
735 default: UNREACHABLE();
736 }
737 }
738
Nicolas Capens75fb4752013-07-10 15:14:47 -0400739 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000740 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400741 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400742 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400743 case TextureFunction::LOD: out << ", float lod"; break;
744 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400745 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400746 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500747 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500748 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400749 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000750 }
751
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500752 if (textureFunction->offset)
753 {
754 switch(textureFunction->sampler)
755 {
756 case EbtSampler2D: out << ", int2 offset"; break;
757 case EbtSampler3D: out << ", int3 offset"; break;
758 case EbtSampler2DArray: out << ", int2 offset"; break;
759 case EbtISampler2D: out << ", int2 offset"; break;
760 case EbtISampler3D: out << ", int3 offset"; break;
761 case EbtISampler2DArray: out << ", int2 offset"; break;
762 case EbtUSampler2D: out << ", int2 offset"; break;
763 case EbtUSampler3D: out << ", int3 offset"; break;
764 case EbtUSampler2DArray: out << ", int2 offset"; break;
765 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500766 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500767 default: UNREACHABLE();
768 }
769 }
770
Nicolas Capens84cfa122014-04-14 13:48:45 -0400771 if (textureFunction->method == TextureFunction::BIAS ||
772 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500773 {
774 out << ", float bias";
775 }
776
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400777 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400778 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400779
Nicolas Capens75fb4752013-07-10 15:14:47 -0400780 if (textureFunction->method == TextureFunction::SIZE)
781 {
782 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
783 {
784 if (IsSamplerArray(textureFunction->sampler))
785 {
786 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
787 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
788 }
789 else
790 {
791 out << " uint width; uint height; uint numberOfLevels;\n"
792 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
793 }
794 }
795 else if (IsSampler3D(textureFunction->sampler))
796 {
797 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
798 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
799 }
800 else UNREACHABLE();
801
802 switch(textureFunction->sampler)
803 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400804 case EbtSampler2D: out << " return int2(width, height);"; break;
805 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
806 case EbtSamplerCube: out << " return int2(width, height);"; break;
807 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
808 case EbtISampler2D: out << " return int2(width, height);"; break;
809 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
810 case EbtISamplerCube: out << " return int2(width, height);"; break;
811 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
812 case EbtUSampler2D: out << " return int2(width, height);"; break;
813 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
814 case EbtUSamplerCube: out << " return int2(width, height);"; break;
815 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
816 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
817 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
818 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400819 default: UNREACHABLE();
820 }
821 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400822 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400823 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500824 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
825 {
826 out << " float width; float height; float layers; float levels;\n";
827
828 out << " uint mip = 0;\n";
829
830 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
831
832 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
833 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
834 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
835 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
836
837 // FACE_POSITIVE_X = 000b
838 // FACE_NEGATIVE_X = 001b
839 // FACE_POSITIVE_Y = 010b
840 // FACE_NEGATIVE_Y = 011b
841 // FACE_POSITIVE_Z = 100b
842 // FACE_NEGATIVE_Z = 101b
843 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
844
845 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
846 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
847 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
848
849 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
850 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
851 }
852 else if (IsIntegerSampler(textureFunction->sampler) &&
853 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400854 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400855 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400856 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400857 if (IsSamplerArray(textureFunction->sampler))
858 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400859 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400860
Nicolas Capens9edebd62013-08-06 10:59:10 -0400861 if (textureFunction->method == TextureFunction::LOD0)
862 {
863 out << " uint mip = 0;\n";
864 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400865 else if (textureFunction->method == TextureFunction::LOD0BIAS)
866 {
867 out << " uint mip = bias;\n";
868 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400869 else
870 {
871 if (textureFunction->method == TextureFunction::IMPLICIT ||
872 textureFunction->method == TextureFunction::BIAS)
873 {
874 out << " x.GetDimensions(0, width, height, layers, levels);\n"
875 " float2 tSized = float2(t.x * width, t.y * height);\n"
876 " float dx = length(ddx(tSized));\n"
877 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500878 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400879
880 if (textureFunction->method == TextureFunction::BIAS)
881 {
882 out << " lod += bias;\n";
883 }
884 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500885 else if (textureFunction->method == TextureFunction::GRAD)
886 {
887 out << " x.GetDimensions(0, width, height, layers, levels);\n"
888 " float lod = log2(max(length(ddx), length(ddy)));\n";
889 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400890
891 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
892 }
893
894 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400895 }
896 else
897 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400898 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400899
Nicolas Capens9edebd62013-08-06 10:59:10 -0400900 if (textureFunction->method == TextureFunction::LOD0)
901 {
902 out << " uint mip = 0;\n";
903 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400904 else if (textureFunction->method == TextureFunction::LOD0BIAS)
905 {
906 out << " uint mip = bias;\n";
907 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400908 else
909 {
910 if (textureFunction->method == TextureFunction::IMPLICIT ||
911 textureFunction->method == TextureFunction::BIAS)
912 {
913 out << " x.GetDimensions(0, width, height, levels);\n"
914 " float2 tSized = float2(t.x * width, t.y * height);\n"
915 " float dx = length(ddx(tSized));\n"
916 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500917 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400918
919 if (textureFunction->method == TextureFunction::BIAS)
920 {
921 out << " lod += bias;\n";
922 }
923 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500924 else if (textureFunction->method == TextureFunction::LOD)
925 {
926 out << " x.GetDimensions(0, width, height, levels);\n";
927 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500928 else if (textureFunction->method == TextureFunction::GRAD)
929 {
930 out << " x.GetDimensions(0, width, height, levels);\n"
931 " float lod = log2(max(length(ddx), length(ddy)));\n";
932 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400933
934 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
935 }
936
937 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400938 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400939 }
940 else if (IsSampler3D(textureFunction->sampler))
941 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400942 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400943
Nicolas Capens9edebd62013-08-06 10:59:10 -0400944 if (textureFunction->method == TextureFunction::LOD0)
945 {
946 out << " uint mip = 0;\n";
947 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400948 else if (textureFunction->method == TextureFunction::LOD0BIAS)
949 {
950 out << " uint mip = bias;\n";
951 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400952 else
953 {
954 if (textureFunction->method == TextureFunction::IMPLICIT ||
955 textureFunction->method == TextureFunction::BIAS)
956 {
957 out << " x.GetDimensions(0, width, height, depth, levels);\n"
958 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
959 " float dx = length(ddx(tSized));\n"
960 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500961 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400962
963 if (textureFunction->method == TextureFunction::BIAS)
964 {
965 out << " lod += bias;\n";
966 }
967 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500968 else if (textureFunction->method == TextureFunction::GRAD)
969 {
970 out << " x.GetDimensions(0, width, height, depth, levels);\n"
971 " float lod = log2(max(length(ddx), length(ddy)));\n";
972 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400973
974 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
975 }
976
977 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400978 }
979 else UNREACHABLE();
980 }
981
982 out << " return ";
983
984 // HLSL intrinsic
985 if (mOutputType == SH_HLSL9_OUTPUT)
986 {
987 switch(textureFunction->sampler)
988 {
989 case EbtSampler2D: out << "tex2D"; break;
990 case EbtSamplerCube: out << "texCUBE"; break;
991 default: UNREACHABLE();
992 }
993
Nicolas Capens75fb4752013-07-10 15:14:47 -0400994 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400995 {
996 case TextureFunction::IMPLICIT: out << "(s, "; break;
997 case TextureFunction::BIAS: out << "bias(s, "; break;
998 case TextureFunction::LOD: out << "lod(s, "; break;
999 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001000 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001001 default: UNREACHABLE();
1002 }
1003 }
1004 else if (mOutputType == SH_HLSL11_OUTPUT)
1005 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001006 if (textureFunction->method == TextureFunction::GRAD)
1007 {
1008 if (IsIntegerSampler(textureFunction->sampler))
1009 {
1010 out << "x.Load(";
1011 }
1012 else if (IsShadowSampler(textureFunction->sampler))
1013 {
1014 out << "x.SampleCmpLevelZero(s, ";
1015 }
1016 else
1017 {
1018 out << "x.SampleGrad(s, ";
1019 }
1020 }
1021 else if (IsIntegerSampler(textureFunction->sampler) ||
1022 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001023 {
1024 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001025 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001026 else if (IsShadowSampler(textureFunction->sampler))
1027 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001028 switch(textureFunction->method)
1029 {
1030 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1031 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1032 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1033 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1034 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1035 default: UNREACHABLE();
1036 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001037 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001038 else
1039 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001040 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001041 {
1042 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1043 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1044 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1045 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001046 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001047 default: UNREACHABLE();
1048 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001049 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001050 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001051 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001052
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001053 // Integer sampling requires integer addresses
1054 TString addressx = "";
1055 TString addressy = "";
1056 TString addressz = "";
1057 TString close = "";
1058
Nicolas Capensfc014542014-02-18 14:47:13 -05001059 if (IsIntegerSampler(textureFunction->sampler) ||
1060 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001061 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001062 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001063 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001064 case 2: out << "int3("; break;
1065 case 3: out << "int4("; break;
1066 default: UNREACHABLE();
1067 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001068
Nicolas Capensfc014542014-02-18 14:47:13 -05001069 // Convert from normalized floating-point to integer
1070 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001071 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001072 addressx = "int(floor(width * frac((";
1073 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001074
Nicolas Capensfc014542014-02-18 14:47:13 -05001075 if (IsSamplerArray(textureFunction->sampler))
1076 {
1077 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1078 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001079 else if (IsSamplerCube(textureFunction->sampler))
1080 {
1081 addressz = "((((";
1082 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001083 else
1084 {
1085 addressz = "int(floor(depth * frac((";
1086 }
1087
1088 close = "))))";
1089 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001090 }
1091 else
1092 {
1093 switch(hlslCoords)
1094 {
1095 case 2: out << "float2("; break;
1096 case 3: out << "float3("; break;
1097 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001098 default: UNREACHABLE();
1099 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001100 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001101
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001102 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001103
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001104 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001105 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001106 switch(textureFunction->coords)
1107 {
1108 case 3: proj = " / t.z"; break;
1109 case 4: proj = " / t.w"; break;
1110 default: UNREACHABLE();
1111 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001112 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001113
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001114 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001115
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001116 if (mOutputType == SH_HLSL9_OUTPUT)
1117 {
1118 if (hlslCoords >= 3)
1119 {
1120 if (textureFunction->coords < 3)
1121 {
1122 out << ", 0";
1123 }
1124 else
1125 {
1126 out << ", t.z" + proj;
1127 }
1128 }
1129
1130 if (hlslCoords == 4)
1131 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001132 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001133 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001134 case TextureFunction::BIAS: out << ", bias"; break;
1135 case TextureFunction::LOD: out << ", lod"; break;
1136 case TextureFunction::LOD0: out << ", 0"; break;
1137 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001138 default: UNREACHABLE();
1139 }
1140 }
1141
1142 out << "));\n";
1143 }
1144 else if (mOutputType == SH_HLSL11_OUTPUT)
1145 {
1146 if (hlslCoords >= 3)
1147 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001148 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1149 {
1150 out << ", face";
1151 }
1152 else
1153 {
1154 out << ", " + addressz + ("t.z" + proj) + close;
1155 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001156 }
1157
Nicolas Capensd11d5492014-02-19 17:06:10 -05001158 if (textureFunction->method == TextureFunction::GRAD)
1159 {
1160 if (IsIntegerSampler(textureFunction->sampler))
1161 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001162 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001163 }
1164 else if (IsShadowSampler(textureFunction->sampler))
1165 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001166 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001167 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001168 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001169 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1170 // The resulting third component of P' in the shadow forms is used as Dref
1171 out << "), t.z" << proj;
1172 }
1173 else
1174 {
1175 switch(textureFunction->coords)
1176 {
1177 case 3: out << "), t.z"; break;
1178 case 4: out << "), t.w"; break;
1179 default: UNREACHABLE();
1180 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001181 }
1182 }
1183 else
1184 {
1185 out << "), ddx, ddy";
1186 }
1187 }
1188 else if (IsIntegerSampler(textureFunction->sampler) ||
1189 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001190 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001191 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001192 }
1193 else if (IsShadowSampler(textureFunction->sampler))
1194 {
1195 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001196 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001197 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001198 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1199 // The resulting third component of P' in the shadow forms is used as Dref
1200 out << "), t.z" << proj;
1201 }
1202 else
1203 {
1204 switch(textureFunction->coords)
1205 {
1206 case 3: out << "), t.z"; break;
1207 case 4: out << "), t.w"; break;
1208 default: UNREACHABLE();
1209 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001210 }
1211 }
1212 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001213 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001214 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001215 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001216 case TextureFunction::IMPLICIT: out << ")"; break;
1217 case TextureFunction::BIAS: out << "), bias"; break;
1218 case TextureFunction::LOD: out << "), lod"; break;
1219 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001220 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001221 default: UNREACHABLE();
1222 }
1223 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001224
1225 if (textureFunction->offset)
1226 {
1227 out << ", offset";
1228 }
1229
1230 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001231 }
1232 else UNREACHABLE();
1233 }
1234
1235 out << "\n"
1236 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001237 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238 }
1239
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001240 if (mUsesFragCoord)
1241 {
1242 out << "#define GL_USES_FRAG_COORD\n";
1243 }
1244
1245 if (mUsesPointCoord)
1246 {
1247 out << "#define GL_USES_POINT_COORD\n";
1248 }
1249
1250 if (mUsesFrontFacing)
1251 {
1252 out << "#define GL_USES_FRONT_FACING\n";
1253 }
1254
1255 if (mUsesPointSize)
1256 {
1257 out << "#define GL_USES_POINT_SIZE\n";
1258 }
1259
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001260 if (mUsesFragDepth)
1261 {
1262 out << "#define GL_USES_FRAG_DEPTH\n";
1263 }
1264
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001265 if (mUsesDepthRange)
1266 {
1267 out << "#define GL_USES_DEPTH_RANGE\n";
1268 }
1269
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001270 if (mUsesXor)
1271 {
1272 out << "bool xor(bool p, bool q)\n"
1273 "{\n"
1274 " return (p || q) && !(p && q);\n"
1275 "}\n"
1276 "\n";
1277 }
1278
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001279 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001280}
1281
1282void OutputHLSL::visitSymbol(TIntermSymbol *node)
1283{
Jamie Madill32aab012015-01-27 14:12:26 -05001284 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001285
Jamie Madill570e04d2013-06-21 09:15:33 -04001286 // Handle accessing std140 structs by value
1287 if (mFlaggedStructMappedNames.count(node) > 0)
1288 {
1289 out << mFlaggedStructMappedNames[node];
1290 return;
1291 }
1292
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001293 TString name = node->getSymbol();
1294
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001295 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001296 {
1297 mUsesDepthRange = true;
1298 out << name;
1299 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300 else
1301 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001302 TQualifier qualifier = node->getQualifier();
1303
1304 if (qualifier == EvqUniform)
1305 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001306 const TType& nodeType = node->getType();
1307 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1308
1309 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001310 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001311 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001312 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001313 else
1314 {
1315 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001316 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001317
Jamie Madill033dae62014-06-18 12:56:28 -04001318 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001319 }
Jamie Madill19571812013-08-12 15:26:34 -07001320 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001321 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001322 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001323 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001324 }
Jamie Madill033dae62014-06-18 12:56:28 -04001325 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001326 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001327 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001328 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001329 }
Jamie Madill19571812013-08-12 15:26:34 -07001330 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001331 {
1332 mReferencedOutputVariables[name] = node;
1333 out << "out_" << name;
1334 }
1335 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001336 {
1337 out << "gl_Color[0]";
1338 mUsesFragColor = true;
1339 }
1340 else if (qualifier == EvqFragData)
1341 {
1342 out << "gl_Color";
1343 mUsesFragData = true;
1344 }
1345 else if (qualifier == EvqFragCoord)
1346 {
1347 mUsesFragCoord = true;
1348 out << name;
1349 }
1350 else if (qualifier == EvqPointCoord)
1351 {
1352 mUsesPointCoord = true;
1353 out << name;
1354 }
1355 else if (qualifier == EvqFrontFacing)
1356 {
1357 mUsesFrontFacing = true;
1358 out << name;
1359 }
1360 else if (qualifier == EvqPointSize)
1361 {
1362 mUsesPointSize = true;
1363 out << name;
1364 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001365 else if (qualifier == EvqInstanceID)
1366 {
1367 mUsesInstanceID = true;
1368 out << name;
1369 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001370 else if (name == "gl_FragDepthEXT")
1371 {
1372 mUsesFragDepth = true;
1373 out << "gl_Depth";
1374 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001375 else if (qualifier == EvqInternal)
1376 {
1377 out << name;
1378 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001379 else
1380 {
Jamie Madill033dae62014-06-18 12:56:28 -04001381 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001382 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001383 }
1384}
1385
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001386void OutputHLSL::visitRaw(TIntermRaw *node)
1387{
Jamie Madill32aab012015-01-27 14:12:26 -05001388 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001389}
1390
Olli Etuaho7fb49552015-03-18 17:27:44 +02001391void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1392{
1393 if (type.isScalar() && !type.isArray())
1394 {
1395 if (op == EOpEqual)
1396 {
1397 outputTriplet(visit, "(", " == ", ")", out);
1398 }
1399 else
1400 {
1401 outputTriplet(visit, "(", " != ", ")", out);
1402 }
1403 }
1404 else
1405 {
1406 if (visit == PreVisit && op == EOpNotEqual)
1407 {
1408 out << "!";
1409 }
1410
1411 if (type.isArray())
1412 {
1413 const TString &functionName = addArrayEqualityFunction(type);
1414 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1415 }
1416 else if (type.getBasicType() == EbtStruct)
1417 {
1418 const TStructure &structure = *type.getStruct();
1419 const TString &functionName = addStructEqualityFunction(structure);
1420 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1421 }
1422 else
1423 {
1424 ASSERT(type.isMatrix() || type.isVector());
1425 outputTriplet(visit, "all(", " == ", ")", out);
1426 }
1427 }
1428}
1429
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001430bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1431{
Jamie Madill32aab012015-01-27 14:12:26 -05001432 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001433
Jamie Madill570e04d2013-06-21 09:15:33 -04001434 // Handle accessing std140 structs by value
1435 if (mFlaggedStructMappedNames.count(node) > 0)
1436 {
1437 out << mFlaggedStructMappedNames[node];
1438 return false;
1439 }
1440
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001441 switch (node->getOp())
1442 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001443 case EOpAssign:
1444 if (node->getLeft()->isArray())
1445 {
1446 UNIMPLEMENTED();
1447 }
1448 else
1449 {
1450 outputTriplet(visit, "(", " = ", ")");
1451 }
1452 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001453 case EOpInitialize:
1454 if (visit == PreVisit)
1455 {
1456 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1457 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1458 // new variable is created before the assignment is evaluated), so we need to convert
1459 // this to "float t = x, x = t;".
1460
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001461 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001462 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001463 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001464
Jamie Madill37997142015-01-28 10:06:34 -05001465 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1466 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001467 {
Jamie Madill37997142015-01-28 10:06:34 -05001468 // For variables which are not constant, defer their real initialization until
1469 // after we initialize other globals: uniforms, attributes and varyings.
1470 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1471 const TString &initString = initializer(node->getType());
1472 node->setRight(new TIntermRaw(node->getType(), initString));
1473 }
1474 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1475 {
1476 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001477 return false;
1478 }
1479 }
1480 else if (visit == InVisit)
1481 {
1482 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001483 }
1484 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001485 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1486 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1487 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1488 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1489 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1490 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001491 if (visit == PreVisit)
1492 {
1493 out << "(";
1494 }
1495 else if (visit == InVisit)
1496 {
1497 out << " = mul(";
1498 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001499 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001500 }
1501 else
1502 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001503 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001504 }
1505 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001506 case EOpMatrixTimesMatrixAssign:
1507 if (visit == PreVisit)
1508 {
1509 out << "(";
1510 }
1511 else if (visit == InVisit)
1512 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001513 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001514 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001515 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001516 }
1517 else
1518 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001519 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001520 }
1521 break;
1522 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001523 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001524 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1525 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1526 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1527 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1528 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001529 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001530 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001531 const TType& leftType = node->getLeft()->getType();
1532 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001533 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001534 if (visit == PreVisit)
1535 {
1536 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1537 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001538 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001539 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001540 return false;
1541 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001542 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001543 else
1544 {
1545 outputTriplet(visit, "", "[", "]");
1546 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001547 }
1548 break;
1549 case EOpIndexIndirect:
1550 // We do not currently support indirect references to interface blocks
1551 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1552 outputTriplet(visit, "", "[", "]");
1553 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001554 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001555 if (visit == InVisit)
1556 {
1557 const TStructure* structure = node->getLeft()->getType().getStruct();
1558 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1559 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001560 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001561
1562 return false;
1563 }
1564 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001565 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001566 if (visit == InVisit)
1567 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001568 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1569 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1570 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001571 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001572
1573 return false;
1574 }
1575 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001576 case EOpVectorSwizzle:
1577 if (visit == InVisit)
1578 {
1579 out << ".";
1580
1581 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1582
1583 if (swizzle)
1584 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001585 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001586
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001587 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588 {
1589 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1590
1591 if (element)
1592 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001593 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001594
1595 switch (i)
1596 {
1597 case 0: out << "x"; break;
1598 case 1: out << "y"; break;
1599 case 2: out << "z"; break;
1600 case 3: out << "w"; break;
1601 default: UNREACHABLE();
1602 }
1603 }
1604 else UNREACHABLE();
1605 }
1606 }
1607 else UNREACHABLE();
1608
1609 return false; // Fully processed
1610 }
1611 break;
1612 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1613 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1614 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1615 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001616 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001617 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1618 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1619 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1620 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1621 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001622 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001623 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001624 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001625 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1627 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1628 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1629 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1630 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001631 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001632 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1633 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001634 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001635 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001636 if (node->getRight()->hasSideEffects())
1637 {
1638 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1639 return false;
1640 }
1641 else
1642 {
1643 outputTriplet(visit, "(", " || ", ")");
1644 return true;
1645 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001646 case EOpLogicalXor:
1647 mUsesXor = true;
1648 outputTriplet(visit, "xor(", ", ", ")");
1649 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001650 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001651 if (node->getRight()->hasSideEffects())
1652 {
1653 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1654 return false;
1655 }
1656 else
1657 {
1658 outputTriplet(visit, "(", " && ", ")");
1659 return true;
1660 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001661 default: UNREACHABLE();
1662 }
1663
1664 return true;
1665}
1666
1667bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1668{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669 switch (node->getOp())
1670 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001671 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001672 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001673 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1674 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001675 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001676 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1677 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1678 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1679 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001680 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1681 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1682 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1683 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1684 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1685 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1686 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1687 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001688 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1689 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1690 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1691 case EOpAsinh:
1692 ASSERT(node->getUseEmulatedFunction());
1693 writeEmulatedFunctionTriplet(visit, "asinh(");
1694 break;
1695 case EOpAcosh:
1696 ASSERT(node->getUseEmulatedFunction());
1697 writeEmulatedFunctionTriplet(visit, "acosh(");
1698 break;
1699 case EOpAtanh:
1700 ASSERT(node->getUseEmulatedFunction());
1701 writeEmulatedFunctionTriplet(visit, "atanh(");
1702 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001703 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1704 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1705 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1706 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1707 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1708 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1709 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1710 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1711 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001712 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1713 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1714 case EOpRoundEven:
1715 ASSERT(node->getUseEmulatedFunction());
1716 writeEmulatedFunctionTriplet(visit, "roundEven(");
1717 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001718 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1719 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301720 case EOpIsNan:
1721 outputTriplet(visit, "isnan(", "", ")");
1722 mRequiresIEEEStrictCompiling = true;
1723 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301724 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001725 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1726 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1727 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1728 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001729 case EOpPackSnorm2x16:
1730 ASSERT(node->getUseEmulatedFunction());
1731 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1732 break;
1733 case EOpPackUnorm2x16:
1734 ASSERT(node->getUseEmulatedFunction());
1735 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1736 break;
1737 case EOpPackHalf2x16:
1738 ASSERT(node->getUseEmulatedFunction());
1739 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1740 break;
1741 case EOpUnpackSnorm2x16:
1742 ASSERT(node->getUseEmulatedFunction());
1743 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1744 break;
1745 case EOpUnpackUnorm2x16:
1746 ASSERT(node->getUseEmulatedFunction());
1747 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1748 break;
1749 case EOpUnpackHalf2x16:
1750 ASSERT(node->getUseEmulatedFunction());
1751 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1752 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001753 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1754 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001755 case EOpDFdx:
1756 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1757 {
1758 outputTriplet(visit, "(", "", ", 0.0)");
1759 }
1760 else
1761 {
1762 outputTriplet(visit, "ddx(", "", ")");
1763 }
1764 break;
1765 case EOpDFdy:
1766 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1767 {
1768 outputTriplet(visit, "(", "", ", 0.0)");
1769 }
1770 else
1771 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001772 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001773 }
1774 break;
1775 case EOpFwidth:
1776 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1777 {
1778 outputTriplet(visit, "(", "", ", 0.0)");
1779 }
1780 else
1781 {
1782 outputTriplet(visit, "fwidth(", "", ")");
1783 }
1784 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001785 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1786 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001787 case EOpInverse:
1788 ASSERT(node->getUseEmulatedFunction());
1789 writeEmulatedFunctionTriplet(visit, "inverse(");
1790 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001791
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001792 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1793 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001794 default: UNREACHABLE();
1795 }
1796
1797 return true;
1798}
1799
1800bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1801{
Jamie Madill32aab012015-01-27 14:12:26 -05001802 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804 switch (node->getOp())
1805 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001806 case EOpSequence:
1807 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001808 if (mInsideFunction)
1809 {
Jamie Madill075edd82013-07-08 13:30:19 -04001810 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001811 out << "{\n";
1812 }
1813
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001814 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001815 {
Jamie Madill075edd82013-07-08 13:30:19 -04001816 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001817
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001818 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001819
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001820 // Don't output ; after case labels, they're terminated by :
1821 // This is needed especially since outputting a ; after a case statement would turn empty
1822 // case statements into non-empty case statements, disallowing fall-through from them.
1823 if ((*sit)->getAsCaseNode() == nullptr)
1824 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001825 }
1826
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001827 if (mInsideFunction)
1828 {
Jamie Madill075edd82013-07-08 13:30:19 -04001829 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001830 out << "}\n";
1831 }
1832
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001833 return false;
1834 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001835 case EOpDeclaration:
1836 if (visit == PreVisit)
1837 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001838 TIntermSequence *sequence = node->getSequence();
1839 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001841 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001843 TStructure *structure = variable->getType().getStruct();
1844
1845 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001846 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001847 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001848 }
1849
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001850 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001851 {
Jamie Madill37997142015-01-28 10:06:34 -05001852 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853 {
Jamie Madill37997142015-01-28 10:06:34 -05001854 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001855 {
Jamie Madill37997142015-01-28 10:06:34 -05001856 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001857 }
1858
Nicolas Capensd974db42014-10-07 10:50:19 -04001859 if (!mInsideFunction)
1860 {
1861 out << "static ";
1862 }
1863
1864 out << TypeString(variable->getType()) + " ";
1865
Jamie Madill37997142015-01-28 10:06:34 -05001866 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001867
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001868 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001870 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001871 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001872 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001873 }
1874 else
1875 {
Jamie Madill37997142015-01-28 10:06:34 -05001876 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001877 }
1878
Jamie Madill37997142015-01-28 10:06:34 -05001879 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001880 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001881 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001882 }
1883 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001884 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001885 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1886 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001887 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001888 }
1889 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001890 }
Jamie Madill033dae62014-06-18 12:56:28 -04001891 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001892 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001893 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001894 {
1895 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1896
1897 if (symbol)
1898 {
1899 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1900 mReferencedVaryings[symbol->getSymbol()] = symbol;
1901 }
1902 else
1903 {
1904 (*sit)->traverse(this);
1905 }
1906 }
1907 }
1908
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909 return false;
1910 }
1911 else if (visit == InVisit)
1912 {
1913 out << ", ";
1914 }
1915 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001916 case EOpInvariantDeclaration:
1917 // Do not do any translation
1918 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001919 case EOpPrototype:
1920 if (visit == PreVisit)
1921 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001922 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001923
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001924 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001925
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001926 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001927 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001928 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001929
1930 if (symbol)
1931 {
1932 out << argumentString(symbol);
1933
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001934 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001935 {
1936 out << ", ";
1937 }
1938 }
1939 else UNREACHABLE();
1940 }
1941
1942 out << ");\n";
1943
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001944 // Also prototype the Lod0 variant if needed
1945 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1946 {
1947 mOutputLod0Function = true;
1948 node->traverse(this);
1949 mOutputLod0Function = false;
1950 }
1951
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001952 return false;
1953 }
1954 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001955 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001956 case EOpFunction:
1957 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001958 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959
Jamie Madill033dae62014-06-18 12:56:28 -04001960 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001961
1962 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001963 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001964 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001966 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967 {
Jamie Madill033dae62014-06-18 12:56:28 -04001968 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001969 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001970
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001971 TIntermSequence *sequence = node->getSequence();
1972 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001973
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001974 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001975 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001976 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001977
1978 if (symbol)
1979 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001980 TStructure *structure = symbol->getType().getStruct();
1981
1982 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001983 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001984 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001985 }
1986
1987 out << argumentString(symbol);
1988
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001989 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001990 {
1991 out << ", ";
1992 }
1993 }
1994 else UNREACHABLE();
1995 }
1996
1997 out << ")\n"
1998 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001999
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002000 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002001 {
2002 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002003 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002004 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002005 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002006
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002007 out << "}\n";
2008
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002009 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2010 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002011 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002012 {
2013 mOutputLod0Function = true;
2014 node->traverse(this);
2015 mOutputLod0Function = false;
2016 }
2017 }
2018
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002019 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002020 }
2021 break;
2022 case EOpFunctionCall:
2023 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002024 TString name = TFunction::unmangleName(node->getName());
2025 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002026 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002027
2028 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029 {
Jamie Madill033dae62014-06-18 12:56:28 -04002030 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031 }
2032 else
2033 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002034 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002035
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002036 TextureFunction textureFunction;
2037 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002038 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002039 textureFunction.method = TextureFunction::IMPLICIT;
2040 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002041 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002042
2043 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002044 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002045 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002046 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002047 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002048 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002049 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002050 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002051 }
Nicolas Capens46485082014-04-15 13:12:50 -04002052 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2053 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002054 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002055 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002056 }
Nicolas Capens46485082014-04-15 13:12:50 -04002057 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002058 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002059 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002060 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002061 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002062 else if (name == "textureSize")
2063 {
2064 textureFunction.method = TextureFunction::SIZE;
2065 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002066 else if (name == "textureOffset")
2067 {
2068 textureFunction.method = TextureFunction::IMPLICIT;
2069 textureFunction.offset = true;
2070 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002071 else if (name == "textureProjOffset")
2072 {
2073 textureFunction.method = TextureFunction::IMPLICIT;
2074 textureFunction.offset = true;
2075 textureFunction.proj = true;
2076 }
2077 else if (name == "textureLodOffset")
2078 {
2079 textureFunction.method = TextureFunction::LOD;
2080 textureFunction.offset = true;
2081 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002082 else if (name == "textureProjLodOffset")
2083 {
2084 textureFunction.method = TextureFunction::LOD;
2085 textureFunction.proj = true;
2086 textureFunction.offset = true;
2087 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002088 else if (name == "texelFetch")
2089 {
2090 textureFunction.method = TextureFunction::FETCH;
2091 }
2092 else if (name == "texelFetchOffset")
2093 {
2094 textureFunction.method = TextureFunction::FETCH;
2095 textureFunction.offset = true;
2096 }
Nicolas Capens46485082014-04-15 13:12:50 -04002097 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002098 {
2099 textureFunction.method = TextureFunction::GRAD;
2100 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002101 else if (name == "textureGradOffset")
2102 {
2103 textureFunction.method = TextureFunction::GRAD;
2104 textureFunction.offset = true;
2105 }
Nicolas Capens46485082014-04-15 13:12:50 -04002106 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002107 {
2108 textureFunction.method = TextureFunction::GRAD;
2109 textureFunction.proj = true;
2110 }
2111 else if (name == "textureProjGradOffset")
2112 {
2113 textureFunction.method = TextureFunction::GRAD;
2114 textureFunction.proj = true;
2115 textureFunction.offset = true;
2116 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002117 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002118
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002119 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002120 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002121 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2122
2123 if (textureFunction.offset)
2124 {
2125 mandatoryArgumentCount++;
2126 }
2127
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002128 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002129
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002130 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002131 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002132 if (bias)
2133 {
2134 textureFunction.method = TextureFunction::LOD0BIAS;
2135 }
2136 else
2137 {
2138 textureFunction.method = TextureFunction::LOD0;
2139 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002140 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002141 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002142 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002143 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002144 }
2145 }
2146
2147 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002148
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002149 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002151
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002152 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002153 {
2154 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2155 {
2156 out << "texture_";
2157 (*arg)->traverse(this);
2158 out << ", sampler_";
2159 }
2160
2161 (*arg)->traverse(this);
2162
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002163 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002164 {
2165 out << ", ";
2166 }
2167 }
2168
2169 out << ")";
2170
2171 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172 }
2173 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002174 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002175 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2176 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2177 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2178 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2179 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2180 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2181 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2182 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2183 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2184 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2185 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2186 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2187 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2188 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2189 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2190 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2191 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2192 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2193 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002194 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002195 {
Jamie Madill033dae62014-06-18 12:56:28 -04002196 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002197 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002198 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002199 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002200 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002201 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2202 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2203 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2204 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2205 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2206 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002207 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002208 ASSERT(node->getUseEmulatedFunction());
2209 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002210 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002211 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002212 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002214 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002215 ASSERT(node->getUseEmulatedFunction());
2216 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002217 break;
2218 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2219 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2220 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2221 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2222 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2223 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2224 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2225 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2226 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002227 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002228 ASSERT(node->getUseEmulatedFunction());
2229 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002230 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002231 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2232 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002233 case EOpOuterProduct:
2234 ASSERT(node->getUseEmulatedFunction());
2235 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2236 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002237 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238 default: UNREACHABLE();
2239 }
2240
2241 return true;
2242}
2243
2244bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2245{
Jamie Madill32aab012015-01-27 14:12:26 -05002246 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002247
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002248 if (node->usesTernaryOperator())
2249 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002250 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002251 }
2252 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002254 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002255
Corentin Wallez80bacde2014-11-10 12:07:37 -08002256 // D3D errors when there is a gradient operation in a loop in an unflattened if
2257 // however flattening all the ifs in branch heavy shaders made D3D error too.
2258 // As a temporary workaround we flatten the ifs only if there is at least a loop
2259 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002260 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002261 {
2262 out << "FLATTEN ";
2263 }
2264
2265 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002266
2267 node->getCondition()->traverse(this);
2268
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002269 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002270
Jamie Madill075edd82013-07-08 13:30:19 -04002271 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002272 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002273
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002274 bool discard = false;
2275
daniel@transgaming.combb885322010-04-15 20:45:24 +00002276 if (node->getTrueBlock())
2277 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002278 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002279
2280 // Detect true discard
2281 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002282 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283
Jamie Madill075edd82013-07-08 13:30:19 -04002284 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002285 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002286
2287 if (node->getFalseBlock())
2288 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002289 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002290
Jamie Madill075edd82013-07-08 13:30:19 -04002291 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002292 out << "{\n";
2293
Jamie Madill075edd82013-07-08 13:30:19 -04002294 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002295 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002296
Jamie Madill075edd82013-07-08 13:30:19 -04002297 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002298 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002299
2300 // Detect false discard
2301 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2302 }
2303
2304 // ANGLE issue 486: Detect problematic conditional discard
2305 if (discard && FindSideEffectRewriting::search(node))
2306 {
2307 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002308 }
2309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310
2311 return false;
2312}
2313
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002314bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002315{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002316 if (node->getStatementList())
2317 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002318 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002319 outputTriplet(visit, "switch (", ") ", "");
2320 // The curly braces get written when visiting the statementList aggregate
2321 }
2322 else
2323 {
2324 // No statementList, so it won't output curly braces
2325 outputTriplet(visit, "switch (", ") {", "}\n");
2326 }
2327 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002328}
2329
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002330bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002331{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002332 if (node->hasCondition())
2333 {
2334 outputTriplet(visit, "case (", "", "):\n");
2335 return true;
2336 }
2337 else
2338 {
2339 TInfoSinkBase &out = getInfoSink();
2340 out << "default:\n";
2341 return false;
2342 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002343}
2344
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2346{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002347 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348}
2349
2350bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2351{
Nicolas Capens655fe362014-04-11 13:12:34 -04002352 mNestedLoopDepth++;
2353
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002354 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2355
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002356 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002357 {
2358 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2359 }
2360
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002361 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002362 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002363 if (handleExcessiveLoop(node))
2364 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002365 mInsideDiscontinuousLoop = wasDiscontinuous;
2366 mNestedLoopDepth--;
2367
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002368 return false;
2369 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002370 }
2371
Jamie Madill32aab012015-01-27 14:12:26 -05002372 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373
alokp@chromium.org52813552010-11-16 18:36:09 +00002374 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002376 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002377
Jamie Madill075edd82013-07-08 13:30:19 -04002378 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002379 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380 }
2381 else
2382 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002383 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002384
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385 if (node->getInit())
2386 {
2387 node->getInit()->traverse(this);
2388 }
2389
2390 out << "; ";
2391
alokp@chromium.org52813552010-11-16 18:36:09 +00002392 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002394 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395 }
2396
2397 out << "; ";
2398
alokp@chromium.org52813552010-11-16 18:36:09 +00002399 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002401 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402 }
2403
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002404 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002405
Jamie Madill075edd82013-07-08 13:30:19 -04002406 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002407 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 }
2409
2410 if (node->getBody())
2411 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002412 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 }
2414
Jamie Madill075edd82013-07-08 13:30:19 -04002415 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002416 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417
alokp@chromium.org52813552010-11-16 18:36:09 +00002418 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419 {
Jamie Madill075edd82013-07-08 13:30:19 -04002420 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 out << "while(\n";
2422
alokp@chromium.org52813552010-11-16 18:36:09 +00002423 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424
daniel@transgaming.com73536982012-03-21 20:45:49 +00002425 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 }
2427
daniel@transgaming.com73536982012-03-21 20:45:49 +00002428 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002430 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002431 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002432
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 return false;
2434}
2435
2436bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2437{
Jamie Madill32aab012015-01-27 14:12:26 -05002438 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439
2440 switch (node->getFlowOp())
2441 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002442 case EOpKill:
2443 outputTriplet(visit, "discard;\n", "", "");
2444 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002445 case EOpBreak:
2446 if (visit == PreVisit)
2447 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002448 if (mNestedLoopDepth > 1)
2449 {
2450 mUsesNestedBreak = true;
2451 }
2452
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002453 if (mExcessiveLoopIndex)
2454 {
2455 out << "{Break";
2456 mExcessiveLoopIndex->traverse(this);
2457 out << " = true; break;}\n";
2458 }
2459 else
2460 {
2461 out << "break;\n";
2462 }
2463 }
2464 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002465 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466 case EOpReturn:
2467 if (visit == PreVisit)
2468 {
2469 if (node->getExpression())
2470 {
2471 out << "return ";
2472 }
2473 else
2474 {
2475 out << "return;\n";
2476 }
2477 }
2478 else if (visit == PostVisit)
2479 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002480 if (node->getExpression())
2481 {
2482 out << ";\n";
2483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002484 }
2485 break;
2486 default: UNREACHABLE();
2487 }
2488
2489 return true;
2490}
2491
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002492void OutputHLSL::traverseStatements(TIntermNode *node)
2493{
2494 if (isSingleStatement(node))
2495 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002496 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002497 }
2498
2499 node->traverse(this);
2500}
2501
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002502bool OutputHLSL::isSingleStatement(TIntermNode *node)
2503{
2504 TIntermAggregate *aggregate = node->getAsAggregate();
2505
2506 if (aggregate)
2507 {
2508 if (aggregate->getOp() == EOpSequence)
2509 {
2510 return false;
2511 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002512 else if (aggregate->getOp() == EOpDeclaration)
2513 {
2514 // Declaring multiple comma-separated variables must be considered multiple statements
2515 // because each individual declaration has side effects which are visible in the next.
2516 return false;
2517 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002518 else
2519 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002520 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002521 {
2522 if (!isSingleStatement(*sit))
2523 {
2524 return false;
2525 }
2526 }
2527
2528 return true;
2529 }
2530 }
2531
2532 return true;
2533}
2534
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002535// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2536// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002537bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2538{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002539 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002540 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002541
2542 // Parse loops of the form:
2543 // for(int index = initial; index [comparator] limit; index += increment)
2544 TIntermSymbol *index = NULL;
2545 TOperator comparator = EOpNull;
2546 int initial = 0;
2547 int limit = 0;
2548 int increment = 0;
2549
2550 // Parse index name and intial value
2551 if (node->getInit())
2552 {
2553 TIntermAggregate *init = node->getInit()->getAsAggregate();
2554
2555 if (init)
2556 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002557 TIntermSequence *sequence = init->getSequence();
2558 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002559
2560 if (variable && variable->getQualifier() == EvqTemporary)
2561 {
2562 TIntermBinary *assign = variable->getAsBinaryNode();
2563
2564 if (assign->getOp() == EOpInitialize)
2565 {
2566 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2567 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2568
2569 if (symbol && constant)
2570 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002571 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002572 {
2573 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002574 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002575 }
2576 }
2577 }
2578 }
2579 }
2580 }
2581
2582 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002583 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002584 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002585 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002586
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002587 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2588 {
2589 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2590
2591 if (constant)
2592 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002593 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002594 {
2595 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002596 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002597 }
2598 }
2599 }
2600 }
2601
2602 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002603 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002604 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002605 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2606 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002607
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002608 if (binaryTerminal)
2609 {
2610 TOperator op = binaryTerminal->getOp();
2611 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2612
2613 if (constant)
2614 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002615 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002616 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002617 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002618
2619 switch (op)
2620 {
2621 case EOpAddAssign: increment = value; break;
2622 case EOpSubAssign: increment = -value; break;
2623 default: UNIMPLEMENTED();
2624 }
2625 }
2626 }
2627 }
2628 else if (unaryTerminal)
2629 {
2630 TOperator op = unaryTerminal->getOp();
2631
2632 switch (op)
2633 {
2634 case EOpPostIncrement: increment = 1; break;
2635 case EOpPostDecrement: increment = -1; break;
2636 case EOpPreIncrement: increment = 1; break;
2637 case EOpPreDecrement: increment = -1; break;
2638 default: UNIMPLEMENTED();
2639 }
2640 }
2641 }
2642
2643 if (index != NULL && comparator != EOpNull && increment != 0)
2644 {
2645 if (comparator == EOpLessThanEqual)
2646 {
2647 comparator = EOpLessThan;
2648 limit += 1;
2649 }
2650
2651 if (comparator == EOpLessThan)
2652 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002653 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002654
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002655 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002656 {
2657 return false; // Not an excessive loop
2658 }
2659
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002660 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2661 mExcessiveLoopIndex = index;
2662
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002663 out << "{int ";
2664 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002665 out << ";\n"
2666 "bool Break";
2667 index->traverse(this);
2668 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002669
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002670 bool firstLoopFragment = true;
2671
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002672 while (iterations > 0)
2673 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002674 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002675
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002676 if (!firstLoopFragment)
2677 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002678 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002679 index->traverse(this);
2680 out << ") {\n";
2681 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002682
2683 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2684 {
2685 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2686 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002687
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002688 // for(int index = initial; index < clampedLimit; index += increment)
2689
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002690 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002691 index->traverse(this);
2692 out << " = ";
2693 out << initial;
2694
2695 out << "; ";
2696 index->traverse(this);
2697 out << " < ";
2698 out << clampedLimit;
2699
2700 out << "; ";
2701 index->traverse(this);
2702 out << " += ";
2703 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002704 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002705
Jamie Madill075edd82013-07-08 13:30:19 -04002706 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002707 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002708
2709 if (node->getBody())
2710 {
2711 node->getBody()->traverse(this);
2712 }
2713
Jamie Madill075edd82013-07-08 13:30:19 -04002714 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002715 out << ";}\n";
2716
2717 if (!firstLoopFragment)
2718 {
2719 out << "}\n";
2720 }
2721
2722 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002723
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002724 initial += MAX_LOOP_ITERATIONS * increment;
2725 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002726 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002727
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002728 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002729
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002730 mExcessiveLoopIndex = restoreIndex;
2731
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002732 return true;
2733 }
2734 else UNIMPLEMENTED();
2735 }
2736
2737 return false; // Not handled as an excessive loop
2738}
2739
Olli Etuaho7fb49552015-03-18 17:27:44 +02002740void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002741{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002742 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002743 {
2744 out << preString;
2745 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002746 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002747 {
2748 out << inString;
2749 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002750 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002751 {
2752 out << postString;
2753 }
2754}
2755
Olli Etuaho7fb49552015-03-18 17:27:44 +02002756void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2757{
2758 outputTriplet(visit, preString, inString, postString, getInfoSink());
2759}
2760
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002761void OutputHLSL::outputLineDirective(int line)
2762{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002763 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002764 {
Jamie Madill32aab012015-01-27 14:12:26 -05002765 TInfoSinkBase &out = getInfoSink();
2766
2767 out << "\n";
2768 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002769
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002770 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002771 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002772 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002773 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002774
Jamie Madill32aab012015-01-27 14:12:26 -05002775 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002776 }
2777}
2778
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002779TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2780{
2781 TQualifier qualifier = symbol->getQualifier();
2782 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002783 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002784
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002785 if (name.empty()) // HLSL demands named arguments, also for prototypes
2786 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002787 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002788 }
2789 else
2790 {
Jamie Madill033dae62014-06-18 12:56:28 -04002791 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002792 }
2793
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002794 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2795 {
Jamie Madill033dae62014-06-18 12:56:28 -04002796 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002797 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002798 }
2799
Jamie Madill033dae62014-06-18 12:56:28 -04002800 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002801}
2802
2803TString OutputHLSL::initializer(const TType &type)
2804{
2805 TString string;
2806
Jamie Madill94bf7f22013-07-08 13:31:15 -04002807 size_t size = type.getObjectSize();
2808 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002809 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002810 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002811
Jamie Madill94bf7f22013-07-08 13:31:15 -04002812 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813 {
2814 string += ", ";
2815 }
2816 }
2817
daniel@transgaming.comead23042010-04-29 03:35:36 +00002818 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002819}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002820
Daniel Bratell29190082015-02-20 16:42:54 +01002821void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002822{
Jamie Madill32aab012015-01-27 14:12:26 -05002823 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002824
2825 if (visit == PreVisit)
2826 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002827 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002828
Daniel Bratell29190082015-02-20 16:42:54 +01002829 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002830 }
2831 else if (visit == InVisit)
2832 {
2833 out << ", ";
2834 }
2835 else if (visit == PostVisit)
2836 {
2837 out << ")";
2838 }
2839}
2840
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002841const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2842{
Jamie Madill32aab012015-01-27 14:12:26 -05002843 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002844
Jamie Madill98493dd2013-07-08 14:39:03 -04002845 const TStructure* structure = type.getStruct();
2846 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002847 {
Jamie Madill033dae62014-06-18 12:56:28 -04002848 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002849
Jamie Madill98493dd2013-07-08 14:39:03 -04002850 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002851
Jamie Madill98493dd2013-07-08 14:39:03 -04002852 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002853 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002854 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002855 constUnion = writeConstantUnion(*fieldType, constUnion);
2856
Jamie Madill98493dd2013-07-08 14:39:03 -04002857 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002858 {
2859 out << ", ";
2860 }
2861 }
2862
2863 out << ")";
2864 }
2865 else
2866 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002867 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002868 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002869
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002870 if (writeType)
2871 {
Jamie Madill033dae62014-06-18 12:56:28 -04002872 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002873 }
2874
Jamie Madill94bf7f22013-07-08 13:31:15 -04002875 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002876 {
2877 switch (constUnion->getType())
2878 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002879 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002880 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002881 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002882 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002883 default: UNREACHABLE();
2884 }
2885
2886 if (i != size - 1)
2887 {
2888 out << ", ";
2889 }
2890 }
2891
2892 if (writeType)
2893 {
2894 out << ")";
2895 }
2896 }
2897
2898 return constUnion;
2899}
2900
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002901void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2902{
2903 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2904 outputTriplet(visit, preString.c_str(), ", ", ")");
2905}
2906
Jamie Madill37997142015-01-28 10:06:34 -05002907bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2908{
2909 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2910 expression->traverse(&searchSymbol);
2911
2912 if (searchSymbol.foundMatch())
2913 {
2914 // Type already printed
2915 out << "t" + str(mUniqueIndex) + " = ";
2916 expression->traverse(this);
2917 out << ", ";
2918 symbolNode->traverse(this);
2919 out << " = t" + str(mUniqueIndex);
2920
2921 mUniqueIndex++;
2922 return true;
2923 }
2924
2925 return false;
2926}
2927
2928void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2929{
2930 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2931 << "\n"
2932 << "void initializeDeferredGlobals()\n"
2933 << "{\n";
2934
2935 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2936 {
2937 TIntermSymbol *symbol = deferredGlobal.first;
2938 TIntermTyped *expression = deferredGlobal.second;
2939 ASSERT(symbol);
2940 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2941
2942 out << " " << Decorate(symbol->getSymbol()) << " = ";
2943
2944 if (!writeSameSymbolInitializer(out, symbol, expression))
2945 {
2946 ASSERT(mInfoSinkStack.top() == &out);
2947 expression->traverse(this);
2948 }
2949
2950 out << ";\n";
2951 }
2952
2953 out << "}\n"
2954 << "\n";
2955}
2956
Jamie Madill55e79e02015-02-09 15:35:00 -05002957TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2958{
2959 const TFieldList &fields = structure.fields();
2960
2961 for (const auto &eqFunction : mStructEqualityFunctions)
2962 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002963 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002964 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002965 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002966 }
2967 }
2968
2969 const TString &structNameString = StructNameString(structure);
2970
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002971 StructEqualityFunction *function = new StructEqualityFunction();
2972 function->structure = &structure;
2973 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002974
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002975 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002976
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002977 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2978 << "{\n"
2979 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002980
2981 for (size_t i = 0; i < fields.size(); i++)
2982 {
2983 const TField *field = fields[i];
2984 const TType *fieldType = field->type();
2985
2986 const TString &fieldNameA = "a." + Decorate(field->name());
2987 const TString &fieldNameB = "b." + Decorate(field->name());
2988
2989 if (i > 0)
2990 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002991 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002992 }
2993
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002994 fnOut << "(";
2995 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2996 fnOut << fieldNameA;
2997 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2998 fnOut << fieldNameB;
2999 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3000 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003001 }
3002
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003003 fnOut << ";\n" << "}\n";
3004
3005 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003006
3007 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003008 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003009
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003010 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003011}
3012
Olli Etuaho7fb49552015-03-18 17:27:44 +02003013TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3014{
3015 for (const auto &eqFunction : mArrayEqualityFunctions)
3016 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003017 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003018 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003019 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003020 }
3021 }
3022
3023 const TString &typeName = TypeString(type);
3024
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003025 ArrayEqualityFunction *function = new ArrayEqualityFunction();
3026 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003027
3028 TInfoSinkBase fnNameOut;
3029 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003030 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003031
3032 TType nonArrayType = type;
3033 nonArrayType.clearArrayness();
3034
3035 TInfoSinkBase fnOut;
3036
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003037 fnOut << "bool " << function->functionName << "("
Olli Etuaho7fb49552015-03-18 17:27:44 +02003038 << typeName << "[" << type.getArraySize() << "] a, "
3039 << typeName << "[" << type.getArraySize() << "] b)\n"
3040 << "{\n"
3041 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3042 " {\n"
3043 " if (";
3044
3045 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3046 fnOut << "a[i]";
3047 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3048 fnOut << "b[i]";
3049 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3050
3051 fnOut << ") { return false; }\n"
3052 " }\n"
3053 " return true;\n"
3054 "}\n";
3055
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003056 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003057
3058 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003059 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003060
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003061 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003062}
3063
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003064}