blob: f3aa55650cfe9d006c78b6806e1f6a72fab07203 [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 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200146 if (mShaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000147 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400148 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
149 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000150 }
151 else
152 {
Cooper Partine6664f02015-01-09 16:22:24 -0800153 // Reserve registers for dx_DepthRange, dx_ViewAdjust and dx_ViewCoords
154 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000155 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000156 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000157
Jamie Madillf91ce812014-06-13 10:04:34 -0400158 // Reserve registers for the default uniform block and driver constants
159 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000162OutputHLSL::~OutputHLSL()
163{
Jamie Madill8daaba12014-06-13 10:04:33 -0400164 SafeDelete(mUnfoldShortCircuit);
165 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400166 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000167}
168
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200169void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000170{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200171 mContainsLoopDiscontinuity = mShaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(treeRoot);
172 mContainsAnyLoop = containsAnyLoop(treeRoot);
173 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400174 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000175
Jamie Madille53c98b2014-02-03 11:57:13 -0500176 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
177 // use a vertex attribute as a condition, and some related computation in the else block.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200178 if (mOutputType == SH_HLSL9_OUTPUT && mShaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500179 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200180 RewriteElseBlocks(treeRoot);
Jamie Madille53c98b2014-02-03 11:57:13 -0500181 }
182
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200183 BuiltInFunctionEmulator builtInFunctionEmulator;
184 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200185 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500186
Jamie Madill37997142015-01-28 10:06:34 -0500187 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500188 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200189 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500190 mInfoSinkStack.pop();
191
Jamie Madill37997142015-01-28 10:06:34 -0500192 mInfoSinkStack.push(&mFooter);
193 if (!mDeferredGlobalInitializers.empty())
194 {
195 writeDeferredGlobalInitializers(mFooter);
196 }
197 mInfoSinkStack.pop();
198
Jamie Madill32aab012015-01-27 14:12:26 -0500199 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200200 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500201 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000202
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200203 objSink << mHeader.c_str();
204 objSink << mBody.c_str();
205 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200206
207 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000208}
209
Jamie Madill570e04d2013-06-21 09:15:33 -0400210void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
211{
212 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
213 {
214 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
215
Jamie Madill32aab012015-01-27 14:12:26 -0500216 TInfoSinkBase structInfoSink;
217 mInfoSinkStack.push(&structInfoSink);
218
Jamie Madill570e04d2013-06-21 09:15:33 -0400219 // This will mark the necessary block elements as referenced
220 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500221
222 TString structName(structInfoSink.c_str());
223 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400224
225 mFlaggedStructOriginalNames[flaggedNode] = structName;
226
227 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
228 {
229 structName.erase(pos, 1);
230 }
231
232 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
233 }
234}
235
Jamie Madill4e1fd412014-07-10 17:50:10 -0400236const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
237{
238 return mUniformHLSL->getInterfaceBlockRegisterMap();
239}
240
Jamie Madill9fe25e92014-07-18 10:33:08 -0400241const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
242{
243 return mUniformHLSL->getUniformRegisterMap();
244}
245
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000246int OutputHLSL::vectorSize(const TType &type) const
247{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000248 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000249 int arraySize = type.isArray() ? type.getArraySize() : 1;
250
251 return elementSize * arraySize;
252}
253
Jamie Madill98493dd2013-07-08 14:39:03 -0400254TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400255{
256 TString init;
257
258 TString preIndentString;
259 TString fullIndentString;
260
261 for (int spaces = 0; spaces < (indent * 4); spaces++)
262 {
263 preIndentString += ' ';
264 }
265
266 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
267 {
268 fullIndentString += ' ';
269 }
270
271 init += preIndentString + "{\n";
272
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 const TFieldList &fields = structure.fields();
274 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400275 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400276 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400277 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400278 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400279
Jamie Madill98493dd2013-07-08 14:39:03 -0400280 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400281 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400282 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400283 }
284 else
285 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400287 }
288 }
289
290 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
291
292 return init;
293}
294
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200295void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296{
Jamie Madill32aab012015-01-27 14:12:26 -0500297 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000299 TString varyings;
300 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400301 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000302
Jamie Madill829f59e2013-11-13 19:40:54 -0500303 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400304 {
305 TIntermTyped *structNode = flaggedStructIt->first;
306 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400307 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400308 const TString &originalName = mFlaggedStructOriginalNames[structNode];
309
Jamie Madill033dae62014-06-18 12:56:28 -0400310 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400311 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400312 flaggedStructs += "\n";
313 }
314
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000315 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
316 {
317 const TType &type = varying->second->getType();
318 const TString &name = varying->second->getSymbol();
319
320 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400321 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
322 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000323 }
324
325 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
326 {
327 const TType &type = attribute->second->getType();
328 const TString &name = attribute->second->getSymbol();
329
Jamie Madill033dae62014-06-18 12:56:28 -0400330 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000331 }
332
Jamie Madill8daaba12014-06-13 10:04:33 -0400333 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400334
Jamie Madillf91ce812014-06-13 10:04:34 -0400335 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
336 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
337
Jamie Madill55e79e02015-02-09 15:35:00 -0500338 if (!mStructEqualityFunctions.empty())
339 {
340 out << "\n// Structure equality functions\n\n";
341 for (const auto &eqFunction : mStructEqualityFunctions)
342 {
343 out << eqFunction.functionDefinition << "\n";
344 }
345 }
346
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500347 if (mUsesDiscardRewriting)
348 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400349 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500350 }
351
Nicolas Capens655fe362014-04-11 13:12:34 -0400352 if (mUsesNestedBreak)
353 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400354 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400355 }
356
Arun Patole44efa0b2015-03-04 17:11:05 +0530357 if (mRequiresIEEEStrictCompiling)
358 {
359 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
360 }
361
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400362 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
363 "#define LOOP [loop]\n"
364 "#define FLATTEN [flatten]\n"
365 "#else\n"
366 "#define LOOP\n"
367 "#define FLATTEN\n"
368 "#endif\n";
369
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200370 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200372 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
373 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000374
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000375 out << "// Varyings\n";
376 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400377 out << "\n";
378
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200379 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000380 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500381 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000382 {
Jamie Madill46131a32013-06-20 11:55:50 -0400383 const TString &variableName = outputVariableIt->first;
384 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400385
Jamie Madill033dae62014-06-18 12:56:28 -0400386 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400387 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000388 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000389 }
Jamie Madill46131a32013-06-20 11:55:50 -0400390 else
391 {
392 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
393
394 out << "static float4 gl_Color[" << numColorValues << "] =\n"
395 "{\n";
396 for (unsigned int i = 0; i < numColorValues; i++)
397 {
398 out << " float4(0, 0, 0, 0)";
399 if (i + 1 != numColorValues)
400 {
401 out << ",";
402 }
403 out << "\n";
404 }
405
406 out << "};\n";
407 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000408
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400409 if (mUsesFragDepth)
410 {
411 out << "static float gl_Depth = 0.0;\n";
412 }
413
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000414 if (mUsesFragCoord)
415 {
416 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
417 }
418
419 if (mUsesPointCoord)
420 {
421 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
422 }
423
424 if (mUsesFrontFacing)
425 {
426 out << "static bool gl_FrontFacing = false;\n";
427 }
428
429 out << "\n";
430
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000431 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000432 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000433 out << "struct gl_DepthRangeParameters\n"
434 "{\n"
435 " float near;\n"
436 " float far;\n"
437 " float diff;\n"
438 "};\n"
439 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000440 }
441
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000442 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000443 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000444 out << "cbuffer DriverConstants : register(b1)\n"
445 "{\n";
446
447 if (mUsesDepthRange)
448 {
449 out << " float3 dx_DepthRange : packoffset(c0);\n";
450 }
451
452 if (mUsesFragCoord)
453 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000454 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000455 }
456
457 if (mUsesFragCoord || mUsesFrontFacing)
458 {
459 out << " float3 dx_DepthFront : packoffset(c2);\n";
460 }
461
462 out << "};\n";
463 }
464 else
465 {
466 if (mUsesDepthRange)
467 {
468 out << "uniform float3 dx_DepthRange : register(c0);";
469 }
470
471 if (mUsesFragCoord)
472 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000473 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000474 }
475
476 if (mUsesFragCoord || mUsesFrontFacing)
477 {
478 out << "uniform float3 dx_DepthFront : register(c2);\n";
479 }
480 }
481
482 out << "\n";
483
484 if (mUsesDepthRange)
485 {
486 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
487 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000488 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000489
Jamie Madillf91ce812014-06-13 10:04:34 -0400490 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000491 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400492 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000493 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400494 out << flaggedStructs;
495 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000496 }
497
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000498 if (usingMRTExtension && mNumRenderTargets > 1)
499 {
500 out << "#define GL_USES_MRT\n";
501 }
502
503 if (mUsesFragColor)
504 {
505 out << "#define GL_USES_FRAG_COLOR\n";
506 }
507
508 if (mUsesFragData)
509 {
510 out << "#define GL_USES_FRAG_DATA\n";
511 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000512 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000513 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000515 out << "// Attributes\n";
516 out << attributes;
517 out << "\n"
518 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400519
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000520 if (mUsesPointSize)
521 {
522 out << "static float gl_PointSize = float(1);\n";
523 }
524
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000525 if (mUsesInstanceID)
526 {
527 out << "static int gl_InstanceID;";
528 }
529
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000530 out << "\n"
531 "// Varyings\n";
532 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000533 out << "\n";
534
535 if (mUsesDepthRange)
536 {
537 out << "struct gl_DepthRangeParameters\n"
538 "{\n"
539 " float near;\n"
540 " float far;\n"
541 " float diff;\n"
542 "};\n"
543 "\n";
544 }
545
546 if (mOutputType == SH_HLSL11_OUTPUT)
547 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800548 out << "cbuffer DriverConstants : register(b1)\n"
549 "{\n";
550
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000551 if (mUsesDepthRange)
552 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800553 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000554 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800555
Cooper Partine6664f02015-01-09 16:22:24 -0800556 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800557 // However, we declare it for all shaders (including Feature Level 10+).
558 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
559 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800560 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800561
562 out << "};\n"
563 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000564 }
565 else
566 {
567 if (mUsesDepthRange)
568 {
569 out << "uniform float3 dx_DepthRange : register(c0);\n";
570 }
571
Cooper Partine6664f02015-01-09 16:22:24 -0800572 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
573 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000574 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000575 }
576
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000577 if (mUsesDepthRange)
578 {
579 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
580 "\n";
581 }
582
Jamie Madillf91ce812014-06-13 10:04:34 -0400583 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000584 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400585 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000586 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400587 out << flaggedStructs;
588 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000589 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400590 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000591
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400592 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
593 {
594 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400595 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000596 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400597 switch(textureFunction->sampler)
598 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400599 case EbtSampler2D: out << "int2 "; break;
600 case EbtSampler3D: out << "int3 "; break;
601 case EbtSamplerCube: out << "int2 "; break;
602 case EbtSampler2DArray: out << "int3 "; break;
603 case EbtISampler2D: out << "int2 "; break;
604 case EbtISampler3D: out << "int3 "; break;
605 case EbtISamplerCube: out << "int2 "; break;
606 case EbtISampler2DArray: out << "int3 "; break;
607 case EbtUSampler2D: out << "int2 "; break;
608 case EbtUSampler3D: out << "int3 "; break;
609 case EbtUSamplerCube: out << "int2 "; break;
610 case EbtUSampler2DArray: out << "int3 "; break;
611 case EbtSampler2DShadow: out << "int2 "; break;
612 case EbtSamplerCubeShadow: out << "int2 "; break;
613 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400614 default: UNREACHABLE();
615 }
616 }
617 else // Sampling function
618 {
619 switch(textureFunction->sampler)
620 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400621 case EbtSampler2D: out << "float4 "; break;
622 case EbtSampler3D: out << "float4 "; break;
623 case EbtSamplerCube: out << "float4 "; break;
624 case EbtSampler2DArray: out << "float4 "; break;
625 case EbtISampler2D: out << "int4 "; break;
626 case EbtISampler3D: out << "int4 "; break;
627 case EbtISamplerCube: out << "int4 "; break;
628 case EbtISampler2DArray: out << "int4 "; break;
629 case EbtUSampler2D: out << "uint4 "; break;
630 case EbtUSampler3D: out << "uint4 "; break;
631 case EbtUSamplerCube: out << "uint4 "; break;
632 case EbtUSampler2DArray: out << "uint4 "; break;
633 case EbtSampler2DShadow: out << "float "; break;
634 case EbtSamplerCubeShadow: out << "float "; break;
635 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400636 default: UNREACHABLE();
637 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000638 }
639
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400640 // Function name
641 out << textureFunction->name();
642
643 // Argument list
644 int hlslCoords = 4;
645
646 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000647 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400648 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000649 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400650 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
651 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
652 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000653 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400654
Nicolas Capens75fb4752013-07-10 15:14:47 -0400655 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000656 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400657 case TextureFunction::IMPLICIT: break;
658 case TextureFunction::BIAS: hlslCoords = 4; break;
659 case TextureFunction::LOD: hlslCoords = 4; break;
660 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400661 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400662 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000663 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400664 }
665 else if (mOutputType == SH_HLSL11_OUTPUT)
666 {
667 switch(textureFunction->sampler)
668 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400669 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
670 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
671 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
672 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
673 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
674 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500675 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400676 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
677 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
678 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500679 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400680 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
681 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
682 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
683 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400684 default: UNREACHABLE();
685 }
686 }
687 else UNREACHABLE();
688
Nicolas Capensfc014542014-02-18 14:47:13 -0500689 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400690 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500691 switch(textureFunction->coords)
692 {
693 case 2: out << ", int2 t"; break;
694 case 3: out << ", int3 t"; break;
695 default: UNREACHABLE();
696 }
697 }
698 else // Floating-point coordinates (except textureSize)
699 {
700 switch(textureFunction->coords)
701 {
702 case 1: out << ", int lod"; break; // textureSize()
703 case 2: out << ", float2 t"; break;
704 case 3: out << ", float3 t"; break;
705 case 4: out << ", float4 t"; break;
706 default: UNREACHABLE();
707 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000708 }
709
Nicolas Capensd11d5492014-02-19 17:06:10 -0500710 if (textureFunction->method == TextureFunction::GRAD)
711 {
712 switch(textureFunction->sampler)
713 {
714 case EbtSampler2D:
715 case EbtISampler2D:
716 case EbtUSampler2D:
717 case EbtSampler2DArray:
718 case EbtISampler2DArray:
719 case EbtUSampler2DArray:
720 case EbtSampler2DShadow:
721 case EbtSampler2DArrayShadow:
722 out << ", float2 ddx, float2 ddy";
723 break;
724 case EbtSampler3D:
725 case EbtISampler3D:
726 case EbtUSampler3D:
727 case EbtSamplerCube:
728 case EbtISamplerCube:
729 case EbtUSamplerCube:
730 case EbtSamplerCubeShadow:
731 out << ", float3 ddx, float3 ddy";
732 break;
733 default: UNREACHABLE();
734 }
735 }
736
Nicolas Capens75fb4752013-07-10 15:14:47 -0400737 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000738 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400739 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400740 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400741 case TextureFunction::LOD: out << ", float lod"; break;
742 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400743 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400744 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500745 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500746 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400747 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000748 }
749
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500750 if (textureFunction->offset)
751 {
752 switch(textureFunction->sampler)
753 {
754 case EbtSampler2D: out << ", int2 offset"; break;
755 case EbtSampler3D: out << ", int3 offset"; break;
756 case EbtSampler2DArray: out << ", int2 offset"; break;
757 case EbtISampler2D: out << ", int2 offset"; break;
758 case EbtISampler3D: out << ", int3 offset"; break;
759 case EbtISampler2DArray: out << ", int2 offset"; break;
760 case EbtUSampler2D: out << ", int2 offset"; break;
761 case EbtUSampler3D: out << ", int3 offset"; break;
762 case EbtUSampler2DArray: out << ", int2 offset"; break;
763 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500764 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500765 default: UNREACHABLE();
766 }
767 }
768
Nicolas Capens84cfa122014-04-14 13:48:45 -0400769 if (textureFunction->method == TextureFunction::BIAS ||
770 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500771 {
772 out << ", float bias";
773 }
774
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400775 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400776 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400777
Nicolas Capens75fb4752013-07-10 15:14:47 -0400778 if (textureFunction->method == TextureFunction::SIZE)
779 {
780 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
781 {
782 if (IsSamplerArray(textureFunction->sampler))
783 {
784 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
785 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
786 }
787 else
788 {
789 out << " uint width; uint height; uint numberOfLevels;\n"
790 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
791 }
792 }
793 else if (IsSampler3D(textureFunction->sampler))
794 {
795 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
796 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
797 }
798 else UNREACHABLE();
799
800 switch(textureFunction->sampler)
801 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400802 case EbtSampler2D: out << " return int2(width, height);"; break;
803 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
804 case EbtSamplerCube: out << " return int2(width, height);"; break;
805 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
806 case EbtISampler2D: out << " return int2(width, height);"; break;
807 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
808 case EbtISamplerCube: out << " return int2(width, height);"; break;
809 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
810 case EbtUSampler2D: out << " return int2(width, height);"; break;
811 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
812 case EbtUSamplerCube: out << " return int2(width, height);"; break;
813 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
814 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
815 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
816 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400817 default: UNREACHABLE();
818 }
819 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400820 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400821 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500822 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
823 {
824 out << " float width; float height; float layers; float levels;\n";
825
826 out << " uint mip = 0;\n";
827
828 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
829
830 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
831 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
832 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
833 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
834
835 // FACE_POSITIVE_X = 000b
836 // FACE_NEGATIVE_X = 001b
837 // FACE_POSITIVE_Y = 010b
838 // FACE_NEGATIVE_Y = 011b
839 // FACE_POSITIVE_Z = 100b
840 // FACE_NEGATIVE_Z = 101b
841 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
842
843 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
844 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
845 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
846
847 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
848 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
849 }
850 else if (IsIntegerSampler(textureFunction->sampler) &&
851 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400852 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400853 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400854 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400855 if (IsSamplerArray(textureFunction->sampler))
856 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400857 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400858
Nicolas Capens9edebd62013-08-06 10:59:10 -0400859 if (textureFunction->method == TextureFunction::LOD0)
860 {
861 out << " uint mip = 0;\n";
862 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400863 else if (textureFunction->method == TextureFunction::LOD0BIAS)
864 {
865 out << " uint mip = bias;\n";
866 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400867 else
868 {
869 if (textureFunction->method == TextureFunction::IMPLICIT ||
870 textureFunction->method == TextureFunction::BIAS)
871 {
872 out << " x.GetDimensions(0, width, height, layers, levels);\n"
873 " float2 tSized = float2(t.x * width, t.y * height);\n"
874 " float dx = length(ddx(tSized));\n"
875 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500876 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400877
878 if (textureFunction->method == TextureFunction::BIAS)
879 {
880 out << " lod += bias;\n";
881 }
882 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500883 else if (textureFunction->method == TextureFunction::GRAD)
884 {
885 out << " x.GetDimensions(0, width, height, layers, levels);\n"
886 " float lod = log2(max(length(ddx), length(ddy)));\n";
887 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400888
889 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
890 }
891
892 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400893 }
894 else
895 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400896 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400897
Nicolas Capens9edebd62013-08-06 10:59:10 -0400898 if (textureFunction->method == TextureFunction::LOD0)
899 {
900 out << " uint mip = 0;\n";
901 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400902 else if (textureFunction->method == TextureFunction::LOD0BIAS)
903 {
904 out << " uint mip = bias;\n";
905 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400906 else
907 {
908 if (textureFunction->method == TextureFunction::IMPLICIT ||
909 textureFunction->method == TextureFunction::BIAS)
910 {
911 out << " x.GetDimensions(0, width, height, levels);\n"
912 " float2 tSized = float2(t.x * width, t.y * height);\n"
913 " float dx = length(ddx(tSized));\n"
914 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500915 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400916
917 if (textureFunction->method == TextureFunction::BIAS)
918 {
919 out << " lod += bias;\n";
920 }
921 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500922 else if (textureFunction->method == TextureFunction::LOD)
923 {
924 out << " x.GetDimensions(0, width, height, levels);\n";
925 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500926 else if (textureFunction->method == TextureFunction::GRAD)
927 {
928 out << " x.GetDimensions(0, width, height, levels);\n"
929 " float lod = log2(max(length(ddx), length(ddy)));\n";
930 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400931
932 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
933 }
934
935 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400936 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400937 }
938 else if (IsSampler3D(textureFunction->sampler))
939 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400940 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400941
Nicolas Capens9edebd62013-08-06 10:59:10 -0400942 if (textureFunction->method == TextureFunction::LOD0)
943 {
944 out << " uint mip = 0;\n";
945 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400946 else if (textureFunction->method == TextureFunction::LOD0BIAS)
947 {
948 out << " uint mip = bias;\n";
949 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400950 else
951 {
952 if (textureFunction->method == TextureFunction::IMPLICIT ||
953 textureFunction->method == TextureFunction::BIAS)
954 {
955 out << " x.GetDimensions(0, width, height, depth, levels);\n"
956 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
957 " float dx = length(ddx(tSized));\n"
958 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500959 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400960
961 if (textureFunction->method == TextureFunction::BIAS)
962 {
963 out << " lod += bias;\n";
964 }
965 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500966 else if (textureFunction->method == TextureFunction::GRAD)
967 {
968 out << " x.GetDimensions(0, width, height, depth, levels);\n"
969 " float lod = log2(max(length(ddx), length(ddy)));\n";
970 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400971
972 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
973 }
974
975 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400976 }
977 else UNREACHABLE();
978 }
979
980 out << " return ";
981
982 // HLSL intrinsic
983 if (mOutputType == SH_HLSL9_OUTPUT)
984 {
985 switch(textureFunction->sampler)
986 {
987 case EbtSampler2D: out << "tex2D"; break;
988 case EbtSamplerCube: out << "texCUBE"; break;
989 default: UNREACHABLE();
990 }
991
Nicolas Capens75fb4752013-07-10 15:14:47 -0400992 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400993 {
994 case TextureFunction::IMPLICIT: out << "(s, "; break;
995 case TextureFunction::BIAS: out << "bias(s, "; break;
996 case TextureFunction::LOD: out << "lod(s, "; break;
997 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400998 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400999 default: UNREACHABLE();
1000 }
1001 }
1002 else if (mOutputType == SH_HLSL11_OUTPUT)
1003 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001004 if (textureFunction->method == TextureFunction::GRAD)
1005 {
1006 if (IsIntegerSampler(textureFunction->sampler))
1007 {
1008 out << "x.Load(";
1009 }
1010 else if (IsShadowSampler(textureFunction->sampler))
1011 {
1012 out << "x.SampleCmpLevelZero(s, ";
1013 }
1014 else
1015 {
1016 out << "x.SampleGrad(s, ";
1017 }
1018 }
1019 else if (IsIntegerSampler(textureFunction->sampler) ||
1020 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001021 {
1022 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001023 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001024 else if (IsShadowSampler(textureFunction->sampler))
1025 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001026 switch(textureFunction->method)
1027 {
1028 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1029 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1030 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1031 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1032 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1033 default: UNREACHABLE();
1034 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001035 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001036 else
1037 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001038 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001039 {
1040 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1041 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1042 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1043 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001044 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001045 default: UNREACHABLE();
1046 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001047 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001048 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001049 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001050
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001051 // Integer sampling requires integer addresses
1052 TString addressx = "";
1053 TString addressy = "";
1054 TString addressz = "";
1055 TString close = "";
1056
Nicolas Capensfc014542014-02-18 14:47:13 -05001057 if (IsIntegerSampler(textureFunction->sampler) ||
1058 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001059 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001060 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001061 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001062 case 2: out << "int3("; break;
1063 case 3: out << "int4("; break;
1064 default: UNREACHABLE();
1065 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001066
Nicolas Capensfc014542014-02-18 14:47:13 -05001067 // Convert from normalized floating-point to integer
1068 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001069 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001070 addressx = "int(floor(width * frac((";
1071 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001072
Nicolas Capensfc014542014-02-18 14:47:13 -05001073 if (IsSamplerArray(textureFunction->sampler))
1074 {
1075 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1076 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001077 else if (IsSamplerCube(textureFunction->sampler))
1078 {
1079 addressz = "((((";
1080 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001081 else
1082 {
1083 addressz = "int(floor(depth * frac((";
1084 }
1085
1086 close = "))))";
1087 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001088 }
1089 else
1090 {
1091 switch(hlslCoords)
1092 {
1093 case 2: out << "float2("; break;
1094 case 3: out << "float3("; break;
1095 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001096 default: UNREACHABLE();
1097 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001098 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001099
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001100 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001101
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001102 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001103 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001104 switch(textureFunction->coords)
1105 {
1106 case 3: proj = " / t.z"; break;
1107 case 4: proj = " / t.w"; break;
1108 default: UNREACHABLE();
1109 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001110 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001111
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001112 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001113
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001114 if (mOutputType == SH_HLSL9_OUTPUT)
1115 {
1116 if (hlslCoords >= 3)
1117 {
1118 if (textureFunction->coords < 3)
1119 {
1120 out << ", 0";
1121 }
1122 else
1123 {
1124 out << ", t.z" + proj;
1125 }
1126 }
1127
1128 if (hlslCoords == 4)
1129 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001130 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001131 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001132 case TextureFunction::BIAS: out << ", bias"; break;
1133 case TextureFunction::LOD: out << ", lod"; break;
1134 case TextureFunction::LOD0: out << ", 0"; break;
1135 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001136 default: UNREACHABLE();
1137 }
1138 }
1139
1140 out << "));\n";
1141 }
1142 else if (mOutputType == SH_HLSL11_OUTPUT)
1143 {
1144 if (hlslCoords >= 3)
1145 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001146 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1147 {
1148 out << ", face";
1149 }
1150 else
1151 {
1152 out << ", " + addressz + ("t.z" + proj) + close;
1153 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001154 }
1155
Nicolas Capensd11d5492014-02-19 17:06:10 -05001156 if (textureFunction->method == TextureFunction::GRAD)
1157 {
1158 if (IsIntegerSampler(textureFunction->sampler))
1159 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001160 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001161 }
1162 else if (IsShadowSampler(textureFunction->sampler))
1163 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001164 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001165 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001166 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001167 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1168 // The resulting third component of P' in the shadow forms is used as Dref
1169 out << "), t.z" << proj;
1170 }
1171 else
1172 {
1173 switch(textureFunction->coords)
1174 {
1175 case 3: out << "), t.z"; break;
1176 case 4: out << "), t.w"; break;
1177 default: UNREACHABLE();
1178 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001179 }
1180 }
1181 else
1182 {
1183 out << "), ddx, ddy";
1184 }
1185 }
1186 else if (IsIntegerSampler(textureFunction->sampler) ||
1187 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001188 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001189 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001190 }
1191 else if (IsShadowSampler(textureFunction->sampler))
1192 {
1193 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001194 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001195 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001196 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1197 // The resulting third component of P' in the shadow forms is used as Dref
1198 out << "), t.z" << proj;
1199 }
1200 else
1201 {
1202 switch(textureFunction->coords)
1203 {
1204 case 3: out << "), t.z"; break;
1205 case 4: out << "), t.w"; break;
1206 default: UNREACHABLE();
1207 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001208 }
1209 }
1210 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001211 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001212 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001213 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001214 case TextureFunction::IMPLICIT: out << ")"; break;
1215 case TextureFunction::BIAS: out << "), bias"; break;
1216 case TextureFunction::LOD: out << "), lod"; break;
1217 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001218 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001219 default: UNREACHABLE();
1220 }
1221 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001222
1223 if (textureFunction->offset)
1224 {
1225 out << ", offset";
1226 }
1227
1228 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001229 }
1230 else UNREACHABLE();
1231 }
1232
1233 out << "\n"
1234 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001235 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001236 }
1237
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001238 if (mUsesFragCoord)
1239 {
1240 out << "#define GL_USES_FRAG_COORD\n";
1241 }
1242
1243 if (mUsesPointCoord)
1244 {
1245 out << "#define GL_USES_POINT_COORD\n";
1246 }
1247
1248 if (mUsesFrontFacing)
1249 {
1250 out << "#define GL_USES_FRONT_FACING\n";
1251 }
1252
1253 if (mUsesPointSize)
1254 {
1255 out << "#define GL_USES_POINT_SIZE\n";
1256 }
1257
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001258 if (mUsesFragDepth)
1259 {
1260 out << "#define GL_USES_FRAG_DEPTH\n";
1261 }
1262
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001263 if (mUsesDepthRange)
1264 {
1265 out << "#define GL_USES_DEPTH_RANGE\n";
1266 }
1267
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001268 if (mUsesXor)
1269 {
1270 out << "bool xor(bool p, bool q)\n"
1271 "{\n"
1272 " return (p || q) && !(p && q);\n"
1273 "}\n"
1274 "\n";
1275 }
1276
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001277 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001278}
1279
1280void OutputHLSL::visitSymbol(TIntermSymbol *node)
1281{
Jamie Madill32aab012015-01-27 14:12:26 -05001282 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001283
Jamie Madill570e04d2013-06-21 09:15:33 -04001284 // Handle accessing std140 structs by value
1285 if (mFlaggedStructMappedNames.count(node) > 0)
1286 {
1287 out << mFlaggedStructMappedNames[node];
1288 return;
1289 }
1290
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001291 TString name = node->getSymbol();
1292
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001293 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001294 {
1295 mUsesDepthRange = true;
1296 out << name;
1297 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001298 else
1299 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001300 TQualifier qualifier = node->getQualifier();
1301
1302 if (qualifier == EvqUniform)
1303 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001304 const TType& nodeType = node->getType();
1305 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1306
1307 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001308 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001309 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001310 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001311 else
1312 {
1313 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001314 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001315
Jamie Madill033dae62014-06-18 12:56:28 -04001316 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001317 }
Jamie Madill19571812013-08-12 15:26:34 -07001318 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001319 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001320 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001321 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001322 }
Jamie Madill033dae62014-06-18 12:56:28 -04001323 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001324 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001325 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001326 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001327 }
Jamie Madill19571812013-08-12 15:26:34 -07001328 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001329 {
1330 mReferencedOutputVariables[name] = node;
1331 out << "out_" << name;
1332 }
1333 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001334 {
1335 out << "gl_Color[0]";
1336 mUsesFragColor = true;
1337 }
1338 else if (qualifier == EvqFragData)
1339 {
1340 out << "gl_Color";
1341 mUsesFragData = true;
1342 }
1343 else if (qualifier == EvqFragCoord)
1344 {
1345 mUsesFragCoord = true;
1346 out << name;
1347 }
1348 else if (qualifier == EvqPointCoord)
1349 {
1350 mUsesPointCoord = true;
1351 out << name;
1352 }
1353 else if (qualifier == EvqFrontFacing)
1354 {
1355 mUsesFrontFacing = true;
1356 out << name;
1357 }
1358 else if (qualifier == EvqPointSize)
1359 {
1360 mUsesPointSize = true;
1361 out << name;
1362 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001363 else if (qualifier == EvqInstanceID)
1364 {
1365 mUsesInstanceID = true;
1366 out << name;
1367 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001368 else if (name == "gl_FragDepthEXT")
1369 {
1370 mUsesFragDepth = true;
1371 out << "gl_Depth";
1372 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001373 else if (qualifier == EvqInternal)
1374 {
1375 out << name;
1376 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001377 else
1378 {
Jamie Madill033dae62014-06-18 12:56:28 -04001379 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001380 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001381 }
1382}
1383
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001384void OutputHLSL::visitRaw(TIntermRaw *node)
1385{
Jamie Madill32aab012015-01-27 14:12:26 -05001386 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001387}
1388
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001389bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1390{
Jamie Madill32aab012015-01-27 14:12:26 -05001391 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001392
Jamie Madill570e04d2013-06-21 09:15:33 -04001393 // Handle accessing std140 structs by value
1394 if (mFlaggedStructMappedNames.count(node) > 0)
1395 {
1396 out << mFlaggedStructMappedNames[node];
1397 return false;
1398 }
1399
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001400 switch (node->getOp())
1401 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001402 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001403 case EOpInitialize:
1404 if (visit == PreVisit)
1405 {
1406 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1407 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1408 // new variable is created before the assignment is evaluated), so we need to convert
1409 // this to "float t = x, x = t;".
1410
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001411 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001412 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001413 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001414
Jamie Madill37997142015-01-28 10:06:34 -05001415 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1416 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001417 {
Jamie Madill37997142015-01-28 10:06:34 -05001418 // For variables which are not constant, defer their real initialization until
1419 // after we initialize other globals: uniforms, attributes and varyings.
1420 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1421 const TString &initString = initializer(node->getType());
1422 node->setRight(new TIntermRaw(node->getType(), initString));
1423 }
1424 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1425 {
1426 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001427 return false;
1428 }
1429 }
1430 else if (visit == InVisit)
1431 {
1432 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001433 }
1434 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001435 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1436 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1437 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1438 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1439 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1440 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001441 if (visit == PreVisit)
1442 {
1443 out << "(";
1444 }
1445 else if (visit == InVisit)
1446 {
1447 out << " = mul(";
1448 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001449 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001450 }
1451 else
1452 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001453 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001454 }
1455 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001456 case EOpMatrixTimesMatrixAssign:
1457 if (visit == PreVisit)
1458 {
1459 out << "(";
1460 }
1461 else if (visit == InVisit)
1462 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001463 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001464 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001465 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001466 }
1467 else
1468 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001469 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001470 }
1471 break;
1472 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001473 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001474 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1475 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1476 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1477 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1478 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001479 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001480 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001481 const TType& leftType = node->getLeft()->getType();
1482 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001483 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001484 if (visit == PreVisit)
1485 {
1486 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1487 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001488 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001489 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001490 return false;
1491 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001492 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001493 else
1494 {
1495 outputTriplet(visit, "", "[", "]");
1496 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001497 }
1498 break;
1499 case EOpIndexIndirect:
1500 // We do not currently support indirect references to interface blocks
1501 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1502 outputTriplet(visit, "", "[", "]");
1503 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001504 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001505 if (visit == InVisit)
1506 {
1507 const TStructure* structure = node->getLeft()->getType().getStruct();
1508 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1509 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001510 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001511
1512 return false;
1513 }
1514 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001515 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001516 if (visit == InVisit)
1517 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001518 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1519 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1520 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001521 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001522
1523 return false;
1524 }
1525 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001526 case EOpVectorSwizzle:
1527 if (visit == InVisit)
1528 {
1529 out << ".";
1530
1531 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1532
1533 if (swizzle)
1534 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001535 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001536
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001537 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001538 {
1539 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1540
1541 if (element)
1542 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001543 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001544
1545 switch (i)
1546 {
1547 case 0: out << "x"; break;
1548 case 1: out << "y"; break;
1549 case 2: out << "z"; break;
1550 case 3: out << "w"; break;
1551 default: UNREACHABLE();
1552 }
1553 }
1554 else UNREACHABLE();
1555 }
1556 }
1557 else UNREACHABLE();
1558
1559 return false; // Fully processed
1560 }
1561 break;
1562 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1563 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1564 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1565 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001566 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001567 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1568 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1569 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1570 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1571 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001572 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001573 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001574 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001575 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001576 if (node->getOp() == EOpEqual)
1577 {
1578 outputTriplet(visit, "(", " == ", ")");
1579 }
1580 else
1581 {
1582 outputTriplet(visit, "(", " != ", ")");
1583 }
1584 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001585 else
1586 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001587 if (visit == PreVisit && node->getOp() == EOpNotEqual)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001588 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001589 out << "!";
1590 }
1591
1592 if (node->getLeft()->getBasicType() == EbtStruct)
1593 {
1594 const TStructure &structure = *node->getLeft()->getType().getStruct();
1595 const TString &functionName = addStructEqualityFunction(structure);
Daniel Bratell29190082015-02-20 16:42:54 +01001596 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001597 }
1598 else
1599 {
Jamie Madill55e79e02015-02-09 15:35:00 -05001600 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
1601 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001602 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001603 }
1604 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001605 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1606 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1607 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1608 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1609 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001610 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001611 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1612 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001613 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001614 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001615 if (node->getRight()->hasSideEffects())
1616 {
1617 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1618 return false;
1619 }
1620 else
1621 {
1622 outputTriplet(visit, "(", " || ", ")");
1623 return true;
1624 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001625 case EOpLogicalXor:
1626 mUsesXor = true;
1627 outputTriplet(visit, "xor(", ", ", ")");
1628 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001629 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001630 if (node->getRight()->hasSideEffects())
1631 {
1632 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1633 return false;
1634 }
1635 else
1636 {
1637 outputTriplet(visit, "(", " && ", ")");
1638 return true;
1639 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001640 default: UNREACHABLE();
1641 }
1642
1643 return true;
1644}
1645
1646bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1647{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001648 switch (node->getOp())
1649 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001650 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001651 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001652 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1653 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001654 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001655 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1656 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1657 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1658 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001659 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1660 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1661 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1662 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1663 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1664 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1665 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1666 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001667 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1668 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1669 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1670 case EOpAsinh:
1671 ASSERT(node->getUseEmulatedFunction());
1672 writeEmulatedFunctionTriplet(visit, "asinh(");
1673 break;
1674 case EOpAcosh:
1675 ASSERT(node->getUseEmulatedFunction());
1676 writeEmulatedFunctionTriplet(visit, "acosh(");
1677 break;
1678 case EOpAtanh:
1679 ASSERT(node->getUseEmulatedFunction());
1680 writeEmulatedFunctionTriplet(visit, "atanh(");
1681 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001682 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1683 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1684 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1685 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1686 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1687 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1688 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1689 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1690 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001691 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1692 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1693 case EOpRoundEven:
1694 ASSERT(node->getUseEmulatedFunction());
1695 writeEmulatedFunctionTriplet(visit, "roundEven(");
1696 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001697 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1698 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301699 case EOpIsNan:
1700 outputTriplet(visit, "isnan(", "", ")");
1701 mRequiresIEEEStrictCompiling = true;
1702 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301703 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001704 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1705 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1706 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1707 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001708 case EOpPackSnorm2x16:
1709 ASSERT(node->getUseEmulatedFunction());
1710 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1711 break;
1712 case EOpPackUnorm2x16:
1713 ASSERT(node->getUseEmulatedFunction());
1714 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1715 break;
1716 case EOpPackHalf2x16:
1717 ASSERT(node->getUseEmulatedFunction());
1718 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1719 break;
1720 case EOpUnpackSnorm2x16:
1721 ASSERT(node->getUseEmulatedFunction());
1722 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1723 break;
1724 case EOpUnpackUnorm2x16:
1725 ASSERT(node->getUseEmulatedFunction());
1726 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1727 break;
1728 case EOpUnpackHalf2x16:
1729 ASSERT(node->getUseEmulatedFunction());
1730 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1731 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001732 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1733 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001734 case EOpDFdx:
1735 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1736 {
1737 outputTriplet(visit, "(", "", ", 0.0)");
1738 }
1739 else
1740 {
1741 outputTriplet(visit, "ddx(", "", ")");
1742 }
1743 break;
1744 case EOpDFdy:
1745 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1746 {
1747 outputTriplet(visit, "(", "", ", 0.0)");
1748 }
1749 else
1750 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001751 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001752 }
1753 break;
1754 case EOpFwidth:
1755 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1756 {
1757 outputTriplet(visit, "(", "", ", 0.0)");
1758 }
1759 else
1760 {
1761 outputTriplet(visit, "fwidth(", "", ")");
1762 }
1763 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001764 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1765 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001766 case EOpInverse:
1767 ASSERT(node->getUseEmulatedFunction());
1768 writeEmulatedFunctionTriplet(visit, "inverse(");
1769 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001770
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001771 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1772 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 default: UNREACHABLE();
1774 }
1775
1776 return true;
1777}
1778
1779bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1780{
Jamie Madill32aab012015-01-27 14:12:26 -05001781 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001782
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783 switch (node->getOp())
1784 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001785 case EOpSequence:
1786 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001787 if (mInsideFunction)
1788 {
Jamie Madill075edd82013-07-08 13:30:19 -04001789 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001790 out << "{\n";
1791 }
1792
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001793 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001794 {
Jamie Madill075edd82013-07-08 13:30:19 -04001795 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001796
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001797 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001798
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001799 // Don't output ; after case labels, they're terminated by :
1800 // This is needed especially since outputting a ; after a case statement would turn empty
1801 // case statements into non-empty case statements, disallowing fall-through from them.
1802 if ((*sit)->getAsCaseNode() == nullptr)
1803 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001804 }
1805
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001806 if (mInsideFunction)
1807 {
Jamie Madill075edd82013-07-08 13:30:19 -04001808 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001809 out << "}\n";
1810 }
1811
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001812 return false;
1813 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814 case EOpDeclaration:
1815 if (visit == PreVisit)
1816 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001817 TIntermSequence *sequence = node->getSequence();
1818 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001820 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001821 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001822 TStructure *structure = variable->getType().getStruct();
1823
1824 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001825 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001826 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001827 }
1828
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001829 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 {
Jamie Madill37997142015-01-28 10:06:34 -05001831 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832 {
Jamie Madill37997142015-01-28 10:06:34 -05001833 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001834 {
Jamie Madill37997142015-01-28 10:06:34 -05001835 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001836 }
1837
Nicolas Capensd974db42014-10-07 10:50:19 -04001838 if (!mInsideFunction)
1839 {
1840 out << "static ";
1841 }
1842
1843 out << TypeString(variable->getType()) + " ";
1844
Jamie Madill37997142015-01-28 10:06:34 -05001845 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001847 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001848 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001849 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001850 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001851 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001852 }
1853 else
1854 {
Jamie Madill37997142015-01-28 10:06:34 -05001855 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001856 }
1857
Jamie Madill37997142015-01-28 10:06:34 -05001858 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001859 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001860 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001861 }
1862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001863 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001864 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1865 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001866 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001867 }
1868 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869 }
Jamie Madill033dae62014-06-18 12:56:28 -04001870 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001871 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001872 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001873 {
1874 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1875
1876 if (symbol)
1877 {
1878 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1879 mReferencedVaryings[symbol->getSymbol()] = symbol;
1880 }
1881 else
1882 {
1883 (*sit)->traverse(this);
1884 }
1885 }
1886 }
1887
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888 return false;
1889 }
1890 else if (visit == InVisit)
1891 {
1892 out << ", ";
1893 }
1894 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001895 case EOpInvariantDeclaration:
1896 // Do not do any translation
1897 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001898 case EOpPrototype:
1899 if (visit == PreVisit)
1900 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001901 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001902
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001903 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001904
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001905 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001906 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001907 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001908
1909 if (symbol)
1910 {
1911 out << argumentString(symbol);
1912
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001913 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001914 {
1915 out << ", ";
1916 }
1917 }
1918 else UNREACHABLE();
1919 }
1920
1921 out << ");\n";
1922
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001923 // Also prototype the Lod0 variant if needed
1924 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1925 {
1926 mOutputLod0Function = true;
1927 node->traverse(this);
1928 mOutputLod0Function = false;
1929 }
1930
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001931 return false;
1932 }
1933 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001934 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935 case EOpFunction:
1936 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001937 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001938
Jamie Madill033dae62014-06-18 12:56:28 -04001939 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001940
1941 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001943 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001945 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946 {
Jamie Madill033dae62014-06-18 12:56:28 -04001947 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001948 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001949
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001950 TIntermSequence *sequence = node->getSequence();
1951 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001952
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001953 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001954 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001955 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001956
1957 if (symbol)
1958 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001959 TStructure *structure = symbol->getType().getStruct();
1960
1961 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001962 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001963 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001964 }
1965
1966 out << argumentString(symbol);
1967
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001968 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001969 {
1970 out << ", ";
1971 }
1972 }
1973 else UNREACHABLE();
1974 }
1975
1976 out << ")\n"
1977 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001978
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001979 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001980 {
1981 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001982 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001983 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001984 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001985
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001986 out << "}\n";
1987
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001988 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1989 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001990 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001991 {
1992 mOutputLod0Function = true;
1993 node->traverse(this);
1994 mOutputLod0Function = false;
1995 }
1996 }
1997
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001998 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 }
2000 break;
2001 case EOpFunctionCall:
2002 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002003 TString name = TFunction::unmangleName(node->getName());
2004 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002005 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002006
2007 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002008 {
Jamie Madill033dae62014-06-18 12:56:28 -04002009 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002010 }
2011 else
2012 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002013 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002014
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002015 TextureFunction textureFunction;
2016 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002017 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002018 textureFunction.method = TextureFunction::IMPLICIT;
2019 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002020 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002021
2022 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002023 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002024 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002025 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002026 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002027 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002028 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002029 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002030 }
Nicolas Capens46485082014-04-15 13:12:50 -04002031 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2032 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002033 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002034 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002035 }
Nicolas Capens46485082014-04-15 13:12:50 -04002036 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002037 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002038 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002039 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002040 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002041 else if (name == "textureSize")
2042 {
2043 textureFunction.method = TextureFunction::SIZE;
2044 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002045 else if (name == "textureOffset")
2046 {
2047 textureFunction.method = TextureFunction::IMPLICIT;
2048 textureFunction.offset = true;
2049 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002050 else if (name == "textureProjOffset")
2051 {
2052 textureFunction.method = TextureFunction::IMPLICIT;
2053 textureFunction.offset = true;
2054 textureFunction.proj = true;
2055 }
2056 else if (name == "textureLodOffset")
2057 {
2058 textureFunction.method = TextureFunction::LOD;
2059 textureFunction.offset = true;
2060 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002061 else if (name == "textureProjLodOffset")
2062 {
2063 textureFunction.method = TextureFunction::LOD;
2064 textureFunction.proj = true;
2065 textureFunction.offset = true;
2066 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002067 else if (name == "texelFetch")
2068 {
2069 textureFunction.method = TextureFunction::FETCH;
2070 }
2071 else if (name == "texelFetchOffset")
2072 {
2073 textureFunction.method = TextureFunction::FETCH;
2074 textureFunction.offset = true;
2075 }
Nicolas Capens46485082014-04-15 13:12:50 -04002076 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002077 {
2078 textureFunction.method = TextureFunction::GRAD;
2079 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002080 else if (name == "textureGradOffset")
2081 {
2082 textureFunction.method = TextureFunction::GRAD;
2083 textureFunction.offset = true;
2084 }
Nicolas Capens46485082014-04-15 13:12:50 -04002085 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002086 {
2087 textureFunction.method = TextureFunction::GRAD;
2088 textureFunction.proj = true;
2089 }
2090 else if (name == "textureProjGradOffset")
2091 {
2092 textureFunction.method = TextureFunction::GRAD;
2093 textureFunction.proj = true;
2094 textureFunction.offset = true;
2095 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002096 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002097
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002098 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002099 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002100 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2101
2102 if (textureFunction.offset)
2103 {
2104 mandatoryArgumentCount++;
2105 }
2106
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002107 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002108
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002109 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002110 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002111 if (bias)
2112 {
2113 textureFunction.method = TextureFunction::LOD0BIAS;
2114 }
2115 else
2116 {
2117 textureFunction.method = TextureFunction::LOD0;
2118 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002119 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002120 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002121 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002122 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002123 }
2124 }
2125
2126 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002127
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002128 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002129 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002130
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002131 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002132 {
2133 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2134 {
2135 out << "texture_";
2136 (*arg)->traverse(this);
2137 out << ", sampler_";
2138 }
2139
2140 (*arg)->traverse(this);
2141
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002142 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002143 {
2144 out << ", ";
2145 }
2146 }
2147
2148 out << ")";
2149
2150 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151 }
2152 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002153 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002154 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2155 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2156 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2157 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2158 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2159 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2160 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2161 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2162 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2163 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2164 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2165 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2166 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2167 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2168 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2169 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2170 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2171 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2172 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002173 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002174 {
Jamie Madill033dae62014-06-18 12:56:28 -04002175 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002176 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002177 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002178 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002179 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002180 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2181 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2182 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2183 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2184 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2185 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002186 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002187 ASSERT(node->getUseEmulatedFunction());
2188 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002189 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002190 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002191 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002193 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002194 ASSERT(node->getUseEmulatedFunction());
2195 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002196 break;
2197 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2198 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2199 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2200 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2201 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2202 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2203 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2204 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2205 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002206 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002207 ASSERT(node->getUseEmulatedFunction());
2208 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002209 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002210 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2211 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002212 case EOpOuterProduct:
2213 ASSERT(node->getUseEmulatedFunction());
2214 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2215 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002216 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002217 default: UNREACHABLE();
2218 }
2219
2220 return true;
2221}
2222
2223bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2224{
Jamie Madill32aab012015-01-27 14:12:26 -05002225 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002226
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002227 if (node->usesTernaryOperator())
2228 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002229 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002230 }
2231 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002232 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002233 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002234
Corentin Wallez80bacde2014-11-10 12:07:37 -08002235 // D3D errors when there is a gradient operation in a loop in an unflattened if
2236 // however flattening all the ifs in branch heavy shaders made D3D error too.
2237 // As a temporary workaround we flatten the ifs only if there is at least a loop
2238 // present somewhere in the shader.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002239 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002240 {
2241 out << "FLATTEN ";
2242 }
2243
2244 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002245
2246 node->getCondition()->traverse(this);
2247
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002248 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002249
Jamie Madill075edd82013-07-08 13:30:19 -04002250 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002251 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002253 bool discard = false;
2254
daniel@transgaming.combb885322010-04-15 20:45:24 +00002255 if (node->getTrueBlock())
2256 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002257 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002258
2259 // Detect true discard
2260 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002261 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262
Jamie Madill075edd82013-07-08 13:30:19 -04002263 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002264 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002265
2266 if (node->getFalseBlock())
2267 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002268 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002269
Jamie Madill075edd82013-07-08 13:30:19 -04002270 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002271 out << "{\n";
2272
Jamie Madill075edd82013-07-08 13:30:19 -04002273 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002274 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002275
Jamie Madill075edd82013-07-08 13:30:19 -04002276 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002277 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002278
2279 // Detect false discard
2280 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2281 }
2282
2283 // ANGLE issue 486: Detect problematic conditional discard
2284 if (discard && FindSideEffectRewriting::search(node))
2285 {
2286 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002287 }
2288 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289
2290 return false;
2291}
2292
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002293bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002294{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002295 if (node->getStatementList())
2296 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002297 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002298 outputTriplet(visit, "switch (", ") ", "");
2299 // The curly braces get written when visiting the statementList aggregate
2300 }
2301 else
2302 {
2303 // No statementList, so it won't output curly braces
2304 outputTriplet(visit, "switch (", ") {", "}\n");
2305 }
2306 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002307}
2308
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002309bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002310{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002311 if (node->hasCondition())
2312 {
2313 outputTriplet(visit, "case (", "", "):\n");
2314 return true;
2315 }
2316 else
2317 {
2318 TInfoSinkBase &out = getInfoSink();
2319 out << "default:\n";
2320 return false;
2321 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002322}
2323
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002324void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2325{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002326 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327}
2328
2329bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2330{
Nicolas Capens655fe362014-04-11 13:12:34 -04002331 mNestedLoopDepth++;
2332
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002333 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2334
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002335 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002336 {
2337 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2338 }
2339
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002340 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002341 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002342 if (handleExcessiveLoop(node))
2343 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002344 mInsideDiscontinuousLoop = wasDiscontinuous;
2345 mNestedLoopDepth--;
2346
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002347 return false;
2348 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002349 }
2350
Jamie Madill32aab012015-01-27 14:12:26 -05002351 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352
alokp@chromium.org52813552010-11-16 18:36:09 +00002353 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002354 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002355 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002356
Jamie Madill075edd82013-07-08 13:30:19 -04002357 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002358 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 }
2360 else
2361 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002362 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002363
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002364 if (node->getInit())
2365 {
2366 node->getInit()->traverse(this);
2367 }
2368
2369 out << "; ";
2370
alokp@chromium.org52813552010-11-16 18:36:09 +00002371 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002373 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 }
2375
2376 out << "; ";
2377
alokp@chromium.org52813552010-11-16 18:36:09 +00002378 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002380 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 }
2382
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002383 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002384
Jamie Madill075edd82013-07-08 13:30:19 -04002385 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002386 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 }
2388
2389 if (node->getBody())
2390 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002391 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392 }
2393
Jamie Madill075edd82013-07-08 13:30:19 -04002394 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002395 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396
alokp@chromium.org52813552010-11-16 18:36:09 +00002397 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398 {
Jamie Madill075edd82013-07-08 13:30:19 -04002399 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 out << "while(\n";
2401
alokp@chromium.org52813552010-11-16 18:36:09 +00002402 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403
daniel@transgaming.com73536982012-03-21 20:45:49 +00002404 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 }
2406
daniel@transgaming.com73536982012-03-21 20:45:49 +00002407 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002409 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002410 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002411
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002412 return false;
2413}
2414
2415bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2416{
Jamie Madill32aab012015-01-27 14:12:26 -05002417 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418
2419 switch (node->getFlowOp())
2420 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002421 case EOpKill:
2422 outputTriplet(visit, "discard;\n", "", "");
2423 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002424 case EOpBreak:
2425 if (visit == PreVisit)
2426 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002427 if (mNestedLoopDepth > 1)
2428 {
2429 mUsesNestedBreak = true;
2430 }
2431
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002432 if (mExcessiveLoopIndex)
2433 {
2434 out << "{Break";
2435 mExcessiveLoopIndex->traverse(this);
2436 out << " = true; break;}\n";
2437 }
2438 else
2439 {
2440 out << "break;\n";
2441 }
2442 }
2443 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002444 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445 case EOpReturn:
2446 if (visit == PreVisit)
2447 {
2448 if (node->getExpression())
2449 {
2450 out << "return ";
2451 }
2452 else
2453 {
2454 out << "return;\n";
2455 }
2456 }
2457 else if (visit == PostVisit)
2458 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002459 if (node->getExpression())
2460 {
2461 out << ";\n";
2462 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463 }
2464 break;
2465 default: UNREACHABLE();
2466 }
2467
2468 return true;
2469}
2470
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002471void OutputHLSL::traverseStatements(TIntermNode *node)
2472{
2473 if (isSingleStatement(node))
2474 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002475 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002476 }
2477
2478 node->traverse(this);
2479}
2480
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002481bool OutputHLSL::isSingleStatement(TIntermNode *node)
2482{
2483 TIntermAggregate *aggregate = node->getAsAggregate();
2484
2485 if (aggregate)
2486 {
2487 if (aggregate->getOp() == EOpSequence)
2488 {
2489 return false;
2490 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002491 else if (aggregate->getOp() == EOpDeclaration)
2492 {
2493 // Declaring multiple comma-separated variables must be considered multiple statements
2494 // because each individual declaration has side effects which are visible in the next.
2495 return false;
2496 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002497 else
2498 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002499 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002500 {
2501 if (!isSingleStatement(*sit))
2502 {
2503 return false;
2504 }
2505 }
2506
2507 return true;
2508 }
2509 }
2510
2511 return true;
2512}
2513
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002514// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2515// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002516bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2517{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002518 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002519 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002520
2521 // Parse loops of the form:
2522 // for(int index = initial; index [comparator] limit; index += increment)
2523 TIntermSymbol *index = NULL;
2524 TOperator comparator = EOpNull;
2525 int initial = 0;
2526 int limit = 0;
2527 int increment = 0;
2528
2529 // Parse index name and intial value
2530 if (node->getInit())
2531 {
2532 TIntermAggregate *init = node->getInit()->getAsAggregate();
2533
2534 if (init)
2535 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002536 TIntermSequence *sequence = init->getSequence();
2537 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002538
2539 if (variable && variable->getQualifier() == EvqTemporary)
2540 {
2541 TIntermBinary *assign = variable->getAsBinaryNode();
2542
2543 if (assign->getOp() == EOpInitialize)
2544 {
2545 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2546 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2547
2548 if (symbol && constant)
2549 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002550 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002551 {
2552 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002553 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002554 }
2555 }
2556 }
2557 }
2558 }
2559 }
2560
2561 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002562 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002563 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002564 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002565
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002566 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2567 {
2568 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2569
2570 if (constant)
2571 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002572 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002573 {
2574 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002575 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002576 }
2577 }
2578 }
2579 }
2580
2581 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002582 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002583 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002584 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2585 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002586
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002587 if (binaryTerminal)
2588 {
2589 TOperator op = binaryTerminal->getOp();
2590 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2591
2592 if (constant)
2593 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002594 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002595 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002596 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002597
2598 switch (op)
2599 {
2600 case EOpAddAssign: increment = value; break;
2601 case EOpSubAssign: increment = -value; break;
2602 default: UNIMPLEMENTED();
2603 }
2604 }
2605 }
2606 }
2607 else if (unaryTerminal)
2608 {
2609 TOperator op = unaryTerminal->getOp();
2610
2611 switch (op)
2612 {
2613 case EOpPostIncrement: increment = 1; break;
2614 case EOpPostDecrement: increment = -1; break;
2615 case EOpPreIncrement: increment = 1; break;
2616 case EOpPreDecrement: increment = -1; break;
2617 default: UNIMPLEMENTED();
2618 }
2619 }
2620 }
2621
2622 if (index != NULL && comparator != EOpNull && increment != 0)
2623 {
2624 if (comparator == EOpLessThanEqual)
2625 {
2626 comparator = EOpLessThan;
2627 limit += 1;
2628 }
2629
2630 if (comparator == EOpLessThan)
2631 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002632 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002633
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002634 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002635 {
2636 return false; // Not an excessive loop
2637 }
2638
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002639 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2640 mExcessiveLoopIndex = index;
2641
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002642 out << "{int ";
2643 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002644 out << ";\n"
2645 "bool Break";
2646 index->traverse(this);
2647 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002648
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002649 bool firstLoopFragment = true;
2650
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002651 while (iterations > 0)
2652 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002653 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002654
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002655 if (!firstLoopFragment)
2656 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002657 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002658 index->traverse(this);
2659 out << ") {\n";
2660 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002661
2662 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2663 {
2664 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2665 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002666
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002667 // for(int index = initial; index < clampedLimit; index += increment)
2668
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002669 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002670 index->traverse(this);
2671 out << " = ";
2672 out << initial;
2673
2674 out << "; ";
2675 index->traverse(this);
2676 out << " < ";
2677 out << clampedLimit;
2678
2679 out << "; ";
2680 index->traverse(this);
2681 out << " += ";
2682 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002683 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002684
Jamie Madill075edd82013-07-08 13:30:19 -04002685 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002686 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002687
2688 if (node->getBody())
2689 {
2690 node->getBody()->traverse(this);
2691 }
2692
Jamie Madill075edd82013-07-08 13:30:19 -04002693 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002694 out << ";}\n";
2695
2696 if (!firstLoopFragment)
2697 {
2698 out << "}\n";
2699 }
2700
2701 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002702
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002703 initial += MAX_LOOP_ITERATIONS * increment;
2704 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002705 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002706
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002707 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002708
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002709 mExcessiveLoopIndex = restoreIndex;
2710
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002711 return true;
2712 }
2713 else UNIMPLEMENTED();
2714 }
2715
2716 return false; // Not handled as an excessive loop
2717}
2718
Daniel Bratell29190082015-02-20 16:42:54 +01002719void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720{
Jamie Madill32aab012015-01-27 14:12:26 -05002721 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002722
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002723 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724 {
2725 out << preString;
2726 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002727 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002728 {
2729 out << inString;
2730 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002731 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002732 {
2733 out << postString;
2734 }
2735}
2736
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002737void OutputHLSL::outputLineDirective(int line)
2738{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002739 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002740 {
Jamie Madill32aab012015-01-27 14:12:26 -05002741 TInfoSinkBase &out = getInfoSink();
2742
2743 out << "\n";
2744 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002745
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002746 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002747 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002748 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002749 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002750
Jamie Madill32aab012015-01-27 14:12:26 -05002751 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002752 }
2753}
2754
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002755TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2756{
2757 TQualifier qualifier = symbol->getQualifier();
2758 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002759 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002760
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002761 if (name.empty()) // HLSL demands named arguments, also for prototypes
2762 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002763 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002764 }
2765 else
2766 {
Jamie Madill033dae62014-06-18 12:56:28 -04002767 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002768 }
2769
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002770 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2771 {
Jamie Madill033dae62014-06-18 12:56:28 -04002772 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002773 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002774 }
2775
Jamie Madill033dae62014-06-18 12:56:28 -04002776 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002777}
2778
2779TString OutputHLSL::initializer(const TType &type)
2780{
2781 TString string;
2782
Jamie Madill94bf7f22013-07-08 13:31:15 -04002783 size_t size = type.getObjectSize();
2784 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002785 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002786 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002787
Jamie Madill94bf7f22013-07-08 13:31:15 -04002788 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789 {
2790 string += ", ";
2791 }
2792 }
2793
daniel@transgaming.comead23042010-04-29 03:35:36 +00002794 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002796
Daniel Bratell29190082015-02-20 16:42:54 +01002797void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002798{
Jamie Madill32aab012015-01-27 14:12:26 -05002799 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002800
2801 if (visit == PreVisit)
2802 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002803 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002804
Daniel Bratell29190082015-02-20 16:42:54 +01002805 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002806 }
2807 else if (visit == InVisit)
2808 {
2809 out << ", ";
2810 }
2811 else if (visit == PostVisit)
2812 {
2813 out << ")";
2814 }
2815}
2816
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002817const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2818{
Jamie Madill32aab012015-01-27 14:12:26 -05002819 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002820
Jamie Madill98493dd2013-07-08 14:39:03 -04002821 const TStructure* structure = type.getStruct();
2822 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002823 {
Jamie Madill033dae62014-06-18 12:56:28 -04002824 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002825
Jamie Madill98493dd2013-07-08 14:39:03 -04002826 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002827
Jamie Madill98493dd2013-07-08 14:39:03 -04002828 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002829 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002830 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002831 constUnion = writeConstantUnion(*fieldType, constUnion);
2832
Jamie Madill98493dd2013-07-08 14:39:03 -04002833 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002834 {
2835 out << ", ";
2836 }
2837 }
2838
2839 out << ")";
2840 }
2841 else
2842 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002843 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002844 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002845
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002846 if (writeType)
2847 {
Jamie Madill033dae62014-06-18 12:56:28 -04002848 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002849 }
2850
Jamie Madill94bf7f22013-07-08 13:31:15 -04002851 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002852 {
2853 switch (constUnion->getType())
2854 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002855 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002856 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002857 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002858 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002859 default: UNREACHABLE();
2860 }
2861
2862 if (i != size - 1)
2863 {
2864 out << ", ";
2865 }
2866 }
2867
2868 if (writeType)
2869 {
2870 out << ")";
2871 }
2872 }
2873
2874 return constUnion;
2875}
2876
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002877void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2878{
2879 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2880 outputTriplet(visit, preString.c_str(), ", ", ")");
2881}
2882
Jamie Madill37997142015-01-28 10:06:34 -05002883bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2884{
2885 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2886 expression->traverse(&searchSymbol);
2887
2888 if (searchSymbol.foundMatch())
2889 {
2890 // Type already printed
2891 out << "t" + str(mUniqueIndex) + " = ";
2892 expression->traverse(this);
2893 out << ", ";
2894 symbolNode->traverse(this);
2895 out << " = t" + str(mUniqueIndex);
2896
2897 mUniqueIndex++;
2898 return true;
2899 }
2900
2901 return false;
2902}
2903
2904void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2905{
2906 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2907 << "\n"
2908 << "void initializeDeferredGlobals()\n"
2909 << "{\n";
2910
2911 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2912 {
2913 TIntermSymbol *symbol = deferredGlobal.first;
2914 TIntermTyped *expression = deferredGlobal.second;
2915 ASSERT(symbol);
2916 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2917
2918 out << " " << Decorate(symbol->getSymbol()) << " = ";
2919
2920 if (!writeSameSymbolInitializer(out, symbol, expression))
2921 {
2922 ASSERT(mInfoSinkStack.top() == &out);
2923 expression->traverse(this);
2924 }
2925
2926 out << ";\n";
2927 }
2928
2929 out << "}\n"
2930 << "\n";
2931}
2932
Jamie Madill55e79e02015-02-09 15:35:00 -05002933TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2934{
2935 const TFieldList &fields = structure.fields();
2936
2937 for (const auto &eqFunction : mStructEqualityFunctions)
2938 {
2939 if (eqFunction.structure == &structure)
2940 {
2941 return eqFunction.functionName;
2942 }
2943 }
2944
2945 const TString &structNameString = StructNameString(structure);
2946
2947 StructEqualityFunction function;
2948 function.structure = &structure;
2949 function.functionName = "angle_eq_" + structNameString;
2950
2951 TString &func = function.functionDefinition;
2952
2953 func = "bool " + function.functionName + "(" + structNameString + " a, " + structNameString + " b)\n" +
2954 "{\n"
2955 " return ";
2956
2957 for (size_t i = 0; i < fields.size(); i++)
2958 {
2959 const TField *field = fields[i];
2960 const TType *fieldType = field->type();
2961
2962 const TString &fieldNameA = "a." + Decorate(field->name());
2963 const TString &fieldNameB = "b." + Decorate(field->name());
2964
2965 if (i > 0)
2966 {
2967 func += " && ";
2968 }
2969
2970 if (fieldType->getBasicType() == EbtStruct)
2971 {
2972 const TStructure &fieldStruct = *fieldType->getStruct();
2973 const TString &functionName = addStructEqualityFunction(fieldStruct);
2974 func += functionName + "(" + fieldNameA + ", " + fieldNameB + ")";
2975 }
2976 else if (fieldType->isScalar())
2977 {
2978 func += "(" + fieldNameA + " == " + fieldNameB + ")";
2979 }
2980 else
2981 {
2982 ASSERT(fieldType->isMatrix() || fieldType->isVector());
2983 func += "all(" + fieldNameA + " == " + fieldNameB + ")";
2984 }
2985 }
2986
2987 func = func + ";\n" + "}\n";
2988
2989 mStructEqualityFunctions.push_back(function);
2990
2991 return function.functionName;
2992}
2993
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002994}