blob: bd391a923a684b9f0ce10670d14548b5bf26a133 [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
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000010#include "common/utilities.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040011#include "common/blocklayout.h"
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +020012#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Geoff Lang17732822013-08-29 13:46:49 -040013#include "compiler/translator/compilerdebug.h"
14#include "compiler/translator/InfoSink.h"
15#include "compiler/translator/DetectDiscontinuity.h"
16#include "compiler/translator/SearchSymbol.h"
17#include "compiler/translator/UnfoldShortCircuit.h"
Geoff Lang17732822013-08-29 13:46:49 -040018#include "compiler/translator/FlagStd140Structs.h"
Jamie Madill3c9eeb92013-11-04 11:09:26 -050019#include "compiler/translator/NodeSearch.h"
Jamie Madille53c98b2014-02-03 11:57:13 -050020#include "compiler/translator/RewriteElseBlocks.h"
Jamie Madill033dae62014-06-18 12:56:28 -040021#include "compiler/translator/UtilsHLSL.h"
22#include "compiler/translator/util.h"
Jamie Madillf91ce812014-06-13 10:04:34 -040023#include "compiler/translator/UniformHLSL.h"
Jamie Madill8daaba12014-06-13 10:04:33 -040024#include "compiler/translator/StructureHLSL.h"
Jamie Madill54ad4f82014-09-03 09:40:46 -040025#include "compiler/translator/TranslatorHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000027#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000028#include <cfloat>
29#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000030
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031namespace sh
32{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000033
Nicolas Capense0ba27a2013-06-24 16:10:52 -040034TString OutputHLSL::TextureFunction::name() const
35{
36 TString name = "gl_texture";
37
Nicolas Capens6d232bb2013-07-08 15:56:38 -040038 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040039 {
40 name += "2D";
41 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040042 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040043 {
44 name += "3D";
45 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040046 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040047 {
48 name += "Cube";
49 }
50 else UNREACHABLE();
51
52 if (proj)
53 {
54 name += "Proj";
55 }
56
Nicolas Capensb1f45b72013-12-19 17:37:19 -050057 if (offset)
58 {
59 name += "Offset";
60 }
61
Nicolas Capens75fb4752013-07-10 15:14:47 -040062 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040063 {
Nicolas Capensfc014542014-02-18 14:47:13 -050064 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040065 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050066 case LOD: name += "Lod"; break;
67 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040068 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050069 case SIZE: name += "Size"; break;
70 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050071 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040072 default: UNREACHABLE();
73 }
74
75 return name + "(";
76}
77
78bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
79{
80 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040081 if (sampler > rhs.sampler) return false;
82
Nicolas Capense0ba27a2013-06-24 16:10:52 -040083 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040084 if (coords > rhs.coords) return false;
85
Nicolas Capense0ba27a2013-06-24 16:10:52 -040086 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040087 if (proj && !rhs.proj) return false;
88
89 if (!offset && rhs.offset) return true;
90 if (offset && !rhs.offset) return false;
91
Nicolas Capens75fb4752013-07-10 15:14:47 -040092 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040093 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040094
95 return false;
96}
97
Jamie Madill54ad4f82014-09-03 09:40:46 -040098OutputHLSL::OutputHLSL(TParseContext &context, TranslatorHLSL *parentTranslator)
99 : TIntermTraverser(true, true, true),
100 mContext(context),
101 mOutputType(parentTranslator->getOutputType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000103 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000104 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000105
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000106 mUsesFragColor = false;
107 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000108 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000109 mUsesFragCoord = false;
110 mUsesPointCoord = false;
111 mUsesFrontFacing = false;
112 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400113 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000114 mUsesXor = false;
115 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000116 mUsesMod2v = false;
117 mUsesMod2f = false;
118 mUsesMod3v = false;
119 mUsesMod3f = false;
120 mUsesMod4v = false;
121 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000122 mUsesFaceforward1 = false;
123 mUsesFaceforward2 = false;
124 mUsesFaceforward3 = false;
125 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000126 mUsesAtan2_1 = false;
127 mUsesAtan2_2 = false;
128 mUsesAtan2_3 = false;
129 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500130 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400131 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000132
Jamie Madill54ad4f82014-09-03 09:40:46 -0400133 const ShBuiltInResources &resources = parentTranslator->getResources();
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000134 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
135
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000136 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000137
138 mContainsLoopDiscontinuity = false;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800139 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000140 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000141 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400142 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000143
144 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000145
Jamie Madill8daaba12014-06-13 10:04:33 -0400146 mStructureHLSL = new StructureHLSL;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400147 mUniformHLSL = new UniformHLSL(mStructureHLSL, parentTranslator);
Jamie Madill8daaba12014-06-13 10:04:33 -0400148
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000149 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000150 {
Jamie Madill183bde52014-07-02 15:31:19 -0400151 if (mContext.shaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000152 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400153 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
154 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000155 }
156 else
157 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400158 // Reserve registers for dx_DepthRange and dx_ViewAdjust
159 mUniformHLSL->reserveUniformRegisters(2);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000160 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000161 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000162
Jamie Madillf91ce812014-06-13 10:04:34 -0400163 // Reserve registers for the default uniform block and driver constants
164 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000165}
166
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000167OutputHLSL::~OutputHLSL()
168{
Jamie Madill8daaba12014-06-13 10:04:33 -0400169 SafeDelete(mUnfoldShortCircuit);
170 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400171 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000172}
173
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000174void OutputHLSL::output()
175{
Jamie Madill183bde52014-07-02 15:31:19 -0400176 mContainsLoopDiscontinuity = mContext.shaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Corentin Wallez80bacde2014-11-10 12:07:37 -0800177 mContainsAnyLoop = containsAnyLoop(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400178 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
179 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000180
Jamie Madille53c98b2014-02-03 11:57:13 -0500181 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
182 // use a vertex attribute as a condition, and some related computation in the else block.
Jamie Madill183bde52014-07-02 15:31:19 -0400183 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500184 {
185 RewriteElseBlocks(mContext.treeRoot);
186 }
187
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200188 BuiltInFunctionEmulatorHLSL builtInFunctionEmulator;
189 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(mContext.treeRoot);
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000190 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000191 header();
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200192 TInfoSinkBase& sink = mContext.infoSink().obj;
193 // Write emulated built-in functions if needed.
194 builtInFunctionEmulator.OutputEmulatedFunctionDefinition(sink, false);
195 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000196
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200197 sink << mHeader.c_str();
198 sink << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000199}
200
Jamie Madill570e04d2013-06-21 09:15:33 -0400201void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
202{
203 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
204 {
205 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
206
207 // This will mark the necessary block elements as referenced
208 flaggedNode->traverse(this);
209 TString structName(mBody.c_str());
210 mBody.erase();
211
212 mFlaggedStructOriginalNames[flaggedNode] = structName;
213
214 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
215 {
216 structName.erase(pos, 1);
217 }
218
219 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
220 }
221}
222
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000223TInfoSinkBase &OutputHLSL::getBodyStream()
224{
225 return mBody;
226}
227
Jamie Madill4e1fd412014-07-10 17:50:10 -0400228const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
229{
230 return mUniformHLSL->getInterfaceBlockRegisterMap();
231}
232
Jamie Madill9fe25e92014-07-18 10:33:08 -0400233const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
234{
235 return mUniformHLSL->getUniformRegisterMap();
236}
237
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000238int OutputHLSL::vectorSize(const TType &type) const
239{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000240 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000241 int arraySize = type.isArray() ? type.getArraySize() : 1;
242
243 return elementSize * arraySize;
244}
245
Jamie Madill98493dd2013-07-08 14:39:03 -0400246TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400247{
248 TString init;
249
250 TString preIndentString;
251 TString fullIndentString;
252
253 for (int spaces = 0; spaces < (indent * 4); spaces++)
254 {
255 preIndentString += ' ';
256 }
257
258 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
259 {
260 fullIndentString += ' ';
261 }
262
263 init += preIndentString + "{\n";
264
Jamie Madill98493dd2013-07-08 14:39:03 -0400265 const TFieldList &fields = structure.fields();
266 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400267 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400268 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400269 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400270 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400271
Jamie Madill98493dd2013-07-08 14:39:03 -0400272 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400273 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400274 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400275 }
276 else
277 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400278 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400279 }
280 }
281
282 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
283
284 return init;
285}
286
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287void OutputHLSL::header()
288{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000289 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000291 TString varyings;
292 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400293 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000294
Jamie Madill829f59e2013-11-13 19:40:54 -0500295 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400296 {
297 TIntermTyped *structNode = flaggedStructIt->first;
298 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400299 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400300 const TString &originalName = mFlaggedStructOriginalNames[structNode];
301
Jamie Madill033dae62014-06-18 12:56:28 -0400302 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400303 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400304 flaggedStructs += "\n";
305 }
306
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000307 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
308 {
309 const TType &type = varying->second->getType();
310 const TString &name = varying->second->getSymbol();
311
312 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400313 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
314 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000315 }
316
317 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
318 {
319 const TType &type = attribute->second->getType();
320 const TString &name = attribute->second->getSymbol();
321
Jamie Madill033dae62014-06-18 12:56:28 -0400322 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000323 }
324
Jamie Madill8daaba12014-06-13 10:04:33 -0400325 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400326
Jamie Madillf91ce812014-06-13 10:04:34 -0400327 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
328 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
329
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500330 if (mUsesDiscardRewriting)
331 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400332 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500333 }
334
Nicolas Capens655fe362014-04-11 13:12:34 -0400335 if (mUsesNestedBreak)
336 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400337 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400338 }
339
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400340 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
341 "#define LOOP [loop]\n"
342 "#define FLATTEN [flatten]\n"
343 "#else\n"
344 "#define LOOP\n"
345 "#define FLATTEN\n"
346 "#endif\n";
347
Jamie Madill183bde52014-07-02 15:31:19 -0400348 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000350 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000351 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000352
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000353 out << "// Varyings\n";
354 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400355 out << "\n";
356
357 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000358 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500359 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000360 {
Jamie Madill46131a32013-06-20 11:55:50 -0400361 const TString &variableName = outputVariableIt->first;
362 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400363
Jamie Madill033dae62014-06-18 12:56:28 -0400364 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400365 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000366 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000367 }
Jamie Madill46131a32013-06-20 11:55:50 -0400368 else
369 {
370 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
371
372 out << "static float4 gl_Color[" << numColorValues << "] =\n"
373 "{\n";
374 for (unsigned int i = 0; i < numColorValues; i++)
375 {
376 out << " float4(0, 0, 0, 0)";
377 if (i + 1 != numColorValues)
378 {
379 out << ",";
380 }
381 out << "\n";
382 }
383
384 out << "};\n";
385 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000386
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400387 if (mUsesFragDepth)
388 {
389 out << "static float gl_Depth = 0.0;\n";
390 }
391
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000392 if (mUsesFragCoord)
393 {
394 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
395 }
396
397 if (mUsesPointCoord)
398 {
399 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
400 }
401
402 if (mUsesFrontFacing)
403 {
404 out << "static bool gl_FrontFacing = false;\n";
405 }
406
407 out << "\n";
408
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000409 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000410 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000411 out << "struct gl_DepthRangeParameters\n"
412 "{\n"
413 " float near;\n"
414 " float far;\n"
415 " float diff;\n"
416 "};\n"
417 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000418 }
419
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000420 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000421 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000422 out << "cbuffer DriverConstants : register(b1)\n"
423 "{\n";
424
425 if (mUsesDepthRange)
426 {
427 out << " float3 dx_DepthRange : packoffset(c0);\n";
428 }
429
430 if (mUsesFragCoord)
431 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000432 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000433 }
434
435 if (mUsesFragCoord || mUsesFrontFacing)
436 {
437 out << " float3 dx_DepthFront : packoffset(c2);\n";
438 }
439
440 out << "};\n";
441 }
442 else
443 {
444 if (mUsesDepthRange)
445 {
446 out << "uniform float3 dx_DepthRange : register(c0);";
447 }
448
449 if (mUsesFragCoord)
450 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000451 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000452 }
453
454 if (mUsesFragCoord || mUsesFrontFacing)
455 {
456 out << "uniform float3 dx_DepthFront : register(c2);\n";
457 }
458 }
459
460 out << "\n";
461
462 if (mUsesDepthRange)
463 {
464 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
465 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000466 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000467
Jamie Madillf91ce812014-06-13 10:04:34 -0400468 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000469 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400470 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000471 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400472 out << flaggedStructs;
473 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000474 }
475
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000476 if (usingMRTExtension && mNumRenderTargets > 1)
477 {
478 out << "#define GL_USES_MRT\n";
479 }
480
481 if (mUsesFragColor)
482 {
483 out << "#define GL_USES_FRAG_COLOR\n";
484 }
485
486 if (mUsesFragData)
487 {
488 out << "#define GL_USES_FRAG_DATA\n";
489 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000491 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000492 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000493 out << "// Attributes\n";
494 out << attributes;
495 out << "\n"
496 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400497
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000498 if (mUsesPointSize)
499 {
500 out << "static float gl_PointSize = float(1);\n";
501 }
502
503 out << "\n"
504 "// Varyings\n";
505 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000506 out << "\n";
507
508 if (mUsesDepthRange)
509 {
510 out << "struct gl_DepthRangeParameters\n"
511 "{\n"
512 " float near;\n"
513 " float far;\n"
514 " float diff;\n"
515 "};\n"
516 "\n";
517 }
518
519 if (mOutputType == SH_HLSL11_OUTPUT)
520 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800521 out << "cbuffer DriverConstants : register(b1)\n"
522 "{\n";
523
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000524 if (mUsesDepthRange)
525 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800526 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000527 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800528
529 // dx_ViewAdjust will only be used in Feature Level 9 shaders.
530 // However, we declare it for all shaders (including Feature Level 10+).
531 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
532 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
533
534 out << "};\n"
535 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000536 }
537 else
538 {
539 if (mUsesDepthRange)
540 {
541 out << "uniform float3 dx_DepthRange : register(c0);\n";
542 }
543
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000544 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000545 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000546 }
547
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000548 if (mUsesDepthRange)
549 {
550 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
551 "\n";
552 }
553
Jamie Madillf91ce812014-06-13 10:04:34 -0400554 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000555 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400556 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000557 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400558 out << flaggedStructs;
559 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000560 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400561 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000562
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400563 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
564 {
565 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400566 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000567 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400568 switch(textureFunction->sampler)
569 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400570 case EbtSampler2D: out << "int2 "; break;
571 case EbtSampler3D: out << "int3 "; break;
572 case EbtSamplerCube: out << "int2 "; break;
573 case EbtSampler2DArray: out << "int3 "; break;
574 case EbtISampler2D: out << "int2 "; break;
575 case EbtISampler3D: out << "int3 "; break;
576 case EbtISamplerCube: out << "int2 "; break;
577 case EbtISampler2DArray: out << "int3 "; break;
578 case EbtUSampler2D: out << "int2 "; break;
579 case EbtUSampler3D: out << "int3 "; break;
580 case EbtUSamplerCube: out << "int2 "; break;
581 case EbtUSampler2DArray: out << "int3 "; break;
582 case EbtSampler2DShadow: out << "int2 "; break;
583 case EbtSamplerCubeShadow: out << "int2 "; break;
584 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400585 default: UNREACHABLE();
586 }
587 }
588 else // Sampling function
589 {
590 switch(textureFunction->sampler)
591 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400592 case EbtSampler2D: out << "float4 "; break;
593 case EbtSampler3D: out << "float4 "; break;
594 case EbtSamplerCube: out << "float4 "; break;
595 case EbtSampler2DArray: out << "float4 "; break;
596 case EbtISampler2D: out << "int4 "; break;
597 case EbtISampler3D: out << "int4 "; break;
598 case EbtISamplerCube: out << "int4 "; break;
599 case EbtISampler2DArray: out << "int4 "; break;
600 case EbtUSampler2D: out << "uint4 "; break;
601 case EbtUSampler3D: out << "uint4 "; break;
602 case EbtUSamplerCube: out << "uint4 "; break;
603 case EbtUSampler2DArray: out << "uint4 "; break;
604 case EbtSampler2DShadow: out << "float "; break;
605 case EbtSamplerCubeShadow: out << "float "; break;
606 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400607 default: UNREACHABLE();
608 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000609 }
610
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400611 // Function name
612 out << textureFunction->name();
613
614 // Argument list
615 int hlslCoords = 4;
616
617 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000618 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400619 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000620 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400621 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
622 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
623 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000624 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400625
Nicolas Capens75fb4752013-07-10 15:14:47 -0400626 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000627 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400628 case TextureFunction::IMPLICIT: break;
629 case TextureFunction::BIAS: hlslCoords = 4; break;
630 case TextureFunction::LOD: hlslCoords = 4; break;
631 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400632 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400633 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000634 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400635 }
636 else if (mOutputType == SH_HLSL11_OUTPUT)
637 {
638 switch(textureFunction->sampler)
639 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400640 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
641 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
642 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
643 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
644 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
645 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500646 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400647 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
648 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
649 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500650 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400651 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
652 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
653 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
654 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400655 default: UNREACHABLE();
656 }
657 }
658 else UNREACHABLE();
659
Nicolas Capensfc014542014-02-18 14:47:13 -0500660 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400661 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500662 switch(textureFunction->coords)
663 {
664 case 2: out << ", int2 t"; break;
665 case 3: out << ", int3 t"; break;
666 default: UNREACHABLE();
667 }
668 }
669 else // Floating-point coordinates (except textureSize)
670 {
671 switch(textureFunction->coords)
672 {
673 case 1: out << ", int lod"; break; // textureSize()
674 case 2: out << ", float2 t"; break;
675 case 3: out << ", float3 t"; break;
676 case 4: out << ", float4 t"; break;
677 default: UNREACHABLE();
678 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000679 }
680
Nicolas Capensd11d5492014-02-19 17:06:10 -0500681 if (textureFunction->method == TextureFunction::GRAD)
682 {
683 switch(textureFunction->sampler)
684 {
685 case EbtSampler2D:
686 case EbtISampler2D:
687 case EbtUSampler2D:
688 case EbtSampler2DArray:
689 case EbtISampler2DArray:
690 case EbtUSampler2DArray:
691 case EbtSampler2DShadow:
692 case EbtSampler2DArrayShadow:
693 out << ", float2 ddx, float2 ddy";
694 break;
695 case EbtSampler3D:
696 case EbtISampler3D:
697 case EbtUSampler3D:
698 case EbtSamplerCube:
699 case EbtISamplerCube:
700 case EbtUSamplerCube:
701 case EbtSamplerCubeShadow:
702 out << ", float3 ddx, float3 ddy";
703 break;
704 default: UNREACHABLE();
705 }
706 }
707
Nicolas Capens75fb4752013-07-10 15:14:47 -0400708 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000709 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400710 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400711 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400712 case TextureFunction::LOD: out << ", float lod"; break;
713 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400714 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400715 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500716 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500717 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400718 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000719 }
720
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500721 if (textureFunction->offset)
722 {
723 switch(textureFunction->sampler)
724 {
725 case EbtSampler2D: out << ", int2 offset"; break;
726 case EbtSampler3D: out << ", int3 offset"; break;
727 case EbtSampler2DArray: out << ", int2 offset"; break;
728 case EbtISampler2D: out << ", int2 offset"; break;
729 case EbtISampler3D: out << ", int3 offset"; break;
730 case EbtISampler2DArray: out << ", int2 offset"; break;
731 case EbtUSampler2D: out << ", int2 offset"; break;
732 case EbtUSampler3D: out << ", int3 offset"; break;
733 case EbtUSampler2DArray: out << ", int2 offset"; break;
734 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500735 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500736 default: UNREACHABLE();
737 }
738 }
739
Nicolas Capens84cfa122014-04-14 13:48:45 -0400740 if (textureFunction->method == TextureFunction::BIAS ||
741 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500742 {
743 out << ", float bias";
744 }
745
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400746 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400747 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400748
Nicolas Capens75fb4752013-07-10 15:14:47 -0400749 if (textureFunction->method == TextureFunction::SIZE)
750 {
751 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
752 {
753 if (IsSamplerArray(textureFunction->sampler))
754 {
755 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
756 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
757 }
758 else
759 {
760 out << " uint width; uint height; uint numberOfLevels;\n"
761 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
762 }
763 }
764 else if (IsSampler3D(textureFunction->sampler))
765 {
766 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
767 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
768 }
769 else UNREACHABLE();
770
771 switch(textureFunction->sampler)
772 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400773 case EbtSampler2D: out << " return int2(width, height);"; break;
774 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
775 case EbtSamplerCube: out << " return int2(width, height);"; break;
776 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
777 case EbtISampler2D: out << " return int2(width, height);"; break;
778 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
779 case EbtISamplerCube: out << " return int2(width, height);"; break;
780 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
781 case EbtUSampler2D: out << " return int2(width, height);"; break;
782 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
783 case EbtUSamplerCube: out << " return int2(width, height);"; break;
784 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
785 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
786 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
787 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400788 default: UNREACHABLE();
789 }
790 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400791 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400792 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500793 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
794 {
795 out << " float width; float height; float layers; float levels;\n";
796
797 out << " uint mip = 0;\n";
798
799 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
800
801 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
802 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
803 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
804 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
805
806 // FACE_POSITIVE_X = 000b
807 // FACE_NEGATIVE_X = 001b
808 // FACE_POSITIVE_Y = 010b
809 // FACE_NEGATIVE_Y = 011b
810 // FACE_POSITIVE_Z = 100b
811 // FACE_NEGATIVE_Z = 101b
812 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
813
814 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
815 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
816 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
817
818 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
819 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
820 }
821 else if (IsIntegerSampler(textureFunction->sampler) &&
822 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400823 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400824 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400825 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400826 if (IsSamplerArray(textureFunction->sampler))
827 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400828 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400829
Nicolas Capens9edebd62013-08-06 10:59:10 -0400830 if (textureFunction->method == TextureFunction::LOD0)
831 {
832 out << " uint mip = 0;\n";
833 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400834 else if (textureFunction->method == TextureFunction::LOD0BIAS)
835 {
836 out << " uint mip = bias;\n";
837 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400838 else
839 {
840 if (textureFunction->method == TextureFunction::IMPLICIT ||
841 textureFunction->method == TextureFunction::BIAS)
842 {
843 out << " x.GetDimensions(0, width, height, layers, levels);\n"
844 " float2 tSized = float2(t.x * width, t.y * height);\n"
845 " float dx = length(ddx(tSized));\n"
846 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500847 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400848
849 if (textureFunction->method == TextureFunction::BIAS)
850 {
851 out << " lod += bias;\n";
852 }
853 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500854 else if (textureFunction->method == TextureFunction::GRAD)
855 {
856 out << " x.GetDimensions(0, width, height, layers, levels);\n"
857 " float lod = log2(max(length(ddx), length(ddy)));\n";
858 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400859
860 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
861 }
862
863 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400864 }
865 else
866 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400867 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400868
Nicolas Capens9edebd62013-08-06 10:59:10 -0400869 if (textureFunction->method == TextureFunction::LOD0)
870 {
871 out << " uint mip = 0;\n";
872 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400873 else if (textureFunction->method == TextureFunction::LOD0BIAS)
874 {
875 out << " uint mip = bias;\n";
876 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400877 else
878 {
879 if (textureFunction->method == TextureFunction::IMPLICIT ||
880 textureFunction->method == TextureFunction::BIAS)
881 {
882 out << " x.GetDimensions(0, width, height, levels);\n"
883 " float2 tSized = float2(t.x * width, t.y * height);\n"
884 " float dx = length(ddx(tSized));\n"
885 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500886 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400887
888 if (textureFunction->method == TextureFunction::BIAS)
889 {
890 out << " lod += bias;\n";
891 }
892 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500893 else if (textureFunction->method == TextureFunction::LOD)
894 {
895 out << " x.GetDimensions(0, width, height, levels);\n";
896 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500897 else if (textureFunction->method == TextureFunction::GRAD)
898 {
899 out << " x.GetDimensions(0, width, height, levels);\n"
900 " float lod = log2(max(length(ddx), length(ddy)));\n";
901 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400902
903 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
904 }
905
906 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400907 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400908 }
909 else if (IsSampler3D(textureFunction->sampler))
910 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400911 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400912
Nicolas Capens9edebd62013-08-06 10:59:10 -0400913 if (textureFunction->method == TextureFunction::LOD0)
914 {
915 out << " uint mip = 0;\n";
916 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400917 else if (textureFunction->method == TextureFunction::LOD0BIAS)
918 {
919 out << " uint mip = bias;\n";
920 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400921 else
922 {
923 if (textureFunction->method == TextureFunction::IMPLICIT ||
924 textureFunction->method == TextureFunction::BIAS)
925 {
926 out << " x.GetDimensions(0, width, height, depth, levels);\n"
927 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
928 " float dx = length(ddx(tSized));\n"
929 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500930 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400931
932 if (textureFunction->method == TextureFunction::BIAS)
933 {
934 out << " lod += bias;\n";
935 }
936 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500937 else if (textureFunction->method == TextureFunction::GRAD)
938 {
939 out << " x.GetDimensions(0, width, height, depth, levels);\n"
940 " float lod = log2(max(length(ddx), length(ddy)));\n";
941 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400942
943 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
944 }
945
946 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400947 }
948 else UNREACHABLE();
949 }
950
951 out << " return ";
952
953 // HLSL intrinsic
954 if (mOutputType == SH_HLSL9_OUTPUT)
955 {
956 switch(textureFunction->sampler)
957 {
958 case EbtSampler2D: out << "tex2D"; break;
959 case EbtSamplerCube: out << "texCUBE"; break;
960 default: UNREACHABLE();
961 }
962
Nicolas Capens75fb4752013-07-10 15:14:47 -0400963 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400964 {
965 case TextureFunction::IMPLICIT: out << "(s, "; break;
966 case TextureFunction::BIAS: out << "bias(s, "; break;
967 case TextureFunction::LOD: out << "lod(s, "; break;
968 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400969 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400970 default: UNREACHABLE();
971 }
972 }
973 else if (mOutputType == SH_HLSL11_OUTPUT)
974 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500975 if (textureFunction->method == TextureFunction::GRAD)
976 {
977 if (IsIntegerSampler(textureFunction->sampler))
978 {
979 out << "x.Load(";
980 }
981 else if (IsShadowSampler(textureFunction->sampler))
982 {
983 out << "x.SampleCmpLevelZero(s, ";
984 }
985 else
986 {
987 out << "x.SampleGrad(s, ";
988 }
989 }
990 else if (IsIntegerSampler(textureFunction->sampler) ||
991 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400992 {
993 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400994 }
Nicolas Capenscb127d32013-07-15 17:26:18 -0400995 else if (IsShadowSampler(textureFunction->sampler))
996 {
997 out << "x.SampleCmp(s, ";
998 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400999 else
1000 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001001 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001002 {
1003 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1004 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1005 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1006 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001007 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001008 default: UNREACHABLE();
1009 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001010 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001011 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001012 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001013
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001014 // Integer sampling requires integer addresses
1015 TString addressx = "";
1016 TString addressy = "";
1017 TString addressz = "";
1018 TString close = "";
1019
Nicolas Capensfc014542014-02-18 14:47:13 -05001020 if (IsIntegerSampler(textureFunction->sampler) ||
1021 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001022 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001023 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001024 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001025 case 2: out << "int3("; break;
1026 case 3: out << "int4("; break;
1027 default: UNREACHABLE();
1028 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001029
Nicolas Capensfc014542014-02-18 14:47:13 -05001030 // Convert from normalized floating-point to integer
1031 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001032 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001033 addressx = "int(floor(width * frac((";
1034 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001035
Nicolas Capensfc014542014-02-18 14:47:13 -05001036 if (IsSamplerArray(textureFunction->sampler))
1037 {
1038 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1039 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001040 else if (IsSamplerCube(textureFunction->sampler))
1041 {
1042 addressz = "((((";
1043 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001044 else
1045 {
1046 addressz = "int(floor(depth * frac((";
1047 }
1048
1049 close = "))))";
1050 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001051 }
1052 else
1053 {
1054 switch(hlslCoords)
1055 {
1056 case 2: out << "float2("; break;
1057 case 3: out << "float3("; break;
1058 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001059 default: UNREACHABLE();
1060 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001061 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001062
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001063 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001064
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001065 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001066 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001067 switch(textureFunction->coords)
1068 {
1069 case 3: proj = " / t.z"; break;
1070 case 4: proj = " / t.w"; break;
1071 default: UNREACHABLE();
1072 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001073 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001074
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001075 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001076
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001077 if (mOutputType == SH_HLSL9_OUTPUT)
1078 {
1079 if (hlslCoords >= 3)
1080 {
1081 if (textureFunction->coords < 3)
1082 {
1083 out << ", 0";
1084 }
1085 else
1086 {
1087 out << ", t.z" + proj;
1088 }
1089 }
1090
1091 if (hlslCoords == 4)
1092 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001093 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001094 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001095 case TextureFunction::BIAS: out << ", bias"; break;
1096 case TextureFunction::LOD: out << ", lod"; break;
1097 case TextureFunction::LOD0: out << ", 0"; break;
1098 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001099 default: UNREACHABLE();
1100 }
1101 }
1102
1103 out << "));\n";
1104 }
1105 else if (mOutputType == SH_HLSL11_OUTPUT)
1106 {
1107 if (hlslCoords >= 3)
1108 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001109 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1110 {
1111 out << ", face";
1112 }
1113 else
1114 {
1115 out << ", " + addressz + ("t.z" + proj) + close;
1116 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001117 }
1118
Nicolas Capensd11d5492014-02-19 17:06:10 -05001119 if (textureFunction->method == TextureFunction::GRAD)
1120 {
1121 if (IsIntegerSampler(textureFunction->sampler))
1122 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001123 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001124 }
1125 else if (IsShadowSampler(textureFunction->sampler))
1126 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001127 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001128 switch(textureFunction->coords)
1129 {
1130 case 3: out << "), t.z"; break;
1131 case 4: out << "), t.w"; break;
1132 default: UNREACHABLE();
1133 }
1134 }
1135 else
1136 {
1137 out << "), ddx, ddy";
1138 }
1139 }
1140 else if (IsIntegerSampler(textureFunction->sampler) ||
1141 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001142 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001143 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001144 }
1145 else if (IsShadowSampler(textureFunction->sampler))
1146 {
1147 // Compare value
1148 switch(textureFunction->coords)
1149 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001150 case 3: out << "), t.z"; break;
1151 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001152 default: UNREACHABLE();
1153 }
1154 }
1155 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001156 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001157 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001158 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001159 case TextureFunction::IMPLICIT: out << ")"; break;
1160 case TextureFunction::BIAS: out << "), bias"; break;
1161 case TextureFunction::LOD: out << "), lod"; break;
1162 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001163 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001164 default: UNREACHABLE();
1165 }
1166 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001167
1168 if (textureFunction->offset)
1169 {
1170 out << ", offset";
1171 }
1172
1173 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001174 }
1175 else UNREACHABLE();
1176 }
1177
1178 out << "\n"
1179 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001180 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001181 }
1182
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001183 if (mUsesFragCoord)
1184 {
1185 out << "#define GL_USES_FRAG_COORD\n";
1186 }
1187
1188 if (mUsesPointCoord)
1189 {
1190 out << "#define GL_USES_POINT_COORD\n";
1191 }
1192
1193 if (mUsesFrontFacing)
1194 {
1195 out << "#define GL_USES_FRONT_FACING\n";
1196 }
1197
1198 if (mUsesPointSize)
1199 {
1200 out << "#define GL_USES_POINT_SIZE\n";
1201 }
1202
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001203 if (mUsesFragDepth)
1204 {
1205 out << "#define GL_USES_FRAG_DEPTH\n";
1206 }
1207
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001208 if (mUsesDepthRange)
1209 {
1210 out << "#define GL_USES_DEPTH_RANGE\n";
1211 }
1212
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001213 if (mUsesXor)
1214 {
1215 out << "bool xor(bool p, bool q)\n"
1216 "{\n"
1217 " return (p || q) && !(p && q);\n"
1218 "}\n"
1219 "\n";
1220 }
1221
1222 if (mUsesMod1)
1223 {
1224 out << "float mod(float x, float y)\n"
1225 "{\n"
1226 " return x - y * floor(x / y);\n"
1227 "}\n"
1228 "\n";
1229 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001230
1231 if (mUsesMod2v)
1232 {
1233 out << "float2 mod(float2 x, float2 y)\n"
1234 "{\n"
1235 " return x - y * floor(x / y);\n"
1236 "}\n"
1237 "\n";
1238 }
1239
1240 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001241 {
1242 out << "float2 mod(float2 x, float y)\n"
1243 "{\n"
1244 " return x - y * floor(x / y);\n"
1245 "}\n"
1246 "\n";
1247 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001248
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001249 if (mUsesMod3v)
1250 {
1251 out << "float3 mod(float3 x, float3 y)\n"
1252 "{\n"
1253 " return x - y * floor(x / y);\n"
1254 "}\n"
1255 "\n";
1256 }
1257
1258 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001259 {
1260 out << "float3 mod(float3 x, float y)\n"
1261 "{\n"
1262 " return x - y * floor(x / y);\n"
1263 "}\n"
1264 "\n";
1265 }
1266
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001267 if (mUsesMod4v)
1268 {
1269 out << "float4 mod(float4 x, float4 y)\n"
1270 "{\n"
1271 " return x - y * floor(x / y);\n"
1272 "}\n"
1273 "\n";
1274 }
1275
1276 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001277 {
1278 out << "float4 mod(float4 x, float y)\n"
1279 "{\n"
1280 " return x - y * floor(x / y);\n"
1281 "}\n"
1282 "\n";
1283 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001284
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001285 if (mUsesFaceforward1)
1286 {
1287 out << "float faceforward(float N, float I, float Nref)\n"
1288 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001289 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001290 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001291 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001292 " }\n"
1293 " else\n"
1294 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001295 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001296 " }\n"
1297 "}\n"
1298 "\n";
1299 }
1300
1301 if (mUsesFaceforward2)
1302 {
1303 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1304 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001305 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001306 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001307 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001308 " }\n"
1309 " else\n"
1310 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001311 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001312 " }\n"
1313 "}\n"
1314 "\n";
1315 }
1316
1317 if (mUsesFaceforward3)
1318 {
1319 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1320 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001321 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001322 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001323 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001324 " }\n"
1325 " else\n"
1326 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001327 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001328 " }\n"
1329 "}\n"
1330 "\n";
1331 }
1332
1333 if (mUsesFaceforward4)
1334 {
1335 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1336 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001337 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001338 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001339 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001340 " }\n"
1341 " else\n"
1342 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001343 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001344 " }\n"
1345 "}\n"
1346 "\n";
1347 }
1348
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001349 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001350 {
1351 out << "float atanyx(float y, float x)\n"
1352 "{\n"
1353 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1354 " return atan2(y, x);\n"
1355 "}\n";
1356 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001357
1358 if (mUsesAtan2_2)
1359 {
1360 out << "float2 atanyx(float2 y, float2 x)\n"
1361 "{\n"
1362 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1363 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1364 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1365 "}\n";
1366 }
1367
1368 if (mUsesAtan2_3)
1369 {
1370 out << "float3 atanyx(float3 y, float3 x)\n"
1371 "{\n"
1372 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1373 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1374 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1375 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1376 "}\n";
1377 }
1378
1379 if (mUsesAtan2_4)
1380 {
1381 out << "float4 atanyx(float4 y, float4 x)\n"
1382 "{\n"
1383 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1384 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1385 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1386 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1387 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1388 "}\n";
1389 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001390}
1391
1392void OutputHLSL::visitSymbol(TIntermSymbol *node)
1393{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001394 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001395
Jamie Madill570e04d2013-06-21 09:15:33 -04001396 // Handle accessing std140 structs by value
1397 if (mFlaggedStructMappedNames.count(node) > 0)
1398 {
1399 out << mFlaggedStructMappedNames[node];
1400 return;
1401 }
1402
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001403 TString name = node->getSymbol();
1404
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001405 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001406 {
1407 mUsesDepthRange = true;
1408 out << name;
1409 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001410 else
1411 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001412 TQualifier qualifier = node->getQualifier();
1413
1414 if (qualifier == EvqUniform)
1415 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001416 const TType& nodeType = node->getType();
1417 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1418
1419 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001420 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001421 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001422 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001423 else
1424 {
1425 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001426 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001427
Jamie Madill033dae62014-06-18 12:56:28 -04001428 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001429 }
Jamie Madill19571812013-08-12 15:26:34 -07001430 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001431 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001432 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001433 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001434 }
Jamie Madill033dae62014-06-18 12:56:28 -04001435 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001436 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001437 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001438 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001439 }
Jamie Madill19571812013-08-12 15:26:34 -07001440 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001441 {
1442 mReferencedOutputVariables[name] = node;
1443 out << "out_" << name;
1444 }
1445 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001446 {
1447 out << "gl_Color[0]";
1448 mUsesFragColor = true;
1449 }
1450 else if (qualifier == EvqFragData)
1451 {
1452 out << "gl_Color";
1453 mUsesFragData = true;
1454 }
1455 else if (qualifier == EvqFragCoord)
1456 {
1457 mUsesFragCoord = true;
1458 out << name;
1459 }
1460 else if (qualifier == EvqPointCoord)
1461 {
1462 mUsesPointCoord = true;
1463 out << name;
1464 }
1465 else if (qualifier == EvqFrontFacing)
1466 {
1467 mUsesFrontFacing = true;
1468 out << name;
1469 }
1470 else if (qualifier == EvqPointSize)
1471 {
1472 mUsesPointSize = true;
1473 out << name;
1474 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001475 else if (name == "gl_FragDepthEXT")
1476 {
1477 mUsesFragDepth = true;
1478 out << "gl_Depth";
1479 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001480 else if (qualifier == EvqInternal)
1481 {
1482 out << name;
1483 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001484 else
1485 {
Jamie Madill033dae62014-06-18 12:56:28 -04001486 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001487 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001488 }
1489}
1490
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001491void OutputHLSL::visitRaw(TIntermRaw *node)
1492{
1493 mBody << node->getRawText();
1494}
1495
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001496bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1497{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001498 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001499
Jamie Madill570e04d2013-06-21 09:15:33 -04001500 // Handle accessing std140 structs by value
1501 if (mFlaggedStructMappedNames.count(node) > 0)
1502 {
1503 out << mFlaggedStructMappedNames[node];
1504 return false;
1505 }
1506
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001507 switch (node->getOp())
1508 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001509 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001510 case EOpInitialize:
1511 if (visit == PreVisit)
1512 {
1513 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1514 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1515 // new variable is created before the assignment is evaluated), so we need to convert
1516 // this to "float t = x, x = t;".
1517
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001518 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1519 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001520
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001521 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1522 expression->traverse(&searchSymbol);
1523 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001524
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001525 if (sameSymbol)
1526 {
1527 // Type already printed
1528 out << "t" + str(mUniqueIndex) + " = ";
1529 expression->traverse(this);
1530 out << ", ";
1531 symbolNode->traverse(this);
1532 out << " = t" + str(mUniqueIndex);
1533
1534 mUniqueIndex++;
1535 return false;
1536 }
1537 }
1538 else if (visit == InVisit)
1539 {
1540 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001541 }
1542 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001543 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1544 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1545 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1546 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1547 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1548 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001549 if (visit == PreVisit)
1550 {
1551 out << "(";
1552 }
1553 else if (visit == InVisit)
1554 {
1555 out << " = mul(";
1556 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001557 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001558 }
1559 else
1560 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001561 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001562 }
1563 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001564 case EOpMatrixTimesMatrixAssign:
1565 if (visit == PreVisit)
1566 {
1567 out << "(";
1568 }
1569 else if (visit == InVisit)
1570 {
1571 out << " = mul(";
1572 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001573 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001574 }
1575 else
1576 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001577 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001578 }
1579 break;
1580 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001581 case EOpModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001582 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001583 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001584 const TType& leftType = node->getLeft()->getType();
1585 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001586 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001587 if (visit == PreVisit)
1588 {
1589 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1590 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001591 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001592 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001593 return false;
1594 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001595 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001596 else
1597 {
1598 outputTriplet(visit, "", "[", "]");
1599 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001600 }
1601 break;
1602 case EOpIndexIndirect:
1603 // We do not currently support indirect references to interface blocks
1604 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1605 outputTriplet(visit, "", "[", "]");
1606 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001607 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001608 if (visit == InVisit)
1609 {
1610 const TStructure* structure = node->getLeft()->getType().getStruct();
1611 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1612 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001613 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001614
1615 return false;
1616 }
1617 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001618 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001619 if (visit == InVisit)
1620 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001621 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1622 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1623 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001624 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001625
1626 return false;
1627 }
1628 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001629 case EOpVectorSwizzle:
1630 if (visit == InVisit)
1631 {
1632 out << ".";
1633
1634 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1635
1636 if (swizzle)
1637 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001638 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001640 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001641 {
1642 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1643
1644 if (element)
1645 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001646 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001647
1648 switch (i)
1649 {
1650 case 0: out << "x"; break;
1651 case 1: out << "y"; break;
1652 case 2: out << "z"; break;
1653 case 3: out << "w"; break;
1654 default: UNREACHABLE();
1655 }
1656 }
1657 else UNREACHABLE();
1658 }
1659 }
1660 else UNREACHABLE();
1661
1662 return false; // Fully processed
1663 }
1664 break;
1665 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1666 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1667 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1668 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001669 case EOpMod: outputTriplet(visit, "(", " % ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001670 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001671 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001672 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001673 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001674 if (node->getOp() == EOpEqual)
1675 {
1676 outputTriplet(visit, "(", " == ", ")");
1677 }
1678 else
1679 {
1680 outputTriplet(visit, "(", " != ", ")");
1681 }
1682 }
1683 else if (node->getLeft()->getBasicType() == EbtStruct)
1684 {
1685 if (node->getOp() == EOpEqual)
1686 {
1687 out << "(";
1688 }
1689 else
1690 {
1691 out << "!(";
1692 }
1693
Jamie Madill98493dd2013-07-08 14:39:03 -04001694 const TStructure &structure = *node->getLeft()->getType().getStruct();
1695 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001696
Jamie Madill98493dd2013-07-08 14:39:03 -04001697 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001698 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001699 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001700
1701 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001702 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001703 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001704 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001705
Jamie Madill98493dd2013-07-08 14:39:03 -04001706 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001707 {
1708 out << " && ";
1709 }
1710 }
1711
1712 out << ")";
1713
1714 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001715 }
1716 else
1717 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001718 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001719
1720 if (node->getOp() == EOpEqual)
1721 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001722 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001723 }
1724 else
1725 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001726 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001727 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001728 }
1729 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001730 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1731 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1732 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1733 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1734 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001735 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001736 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1737 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001738 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001739 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001740 if (node->getRight()->hasSideEffects())
1741 {
1742 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1743 return false;
1744 }
1745 else
1746 {
1747 outputTriplet(visit, "(", " || ", ")");
1748 return true;
1749 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001750 case EOpLogicalXor:
1751 mUsesXor = true;
1752 outputTriplet(visit, "xor(", ", ", ")");
1753 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001754 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001755 if (node->getRight()->hasSideEffects())
1756 {
1757 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1758 return false;
1759 }
1760 else
1761 {
1762 outputTriplet(visit, "(", " && ", ")");
1763 return true;
1764 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001765 default: UNREACHABLE();
1766 }
1767
1768 return true;
1769}
1770
1771bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1772{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 switch (node->getOp())
1774 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001775 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001776 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001777 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1778 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1779 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1780 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1781 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1782 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001783 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1784 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1785 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1786 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1787 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1788 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1789 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1790 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001791 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1792 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1793 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1794 case EOpAsinh:
1795 ASSERT(node->getUseEmulatedFunction());
1796 writeEmulatedFunctionTriplet(visit, "asinh(");
1797 break;
1798 case EOpAcosh:
1799 ASSERT(node->getUseEmulatedFunction());
1800 writeEmulatedFunctionTriplet(visit, "acosh(");
1801 break;
1802 case EOpAtanh:
1803 ASSERT(node->getUseEmulatedFunction());
1804 writeEmulatedFunctionTriplet(visit, "atanh(");
1805 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001806 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1807 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1808 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1809 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1810 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1811 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1812 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1813 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1814 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1815 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1816 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001817 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1818 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1819 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1820 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001821 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1822 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001823 case EOpDFdx:
1824 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1825 {
1826 outputTriplet(visit, "(", "", ", 0.0)");
1827 }
1828 else
1829 {
1830 outputTriplet(visit, "ddx(", "", ")");
1831 }
1832 break;
1833 case EOpDFdy:
1834 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1835 {
1836 outputTriplet(visit, "(", "", ", 0.0)");
1837 }
1838 else
1839 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001840 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001841 }
1842 break;
1843 case EOpFwidth:
1844 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1845 {
1846 outputTriplet(visit, "(", "", ", 0.0)");
1847 }
1848 else
1849 {
1850 outputTriplet(visit, "fwidth(", "", ")");
1851 }
1852 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001853 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1854 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855 default: UNREACHABLE();
1856 }
1857
1858 return true;
1859}
1860
1861bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1862{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001863 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001865 switch (node->getOp())
1866 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001867 case EOpSequence:
1868 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001869 if (mInsideFunction)
1870 {
Jamie Madill075edd82013-07-08 13:30:19 -04001871 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001872 out << "{\n";
1873 }
1874
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001875 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001876 {
Jamie Madill075edd82013-07-08 13:30:19 -04001877 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001878
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001879 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001880
1881 out << ";\n";
1882 }
1883
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001884 if (mInsideFunction)
1885 {
Jamie Madill075edd82013-07-08 13:30:19 -04001886 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001887 out << "}\n";
1888 }
1889
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001890 return false;
1891 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892 case EOpDeclaration:
1893 if (visit == PreVisit)
1894 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001895 TIntermSequence *sequence = node->getSequence();
1896 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001898 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001899 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001900 TStructure *structure = variable->getType().getStruct();
1901
1902 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001903 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001904 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001905 }
1906
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001907 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001909 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001910 {
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001911 if (isSingleStatement(*sit))
1912 {
1913 mUnfoldShortCircuit->traverse(*sit);
1914 }
1915
Nicolas Capensd974db42014-10-07 10:50:19 -04001916 if (!mInsideFunction)
1917 {
1918 out << "static ";
1919 }
1920
1921 out << TypeString(variable->getType()) + " ";
1922
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001923 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001925 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001927 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001928 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001929 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001930 }
1931 else
1932 {
1933 (*sit)->traverse(this);
1934 }
1935
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001936 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001937 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001938 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 }
1940 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001942 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1943 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001944 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001945 }
1946 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947 }
Jamie Madill033dae62014-06-18 12:56:28 -04001948 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001949 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001950 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001951 {
1952 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1953
1954 if (symbol)
1955 {
1956 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1957 mReferencedVaryings[symbol->getSymbol()] = symbol;
1958 }
1959 else
1960 {
1961 (*sit)->traverse(this);
1962 }
1963 }
1964 }
1965
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001966 return false;
1967 }
1968 else if (visit == InVisit)
1969 {
1970 out << ", ";
1971 }
1972 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001973 case EOpInvariantDeclaration:
1974 // Do not do any translation
1975 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001976 case EOpPrototype:
1977 if (visit == PreVisit)
1978 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001979 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001980
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001981 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001982
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001983 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001984 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001985 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001986
1987 if (symbol)
1988 {
1989 out << argumentString(symbol);
1990
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001991 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001992 {
1993 out << ", ";
1994 }
1995 }
1996 else UNREACHABLE();
1997 }
1998
1999 out << ");\n";
2000
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002001 // Also prototype the Lod0 variant if needed
2002 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2003 {
2004 mOutputLod0Function = true;
2005 node->traverse(this);
2006 mOutputLod0Function = false;
2007 }
2008
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002009 return false;
2010 }
2011 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002012 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013 case EOpFunction:
2014 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002015 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016
Jamie Madill033dae62014-06-18 12:56:28 -04002017 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002018
2019 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002020 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002021 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002023 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024 {
Jamie Madill033dae62014-06-18 12:56:28 -04002025 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002026 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002027
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002028 TIntermSequence *sequence = node->getSequence();
2029 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002030
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002031 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002032 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002033 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002034
2035 if (symbol)
2036 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002037 TStructure *structure = symbol->getType().getStruct();
2038
2039 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002040 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002041 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002042 }
2043
2044 out << argumentString(symbol);
2045
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002046 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002047 {
2048 out << ", ";
2049 }
2050 }
2051 else UNREACHABLE();
2052 }
2053
2054 out << ")\n"
2055 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002056
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002057 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002058 {
2059 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002060 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002061 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002063
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002064 out << "}\n";
2065
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002066 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2067 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002068 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002069 {
2070 mOutputLod0Function = true;
2071 node->traverse(this);
2072 mOutputLod0Function = false;
2073 }
2074 }
2075
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002076 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 }
2078 break;
2079 case EOpFunctionCall:
2080 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002081 TString name = TFunction::unmangleName(node->getName());
2082 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002083 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002084
2085 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 {
Jamie Madill033dae62014-06-18 12:56:28 -04002087 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 }
2089 else
2090 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002091 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002092
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002093 TextureFunction textureFunction;
2094 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002095 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002096 textureFunction.method = TextureFunction::IMPLICIT;
2097 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002098 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002099
2100 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002101 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002102 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002103 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002104 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002105 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002106 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002107 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002108 }
Nicolas Capens46485082014-04-15 13:12:50 -04002109 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2110 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002111 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002112 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002113 }
Nicolas Capens46485082014-04-15 13:12:50 -04002114 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002115 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002116 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002117 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002118 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002119 else if (name == "textureSize")
2120 {
2121 textureFunction.method = TextureFunction::SIZE;
2122 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002123 else if (name == "textureOffset")
2124 {
2125 textureFunction.method = TextureFunction::IMPLICIT;
2126 textureFunction.offset = true;
2127 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002128 else if (name == "textureProjOffset")
2129 {
2130 textureFunction.method = TextureFunction::IMPLICIT;
2131 textureFunction.offset = true;
2132 textureFunction.proj = true;
2133 }
2134 else if (name == "textureLodOffset")
2135 {
2136 textureFunction.method = TextureFunction::LOD;
2137 textureFunction.offset = true;
2138 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002139 else if (name == "textureProjLodOffset")
2140 {
2141 textureFunction.method = TextureFunction::LOD;
2142 textureFunction.proj = true;
2143 textureFunction.offset = true;
2144 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002145 else if (name == "texelFetch")
2146 {
2147 textureFunction.method = TextureFunction::FETCH;
2148 }
2149 else if (name == "texelFetchOffset")
2150 {
2151 textureFunction.method = TextureFunction::FETCH;
2152 textureFunction.offset = true;
2153 }
Nicolas Capens46485082014-04-15 13:12:50 -04002154 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002155 {
2156 textureFunction.method = TextureFunction::GRAD;
2157 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002158 else if (name == "textureGradOffset")
2159 {
2160 textureFunction.method = TextureFunction::GRAD;
2161 textureFunction.offset = true;
2162 }
Nicolas Capens46485082014-04-15 13:12:50 -04002163 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002164 {
2165 textureFunction.method = TextureFunction::GRAD;
2166 textureFunction.proj = true;
2167 }
2168 else if (name == "textureProjGradOffset")
2169 {
2170 textureFunction.method = TextureFunction::GRAD;
2171 textureFunction.proj = true;
2172 textureFunction.offset = true;
2173 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002174 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002175
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002176 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002177 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002178 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2179
2180 if (textureFunction.offset)
2181 {
2182 mandatoryArgumentCount++;
2183 }
2184
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002185 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002186
Jamie Madill183bde52014-07-02 15:31:19 -04002187 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002188 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002189 if (bias)
2190 {
2191 textureFunction.method = TextureFunction::LOD0BIAS;
2192 }
2193 else
2194 {
2195 textureFunction.method = TextureFunction::LOD0;
2196 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002197 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002198 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002199 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002200 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002201 }
2202 }
2203
2204 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002205
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002206 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002207 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002208
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002209 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002210 {
2211 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2212 {
2213 out << "texture_";
2214 (*arg)->traverse(this);
2215 out << ", sampler_";
2216 }
2217
2218 (*arg)->traverse(this);
2219
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002220 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002221 {
2222 out << ", ";
2223 }
2224 }
2225
2226 out << ")";
2227
2228 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002229 }
2230 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002231 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002232 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2233 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2234 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2235 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2236 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2237 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2238 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2239 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2240 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2241 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2242 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2243 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2244 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2245 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2246 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2247 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2248 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2249 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2250 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002251 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002252 {
Jamie Madill033dae62014-06-18 12:56:28 -04002253 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002254 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002255 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2256 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002257 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002258 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2259 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2260 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2261 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2262 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2263 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002264 case EOpMod:
2265 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002266 // We need to look at the number of components in both arguments
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002267 const int modValue = (*node->getSequence())[0]->getAsTyped()->getNominalSize() * 10 +
2268 (*node->getSequence())[1]->getAsTyped()->getNominalSize();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002269 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002270 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002271 case 11: mUsesMod1 = true; break;
2272 case 22: mUsesMod2v = true; break;
2273 case 21: mUsesMod2f = true; break;
2274 case 33: mUsesMod3v = true; break;
2275 case 31: mUsesMod3f = true; break;
2276 case 44: mUsesMod4v = true; break;
2277 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002278 default: UNREACHABLE();
2279 }
2280
2281 outputTriplet(visit, "mod(", ", ", ")");
2282 }
2283 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002284 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002286 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2287 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize())
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002288 {
2289 case 1: mUsesAtan2_1 = true; break;
2290 case 2: mUsesAtan2_2 = true; break;
2291 case 3: mUsesAtan2_3 = true; break;
2292 case 4: mUsesAtan2_4 = true; break;
2293 default: UNREACHABLE();
2294 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002295 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 break;
2297 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2298 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2299 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2300 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2301 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2302 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2303 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2304 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2305 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002306 case EOpFaceForward:
2307 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002308 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002309 {
2310 case 1: mUsesFaceforward1 = true; break;
2311 case 2: mUsesFaceforward2 = true; break;
2312 case 3: mUsesFaceforward3 = true; break;
2313 case 4: mUsesFaceforward4 = true; break;
2314 default: UNREACHABLE();
2315 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002316
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002317 outputTriplet(visit, "faceforward(", ", ", ")");
2318 }
2319 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002320 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2321 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2322 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323 default: UNREACHABLE();
2324 }
2325
2326 return true;
2327}
2328
2329bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2330{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002331 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002333 if (node->usesTernaryOperator())
2334 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002335 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002336 }
2337 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002339 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002340
Corentin Wallez80bacde2014-11-10 12:07:37 -08002341 // D3D errors when there is a gradient operation in a loop in an unflattened if
2342 // however flattening all the ifs in branch heavy shaders made D3D error too.
2343 // As a temporary workaround we flatten the ifs only if there is at least a loop
2344 // present somewhere in the shader.
2345 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2346 {
2347 out << "FLATTEN ";
2348 }
2349
2350 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002351
2352 node->getCondition()->traverse(this);
2353
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002354 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002355
Jamie Madill075edd82013-07-08 13:30:19 -04002356 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002357 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002359 bool discard = false;
2360
daniel@transgaming.combb885322010-04-15 20:45:24 +00002361 if (node->getTrueBlock())
2362 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002363 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002364
2365 // Detect true discard
2366 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002367 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368
Jamie Madill075edd82013-07-08 13:30:19 -04002369 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002370 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002371
2372 if (node->getFalseBlock())
2373 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002374 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002375
Jamie Madill075edd82013-07-08 13:30:19 -04002376 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002377 out << "{\n";
2378
Jamie Madill075edd82013-07-08 13:30:19 -04002379 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002380 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002381
Jamie Madill075edd82013-07-08 13:30:19 -04002382 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002383 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002384
2385 // Detect false discard
2386 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2387 }
2388
2389 // ANGLE issue 486: Detect problematic conditional discard
2390 if (discard && FindSideEffectRewriting::search(node))
2391 {
2392 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002393 }
2394 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395
2396 return false;
2397}
2398
2399void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2400{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002401 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402}
2403
2404bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2405{
Nicolas Capens655fe362014-04-11 13:12:34 -04002406 mNestedLoopDepth++;
2407
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002408 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2409
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002410 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002411 {
2412 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2413 }
2414
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002415 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002416 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002417 if (handleExcessiveLoop(node))
2418 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002419 mInsideDiscontinuousLoop = wasDiscontinuous;
2420 mNestedLoopDepth--;
2421
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002422 return false;
2423 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002424 }
2425
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002426 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427
alokp@chromium.org52813552010-11-16 18:36:09 +00002428 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002430 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002431
Jamie Madill075edd82013-07-08 13:30:19 -04002432 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002433 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 }
2435 else
2436 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002437 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002438
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439 if (node->getInit())
2440 {
2441 node->getInit()->traverse(this);
2442 }
2443
2444 out << "; ";
2445
alokp@chromium.org52813552010-11-16 18:36:09 +00002446 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002448 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 }
2450
2451 out << "; ";
2452
alokp@chromium.org52813552010-11-16 18:36:09 +00002453 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002455 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 }
2457
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002458 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002459
Jamie Madill075edd82013-07-08 13:30:19 -04002460 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002461 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 }
2463
2464 if (node->getBody())
2465 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002466 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 }
2468
Jamie Madill075edd82013-07-08 13:30:19 -04002469 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002470 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471
alokp@chromium.org52813552010-11-16 18:36:09 +00002472 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 {
Jamie Madill075edd82013-07-08 13:30:19 -04002474 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002475 out << "while(\n";
2476
alokp@chromium.org52813552010-11-16 18:36:09 +00002477 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478
daniel@transgaming.com73536982012-03-21 20:45:49 +00002479 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 }
2481
daniel@transgaming.com73536982012-03-21 20:45:49 +00002482 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002484 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002485 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002486
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487 return false;
2488}
2489
2490bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2491{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002492 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493
2494 switch (node->getFlowOp())
2495 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002496 case EOpKill:
2497 outputTriplet(visit, "discard;\n", "", "");
2498 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002499 case EOpBreak:
2500 if (visit == PreVisit)
2501 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002502 if (mNestedLoopDepth > 1)
2503 {
2504 mUsesNestedBreak = true;
2505 }
2506
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002507 if (mExcessiveLoopIndex)
2508 {
2509 out << "{Break";
2510 mExcessiveLoopIndex->traverse(this);
2511 out << " = true; break;}\n";
2512 }
2513 else
2514 {
2515 out << "break;\n";
2516 }
2517 }
2518 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002519 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002520 case EOpReturn:
2521 if (visit == PreVisit)
2522 {
2523 if (node->getExpression())
2524 {
2525 out << "return ";
2526 }
2527 else
2528 {
2529 out << "return;\n";
2530 }
2531 }
2532 else if (visit == PostVisit)
2533 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002534 if (node->getExpression())
2535 {
2536 out << ";\n";
2537 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538 }
2539 break;
2540 default: UNREACHABLE();
2541 }
2542
2543 return true;
2544}
2545
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002546void OutputHLSL::traverseStatements(TIntermNode *node)
2547{
2548 if (isSingleStatement(node))
2549 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002550 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002551 }
2552
2553 node->traverse(this);
2554}
2555
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002556bool OutputHLSL::isSingleStatement(TIntermNode *node)
2557{
2558 TIntermAggregate *aggregate = node->getAsAggregate();
2559
2560 if (aggregate)
2561 {
2562 if (aggregate->getOp() == EOpSequence)
2563 {
2564 return false;
2565 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002566 else if (aggregate->getOp() == EOpDeclaration)
2567 {
2568 // Declaring multiple comma-separated variables must be considered multiple statements
2569 // because each individual declaration has side effects which are visible in the next.
2570 return false;
2571 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002572 else
2573 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002574 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002575 {
2576 if (!isSingleStatement(*sit))
2577 {
2578 return false;
2579 }
2580 }
2581
2582 return true;
2583 }
2584 }
2585
2586 return true;
2587}
2588
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002589// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2590// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002591bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2592{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002593 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002594 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002595
2596 // Parse loops of the form:
2597 // for(int index = initial; index [comparator] limit; index += increment)
2598 TIntermSymbol *index = NULL;
2599 TOperator comparator = EOpNull;
2600 int initial = 0;
2601 int limit = 0;
2602 int increment = 0;
2603
2604 // Parse index name and intial value
2605 if (node->getInit())
2606 {
2607 TIntermAggregate *init = node->getInit()->getAsAggregate();
2608
2609 if (init)
2610 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002611 TIntermSequence *sequence = init->getSequence();
2612 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002613
2614 if (variable && variable->getQualifier() == EvqTemporary)
2615 {
2616 TIntermBinary *assign = variable->getAsBinaryNode();
2617
2618 if (assign->getOp() == EOpInitialize)
2619 {
2620 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2621 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2622
2623 if (symbol && constant)
2624 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002625 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002626 {
2627 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002628 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002629 }
2630 }
2631 }
2632 }
2633 }
2634 }
2635
2636 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002637 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002638 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002639 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002640
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002641 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2642 {
2643 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2644
2645 if (constant)
2646 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002647 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002648 {
2649 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002650 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002651 }
2652 }
2653 }
2654 }
2655
2656 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002657 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002658 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002659 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2660 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002661
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002662 if (binaryTerminal)
2663 {
2664 TOperator op = binaryTerminal->getOp();
2665 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2666
2667 if (constant)
2668 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002669 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002670 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002671 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002672
2673 switch (op)
2674 {
2675 case EOpAddAssign: increment = value; break;
2676 case EOpSubAssign: increment = -value; break;
2677 default: UNIMPLEMENTED();
2678 }
2679 }
2680 }
2681 }
2682 else if (unaryTerminal)
2683 {
2684 TOperator op = unaryTerminal->getOp();
2685
2686 switch (op)
2687 {
2688 case EOpPostIncrement: increment = 1; break;
2689 case EOpPostDecrement: increment = -1; break;
2690 case EOpPreIncrement: increment = 1; break;
2691 case EOpPreDecrement: increment = -1; break;
2692 default: UNIMPLEMENTED();
2693 }
2694 }
2695 }
2696
2697 if (index != NULL && comparator != EOpNull && increment != 0)
2698 {
2699 if (comparator == EOpLessThanEqual)
2700 {
2701 comparator = EOpLessThan;
2702 limit += 1;
2703 }
2704
2705 if (comparator == EOpLessThan)
2706 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002707 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002708
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002709 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002710 {
2711 return false; // Not an excessive loop
2712 }
2713
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002714 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2715 mExcessiveLoopIndex = index;
2716
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002717 out << "{int ";
2718 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002719 out << ";\n"
2720 "bool Break";
2721 index->traverse(this);
2722 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002723
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002724 bool firstLoopFragment = true;
2725
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002726 while (iterations > 0)
2727 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002728 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002729
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002730 if (!firstLoopFragment)
2731 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002732 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002733 index->traverse(this);
2734 out << ") {\n";
2735 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002736
2737 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2738 {
2739 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2740 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002741
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002742 // for(int index = initial; index < clampedLimit; index += increment)
2743
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002744 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002745 index->traverse(this);
2746 out << " = ";
2747 out << initial;
2748
2749 out << "; ";
2750 index->traverse(this);
2751 out << " < ";
2752 out << clampedLimit;
2753
2754 out << "; ";
2755 index->traverse(this);
2756 out << " += ";
2757 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002758 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002759
Jamie Madill075edd82013-07-08 13:30:19 -04002760 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002761 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002762
2763 if (node->getBody())
2764 {
2765 node->getBody()->traverse(this);
2766 }
2767
Jamie Madill075edd82013-07-08 13:30:19 -04002768 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002769 out << ";}\n";
2770
2771 if (!firstLoopFragment)
2772 {
2773 out << "}\n";
2774 }
2775
2776 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002777
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002778 initial += MAX_LOOP_ITERATIONS * increment;
2779 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002780 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002781
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002782 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002783
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002784 mExcessiveLoopIndex = restoreIndex;
2785
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002786 return true;
2787 }
2788 else UNIMPLEMENTED();
2789 }
2790
2791 return false; // Not handled as an excessive loop
2792}
2793
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002794void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002796 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002797
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002798 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002799 {
2800 out << preString;
2801 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002802 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002803 {
2804 out << inString;
2805 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002806 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807 {
2808 out << postString;
2809 }
2810}
2811
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002812void OutputHLSL::outputLineDirective(int line)
2813{
2814 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2815 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002816 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002817 mBody << "#line " << line;
2818
2819 if (mContext.sourcePath)
2820 {
2821 mBody << " \"" << mContext.sourcePath << "\"";
2822 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002823
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002824 mBody << "\n";
2825 }
2826}
2827
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002828TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2829{
2830 TQualifier qualifier = symbol->getQualifier();
2831 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002832 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002833
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002834 if (name.empty()) // HLSL demands named arguments, also for prototypes
2835 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002836 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002837 }
2838 else
2839 {
Jamie Madill033dae62014-06-18 12:56:28 -04002840 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002841 }
2842
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002843 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2844 {
Jamie Madill033dae62014-06-18 12:56:28 -04002845 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002846 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002847 }
2848
Jamie Madill033dae62014-06-18 12:56:28 -04002849 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002850}
2851
2852TString OutputHLSL::initializer(const TType &type)
2853{
2854 TString string;
2855
Jamie Madill94bf7f22013-07-08 13:31:15 -04002856 size_t size = type.getObjectSize();
2857 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002858 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002859 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002860
Jamie Madill94bf7f22013-07-08 13:31:15 -04002861 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002862 {
2863 string += ", ";
2864 }
2865 }
2866
daniel@transgaming.comead23042010-04-29 03:35:36 +00002867 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002868}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002869
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002870void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2871{
2872 TInfoSinkBase &out = mBody;
2873
2874 if (visit == PreVisit)
2875 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002876 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002877
2878 out << name + "(";
2879 }
2880 else if (visit == InVisit)
2881 {
2882 out << ", ";
2883 }
2884 else if (visit == PostVisit)
2885 {
2886 out << ")";
2887 }
2888}
2889
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002890const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2891{
2892 TInfoSinkBase &out = mBody;
2893
Jamie Madill98493dd2013-07-08 14:39:03 -04002894 const TStructure* structure = type.getStruct();
2895 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002896 {
Jamie Madill033dae62014-06-18 12:56:28 -04002897 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002898
Jamie Madill98493dd2013-07-08 14:39:03 -04002899 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002900
Jamie Madill98493dd2013-07-08 14:39:03 -04002901 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002902 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002903 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002904 constUnion = writeConstantUnion(*fieldType, constUnion);
2905
Jamie Madill98493dd2013-07-08 14:39:03 -04002906 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002907 {
2908 out << ", ";
2909 }
2910 }
2911
2912 out << ")";
2913 }
2914 else
2915 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002916 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002917 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002918
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002919 if (writeType)
2920 {
Jamie Madill033dae62014-06-18 12:56:28 -04002921 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002922 }
2923
Jamie Madill94bf7f22013-07-08 13:31:15 -04002924 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002925 {
2926 switch (constUnion->getType())
2927 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002928 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002929 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002930 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002931 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002932 default: UNREACHABLE();
2933 }
2934
2935 if (i != size - 1)
2936 {
2937 out << ", ";
2938 }
2939 }
2940
2941 if (writeType)
2942 {
2943 out << ")";
2944 }
2945 }
2946
2947 return constUnion;
2948}
2949
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002950void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2951{
2952 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2953 outputTriplet(visit, preString.c_str(), ", ", ")");
2954}
2955
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002956}