blob: 80782e2d817452bd333936ebb5aa88f0c099abe0 [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"
Corentin Wallez938f0022015-04-08 19:35:40 +000017#include "compiler/translator/DetectDiscontinuity.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/FlagStd140Structs.h"
19#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/RewriteElseBlocks.h"
23#include "compiler/translator/SearchSymbol.h"
24#include "compiler/translator/StructureHLSL.h"
25#include "compiler/translator/TranslatorHLSL.h"
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),
Corentin Wallez938f0022015-04-08 19:35:40 +0000112 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
Corentin Wallez938f0022015-04-08 19:35:40 +0000133 mContainsLoopDiscontinuity = false;
134 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000135 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000136 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400137 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000138
139 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000140
Jamie Madill8daaba12014-06-13 10:04:33 -0400141 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200142 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400143
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000144 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000145 {
Arun Patole63419392015-03-13 11:51:07 +0530146 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
147 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
148 // In both cases total 3 uniform registers need to be reserved.
149 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000150 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000151
Jamie Madillf91ce812014-06-13 10:04:34 -0400152 // Reserve registers for the default uniform block and driver constants
153 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154}
155
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000156OutputHLSL::~OutputHLSL()
157{
Jamie Madill8daaba12014-06-13 10:04:33 -0400158 SafeDelete(mUnfoldShortCircuit);
159 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400160 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200161 for (auto &eqFunction : mStructEqualityFunctions)
162 {
163 SafeDelete(eqFunction);
164 }
165 for (auto &eqFunction : mArrayEqualityFunctions)
166 {
167 SafeDelete(eqFunction);
168 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000169}
170
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200171void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000172{
Corentin Wallez938f0022015-04-08 19:35:40 +0000173 mContainsLoopDiscontinuity = mShaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(treeRoot);
174 mContainsAnyLoop = containsAnyLoop(treeRoot);
175
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200176 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400177 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000178
Jamie Madille53c98b2014-02-03 11:57:13 -0500179 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
180 // use a vertex attribute as a condition, and some related computation in the else block.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200181 if (mOutputType == SH_HLSL9_OUTPUT && mShaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500182 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200183 RewriteElseBlocks(treeRoot);
Jamie Madille53c98b2014-02-03 11:57:13 -0500184 }
185
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200186 BuiltInFunctionEmulator builtInFunctionEmulator;
187 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200188 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500189
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700190 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez938f0022015-04-08 19:35:40 +0000191 {
192 CallDAG dag;
193 CallDAG::InitResult success = dag.init(treeRoot, &objSink);
194 ASSERT(success == CallDAG::INITDAG_SUCCESS);
195 mASTAnalyses = CreateASTMetadataHLSL(treeRoot, dag);
196 }
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700197
Jamie Madill37997142015-01-28 10:06:34 -0500198 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500199 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200200 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500201 mInfoSinkStack.pop();
202
Jamie Madill37997142015-01-28 10:06:34 -0500203 mInfoSinkStack.push(&mFooter);
204 if (!mDeferredGlobalInitializers.empty())
205 {
206 writeDeferredGlobalInitializers(mFooter);
207 }
208 mInfoSinkStack.pop();
209
Jamie Madill32aab012015-01-27 14:12:26 -0500210 mInfoSinkStack.push(&mHeader);
Olli Etuahoe17e3192015-01-02 12:47:59 +0200211 header(&builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500212 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000213
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200214 objSink << mHeader.c_str();
215 objSink << mBody.c_str();
216 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200217
218 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000219}
220
Jamie Madill570e04d2013-06-21 09:15:33 -0400221void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
222{
223 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
224 {
225 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
226
Jamie Madill32aab012015-01-27 14:12:26 -0500227 TInfoSinkBase structInfoSink;
228 mInfoSinkStack.push(&structInfoSink);
229
Jamie Madill570e04d2013-06-21 09:15:33 -0400230 // This will mark the necessary block elements as referenced
231 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500232
233 TString structName(structInfoSink.c_str());
234 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400235
236 mFlaggedStructOriginalNames[flaggedNode] = structName;
237
238 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
239 {
240 structName.erase(pos, 1);
241 }
242
243 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
244 }
245}
246
Jamie Madill4e1fd412014-07-10 17:50:10 -0400247const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
248{
249 return mUniformHLSL->getInterfaceBlockRegisterMap();
250}
251
Jamie Madill9fe25e92014-07-18 10:33:08 -0400252const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
253{
254 return mUniformHLSL->getUniformRegisterMap();
255}
256
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000257int OutputHLSL::vectorSize(const TType &type) const
258{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000259 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000260 int arraySize = type.isArray() ? type.getArraySize() : 1;
261
262 return elementSize * arraySize;
263}
264
Jamie Madill98493dd2013-07-08 14:39:03 -0400265TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400266{
267 TString init;
268
269 TString preIndentString;
270 TString fullIndentString;
271
272 for (int spaces = 0; spaces < (indent * 4); spaces++)
273 {
274 preIndentString += ' ';
275 }
276
277 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
278 {
279 fullIndentString += ' ';
280 }
281
282 init += preIndentString + "{\n";
283
Jamie Madill98493dd2013-07-08 14:39:03 -0400284 const TFieldList &fields = structure.fields();
285 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400287 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400288 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400289 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400290
Jamie Madill98493dd2013-07-08 14:39:03 -0400291 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400292 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400293 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400294 }
295 else
296 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400297 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400298 }
299 }
300
301 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
302
303 return init;
304}
305
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200306void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307{
Jamie Madill32aab012015-01-27 14:12:26 -0500308 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000310 TString varyings;
311 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400312 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000313
Jamie Madill829f59e2013-11-13 19:40:54 -0500314 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400315 {
316 TIntermTyped *structNode = flaggedStructIt->first;
317 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400318 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400319 const TString &originalName = mFlaggedStructOriginalNames[structNode];
320
Jamie Madill033dae62014-06-18 12:56:28 -0400321 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400322 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400323 flaggedStructs += "\n";
324 }
325
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000326 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
327 {
328 const TType &type = varying->second->getType();
329 const TString &name = varying->second->getSymbol();
330
331 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400332 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
333 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000334 }
335
336 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
337 {
338 const TType &type = attribute->second->getType();
339 const TString &name = attribute->second->getSymbol();
340
Jamie Madill033dae62014-06-18 12:56:28 -0400341 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000342 }
343
Jamie Madill8daaba12014-06-13 10:04:33 -0400344 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400345
Jamie Madillf91ce812014-06-13 10:04:34 -0400346 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
347 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
348
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200349 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500350 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200351 out << "\n// Equality functions\n\n";
352 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500353 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200354 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200355 }
356 }
Olli Etuaho12690762015-03-31 12:55:28 +0300357 if (!mArrayAssignmentFunctions.empty())
358 {
359 out << "\n// Assignment functions\n\n";
360 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
361 {
362 out << assignmentFunction.functionDefinition << "\n";
363 }
364 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300365 if (!mArrayConstructIntoFunctions.empty())
366 {
367 out << "\n// Array constructor functions\n\n";
368 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
369 {
370 out << constructIntoFunction.functionDefinition << "\n";
371 }
372 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200373
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500374 if (mUsesDiscardRewriting)
375 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400376 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500377 }
378
Nicolas Capens655fe362014-04-11 13:12:34 -0400379 if (mUsesNestedBreak)
380 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400381 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400382 }
383
Arun Patole44efa0b2015-03-04 17:11:05 +0530384 if (mRequiresIEEEStrictCompiling)
385 {
386 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
387 }
388
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400389 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
390 "#define LOOP [loop]\n"
391 "#define FLATTEN [flatten]\n"
392 "#else\n"
393 "#define LOOP\n"
394 "#define FLATTEN\n"
395 "#endif\n";
396
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200397 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200399 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
400 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000401
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000402 out << "// Varyings\n";
403 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400404 out << "\n";
405
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200406 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000407 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500408 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000409 {
Jamie Madill46131a32013-06-20 11:55:50 -0400410 const TString &variableName = outputVariableIt->first;
411 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400412
Jamie Madill033dae62014-06-18 12:56:28 -0400413 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400414 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000415 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000416 }
Jamie Madill46131a32013-06-20 11:55:50 -0400417 else
418 {
419 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
420
421 out << "static float4 gl_Color[" << numColorValues << "] =\n"
422 "{\n";
423 for (unsigned int i = 0; i < numColorValues; i++)
424 {
425 out << " float4(0, 0, 0, 0)";
426 if (i + 1 != numColorValues)
427 {
428 out << ",";
429 }
430 out << "\n";
431 }
432
433 out << "};\n";
434 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000435
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400436 if (mUsesFragDepth)
437 {
438 out << "static float gl_Depth = 0.0;\n";
439 }
440
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000441 if (mUsesFragCoord)
442 {
443 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
444 }
445
446 if (mUsesPointCoord)
447 {
448 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
449 }
450
451 if (mUsesFrontFacing)
452 {
453 out << "static bool gl_FrontFacing = false;\n";
454 }
455
456 out << "\n";
457
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000458 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000459 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000460 out << "struct gl_DepthRangeParameters\n"
461 "{\n"
462 " float near;\n"
463 " float far;\n"
464 " float diff;\n"
465 "};\n"
466 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000467 }
468
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000469 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000470 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000471 out << "cbuffer DriverConstants : register(b1)\n"
472 "{\n";
473
474 if (mUsesDepthRange)
475 {
476 out << " float3 dx_DepthRange : packoffset(c0);\n";
477 }
478
479 if (mUsesFragCoord)
480 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000481 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000482 }
483
484 if (mUsesFragCoord || mUsesFrontFacing)
485 {
486 out << " float3 dx_DepthFront : packoffset(c2);\n";
487 }
488
489 out << "};\n";
490 }
491 else
492 {
493 if (mUsesDepthRange)
494 {
495 out << "uniform float3 dx_DepthRange : register(c0);";
496 }
497
498 if (mUsesFragCoord)
499 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000500 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000501 }
502
503 if (mUsesFragCoord || mUsesFrontFacing)
504 {
505 out << "uniform float3 dx_DepthFront : register(c2);\n";
506 }
507 }
508
509 out << "\n";
510
511 if (mUsesDepthRange)
512 {
513 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
514 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000515 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000516
Jamie Madillf91ce812014-06-13 10:04:34 -0400517 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000518 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400519 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000520 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400521 out << flaggedStructs;
522 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000523 }
524
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000525 if (usingMRTExtension && mNumRenderTargets > 1)
526 {
527 out << "#define GL_USES_MRT\n";
528 }
529
530 if (mUsesFragColor)
531 {
532 out << "#define GL_USES_FRAG_COLOR\n";
533 }
534
535 if (mUsesFragData)
536 {
537 out << "#define GL_USES_FRAG_DATA\n";
538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000540 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000541 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000542 out << "// Attributes\n";
543 out << attributes;
544 out << "\n"
545 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400546
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000547 if (mUsesPointSize)
548 {
549 out << "static float gl_PointSize = float(1);\n";
550 }
551
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000552 if (mUsesInstanceID)
553 {
554 out << "static int gl_InstanceID;";
555 }
556
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000557 out << "\n"
558 "// Varyings\n";
559 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000560 out << "\n";
561
562 if (mUsesDepthRange)
563 {
564 out << "struct gl_DepthRangeParameters\n"
565 "{\n"
566 " float near;\n"
567 " float far;\n"
568 " float diff;\n"
569 "};\n"
570 "\n";
571 }
572
573 if (mOutputType == SH_HLSL11_OUTPUT)
574 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800575 out << "cbuffer DriverConstants : register(b1)\n"
576 "{\n";
577
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000578 if (mUsesDepthRange)
579 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800580 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000581 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800582
Cooper Partine6664f02015-01-09 16:22:24 -0800583 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800584 // However, we declare it for all shaders (including Feature Level 10+).
585 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
586 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800587 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800588
589 out << "};\n"
590 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000591 }
592 else
593 {
594 if (mUsesDepthRange)
595 {
596 out << "uniform float3 dx_DepthRange : register(c0);\n";
597 }
598
Cooper Partine6664f02015-01-09 16:22:24 -0800599 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
600 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000601 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000602 }
603
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000604 if (mUsesDepthRange)
605 {
606 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
607 "\n";
608 }
609
Jamie Madillf91ce812014-06-13 10:04:34 -0400610 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000611 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400612 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000613 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400614 out << flaggedStructs;
615 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000616 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400617 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000618
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400619 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
620 {
621 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400622 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000623 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400624 switch(textureFunction->sampler)
625 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400626 case EbtSampler2D: out << "int2 "; break;
627 case EbtSampler3D: out << "int3 "; break;
628 case EbtSamplerCube: out << "int2 "; break;
629 case EbtSampler2DArray: out << "int3 "; break;
630 case EbtISampler2D: out << "int2 "; break;
631 case EbtISampler3D: out << "int3 "; break;
632 case EbtISamplerCube: out << "int2 "; break;
633 case EbtISampler2DArray: out << "int3 "; break;
634 case EbtUSampler2D: out << "int2 "; break;
635 case EbtUSampler3D: out << "int3 "; break;
636 case EbtUSamplerCube: out << "int2 "; break;
637 case EbtUSampler2DArray: out << "int3 "; break;
638 case EbtSampler2DShadow: out << "int2 "; break;
639 case EbtSamplerCubeShadow: out << "int2 "; break;
640 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400641 default: UNREACHABLE();
642 }
643 }
644 else // Sampling function
645 {
646 switch(textureFunction->sampler)
647 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400648 case EbtSampler2D: out << "float4 "; break;
649 case EbtSampler3D: out << "float4 "; break;
650 case EbtSamplerCube: out << "float4 "; break;
651 case EbtSampler2DArray: out << "float4 "; break;
652 case EbtISampler2D: out << "int4 "; break;
653 case EbtISampler3D: out << "int4 "; break;
654 case EbtISamplerCube: out << "int4 "; break;
655 case EbtISampler2DArray: out << "int4 "; break;
656 case EbtUSampler2D: out << "uint4 "; break;
657 case EbtUSampler3D: out << "uint4 "; break;
658 case EbtUSamplerCube: out << "uint4 "; break;
659 case EbtUSampler2DArray: out << "uint4 "; break;
660 case EbtSampler2DShadow: out << "float "; break;
661 case EbtSamplerCubeShadow: out << "float "; break;
662 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400663 default: UNREACHABLE();
664 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000665 }
666
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400667 // Function name
668 out << textureFunction->name();
669
670 // Argument list
671 int hlslCoords = 4;
672
673 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000674 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400675 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000676 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400677 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
678 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
679 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000680 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400681
Nicolas Capens75fb4752013-07-10 15:14:47 -0400682 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000683 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400684 case TextureFunction::IMPLICIT: break;
685 case TextureFunction::BIAS: hlslCoords = 4; break;
686 case TextureFunction::LOD: hlslCoords = 4; break;
687 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400688 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400689 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000690 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400691 }
692 else if (mOutputType == SH_HLSL11_OUTPUT)
693 {
694 switch(textureFunction->sampler)
695 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400696 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
697 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
698 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
699 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
700 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
701 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500702 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400703 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
704 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
705 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500706 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400707 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
708 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
709 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
710 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400711 default: UNREACHABLE();
712 }
713 }
714 else UNREACHABLE();
715
Nicolas Capensfc014542014-02-18 14:47:13 -0500716 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400717 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500718 switch(textureFunction->coords)
719 {
720 case 2: out << ", int2 t"; break;
721 case 3: out << ", int3 t"; break;
722 default: UNREACHABLE();
723 }
724 }
725 else // Floating-point coordinates (except textureSize)
726 {
727 switch(textureFunction->coords)
728 {
729 case 1: out << ", int lod"; break; // textureSize()
730 case 2: out << ", float2 t"; break;
731 case 3: out << ", float3 t"; break;
732 case 4: out << ", float4 t"; break;
733 default: UNREACHABLE();
734 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000735 }
736
Nicolas Capensd11d5492014-02-19 17:06:10 -0500737 if (textureFunction->method == TextureFunction::GRAD)
738 {
739 switch(textureFunction->sampler)
740 {
741 case EbtSampler2D:
742 case EbtISampler2D:
743 case EbtUSampler2D:
744 case EbtSampler2DArray:
745 case EbtISampler2DArray:
746 case EbtUSampler2DArray:
747 case EbtSampler2DShadow:
748 case EbtSampler2DArrayShadow:
749 out << ", float2 ddx, float2 ddy";
750 break;
751 case EbtSampler3D:
752 case EbtISampler3D:
753 case EbtUSampler3D:
754 case EbtSamplerCube:
755 case EbtISamplerCube:
756 case EbtUSamplerCube:
757 case EbtSamplerCubeShadow:
758 out << ", float3 ddx, float3 ddy";
759 break;
760 default: UNREACHABLE();
761 }
762 }
763
Nicolas Capens75fb4752013-07-10 15:14:47 -0400764 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000765 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400766 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400767 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400768 case TextureFunction::LOD: out << ", float lod"; break;
769 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400770 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400771 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500772 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500773 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400774 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000775 }
776
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500777 if (textureFunction->offset)
778 {
779 switch(textureFunction->sampler)
780 {
781 case EbtSampler2D: out << ", int2 offset"; break;
782 case EbtSampler3D: out << ", int3 offset"; break;
783 case EbtSampler2DArray: out << ", int2 offset"; break;
784 case EbtISampler2D: out << ", int2 offset"; break;
785 case EbtISampler3D: out << ", int3 offset"; break;
786 case EbtISampler2DArray: out << ", int2 offset"; break;
787 case EbtUSampler2D: out << ", int2 offset"; break;
788 case EbtUSampler3D: out << ", int3 offset"; break;
789 case EbtUSampler2DArray: out << ", int2 offset"; break;
790 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500791 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500792 default: UNREACHABLE();
793 }
794 }
795
Nicolas Capens84cfa122014-04-14 13:48:45 -0400796 if (textureFunction->method == TextureFunction::BIAS ||
797 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500798 {
799 out << ", float bias";
800 }
801
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400802 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400803 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400804
Nicolas Capens75fb4752013-07-10 15:14:47 -0400805 if (textureFunction->method == TextureFunction::SIZE)
806 {
807 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
808 {
809 if (IsSamplerArray(textureFunction->sampler))
810 {
811 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
812 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
813 }
814 else
815 {
816 out << " uint width; uint height; uint numberOfLevels;\n"
817 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
818 }
819 }
820 else if (IsSampler3D(textureFunction->sampler))
821 {
822 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
823 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
824 }
825 else UNREACHABLE();
826
827 switch(textureFunction->sampler)
828 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400829 case EbtSampler2D: out << " return int2(width, height);"; break;
830 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
831 case EbtSamplerCube: out << " return int2(width, height);"; break;
832 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
833 case EbtISampler2D: out << " return int2(width, height);"; break;
834 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
835 case EbtISamplerCube: out << " return int2(width, height);"; break;
836 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
837 case EbtUSampler2D: out << " return int2(width, height);"; break;
838 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
839 case EbtUSamplerCube: out << " return int2(width, height);"; break;
840 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
841 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
842 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
843 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400844 default: UNREACHABLE();
845 }
846 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400847 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400848 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500849 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
850 {
851 out << " float width; float height; float layers; float levels;\n";
852
853 out << " uint mip = 0;\n";
854
855 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
856
857 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
858 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
859 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
860 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
861
862 // FACE_POSITIVE_X = 000b
863 // FACE_NEGATIVE_X = 001b
864 // FACE_POSITIVE_Y = 010b
865 // FACE_NEGATIVE_Y = 011b
866 // FACE_POSITIVE_Z = 100b
867 // FACE_NEGATIVE_Z = 101b
868 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
869
870 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
871 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
872 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
873
874 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
875 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
876 }
877 else if (IsIntegerSampler(textureFunction->sampler) &&
878 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400879 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400880 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400881 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400882 if (IsSamplerArray(textureFunction->sampler))
883 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400884 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400885
Nicolas Capens9edebd62013-08-06 10:59:10 -0400886 if (textureFunction->method == TextureFunction::LOD0)
887 {
888 out << " uint mip = 0;\n";
889 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400890 else if (textureFunction->method == TextureFunction::LOD0BIAS)
891 {
892 out << " uint mip = bias;\n";
893 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400894 else
895 {
896 if (textureFunction->method == TextureFunction::IMPLICIT ||
897 textureFunction->method == TextureFunction::BIAS)
898 {
899 out << " x.GetDimensions(0, width, height, layers, levels);\n"
900 " float2 tSized = float2(t.x * width, t.y * height);\n"
901 " float dx = length(ddx(tSized));\n"
902 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500903 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400904
905 if (textureFunction->method == TextureFunction::BIAS)
906 {
907 out << " lod += bias;\n";
908 }
909 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500910 else if (textureFunction->method == TextureFunction::GRAD)
911 {
912 out << " x.GetDimensions(0, width, height, layers, levels);\n"
913 " float lod = log2(max(length(ddx), length(ddy)));\n";
914 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400915
916 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
917 }
918
919 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400920 }
921 else
922 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400923 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400924
Nicolas Capens9edebd62013-08-06 10:59:10 -0400925 if (textureFunction->method == TextureFunction::LOD0)
926 {
927 out << " uint mip = 0;\n";
928 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400929 else if (textureFunction->method == TextureFunction::LOD0BIAS)
930 {
931 out << " uint mip = bias;\n";
932 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400933 else
934 {
935 if (textureFunction->method == TextureFunction::IMPLICIT ||
936 textureFunction->method == TextureFunction::BIAS)
937 {
938 out << " x.GetDimensions(0, width, height, levels);\n"
939 " float2 tSized = float2(t.x * width, t.y * height);\n"
940 " float dx = length(ddx(tSized));\n"
941 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500942 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400943
944 if (textureFunction->method == TextureFunction::BIAS)
945 {
946 out << " lod += bias;\n";
947 }
948 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500949 else if (textureFunction->method == TextureFunction::LOD)
950 {
951 out << " x.GetDimensions(0, width, height, levels);\n";
952 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500953 else if (textureFunction->method == TextureFunction::GRAD)
954 {
955 out << " x.GetDimensions(0, width, height, levels);\n"
956 " float lod = log2(max(length(ddx), length(ddy)));\n";
957 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400958
959 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
960 }
961
962 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400963 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400964 }
965 else if (IsSampler3D(textureFunction->sampler))
966 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400967 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400968
Nicolas Capens9edebd62013-08-06 10:59:10 -0400969 if (textureFunction->method == TextureFunction::LOD0)
970 {
971 out << " uint mip = 0;\n";
972 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400973 else if (textureFunction->method == TextureFunction::LOD0BIAS)
974 {
975 out << " uint mip = bias;\n";
976 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400977 else
978 {
979 if (textureFunction->method == TextureFunction::IMPLICIT ||
980 textureFunction->method == TextureFunction::BIAS)
981 {
982 out << " x.GetDimensions(0, width, height, depth, levels);\n"
983 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
984 " float dx = length(ddx(tSized));\n"
985 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500986 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400987
988 if (textureFunction->method == TextureFunction::BIAS)
989 {
990 out << " lod += bias;\n";
991 }
992 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500993 else if (textureFunction->method == TextureFunction::GRAD)
994 {
995 out << " x.GetDimensions(0, width, height, depth, levels);\n"
996 " float lod = log2(max(length(ddx), length(ddy)));\n";
997 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400998
999 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1000 }
1001
1002 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001003 }
1004 else UNREACHABLE();
1005 }
1006
1007 out << " return ";
1008
1009 // HLSL intrinsic
1010 if (mOutputType == SH_HLSL9_OUTPUT)
1011 {
1012 switch(textureFunction->sampler)
1013 {
1014 case EbtSampler2D: out << "tex2D"; break;
1015 case EbtSamplerCube: out << "texCUBE"; break;
1016 default: UNREACHABLE();
1017 }
1018
Nicolas Capens75fb4752013-07-10 15:14:47 -04001019 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001020 {
1021 case TextureFunction::IMPLICIT: out << "(s, "; break;
1022 case TextureFunction::BIAS: out << "bias(s, "; break;
1023 case TextureFunction::LOD: out << "lod(s, "; break;
1024 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001025 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001026 default: UNREACHABLE();
1027 }
1028 }
1029 else if (mOutputType == SH_HLSL11_OUTPUT)
1030 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001031 if (textureFunction->method == TextureFunction::GRAD)
1032 {
1033 if (IsIntegerSampler(textureFunction->sampler))
1034 {
1035 out << "x.Load(";
1036 }
1037 else if (IsShadowSampler(textureFunction->sampler))
1038 {
1039 out << "x.SampleCmpLevelZero(s, ";
1040 }
1041 else
1042 {
1043 out << "x.SampleGrad(s, ";
1044 }
1045 }
1046 else if (IsIntegerSampler(textureFunction->sampler) ||
1047 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001048 {
1049 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001050 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001051 else if (IsShadowSampler(textureFunction->sampler))
1052 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001053 switch(textureFunction->method)
1054 {
1055 case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break;
1056 case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break;
1057 case TextureFunction::LOD: out << "x.SampleCmp(s, "; break;
1058 case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break;
1059 case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break;
1060 default: UNREACHABLE();
1061 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001062 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001063 else
1064 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001065 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001066 {
1067 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1068 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1069 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1070 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001071 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001072 default: UNREACHABLE();
1073 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001074 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001075 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001076 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001077
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001078 // Integer sampling requires integer addresses
1079 TString addressx = "";
1080 TString addressy = "";
1081 TString addressz = "";
1082 TString close = "";
1083
Nicolas Capensfc014542014-02-18 14:47:13 -05001084 if (IsIntegerSampler(textureFunction->sampler) ||
1085 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001086 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001087 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001088 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001089 case 2: out << "int3("; break;
1090 case 3: out << "int4("; break;
1091 default: UNREACHABLE();
1092 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001093
Nicolas Capensfc014542014-02-18 14:47:13 -05001094 // Convert from normalized floating-point to integer
1095 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001096 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001097 addressx = "int(floor(width * frac((";
1098 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001099
Nicolas Capensfc014542014-02-18 14:47:13 -05001100 if (IsSamplerArray(textureFunction->sampler))
1101 {
1102 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1103 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001104 else if (IsSamplerCube(textureFunction->sampler))
1105 {
1106 addressz = "((((";
1107 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001108 else
1109 {
1110 addressz = "int(floor(depth * frac((";
1111 }
1112
1113 close = "))))";
1114 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001115 }
1116 else
1117 {
1118 switch(hlslCoords)
1119 {
1120 case 2: out << "float2("; break;
1121 case 3: out << "float3("; break;
1122 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001123 default: UNREACHABLE();
1124 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001125 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001126
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001127 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001128
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001129 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001130 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001131 switch(textureFunction->coords)
1132 {
1133 case 3: proj = " / t.z"; break;
1134 case 4: proj = " / t.w"; break;
1135 default: UNREACHABLE();
1136 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001137 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001138
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001139 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001140
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001141 if (mOutputType == SH_HLSL9_OUTPUT)
1142 {
1143 if (hlslCoords >= 3)
1144 {
1145 if (textureFunction->coords < 3)
1146 {
1147 out << ", 0";
1148 }
1149 else
1150 {
1151 out << ", t.z" + proj;
1152 }
1153 }
1154
1155 if (hlslCoords == 4)
1156 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001157 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001158 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001159 case TextureFunction::BIAS: out << ", bias"; break;
1160 case TextureFunction::LOD: out << ", lod"; break;
1161 case TextureFunction::LOD0: out << ", 0"; break;
1162 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001163 default: UNREACHABLE();
1164 }
1165 }
1166
1167 out << "));\n";
1168 }
1169 else if (mOutputType == SH_HLSL11_OUTPUT)
1170 {
1171 if (hlslCoords >= 3)
1172 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001173 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1174 {
1175 out << ", face";
1176 }
1177 else
1178 {
1179 out << ", " + addressz + ("t.z" + proj) + close;
1180 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001181 }
1182
Nicolas Capensd11d5492014-02-19 17:06:10 -05001183 if (textureFunction->method == TextureFunction::GRAD)
1184 {
1185 if (IsIntegerSampler(textureFunction->sampler))
1186 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001187 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001188 }
1189 else if (IsShadowSampler(textureFunction->sampler))
1190 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001191 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001192 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001193 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001194 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1195 // The resulting third component of P' in the shadow forms is used as Dref
1196 out << "), t.z" << proj;
1197 }
1198 else
1199 {
1200 switch(textureFunction->coords)
1201 {
1202 case 3: out << "), t.z"; break;
1203 case 4: out << "), t.w"; break;
1204 default: UNREACHABLE();
1205 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001206 }
1207 }
1208 else
1209 {
1210 out << "), ddx, ddy";
1211 }
1212 }
1213 else if (IsIntegerSampler(textureFunction->sampler) ||
1214 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001215 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001216 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001217 }
1218 else if (IsShadowSampler(textureFunction->sampler))
1219 {
1220 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001221 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001222 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001223 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1224 // The resulting third component of P' in the shadow forms is used as Dref
1225 out << "), t.z" << proj;
1226 }
1227 else
1228 {
1229 switch(textureFunction->coords)
1230 {
1231 case 3: out << "), t.z"; break;
1232 case 4: out << "), t.w"; break;
1233 default: UNREACHABLE();
1234 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001235 }
1236 }
1237 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001238 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001239 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001240 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001241 case TextureFunction::IMPLICIT: out << ")"; break;
1242 case TextureFunction::BIAS: out << "), bias"; break;
1243 case TextureFunction::LOD: out << "), lod"; break;
1244 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001245 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001246 default: UNREACHABLE();
1247 }
1248 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001249
1250 if (textureFunction->offset)
1251 {
1252 out << ", offset";
1253 }
1254
1255 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001256 }
1257 else UNREACHABLE();
1258 }
1259
1260 out << "\n"
1261 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001262 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001263 }
1264
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001265 if (mUsesFragCoord)
1266 {
1267 out << "#define GL_USES_FRAG_COORD\n";
1268 }
1269
1270 if (mUsesPointCoord)
1271 {
1272 out << "#define GL_USES_POINT_COORD\n";
1273 }
1274
1275 if (mUsesFrontFacing)
1276 {
1277 out << "#define GL_USES_FRONT_FACING\n";
1278 }
1279
1280 if (mUsesPointSize)
1281 {
1282 out << "#define GL_USES_POINT_SIZE\n";
1283 }
1284
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001285 if (mUsesFragDepth)
1286 {
1287 out << "#define GL_USES_FRAG_DEPTH\n";
1288 }
1289
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001290 if (mUsesDepthRange)
1291 {
1292 out << "#define GL_USES_DEPTH_RANGE\n";
1293 }
1294
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001295 if (mUsesXor)
1296 {
1297 out << "bool xor(bool p, bool q)\n"
1298 "{\n"
1299 " return (p || q) && !(p && q);\n"
1300 "}\n"
1301 "\n";
1302 }
1303
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001304 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001305}
1306
1307void OutputHLSL::visitSymbol(TIntermSymbol *node)
1308{
Jamie Madill32aab012015-01-27 14:12:26 -05001309 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001310
Jamie Madill570e04d2013-06-21 09:15:33 -04001311 // Handle accessing std140 structs by value
1312 if (mFlaggedStructMappedNames.count(node) > 0)
1313 {
1314 out << mFlaggedStructMappedNames[node];
1315 return;
1316 }
1317
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001318 TString name = node->getSymbol();
1319
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001320 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001321 {
1322 mUsesDepthRange = true;
1323 out << name;
1324 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001325 else
1326 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001327 TQualifier qualifier = node->getQualifier();
1328
1329 if (qualifier == EvqUniform)
1330 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001331 const TType& nodeType = node->getType();
1332 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1333
1334 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001335 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001336 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001337 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001338 else
1339 {
1340 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001341 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001342
Jamie Madill033dae62014-06-18 12:56:28 -04001343 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001344 }
Jamie Madill19571812013-08-12 15:26:34 -07001345 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001346 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001347 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001348 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001349 }
Jamie Madill033dae62014-06-18 12:56:28 -04001350 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001351 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001352 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001353 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001354 }
Jamie Madill19571812013-08-12 15:26:34 -07001355 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001356 {
1357 mReferencedOutputVariables[name] = node;
1358 out << "out_" << name;
1359 }
1360 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001361 {
1362 out << "gl_Color[0]";
1363 mUsesFragColor = true;
1364 }
1365 else if (qualifier == EvqFragData)
1366 {
1367 out << "gl_Color";
1368 mUsesFragData = true;
1369 }
1370 else if (qualifier == EvqFragCoord)
1371 {
1372 mUsesFragCoord = true;
1373 out << name;
1374 }
1375 else if (qualifier == EvqPointCoord)
1376 {
1377 mUsesPointCoord = true;
1378 out << name;
1379 }
1380 else if (qualifier == EvqFrontFacing)
1381 {
1382 mUsesFrontFacing = true;
1383 out << name;
1384 }
1385 else if (qualifier == EvqPointSize)
1386 {
1387 mUsesPointSize = true;
1388 out << name;
1389 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001390 else if (qualifier == EvqInstanceID)
1391 {
1392 mUsesInstanceID = true;
1393 out << name;
1394 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001395 else if (name == "gl_FragDepthEXT")
1396 {
1397 mUsesFragDepth = true;
1398 out << "gl_Depth";
1399 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001400 else if (qualifier == EvqInternal)
1401 {
1402 out << name;
1403 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001404 else
1405 {
Jamie Madill033dae62014-06-18 12:56:28 -04001406 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001407 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001408 }
1409}
1410
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001411void OutputHLSL::visitRaw(TIntermRaw *node)
1412{
Jamie Madill32aab012015-01-27 14:12:26 -05001413 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001414}
1415
Olli Etuaho7fb49552015-03-18 17:27:44 +02001416void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1417{
1418 if (type.isScalar() && !type.isArray())
1419 {
1420 if (op == EOpEqual)
1421 {
1422 outputTriplet(visit, "(", " == ", ")", out);
1423 }
1424 else
1425 {
1426 outputTriplet(visit, "(", " != ", ")", out);
1427 }
1428 }
1429 else
1430 {
1431 if (visit == PreVisit && op == EOpNotEqual)
1432 {
1433 out << "!";
1434 }
1435
1436 if (type.isArray())
1437 {
1438 const TString &functionName = addArrayEqualityFunction(type);
1439 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1440 }
1441 else if (type.getBasicType() == EbtStruct)
1442 {
1443 const TStructure &structure = *type.getStruct();
1444 const TString &functionName = addStructEqualityFunction(structure);
1445 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")", out);
1446 }
1447 else
1448 {
1449 ASSERT(type.isMatrix() || type.isVector());
1450 outputTriplet(visit, "all(", " == ", ")", out);
1451 }
1452 }
1453}
1454
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001455bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1456{
Jamie Madill32aab012015-01-27 14:12:26 -05001457 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001458
Jamie Madill570e04d2013-06-21 09:15:33 -04001459 // Handle accessing std140 structs by value
1460 if (mFlaggedStructMappedNames.count(node) > 0)
1461 {
1462 out << mFlaggedStructMappedNames[node];
1463 return false;
1464 }
1465
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001466 switch (node->getOp())
1467 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001468 case EOpAssign:
1469 if (node->getLeft()->isArray())
1470 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001471 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1472 if (rightAgg != nullptr && rightAgg->isConstructor())
1473 {
1474 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1475 out << functionName << "(";
1476 node->getLeft()->traverse(this);
1477 TIntermSequence *seq = rightAgg->getSequence();
1478 for (auto &arrayElement : *seq)
1479 {
1480 out << ", ";
1481 arrayElement->traverse(this);
1482 }
1483 out << ")";
1484 return false;
1485 }
1486 else
1487 {
1488 const TString &functionName = addArrayAssignmentFunction(node->getType());
1489 outputTriplet(visit, (functionName + "(").c_str(), ", ", ")");
1490 }
Olli Etuahoe79904c2015-03-18 16:56:42 +02001491 }
1492 else
1493 {
1494 outputTriplet(visit, "(", " = ", ")");
1495 }
1496 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001497 case EOpInitialize:
1498 if (visit == PreVisit)
1499 {
1500 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1501 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1502 // new variable is created before the assignment is evaluated), so we need to convert
1503 // this to "float t = x, x = t;".
1504
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001505 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001506 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001507 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001508
Jamie Madill37997142015-01-28 10:06:34 -05001509 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1510 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001511 {
Jamie Madill37997142015-01-28 10:06:34 -05001512 // For variables which are not constant, defer their real initialization until
1513 // after we initialize other globals: uniforms, attributes and varyings.
1514 mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression));
1515 const TString &initString = initializer(node->getType());
1516 node->setRight(new TIntermRaw(node->getType(), initString));
1517 }
1518 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1519 {
1520 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001521 return false;
1522 }
1523 }
1524 else if (visit == InVisit)
1525 {
1526 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001527 }
1528 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001529 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1530 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1531 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1532 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1533 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1534 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001535 if (visit == PreVisit)
1536 {
1537 out << "(";
1538 }
1539 else if (visit == InVisit)
1540 {
1541 out << " = mul(";
1542 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001543 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001544 }
1545 else
1546 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001547 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001548 }
1549 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001550 case EOpMatrixTimesMatrixAssign:
1551 if (visit == PreVisit)
1552 {
1553 out << "(";
1554 }
1555 else if (visit == InVisit)
1556 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001557 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001558 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001559 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001560 }
1561 else
1562 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001563 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001564 }
1565 break;
1566 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001567 case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001568 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1569 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1570 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1571 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1572 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001573 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001574 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001575 const TType& leftType = node->getLeft()->getType();
1576 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001577 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001578 if (visit == PreVisit)
1579 {
1580 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1581 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001582 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001583 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001584 return false;
1585 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001586 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001587 else
1588 {
1589 outputTriplet(visit, "", "[", "]");
1590 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001591 }
1592 break;
1593 case EOpIndexIndirect:
1594 // We do not currently support indirect references to interface blocks
1595 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1596 outputTriplet(visit, "", "[", "]");
1597 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001598 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001599 if (visit == InVisit)
1600 {
1601 const TStructure* structure = node->getLeft()->getType().getStruct();
1602 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1603 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001604 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001605
1606 return false;
1607 }
1608 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001609 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001610 if (visit == InVisit)
1611 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001612 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1613 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1614 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001615 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001616
1617 return false;
1618 }
1619 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001620 case EOpVectorSwizzle:
1621 if (visit == InVisit)
1622 {
1623 out << ".";
1624
1625 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1626
1627 if (swizzle)
1628 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001629 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001630
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001631 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632 {
1633 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1634
1635 if (element)
1636 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001637 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001638
1639 switch (i)
1640 {
1641 case 0: out << "x"; break;
1642 case 1: out << "y"; break;
1643 case 2: out << "z"; break;
1644 case 3: out << "w"; break;
1645 default: UNREACHABLE();
1646 }
1647 }
1648 else UNREACHABLE();
1649 }
1650 }
1651 else UNREACHABLE();
1652
1653 return false; // Fully processed
1654 }
1655 break;
1656 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1657 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1658 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1659 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Olli Etuahoff805cc2015-02-13 10:59:34 +02001660 case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001661 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1662 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1663 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1664 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1665 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001666 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001667 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001668 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001669 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1671 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1672 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1673 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1674 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001675 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001676 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1677 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001678 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001679 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001680 if (node->getRight()->hasSideEffects())
1681 {
1682 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1683 return false;
1684 }
1685 else
1686 {
1687 outputTriplet(visit, "(", " || ", ")");
1688 return true;
1689 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001690 case EOpLogicalXor:
1691 mUsesXor = true;
1692 outputTriplet(visit, "xor(", ", ", ")");
1693 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001694 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001695 if (node->getRight()->hasSideEffects())
1696 {
1697 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1698 return false;
1699 }
1700 else
1701 {
1702 outputTriplet(visit, "(", " && ", ")");
1703 return true;
1704 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705 default: UNREACHABLE();
1706 }
1707
1708 return true;
1709}
1710
1711bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1712{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001713 switch (node->getOp())
1714 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001715 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001716 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001717 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1718 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001719 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001720 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1721 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1722 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1723 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001724 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1725 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1726 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1727 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1728 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1729 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1730 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1731 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001732 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1733 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1734 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1735 case EOpAsinh:
1736 ASSERT(node->getUseEmulatedFunction());
1737 writeEmulatedFunctionTriplet(visit, "asinh(");
1738 break;
1739 case EOpAcosh:
1740 ASSERT(node->getUseEmulatedFunction());
1741 writeEmulatedFunctionTriplet(visit, "acosh(");
1742 break;
1743 case EOpAtanh:
1744 ASSERT(node->getUseEmulatedFunction());
1745 writeEmulatedFunctionTriplet(visit, "atanh(");
1746 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001747 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1748 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1749 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1750 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1751 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1752 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1753 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1754 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1755 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001756 case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break;
1757 case EOpRound: outputTriplet(visit, "round(", "", ")"); break;
1758 case EOpRoundEven:
1759 ASSERT(node->getUseEmulatedFunction());
1760 writeEmulatedFunctionTriplet(visit, "roundEven(");
1761 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001762 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1763 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301764 case EOpIsNan:
1765 outputTriplet(visit, "isnan(", "", ")");
1766 mRequiresIEEEStrictCompiling = true;
1767 break;
Arun Patole0c1726e2015-02-18 14:35:02 +05301768 case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001769 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1770 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1771 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1772 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001773 case EOpPackSnorm2x16:
1774 ASSERT(node->getUseEmulatedFunction());
1775 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1776 break;
1777 case EOpPackUnorm2x16:
1778 ASSERT(node->getUseEmulatedFunction());
1779 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1780 break;
1781 case EOpPackHalf2x16:
1782 ASSERT(node->getUseEmulatedFunction());
1783 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1784 break;
1785 case EOpUnpackSnorm2x16:
1786 ASSERT(node->getUseEmulatedFunction());
1787 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1788 break;
1789 case EOpUnpackUnorm2x16:
1790 ASSERT(node->getUseEmulatedFunction());
1791 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1792 break;
1793 case EOpUnpackHalf2x16:
1794 ASSERT(node->getUseEmulatedFunction());
1795 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1796 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001797 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1798 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001799 case EOpDFdx:
1800 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1801 {
1802 outputTriplet(visit, "(", "", ", 0.0)");
1803 }
1804 else
1805 {
1806 outputTriplet(visit, "ddx(", "", ")");
1807 }
1808 break;
1809 case EOpDFdy:
1810 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1811 {
1812 outputTriplet(visit, "(", "", ", 0.0)");
1813 }
1814 else
1815 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001816 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001817 }
1818 break;
1819 case EOpFwidth:
1820 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1821 {
1822 outputTriplet(visit, "(", "", ", 0.0)");
1823 }
1824 else
1825 {
1826 outputTriplet(visit, "fwidth(", "", ")");
1827 }
1828 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001829 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1830 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001831 case EOpInverse:
1832 ASSERT(node->getUseEmulatedFunction());
1833 writeEmulatedFunctionTriplet(visit, "inverse(");
1834 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001835
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001836 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1837 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838 default: UNREACHABLE();
1839 }
1840
1841 return true;
1842}
1843
1844bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1845{
Jamie Madill32aab012015-01-27 14:12:26 -05001846 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001847
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001848 switch (node->getOp())
1849 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001850 case EOpSequence:
1851 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001852 if (mInsideFunction)
1853 {
Jamie Madill075edd82013-07-08 13:30:19 -04001854 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001855 out << "{\n";
1856 }
1857
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001858 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001859 {
Jamie Madill075edd82013-07-08 13:30:19 -04001860 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001861
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001862 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001863
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001864 // Don't output ; after case labels, they're terminated by :
1865 // This is needed especially since outputting a ; after a case statement would turn empty
1866 // case statements into non-empty case statements, disallowing fall-through from them.
1867 if ((*sit)->getAsCaseNode() == nullptr)
1868 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001869 }
1870
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001871 if (mInsideFunction)
1872 {
Jamie Madill075edd82013-07-08 13:30:19 -04001873 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001874 out << "}\n";
1875 }
1876
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001877 return false;
1878 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879 case EOpDeclaration:
1880 if (visit == PreVisit)
1881 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001882 TIntermSequence *sequence = node->getSequence();
1883 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001884
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001885 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001886 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001887 TStructure *structure = variable->getType().getStruct();
1888
1889 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001890 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001891 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001892 }
1893
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001894 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895 {
Jamie Madill37997142015-01-28 10:06:34 -05001896 for (const auto &seqElement : *sequence)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897 {
Jamie Madill37997142015-01-28 10:06:34 -05001898 if (isSingleStatement(seqElement))
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001899 {
Jamie Madill37997142015-01-28 10:06:34 -05001900 mUnfoldShortCircuit->traverse(seqElement);
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001901 }
1902
Nicolas Capensd974db42014-10-07 10:50:19 -04001903 if (!mInsideFunction)
1904 {
1905 out << "static ";
1906 }
1907
1908 out << TypeString(variable->getType()) + " ";
1909
Jamie Madill37997142015-01-28 10:06:34 -05001910 TIntermSymbol *symbol = seqElement->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001912 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001914 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001915 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001916 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001917 }
1918 else
1919 {
Jamie Madill37997142015-01-28 10:06:34 -05001920 seqElement->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001921 }
1922
Jamie Madill37997142015-01-28 10:06:34 -05001923 if (seqElement != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001924 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001925 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 }
1927 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001928 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001929 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1930 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001931 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001932 }
1933 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001934 }
Jamie Madill033dae62014-06-18 12:56:28 -04001935 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001936 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001937 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001938 {
1939 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1940
1941 if (symbol)
1942 {
1943 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1944 mReferencedVaryings[symbol->getSymbol()] = symbol;
1945 }
1946 else
1947 {
1948 (*sit)->traverse(this);
1949 }
1950 }
1951 }
1952
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953 return false;
1954 }
1955 else if (visit == InVisit)
1956 {
1957 out << ", ";
1958 }
1959 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001960 case EOpInvariantDeclaration:
1961 // Do not do any translation
1962 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001963 case EOpPrototype:
1964 if (visit == PreVisit)
1965 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001966 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001967
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001968 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001969
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001970 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001971 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001972 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001973
1974 if (symbol)
1975 {
1976 out << argumentString(symbol);
1977
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001978 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001979 {
1980 out << ", ";
1981 }
1982 }
1983 else UNREACHABLE();
1984 }
1985
1986 out << ");\n";
1987
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001988 // Also prototype the Lod0 variant if needed
Corentin Wallez938f0022015-04-08 19:35:40 +00001989 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001990 {
1991 mOutputLod0Function = true;
1992 node->traverse(this);
1993 mOutputLod0Function = false;
1994 }
1995
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001996 return false;
1997 }
1998 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001999 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000 case EOpFunction:
2001 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002002 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002003
Jamie Madill033dae62014-06-18 12:56:28 -04002004 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002005
2006 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002008 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002010 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002011 {
Jamie Madill033dae62014-06-18 12:56:28 -04002012 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002013 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002014
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002015 TIntermSequence *sequence = node->getSequence();
2016 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002017
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002018 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002019 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002020 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002021
2022 if (symbol)
2023 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002024 TStructure *structure = symbol->getType().getStruct();
2025
2026 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002027 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002028 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002029 }
2030
2031 out << argumentString(symbol);
2032
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002033 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002034 {
2035 out << ", ";
2036 }
2037 }
2038 else UNREACHABLE();
2039 }
2040
2041 out << ")\n"
2042 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002043
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002044 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002045 {
2046 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002047 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002048 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002050
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002051 out << "}\n";
2052
Corentin Wallez938f0022015-04-08 19:35:40 +00002053 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002054 {
Corentin Wallez938f0022015-04-08 19:35:40 +00002055 if (name != "main")
2056 {
2057 mOutputLod0Function = true;
2058 node->traverse(this);
2059 mOutputLod0Function = false;
2060 }
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002061 }
2062
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002063 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064 }
2065 break;
2066 case EOpFunctionCall:
2067 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002068 TString name = TFunction::unmangleName(node->getName());
Corentin Wallez938f0022015-04-08 19:35:40 +00002069 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002070 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002071
2072 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073 {
Jamie Madill033dae62014-06-18 12:56:28 -04002074 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 }
2076 else
2077 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002078 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002079
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002080 TextureFunction textureFunction;
2081 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002082 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002083 textureFunction.method = TextureFunction::IMPLICIT;
2084 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002085 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002086
2087 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002088 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002089 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002090 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002091 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002092 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002093 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002094 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002095 }
Nicolas Capens46485082014-04-15 13:12:50 -04002096 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2097 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002098 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002099 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002100 }
Nicolas Capens46485082014-04-15 13:12:50 -04002101 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002102 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002103 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002104 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002105 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002106 else if (name == "textureSize")
2107 {
2108 textureFunction.method = TextureFunction::SIZE;
2109 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002110 else if (name == "textureOffset")
2111 {
2112 textureFunction.method = TextureFunction::IMPLICIT;
2113 textureFunction.offset = true;
2114 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002115 else if (name == "textureProjOffset")
2116 {
2117 textureFunction.method = TextureFunction::IMPLICIT;
2118 textureFunction.offset = true;
2119 textureFunction.proj = true;
2120 }
2121 else if (name == "textureLodOffset")
2122 {
2123 textureFunction.method = TextureFunction::LOD;
2124 textureFunction.offset = true;
2125 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002126 else if (name == "textureProjLodOffset")
2127 {
2128 textureFunction.method = TextureFunction::LOD;
2129 textureFunction.proj = true;
2130 textureFunction.offset = true;
2131 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002132 else if (name == "texelFetch")
2133 {
2134 textureFunction.method = TextureFunction::FETCH;
2135 }
2136 else if (name == "texelFetchOffset")
2137 {
2138 textureFunction.method = TextureFunction::FETCH;
2139 textureFunction.offset = true;
2140 }
Nicolas Capens46485082014-04-15 13:12:50 -04002141 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002142 {
2143 textureFunction.method = TextureFunction::GRAD;
2144 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002145 else if (name == "textureGradOffset")
2146 {
2147 textureFunction.method = TextureFunction::GRAD;
2148 textureFunction.offset = true;
2149 }
Nicolas Capens46485082014-04-15 13:12:50 -04002150 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002151 {
2152 textureFunction.method = TextureFunction::GRAD;
2153 textureFunction.proj = true;
2154 }
2155 else if (name == "textureProjGradOffset")
2156 {
2157 textureFunction.method = TextureFunction::GRAD;
2158 textureFunction.proj = true;
2159 textureFunction.offset = true;
2160 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002161 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002162
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002163 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002164 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002165 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2166
2167 if (textureFunction.offset)
2168 {
2169 mandatoryArgumentCount++;
2170 }
2171
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002172 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002173
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002174 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002175 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002176 if (bias)
2177 {
2178 textureFunction.method = TextureFunction::LOD0BIAS;
2179 }
2180 else
2181 {
2182 textureFunction.method = TextureFunction::LOD0;
2183 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002184 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002185 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002186 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002187 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002188 }
2189 }
2190
2191 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002192
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002193 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002194 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002195
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002196 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002197 {
2198 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2199 {
2200 out << "texture_";
2201 (*arg)->traverse(this);
2202 out << ", sampler_";
2203 }
2204
2205 (*arg)->traverse(this);
2206
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002207 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002208 {
2209 out << ", ";
2210 }
2211 }
2212
2213 out << ")";
2214
2215 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002216 }
2217 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002218 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002219 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2220 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2221 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2222 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2223 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2224 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2225 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2226 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2227 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2228 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2229 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2230 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2231 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2232 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2233 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2234 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2235 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2236 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2237 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002238 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002239 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002240 if (node->getType().isArray())
2241 {
2242 UNIMPLEMENTED();
2243 }
Jamie Madill033dae62014-06-18 12:56:28 -04002244 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002245 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Daniel Bratell29190082015-02-20 16:42:54 +01002246 outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002247 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002248 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002249 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2250 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2251 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2252 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2253 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2254 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002255 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002256 ASSERT(node->getUseEmulatedFunction());
2257 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002258 break;
Olli Etuahob6e07a62015-02-16 12:22:10 +02002259 case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002260 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002261 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002262 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002263 ASSERT(node->getUseEmulatedFunction());
2264 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265 break;
2266 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2267 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2268 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2269 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2270 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2271 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2272 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2273 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2274 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002275 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002276 ASSERT(node->getUseEmulatedFunction());
2277 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002278 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2280 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002281 case EOpOuterProduct:
2282 ASSERT(node->getUseEmulatedFunction());
2283 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2284 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286 default: UNREACHABLE();
2287 }
2288
2289 return true;
2290}
2291
2292bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2293{
Jamie Madill32aab012015-01-27 14:12:26 -05002294 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002296 if (node->usesTernaryOperator())
2297 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002298 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002299 }
2300 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002302 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002303
Corentin Wallez938f0022015-04-08 19:35:40 +00002304 // D3D errors when there is a gradient operation in a loop in an unflattened if
2305 // however flattening all the ifs in branch heavy shaders made D3D error too.
2306 // As a temporary workaround we flatten the ifs only if there is at least a loop
2307 // present somewhere in the shader.
2308 if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
Corentin Wallez80bacde2014-11-10 12:07:37 -08002309 {
2310 out << "FLATTEN ";
2311 }
2312
2313 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002314
2315 node->getCondition()->traverse(this);
2316
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002317 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002318
Jamie Madill075edd82013-07-08 13:30:19 -04002319 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002320 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002322 bool discard = false;
2323
daniel@transgaming.combb885322010-04-15 20:45:24 +00002324 if (node->getTrueBlock())
2325 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002326 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002327
2328 // Detect true discard
2329 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002330 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331
Jamie Madill075edd82013-07-08 13:30:19 -04002332 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002333 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002334
2335 if (node->getFalseBlock())
2336 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002337 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002338
Jamie Madill075edd82013-07-08 13:30:19 -04002339 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002340 out << "{\n";
2341
Jamie Madill075edd82013-07-08 13:30:19 -04002342 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002343 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002344
Jamie Madill075edd82013-07-08 13:30:19 -04002345 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002346 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002347
2348 // Detect false discard
2349 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2350 }
2351
2352 // ANGLE issue 486: Detect problematic conditional discard
2353 if (discard && FindSideEffectRewriting::search(node))
2354 {
2355 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002356 }
2357 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358
2359 return false;
2360}
2361
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002362bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002363{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002364 if (node->getStatementList())
2365 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002366 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002367 outputTriplet(visit, "switch (", ") ", "");
2368 // The curly braces get written when visiting the statementList aggregate
2369 }
2370 else
2371 {
2372 // No statementList, so it won't output curly braces
2373 outputTriplet(visit, "switch (", ") {", "}\n");
2374 }
2375 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002376}
2377
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002378bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002379{
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002380 if (node->hasCondition())
2381 {
2382 outputTriplet(visit, "case (", "", "):\n");
2383 return true;
2384 }
2385 else
2386 {
2387 TInfoSinkBase &out = getInfoSink();
2388 out << "default:\n";
2389 return false;
2390 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002391}
2392
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2394{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002395 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396}
2397
2398bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2399{
Nicolas Capens655fe362014-04-11 13:12:34 -04002400 mNestedLoopDepth++;
2401
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002402 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez938f0022015-04-08 19:35:40 +00002403
2404 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
2405 {
2406 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2407 }
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002408
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002409 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002410 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002411 if (handleExcessiveLoop(node))
2412 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002413 mInsideDiscontinuousLoop = wasDiscontinuous;
2414 mNestedLoopDepth--;
2415
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002416 return false;
2417 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002418 }
2419
Jamie Madill32aab012015-01-27 14:12:26 -05002420 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421
alokp@chromium.org52813552010-11-16 18:36:09 +00002422 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423 {
Corentin Wallez938f0022015-04-08 19:35:40 +00002424 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002425
Jamie Madill075edd82013-07-08 13:30:19 -04002426 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002427 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 }
2429 else
2430 {
Corentin Wallez938f0022015-04-08 19:35:40 +00002431 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002432
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 if (node->getInit())
2434 {
2435 node->getInit()->traverse(this);
2436 }
2437
2438 out << "; ";
2439
alokp@chromium.org52813552010-11-16 18:36:09 +00002440 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002442 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002443 }
2444
2445 out << "; ";
2446
alokp@chromium.org52813552010-11-16 18:36:09 +00002447 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002449 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450 }
2451
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002452 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002453
Jamie Madill075edd82013-07-08 13:30:19 -04002454 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002455 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 }
2457
2458 if (node->getBody())
2459 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002460 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461 }
2462
Jamie Madill075edd82013-07-08 13:30:19 -04002463 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002464 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465
alokp@chromium.org52813552010-11-16 18:36:09 +00002466 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 {
Jamie Madill075edd82013-07-08 13:30:19 -04002468 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469 out << "while(\n";
2470
alokp@chromium.org52813552010-11-16 18:36:09 +00002471 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002472
daniel@transgaming.com73536982012-03-21 20:45:49 +00002473 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474 }
2475
daniel@transgaming.com73536982012-03-21 20:45:49 +00002476 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002478 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002479 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002480
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481 return false;
2482}
2483
2484bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2485{
Jamie Madill32aab012015-01-27 14:12:26 -05002486 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487
2488 switch (node->getFlowOp())
2489 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002490 case EOpKill:
2491 outputTriplet(visit, "discard;\n", "", "");
2492 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002493 case EOpBreak:
2494 if (visit == PreVisit)
2495 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002496 if (mNestedLoopDepth > 1)
2497 {
2498 mUsesNestedBreak = true;
2499 }
2500
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002501 if (mExcessiveLoopIndex)
2502 {
2503 out << "{Break";
2504 mExcessiveLoopIndex->traverse(this);
2505 out << " = true; break;}\n";
2506 }
2507 else
2508 {
2509 out << "break;\n";
2510 }
2511 }
2512 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002513 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514 case EOpReturn:
2515 if (visit == PreVisit)
2516 {
2517 if (node->getExpression())
2518 {
2519 out << "return ";
2520 }
2521 else
2522 {
2523 out << "return;\n";
2524 }
2525 }
2526 else if (visit == PostVisit)
2527 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002528 if (node->getExpression())
2529 {
2530 out << ";\n";
2531 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532 }
2533 break;
2534 default: UNREACHABLE();
2535 }
2536
2537 return true;
2538}
2539
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002540void OutputHLSL::traverseStatements(TIntermNode *node)
2541{
2542 if (isSingleStatement(node))
2543 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002544 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002545 }
2546
2547 node->traverse(this);
2548}
2549
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002550bool OutputHLSL::isSingleStatement(TIntermNode *node)
2551{
2552 TIntermAggregate *aggregate = node->getAsAggregate();
2553
2554 if (aggregate)
2555 {
2556 if (aggregate->getOp() == EOpSequence)
2557 {
2558 return false;
2559 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002560 else if (aggregate->getOp() == EOpDeclaration)
2561 {
2562 // Declaring multiple comma-separated variables must be considered multiple statements
2563 // because each individual declaration has side effects which are visible in the next.
2564 return false;
2565 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002566 else
2567 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002568 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002569 {
2570 if (!isSingleStatement(*sit))
2571 {
2572 return false;
2573 }
2574 }
2575
2576 return true;
2577 }
2578 }
2579
2580 return true;
2581}
2582
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002583// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2584// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002585bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2586{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002587 const int MAX_LOOP_ITERATIONS = 254;
Jamie Madill32aab012015-01-27 14:12:26 -05002588 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002589
2590 // Parse loops of the form:
2591 // for(int index = initial; index [comparator] limit; index += increment)
2592 TIntermSymbol *index = NULL;
2593 TOperator comparator = EOpNull;
2594 int initial = 0;
2595 int limit = 0;
2596 int increment = 0;
2597
2598 // Parse index name and intial value
2599 if (node->getInit())
2600 {
2601 TIntermAggregate *init = node->getInit()->getAsAggregate();
2602
2603 if (init)
2604 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002605 TIntermSequence *sequence = init->getSequence();
2606 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002607
2608 if (variable && variable->getQualifier() == EvqTemporary)
2609 {
2610 TIntermBinary *assign = variable->getAsBinaryNode();
2611
2612 if (assign->getOp() == EOpInitialize)
2613 {
2614 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2615 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2616
2617 if (symbol && constant)
2618 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002619 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002620 {
2621 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002622 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002623 }
2624 }
2625 }
2626 }
2627 }
2628 }
2629
2630 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002631 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002632 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002633 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002634
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002635 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2636 {
2637 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2638
2639 if (constant)
2640 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002641 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002642 {
2643 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002644 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002645 }
2646 }
2647 }
2648 }
2649
2650 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002651 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002652 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002653 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2654 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002655
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002656 if (binaryTerminal)
2657 {
2658 TOperator op = binaryTerminal->getOp();
2659 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2660
2661 if (constant)
2662 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002663 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002664 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002665 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002666
2667 switch (op)
2668 {
2669 case EOpAddAssign: increment = value; break;
2670 case EOpSubAssign: increment = -value; break;
2671 default: UNIMPLEMENTED();
2672 }
2673 }
2674 }
2675 }
2676 else if (unaryTerminal)
2677 {
2678 TOperator op = unaryTerminal->getOp();
2679
2680 switch (op)
2681 {
2682 case EOpPostIncrement: increment = 1; break;
2683 case EOpPostDecrement: increment = -1; break;
2684 case EOpPreIncrement: increment = 1; break;
2685 case EOpPreDecrement: increment = -1; break;
2686 default: UNIMPLEMENTED();
2687 }
2688 }
2689 }
2690
2691 if (index != NULL && comparator != EOpNull && increment != 0)
2692 {
2693 if (comparator == EOpLessThanEqual)
2694 {
2695 comparator = EOpLessThan;
2696 limit += 1;
2697 }
2698
2699 if (comparator == EOpLessThan)
2700 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002701 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002702
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002703 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002704 {
2705 return false; // Not an excessive loop
2706 }
2707
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002708 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2709 mExcessiveLoopIndex = index;
2710
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002711 out << "{int ";
2712 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002713 out << ";\n"
2714 "bool Break";
2715 index->traverse(this);
2716 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002717
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002718 bool firstLoopFragment = true;
2719
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002720 while (iterations > 0)
2721 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002722 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002723
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002724 if (!firstLoopFragment)
2725 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002726 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002727 index->traverse(this);
2728 out << ") {\n";
2729 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002730
2731 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2732 {
2733 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2734 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002735
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002736 // for(int index = initial; index < clampedLimit; index += increment)
2737
Corentin Wallez938f0022015-04-08 19:35:40 +00002738 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002739 index->traverse(this);
2740 out << " = ";
2741 out << initial;
2742
2743 out << "; ";
2744 index->traverse(this);
2745 out << " < ";
2746 out << clampedLimit;
2747
2748 out << "; ";
2749 index->traverse(this);
2750 out << " += ";
2751 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002752 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002753
Jamie Madill075edd82013-07-08 13:30:19 -04002754 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002755 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002756
2757 if (node->getBody())
2758 {
2759 node->getBody()->traverse(this);
2760 }
2761
Jamie Madill075edd82013-07-08 13:30:19 -04002762 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002763 out << ";}\n";
2764
2765 if (!firstLoopFragment)
2766 {
2767 out << "}\n";
2768 }
2769
2770 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002771
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002772 initial += MAX_LOOP_ITERATIONS * increment;
2773 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002774 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002775
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002776 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002777
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002778 mExcessiveLoopIndex = restoreIndex;
2779
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002780 return true;
2781 }
2782 else UNIMPLEMENTED();
2783 }
2784
2785 return false; // Not handled as an excessive loop
2786}
2787
Olli Etuaho7fb49552015-03-18 17:27:44 +02002788void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002790 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002791 {
2792 out << preString;
2793 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002794 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795 {
2796 out << inString;
2797 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002798 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002799 {
2800 out << postString;
2801 }
2802}
2803
Olli Etuaho7fb49552015-03-18 17:27:44 +02002804void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
2805{
2806 outputTriplet(visit, preString, inString, postString, getInfoSink());
2807}
2808
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002809void OutputHLSL::outputLineDirective(int line)
2810{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002811 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002812 {
Jamie Madill32aab012015-01-27 14:12:26 -05002813 TInfoSinkBase &out = getInfoSink();
2814
2815 out << "\n";
2816 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002817
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002818 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002819 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002820 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002821 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002822
Jamie Madill32aab012015-01-27 14:12:26 -05002823 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002824 }
2825}
2826
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002827TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2828{
2829 TQualifier qualifier = symbol->getQualifier();
2830 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002831 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002832
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002833 if (name.empty()) // HLSL demands named arguments, also for prototypes
2834 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002835 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002836 }
2837 else
2838 {
Jamie Madill033dae62014-06-18 12:56:28 -04002839 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002840 }
2841
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002842 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2843 {
Jamie Madill033dae62014-06-18 12:56:28 -04002844 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002845 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002846 }
2847
Jamie Madill033dae62014-06-18 12:56:28 -04002848 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002849}
2850
2851TString OutputHLSL::initializer(const TType &type)
2852{
2853 TString string;
2854
Jamie Madill94bf7f22013-07-08 13:31:15 -04002855 size_t size = type.getObjectSize();
2856 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002857 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002858 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002859
Jamie Madill94bf7f22013-07-08 13:31:15 -04002860 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002861 {
2862 string += ", ";
2863 }
2864 }
2865
daniel@transgaming.comead23042010-04-29 03:35:36 +00002866 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002868
Daniel Bratell29190082015-02-20 16:42:54 +01002869void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002870{
Olli Etuahof40319e2015-03-10 14:33:00 +02002871 if (type.isArray())
2872 {
2873 UNIMPLEMENTED();
2874 }
Jamie Madill32aab012015-01-27 14:12:26 -05002875 TInfoSinkBase &out = getInfoSink();
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002876
2877 if (visit == PreVisit)
2878 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002879 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002880
Daniel Bratell29190082015-02-20 16:42:54 +01002881 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002882 }
2883 else if (visit == InVisit)
2884 {
2885 out << ", ";
2886 }
2887 else if (visit == PostVisit)
2888 {
2889 out << ")";
2890 }
2891}
2892
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002893const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2894{
Jamie Madill32aab012015-01-27 14:12:26 -05002895 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002896
Jamie Madill98493dd2013-07-08 14:39:03 -04002897 const TStructure* structure = type.getStruct();
2898 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002899 {
Jamie Madill033dae62014-06-18 12:56:28 -04002900 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002901
Jamie Madill98493dd2013-07-08 14:39:03 -04002902 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002903
Jamie Madill98493dd2013-07-08 14:39:03 -04002904 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002905 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002906 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002907 constUnion = writeConstantUnion(*fieldType, constUnion);
2908
Jamie Madill98493dd2013-07-08 14:39:03 -04002909 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002910 {
2911 out << ", ";
2912 }
2913 }
2914
2915 out << ")";
2916 }
2917 else
2918 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002919 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002920 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002921
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002922 if (writeType)
2923 {
Jamie Madill033dae62014-06-18 12:56:28 -04002924 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002925 }
2926
Jamie Madill94bf7f22013-07-08 13:31:15 -04002927 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002928 {
2929 switch (constUnion->getType())
2930 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002931 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002932 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002933 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002934 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002935 default: UNREACHABLE();
2936 }
2937
2938 if (i != size - 1)
2939 {
2940 out << ", ";
2941 }
2942 }
2943
2944 if (writeType)
2945 {
2946 out << ")";
2947 }
2948 }
2949
2950 return constUnion;
2951}
2952
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002953void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2954{
2955 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2956 outputTriplet(visit, preString.c_str(), ", ", ")");
2957}
2958
Jamie Madill37997142015-01-28 10:06:34 -05002959bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2960{
2961 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2962 expression->traverse(&searchSymbol);
2963
2964 if (searchSymbol.foundMatch())
2965 {
2966 // Type already printed
2967 out << "t" + str(mUniqueIndex) + " = ";
2968 expression->traverse(this);
2969 out << ", ";
2970 symbolNode->traverse(this);
2971 out << " = t" + str(mUniqueIndex);
2972
2973 mUniqueIndex++;
2974 return true;
2975 }
2976
2977 return false;
2978}
2979
2980void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
2981{
2982 out << "#define ANGLE_USES_DEFERRED_INIT\n"
2983 << "\n"
2984 << "void initializeDeferredGlobals()\n"
2985 << "{\n";
2986
2987 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
2988 {
2989 TIntermSymbol *symbol = deferredGlobal.first;
2990 TIntermTyped *expression = deferredGlobal.second;
2991 ASSERT(symbol);
2992 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
2993
2994 out << " " << Decorate(symbol->getSymbol()) << " = ";
2995
2996 if (!writeSameSymbolInitializer(out, symbol, expression))
2997 {
2998 ASSERT(mInfoSinkStack.top() == &out);
2999 expression->traverse(this);
3000 }
3001
3002 out << ";\n";
3003 }
3004
3005 out << "}\n"
3006 << "\n";
3007}
3008
Jamie Madill55e79e02015-02-09 15:35:00 -05003009TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3010{
3011 const TFieldList &fields = structure.fields();
3012
3013 for (const auto &eqFunction : mStructEqualityFunctions)
3014 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003015 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003016 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003017 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003018 }
3019 }
3020
3021 const TString &structNameString = StructNameString(structure);
3022
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003023 StructEqualityFunction *function = new StructEqualityFunction();
3024 function->structure = &structure;
3025 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003026
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003027 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003028
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003029 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3030 << "{\n"
3031 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003032
3033 for (size_t i = 0; i < fields.size(); i++)
3034 {
3035 const TField *field = fields[i];
3036 const TType *fieldType = field->type();
3037
3038 const TString &fieldNameA = "a." + Decorate(field->name());
3039 const TString &fieldNameB = "b." + Decorate(field->name());
3040
3041 if (i > 0)
3042 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003043 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003044 }
3045
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003046 fnOut << "(";
3047 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3048 fnOut << fieldNameA;
3049 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3050 fnOut << fieldNameB;
3051 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3052 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003053 }
3054
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003055 fnOut << ";\n" << "}\n";
3056
3057 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003058
3059 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003060 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003061
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003062 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003063}
3064
Olli Etuaho7fb49552015-03-18 17:27:44 +02003065TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3066{
3067 for (const auto &eqFunction : mArrayEqualityFunctions)
3068 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003069 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003070 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003071 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003072 }
3073 }
3074
3075 const TString &typeName = TypeString(type);
3076
Olli Etuaho12690762015-03-31 12:55:28 +03003077 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003078 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003079
3080 TInfoSinkBase fnNameOut;
3081 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003082 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003083
3084 TType nonArrayType = type;
3085 nonArrayType.clearArrayness();
3086
3087 TInfoSinkBase fnOut;
3088
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003089 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003090 << typeName << " a[" << type.getArraySize() << "], "
3091 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003092 << "{\n"
3093 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3094 " {\n"
3095 " if (";
3096
3097 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3098 fnOut << "a[i]";
3099 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3100 fnOut << "b[i]";
3101 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3102
3103 fnOut << ") { return false; }\n"
3104 " }\n"
3105 " return true;\n"
3106 "}\n";
3107
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003108 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003109
3110 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003111 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003112
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003113 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003114}
3115
Olli Etuaho12690762015-03-31 12:55:28 +03003116TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3117{
3118 for (const auto &assignFunction : mArrayAssignmentFunctions)
3119 {
3120 if (assignFunction.type == type)
3121 {
3122 return assignFunction.functionName;
3123 }
3124 }
3125
3126 const TString &typeName = TypeString(type);
3127
3128 ArrayHelperFunction function;
3129 function.type = type;
3130
3131 TInfoSinkBase fnNameOut;
3132 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3133 function.functionName = fnNameOut.c_str();
3134
3135 TInfoSinkBase fnOut;
3136
3137 fnOut << "void " << function.functionName << "(out "
3138 << typeName << " a[" << type.getArraySize() << "], "
3139 << typeName << " b[" << type.getArraySize() << "])\n"
3140 << "{\n"
3141 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3142 " {\n"
3143 " a[i] = b[i];\n"
3144 " }\n"
3145 "}\n";
3146
3147 function.functionDefinition = fnOut.c_str();
3148
3149 mArrayAssignmentFunctions.push_back(function);
3150
3151 return function.functionName;
3152}
3153
Olli Etuaho9638c352015-04-01 14:34:52 +03003154TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3155{
3156 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3157 {
3158 if (constructIntoFunction.type == type)
3159 {
3160 return constructIntoFunction.functionName;
3161 }
3162 }
3163
3164 const TString &typeName = TypeString(type);
3165
3166 ArrayHelperFunction function;
3167 function.type = type;
3168
3169 TInfoSinkBase fnNameOut;
3170 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3171 function.functionName = fnNameOut.c_str();
3172
3173 TInfoSinkBase fnOut;
3174
3175 fnOut << "void " << function.functionName << "(out "
3176 << typeName << " a[" << type.getArraySize() << "]";
3177 for (int i = 0; i < type.getArraySize(); ++i)
3178 {
3179 fnOut << ", " << typeName << " b" << i;
3180 }
3181 fnOut << ")\n"
3182 "{\n";
3183
3184 for (int i = 0; i < type.getArraySize(); ++i)
3185 {
3186 fnOut << " a[" << i << "] = b" << i << ";\n";
3187 }
3188 fnOut << "}\n";
3189
3190 function.functionDefinition = fnOut.c_str();
3191
3192 mArrayConstructIntoFunctions.push_back(function);
3193
3194 return function.functionName;
3195}
3196
3197
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003198}