blob: 0df234399edaf7c5c6a252bbd7fc1019adefb824 [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"
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/compilerdebug.h"
13#include "compiler/translator/InfoSink.h"
14#include "compiler/translator/DetectDiscontinuity.h"
15#include "compiler/translator/SearchSymbol.h"
16#include "compiler/translator/UnfoldShortCircuit.h"
Geoff Lang17732822013-08-29 13:46:49 -040017#include "compiler/translator/FlagStd140Structs.h"
Jamie Madill3c9eeb92013-11-04 11:09:26 -050018#include "compiler/translator/NodeSearch.h"
Jamie Madille53c98b2014-02-03 11:57:13 -050019#include "compiler/translator/RewriteElseBlocks.h"
Jamie Madill033dae62014-06-18 12:56:28 -040020#include "compiler/translator/UtilsHLSL.h"
21#include "compiler/translator/util.h"
Jamie Madillf91ce812014-06-13 10:04:34 -040022#include "compiler/translator/UniformHLSL.h"
Jamie Madill8daaba12014-06-13 10:04:33 -040023#include "compiler/translator/StructureHLSL.h"
Jamie Madill54ad4f82014-09-03 09:40:46 -040024#include "compiler/translator/TranslatorHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000026#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000027#include <cfloat>
28#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030namespace sh
31{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000032
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033TString OutputHLSL::TextureFunction::name() const
34{
35 TString name = "gl_texture";
36
Nicolas Capens6d232bb2013-07-08 15:56:38 -040037 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040038 {
39 name += "2D";
40 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040041 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040042 {
43 name += "3D";
44 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040045 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040046 {
47 name += "Cube";
48 }
49 else UNREACHABLE();
50
51 if (proj)
52 {
53 name += "Proj";
54 }
55
Nicolas Capensb1f45b72013-12-19 17:37:19 -050056 if (offset)
57 {
58 name += "Offset";
59 }
60
Nicolas Capens75fb4752013-07-10 15:14:47 -040061 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040062 {
Nicolas Capensfc014542014-02-18 14:47:13 -050063 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040064 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050065 case LOD: name += "Lod"; break;
66 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040067 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050068 case SIZE: name += "Size"; break;
69 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050070 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040071 default: UNREACHABLE();
72 }
73
74 return name + "(";
75}
76
77bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
78{
79 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040080 if (sampler > rhs.sampler) return false;
81
Nicolas Capense0ba27a2013-06-24 16:10:52 -040082 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040083 if (coords > rhs.coords) return false;
84
Nicolas Capense0ba27a2013-06-24 16:10:52 -040085 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040086 if (proj && !rhs.proj) return false;
87
88 if (!offset && rhs.offset) return true;
89 if (offset && !rhs.offset) return false;
90
Nicolas Capens75fb4752013-07-10 15:14:47 -040091 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040092 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040093
94 return false;
95}
96
Jamie Madill54ad4f82014-09-03 09:40:46 -040097OutputHLSL::OutputHLSL(TParseContext &context, TranslatorHLSL *parentTranslator)
98 : TIntermTraverser(true, true, true),
99 mContext(context),
100 mOutputType(parentTranslator->getOutputType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000102 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000103 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000104
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000105 mUsesFragColor = false;
106 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000107 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000108 mUsesFragCoord = false;
109 mUsesPointCoord = false;
110 mUsesFrontFacing = false;
111 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400112 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000113 mUsesXor = false;
114 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000115 mUsesMod2v = false;
116 mUsesMod2f = false;
117 mUsesMod3v = false;
118 mUsesMod3f = false;
119 mUsesMod4v = false;
120 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000121 mUsesFaceforward1 = false;
122 mUsesFaceforward2 = false;
123 mUsesFaceforward3 = false;
124 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000125 mUsesAtan2_1 = false;
126 mUsesAtan2_2 = false;
127 mUsesAtan2_3 = false;
128 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500129 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400130 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000131
Jamie Madill54ad4f82014-09-03 09:40:46 -0400132 const ShBuiltInResources &resources = parentTranslator->getResources();
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000133 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
134
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000135 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000136
137 mContainsLoopDiscontinuity = false;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800138 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000139 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000140 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400141 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000142
143 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000144
Jamie Madill8daaba12014-06-13 10:04:33 -0400145 mStructureHLSL = new StructureHLSL;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400146 mUniformHLSL = new UniformHLSL(mStructureHLSL, parentTranslator);
Jamie Madill8daaba12014-06-13 10:04:33 -0400147
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000148 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000149 {
Jamie Madill183bde52014-07-02 15:31:19 -0400150 if (mContext.shaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000151 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400152 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
153 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000154 }
155 else
156 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400157 // Reserve registers for dx_DepthRange and dx_ViewAdjust
158 mUniformHLSL->reserveUniformRegisters(2);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000159 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000160 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000161
Jamie Madillf91ce812014-06-13 10:04:34 -0400162 // Reserve registers for the default uniform block and driver constants
163 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164}
165
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000166OutputHLSL::~OutputHLSL()
167{
Jamie Madill8daaba12014-06-13 10:04:33 -0400168 SafeDelete(mUnfoldShortCircuit);
169 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400170 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000171}
172
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000173void OutputHLSL::output()
174{
Jamie Madill183bde52014-07-02 15:31:19 -0400175 mContainsLoopDiscontinuity = mContext.shaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Corentin Wallez80bacde2014-11-10 12:07:37 -0800176 mContainsAnyLoop = containsAnyLoop(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400177 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
178 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000179
Jamie Madille53c98b2014-02-03 11:57:13 -0500180 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
181 // use a vertex attribute as a condition, and some related computation in the else block.
Jamie Madill183bde52014-07-02 15:31:19 -0400182 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500183 {
184 RewriteElseBlocks(mContext.treeRoot);
185 }
186
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000187 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 +0000188 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000189
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000190 mContext.infoSink().obj << mHeader.c_str();
191 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000192}
193
Jamie Madill570e04d2013-06-21 09:15:33 -0400194void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
195{
196 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
197 {
198 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
199
200 // This will mark the necessary block elements as referenced
201 flaggedNode->traverse(this);
202 TString structName(mBody.c_str());
203 mBody.erase();
204
205 mFlaggedStructOriginalNames[flaggedNode] = structName;
206
207 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
208 {
209 structName.erase(pos, 1);
210 }
211
212 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
213 }
214}
215
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000216TInfoSinkBase &OutputHLSL::getBodyStream()
217{
218 return mBody;
219}
220
Jamie Madill4e1fd412014-07-10 17:50:10 -0400221const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
222{
223 return mUniformHLSL->getInterfaceBlockRegisterMap();
224}
225
Jamie Madill9fe25e92014-07-18 10:33:08 -0400226const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
227{
228 return mUniformHLSL->getUniformRegisterMap();
229}
230
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000231int OutputHLSL::vectorSize(const TType &type) const
232{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000233 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000234 int arraySize = type.isArray() ? type.getArraySize() : 1;
235
236 return elementSize * arraySize;
237}
238
Jamie Madill98493dd2013-07-08 14:39:03 -0400239TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400240{
241 TString init;
242
243 TString preIndentString;
244 TString fullIndentString;
245
246 for (int spaces = 0; spaces < (indent * 4); spaces++)
247 {
248 preIndentString += ' ';
249 }
250
251 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
252 {
253 fullIndentString += ' ';
254 }
255
256 init += preIndentString + "{\n";
257
Jamie Madill98493dd2013-07-08 14:39:03 -0400258 const TFieldList &fields = structure.fields();
259 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400260 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400261 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400262 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400264
Jamie Madill98493dd2013-07-08 14:39:03 -0400265 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400266 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400268 }
269 else
270 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400271 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400272 }
273 }
274
275 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
276
277 return init;
278}
279
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280void OutputHLSL::header()
281{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000282 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000284 TString varyings;
285 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000287
Jamie Madill829f59e2013-11-13 19:40:54 -0500288 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400289 {
290 TIntermTyped *structNode = flaggedStructIt->first;
291 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400292 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400293 const TString &originalName = mFlaggedStructOriginalNames[structNode];
294
Jamie Madill033dae62014-06-18 12:56:28 -0400295 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400296 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400297 flaggedStructs += "\n";
298 }
299
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000300 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
301 {
302 const TType &type = varying->second->getType();
303 const TString &name = varying->second->getSymbol();
304
305 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400306 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
307 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000308 }
309
310 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
311 {
312 const TType &type = attribute->second->getType();
313 const TString &name = attribute->second->getSymbol();
314
Jamie Madill033dae62014-06-18 12:56:28 -0400315 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000316 }
317
Jamie Madill8daaba12014-06-13 10:04:33 -0400318 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400319
Jamie Madillf91ce812014-06-13 10:04:34 -0400320 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
321 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
322
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500323 if (mUsesDiscardRewriting)
324 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400325 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500326 }
327
Nicolas Capens655fe362014-04-11 13:12:34 -0400328 if (mUsesNestedBreak)
329 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400330 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400331 }
332
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400333 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
334 "#define LOOP [loop]\n"
335 "#define FLATTEN [flatten]\n"
336 "#else\n"
337 "#define LOOP\n"
338 "#define FLATTEN\n"
339 "#endif\n";
340
Jamie Madill183bde52014-07-02 15:31:19 -0400341 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000343 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000344 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000345
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000346 out << "// Varyings\n";
347 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400348 out << "\n";
349
350 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000351 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500352 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000353 {
Jamie Madill46131a32013-06-20 11:55:50 -0400354 const TString &variableName = outputVariableIt->first;
355 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400356
Jamie Madill033dae62014-06-18 12:56:28 -0400357 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400358 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000359 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000360 }
Jamie Madill46131a32013-06-20 11:55:50 -0400361 else
362 {
363 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
364
365 out << "static float4 gl_Color[" << numColorValues << "] =\n"
366 "{\n";
367 for (unsigned int i = 0; i < numColorValues; i++)
368 {
369 out << " float4(0, 0, 0, 0)";
370 if (i + 1 != numColorValues)
371 {
372 out << ",";
373 }
374 out << "\n";
375 }
376
377 out << "};\n";
378 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000379
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400380 if (mUsesFragDepth)
381 {
382 out << "static float gl_Depth = 0.0;\n";
383 }
384
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000385 if (mUsesFragCoord)
386 {
387 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
388 }
389
390 if (mUsesPointCoord)
391 {
392 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
393 }
394
395 if (mUsesFrontFacing)
396 {
397 out << "static bool gl_FrontFacing = false;\n";
398 }
399
400 out << "\n";
401
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000402 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000403 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000404 out << "struct gl_DepthRangeParameters\n"
405 "{\n"
406 " float near;\n"
407 " float far;\n"
408 " float diff;\n"
409 "};\n"
410 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000411 }
412
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000413 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000414 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000415 out << "cbuffer DriverConstants : register(b1)\n"
416 "{\n";
417
418 if (mUsesDepthRange)
419 {
420 out << " float3 dx_DepthRange : packoffset(c0);\n";
421 }
422
423 if (mUsesFragCoord)
424 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000425 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000426 }
427
428 if (mUsesFragCoord || mUsesFrontFacing)
429 {
430 out << " float3 dx_DepthFront : packoffset(c2);\n";
431 }
432
433 out << "};\n";
434 }
435 else
436 {
437 if (mUsesDepthRange)
438 {
439 out << "uniform float3 dx_DepthRange : register(c0);";
440 }
441
442 if (mUsesFragCoord)
443 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000444 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000445 }
446
447 if (mUsesFragCoord || mUsesFrontFacing)
448 {
449 out << "uniform float3 dx_DepthFront : register(c2);\n";
450 }
451 }
452
453 out << "\n";
454
455 if (mUsesDepthRange)
456 {
457 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
458 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000459 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000460
Jamie Madillf91ce812014-06-13 10:04:34 -0400461 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000462 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400463 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000464 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400465 out << flaggedStructs;
466 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000467 }
468
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000469 if (usingMRTExtension && mNumRenderTargets > 1)
470 {
471 out << "#define GL_USES_MRT\n";
472 }
473
474 if (mUsesFragColor)
475 {
476 out << "#define GL_USES_FRAG_COLOR\n";
477 }
478
479 if (mUsesFragData)
480 {
481 out << "#define GL_USES_FRAG_DATA\n";
482 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000484 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000486 out << "// Attributes\n";
487 out << attributes;
488 out << "\n"
489 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400490
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000491 if (mUsesPointSize)
492 {
493 out << "static float gl_PointSize = float(1);\n";
494 }
495
496 out << "\n"
497 "// Varyings\n";
498 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000499 out << "\n";
500
501 if (mUsesDepthRange)
502 {
503 out << "struct gl_DepthRangeParameters\n"
504 "{\n"
505 " float near;\n"
506 " float far;\n"
507 " float diff;\n"
508 "};\n"
509 "\n";
510 }
511
512 if (mOutputType == SH_HLSL11_OUTPUT)
513 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800514 out << "cbuffer DriverConstants : register(b1)\n"
515 "{\n";
516
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000517 if (mUsesDepthRange)
518 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800519 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000520 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800521
522 // dx_ViewAdjust will only be used in Feature Level 9 shaders.
523 // However, we declare it for all shaders (including Feature Level 10+).
524 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
525 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
526
527 out << "};\n"
528 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000529 }
530 else
531 {
532 if (mUsesDepthRange)
533 {
534 out << "uniform float3 dx_DepthRange : register(c0);\n";
535 }
536
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000537 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000538 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000539 }
540
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000541 if (mUsesDepthRange)
542 {
543 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
544 "\n";
545 }
546
Jamie Madillf91ce812014-06-13 10:04:34 -0400547 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000548 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400549 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000550 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400551 out << flaggedStructs;
552 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000553 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400554 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000555
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400556 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
557 {
558 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400559 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000560 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400561 switch(textureFunction->sampler)
562 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400563 case EbtSampler2D: out << "int2 "; break;
564 case EbtSampler3D: out << "int3 "; break;
565 case EbtSamplerCube: out << "int2 "; break;
566 case EbtSampler2DArray: out << "int3 "; break;
567 case EbtISampler2D: out << "int2 "; break;
568 case EbtISampler3D: out << "int3 "; break;
569 case EbtISamplerCube: out << "int2 "; break;
570 case EbtISampler2DArray: out << "int3 "; break;
571 case EbtUSampler2D: out << "int2 "; break;
572 case EbtUSampler3D: out << "int3 "; break;
573 case EbtUSamplerCube: out << "int2 "; break;
574 case EbtUSampler2DArray: out << "int3 "; break;
575 case EbtSampler2DShadow: out << "int2 "; break;
576 case EbtSamplerCubeShadow: out << "int2 "; break;
577 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400578 default: UNREACHABLE();
579 }
580 }
581 else // Sampling function
582 {
583 switch(textureFunction->sampler)
584 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400585 case EbtSampler2D: out << "float4 "; break;
586 case EbtSampler3D: out << "float4 "; break;
587 case EbtSamplerCube: out << "float4 "; break;
588 case EbtSampler2DArray: out << "float4 "; break;
589 case EbtISampler2D: out << "int4 "; break;
590 case EbtISampler3D: out << "int4 "; break;
591 case EbtISamplerCube: out << "int4 "; break;
592 case EbtISampler2DArray: out << "int4 "; break;
593 case EbtUSampler2D: out << "uint4 "; break;
594 case EbtUSampler3D: out << "uint4 "; break;
595 case EbtUSamplerCube: out << "uint4 "; break;
596 case EbtUSampler2DArray: out << "uint4 "; break;
597 case EbtSampler2DShadow: out << "float "; break;
598 case EbtSamplerCubeShadow: out << "float "; break;
599 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400600 default: UNREACHABLE();
601 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000602 }
603
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400604 // Function name
605 out << textureFunction->name();
606
607 // Argument list
608 int hlslCoords = 4;
609
610 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000611 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400612 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000613 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400614 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
615 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
616 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000617 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400618
Nicolas Capens75fb4752013-07-10 15:14:47 -0400619 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000620 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400621 case TextureFunction::IMPLICIT: break;
622 case TextureFunction::BIAS: hlslCoords = 4; break;
623 case TextureFunction::LOD: hlslCoords = 4; break;
624 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400625 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400626 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000627 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400628 }
629 else if (mOutputType == SH_HLSL11_OUTPUT)
630 {
631 switch(textureFunction->sampler)
632 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400633 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
634 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
635 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
636 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
637 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
638 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500639 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400640 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
641 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
642 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500643 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400644 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
645 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
646 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
647 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400648 default: UNREACHABLE();
649 }
650 }
651 else UNREACHABLE();
652
Nicolas Capensfc014542014-02-18 14:47:13 -0500653 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400654 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500655 switch(textureFunction->coords)
656 {
657 case 2: out << ", int2 t"; break;
658 case 3: out << ", int3 t"; break;
659 default: UNREACHABLE();
660 }
661 }
662 else // Floating-point coordinates (except textureSize)
663 {
664 switch(textureFunction->coords)
665 {
666 case 1: out << ", int lod"; break; // textureSize()
667 case 2: out << ", float2 t"; break;
668 case 3: out << ", float3 t"; break;
669 case 4: out << ", float4 t"; break;
670 default: UNREACHABLE();
671 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000672 }
673
Nicolas Capensd11d5492014-02-19 17:06:10 -0500674 if (textureFunction->method == TextureFunction::GRAD)
675 {
676 switch(textureFunction->sampler)
677 {
678 case EbtSampler2D:
679 case EbtISampler2D:
680 case EbtUSampler2D:
681 case EbtSampler2DArray:
682 case EbtISampler2DArray:
683 case EbtUSampler2DArray:
684 case EbtSampler2DShadow:
685 case EbtSampler2DArrayShadow:
686 out << ", float2 ddx, float2 ddy";
687 break;
688 case EbtSampler3D:
689 case EbtISampler3D:
690 case EbtUSampler3D:
691 case EbtSamplerCube:
692 case EbtISamplerCube:
693 case EbtUSamplerCube:
694 case EbtSamplerCubeShadow:
695 out << ", float3 ddx, float3 ddy";
696 break;
697 default: UNREACHABLE();
698 }
699 }
700
Nicolas Capens75fb4752013-07-10 15:14:47 -0400701 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000702 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400703 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400704 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400705 case TextureFunction::LOD: out << ", float lod"; break;
706 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400707 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400708 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500709 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500710 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400711 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000712 }
713
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500714 if (textureFunction->offset)
715 {
716 switch(textureFunction->sampler)
717 {
718 case EbtSampler2D: out << ", int2 offset"; break;
719 case EbtSampler3D: out << ", int3 offset"; break;
720 case EbtSampler2DArray: out << ", int2 offset"; break;
721 case EbtISampler2D: out << ", int2 offset"; break;
722 case EbtISampler3D: out << ", int3 offset"; break;
723 case EbtISampler2DArray: out << ", int2 offset"; break;
724 case EbtUSampler2D: out << ", int2 offset"; break;
725 case EbtUSampler3D: out << ", int3 offset"; break;
726 case EbtUSampler2DArray: out << ", int2 offset"; break;
727 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500728 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500729 default: UNREACHABLE();
730 }
731 }
732
Nicolas Capens84cfa122014-04-14 13:48:45 -0400733 if (textureFunction->method == TextureFunction::BIAS ||
734 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500735 {
736 out << ", float bias";
737 }
738
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400739 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400740 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400741
Nicolas Capens75fb4752013-07-10 15:14:47 -0400742 if (textureFunction->method == TextureFunction::SIZE)
743 {
744 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
745 {
746 if (IsSamplerArray(textureFunction->sampler))
747 {
748 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
749 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
750 }
751 else
752 {
753 out << " uint width; uint height; uint numberOfLevels;\n"
754 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
755 }
756 }
757 else if (IsSampler3D(textureFunction->sampler))
758 {
759 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
760 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
761 }
762 else UNREACHABLE();
763
764 switch(textureFunction->sampler)
765 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400766 case EbtSampler2D: out << " return int2(width, height);"; break;
767 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
768 case EbtSamplerCube: out << " return int2(width, height);"; break;
769 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
770 case EbtISampler2D: out << " return int2(width, height);"; break;
771 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
772 case EbtISamplerCube: out << " return int2(width, height);"; break;
773 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
774 case EbtUSampler2D: out << " return int2(width, height);"; break;
775 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
776 case EbtUSamplerCube: out << " return int2(width, height);"; break;
777 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
778 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
779 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
780 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400781 default: UNREACHABLE();
782 }
783 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400784 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400785 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500786 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
787 {
788 out << " float width; float height; float layers; float levels;\n";
789
790 out << " uint mip = 0;\n";
791
792 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
793
794 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
795 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
796 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
797 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
798
799 // FACE_POSITIVE_X = 000b
800 // FACE_NEGATIVE_X = 001b
801 // FACE_POSITIVE_Y = 010b
802 // FACE_NEGATIVE_Y = 011b
803 // FACE_POSITIVE_Z = 100b
804 // FACE_NEGATIVE_Z = 101b
805 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
806
807 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
808 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
809 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
810
811 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
812 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
813 }
814 else if (IsIntegerSampler(textureFunction->sampler) &&
815 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400816 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400817 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400818 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400819 if (IsSamplerArray(textureFunction->sampler))
820 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400821 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400822
Nicolas Capens9edebd62013-08-06 10:59:10 -0400823 if (textureFunction->method == TextureFunction::LOD0)
824 {
825 out << " uint mip = 0;\n";
826 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400827 else if (textureFunction->method == TextureFunction::LOD0BIAS)
828 {
829 out << " uint mip = bias;\n";
830 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400831 else
832 {
833 if (textureFunction->method == TextureFunction::IMPLICIT ||
834 textureFunction->method == TextureFunction::BIAS)
835 {
836 out << " x.GetDimensions(0, width, height, layers, levels);\n"
837 " float2 tSized = float2(t.x * width, t.y * height);\n"
838 " float dx = length(ddx(tSized));\n"
839 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500840 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400841
842 if (textureFunction->method == TextureFunction::BIAS)
843 {
844 out << " lod += bias;\n";
845 }
846 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500847 else if (textureFunction->method == TextureFunction::GRAD)
848 {
849 out << " x.GetDimensions(0, width, height, layers, levels);\n"
850 " float lod = log2(max(length(ddx), length(ddy)));\n";
851 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400852
853 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
854 }
855
856 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400857 }
858 else
859 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400860 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400861
Nicolas Capens9edebd62013-08-06 10:59:10 -0400862 if (textureFunction->method == TextureFunction::LOD0)
863 {
864 out << " uint mip = 0;\n";
865 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400866 else if (textureFunction->method == TextureFunction::LOD0BIAS)
867 {
868 out << " uint mip = bias;\n";
869 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400870 else
871 {
872 if (textureFunction->method == TextureFunction::IMPLICIT ||
873 textureFunction->method == TextureFunction::BIAS)
874 {
875 out << " x.GetDimensions(0, width, height, levels);\n"
876 " float2 tSized = float2(t.x * width, t.y * height);\n"
877 " float dx = length(ddx(tSized));\n"
878 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500879 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400880
881 if (textureFunction->method == TextureFunction::BIAS)
882 {
883 out << " lod += bias;\n";
884 }
885 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500886 else if (textureFunction->method == TextureFunction::LOD)
887 {
888 out << " x.GetDimensions(0, width, height, levels);\n";
889 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500890 else if (textureFunction->method == TextureFunction::GRAD)
891 {
892 out << " x.GetDimensions(0, width, height, levels);\n"
893 " float lod = log2(max(length(ddx), length(ddy)));\n";
894 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400895
896 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
897 }
898
899 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400900 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400901 }
902 else if (IsSampler3D(textureFunction->sampler))
903 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400904 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400905
Nicolas Capens9edebd62013-08-06 10:59:10 -0400906 if (textureFunction->method == TextureFunction::LOD0)
907 {
908 out << " uint mip = 0;\n";
909 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400910 else if (textureFunction->method == TextureFunction::LOD0BIAS)
911 {
912 out << " uint mip = bias;\n";
913 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400914 else
915 {
916 if (textureFunction->method == TextureFunction::IMPLICIT ||
917 textureFunction->method == TextureFunction::BIAS)
918 {
919 out << " x.GetDimensions(0, width, height, depth, levels);\n"
920 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
921 " float dx = length(ddx(tSized));\n"
922 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500923 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400924
925 if (textureFunction->method == TextureFunction::BIAS)
926 {
927 out << " lod += bias;\n";
928 }
929 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500930 else if (textureFunction->method == TextureFunction::GRAD)
931 {
932 out << " x.GetDimensions(0, width, height, depth, levels);\n"
933 " float lod = log2(max(length(ddx), length(ddy)));\n";
934 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400935
936 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
937 }
938
939 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400940 }
941 else UNREACHABLE();
942 }
943
944 out << " return ";
945
946 // HLSL intrinsic
947 if (mOutputType == SH_HLSL9_OUTPUT)
948 {
949 switch(textureFunction->sampler)
950 {
951 case EbtSampler2D: out << "tex2D"; break;
952 case EbtSamplerCube: out << "texCUBE"; break;
953 default: UNREACHABLE();
954 }
955
Nicolas Capens75fb4752013-07-10 15:14:47 -0400956 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400957 {
958 case TextureFunction::IMPLICIT: out << "(s, "; break;
959 case TextureFunction::BIAS: out << "bias(s, "; break;
960 case TextureFunction::LOD: out << "lod(s, "; break;
961 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400962 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400963 default: UNREACHABLE();
964 }
965 }
966 else if (mOutputType == SH_HLSL11_OUTPUT)
967 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500968 if (textureFunction->method == TextureFunction::GRAD)
969 {
970 if (IsIntegerSampler(textureFunction->sampler))
971 {
972 out << "x.Load(";
973 }
974 else if (IsShadowSampler(textureFunction->sampler))
975 {
976 out << "x.SampleCmpLevelZero(s, ";
977 }
978 else
979 {
980 out << "x.SampleGrad(s, ";
981 }
982 }
983 else if (IsIntegerSampler(textureFunction->sampler) ||
984 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400985 {
986 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400987 }
Nicolas Capenscb127d32013-07-15 17:26:18 -0400988 else if (IsShadowSampler(textureFunction->sampler))
989 {
990 out << "x.SampleCmp(s, ";
991 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400992 else
993 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400994 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400995 {
996 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
997 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
998 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
999 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001000 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001001 default: UNREACHABLE();
1002 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001003 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001004 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001005 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001006
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001007 // Integer sampling requires integer addresses
1008 TString addressx = "";
1009 TString addressy = "";
1010 TString addressz = "";
1011 TString close = "";
1012
Nicolas Capensfc014542014-02-18 14:47:13 -05001013 if (IsIntegerSampler(textureFunction->sampler) ||
1014 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001015 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001016 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001017 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001018 case 2: out << "int3("; break;
1019 case 3: out << "int4("; break;
1020 default: UNREACHABLE();
1021 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001022
Nicolas Capensfc014542014-02-18 14:47:13 -05001023 // Convert from normalized floating-point to integer
1024 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001025 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001026 addressx = "int(floor(width * frac((";
1027 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001028
Nicolas Capensfc014542014-02-18 14:47:13 -05001029 if (IsSamplerArray(textureFunction->sampler))
1030 {
1031 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1032 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001033 else if (IsSamplerCube(textureFunction->sampler))
1034 {
1035 addressz = "((((";
1036 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001037 else
1038 {
1039 addressz = "int(floor(depth * frac((";
1040 }
1041
1042 close = "))))";
1043 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001044 }
1045 else
1046 {
1047 switch(hlslCoords)
1048 {
1049 case 2: out << "float2("; break;
1050 case 3: out << "float3("; break;
1051 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001052 default: UNREACHABLE();
1053 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001054 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001055
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001056 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001057
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001058 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001059 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001060 switch(textureFunction->coords)
1061 {
1062 case 3: proj = " / t.z"; break;
1063 case 4: proj = " / t.w"; break;
1064 default: UNREACHABLE();
1065 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001066 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001067
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001068 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001069
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001070 if (mOutputType == SH_HLSL9_OUTPUT)
1071 {
1072 if (hlslCoords >= 3)
1073 {
1074 if (textureFunction->coords < 3)
1075 {
1076 out << ", 0";
1077 }
1078 else
1079 {
1080 out << ", t.z" + proj;
1081 }
1082 }
1083
1084 if (hlslCoords == 4)
1085 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001086 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001087 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001088 case TextureFunction::BIAS: out << ", bias"; break;
1089 case TextureFunction::LOD: out << ", lod"; break;
1090 case TextureFunction::LOD0: out << ", 0"; break;
1091 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001092 default: UNREACHABLE();
1093 }
1094 }
1095
1096 out << "));\n";
1097 }
1098 else if (mOutputType == SH_HLSL11_OUTPUT)
1099 {
1100 if (hlslCoords >= 3)
1101 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001102 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1103 {
1104 out << ", face";
1105 }
1106 else
1107 {
1108 out << ", " + addressz + ("t.z" + proj) + close;
1109 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001110 }
1111
Nicolas Capensd11d5492014-02-19 17:06:10 -05001112 if (textureFunction->method == TextureFunction::GRAD)
1113 {
1114 if (IsIntegerSampler(textureFunction->sampler))
1115 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001116 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001117 }
1118 else if (IsShadowSampler(textureFunction->sampler))
1119 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001120 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001121 switch(textureFunction->coords)
1122 {
1123 case 3: out << "), t.z"; break;
1124 case 4: out << "), t.w"; break;
1125 default: UNREACHABLE();
1126 }
1127 }
1128 else
1129 {
1130 out << "), ddx, ddy";
1131 }
1132 }
1133 else if (IsIntegerSampler(textureFunction->sampler) ||
1134 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001135 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001136 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001137 }
1138 else if (IsShadowSampler(textureFunction->sampler))
1139 {
1140 // Compare value
1141 switch(textureFunction->coords)
1142 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001143 case 3: out << "), t.z"; break;
1144 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001145 default: UNREACHABLE();
1146 }
1147 }
1148 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001149 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001150 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001151 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001152 case TextureFunction::IMPLICIT: out << ")"; break;
1153 case TextureFunction::BIAS: out << "), bias"; break;
1154 case TextureFunction::LOD: out << "), lod"; break;
1155 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001156 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001157 default: UNREACHABLE();
1158 }
1159 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001160
1161 if (textureFunction->offset)
1162 {
1163 out << ", offset";
1164 }
1165
1166 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001167 }
1168 else UNREACHABLE();
1169 }
1170
1171 out << "\n"
1172 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001173 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001174 }
1175
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001176 if (mUsesFragCoord)
1177 {
1178 out << "#define GL_USES_FRAG_COORD\n";
1179 }
1180
1181 if (mUsesPointCoord)
1182 {
1183 out << "#define GL_USES_POINT_COORD\n";
1184 }
1185
1186 if (mUsesFrontFacing)
1187 {
1188 out << "#define GL_USES_FRONT_FACING\n";
1189 }
1190
1191 if (mUsesPointSize)
1192 {
1193 out << "#define GL_USES_POINT_SIZE\n";
1194 }
1195
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001196 if (mUsesFragDepth)
1197 {
1198 out << "#define GL_USES_FRAG_DEPTH\n";
1199 }
1200
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001201 if (mUsesDepthRange)
1202 {
1203 out << "#define GL_USES_DEPTH_RANGE\n";
1204 }
1205
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001206 if (mUsesXor)
1207 {
1208 out << "bool xor(bool p, bool q)\n"
1209 "{\n"
1210 " return (p || q) && !(p && q);\n"
1211 "}\n"
1212 "\n";
1213 }
1214
1215 if (mUsesMod1)
1216 {
1217 out << "float mod(float x, float y)\n"
1218 "{\n"
1219 " return x - y * floor(x / y);\n"
1220 "}\n"
1221 "\n";
1222 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001223
1224 if (mUsesMod2v)
1225 {
1226 out << "float2 mod(float2 x, float2 y)\n"
1227 "{\n"
1228 " return x - y * floor(x / y);\n"
1229 "}\n"
1230 "\n";
1231 }
1232
1233 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001234 {
1235 out << "float2 mod(float2 x, float y)\n"
1236 "{\n"
1237 " return x - y * floor(x / y);\n"
1238 "}\n"
1239 "\n";
1240 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001241
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001242 if (mUsesMod3v)
1243 {
1244 out << "float3 mod(float3 x, float3 y)\n"
1245 "{\n"
1246 " return x - y * floor(x / y);\n"
1247 "}\n"
1248 "\n";
1249 }
1250
1251 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001252 {
1253 out << "float3 mod(float3 x, float y)\n"
1254 "{\n"
1255 " return x - y * floor(x / y);\n"
1256 "}\n"
1257 "\n";
1258 }
1259
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001260 if (mUsesMod4v)
1261 {
1262 out << "float4 mod(float4 x, float4 y)\n"
1263 "{\n"
1264 " return x - y * floor(x / y);\n"
1265 "}\n"
1266 "\n";
1267 }
1268
1269 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001270 {
1271 out << "float4 mod(float4 x, float y)\n"
1272 "{\n"
1273 " return x - y * floor(x / y);\n"
1274 "}\n"
1275 "\n";
1276 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001277
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001278 if (mUsesFaceforward1)
1279 {
1280 out << "float faceforward(float N, float I, float Nref)\n"
1281 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001282 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001283 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001284 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001285 " }\n"
1286 " else\n"
1287 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001288 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001289 " }\n"
1290 "}\n"
1291 "\n";
1292 }
1293
1294 if (mUsesFaceforward2)
1295 {
1296 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1297 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001298 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001299 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001300 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001301 " }\n"
1302 " else\n"
1303 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001304 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001305 " }\n"
1306 "}\n"
1307 "\n";
1308 }
1309
1310 if (mUsesFaceforward3)
1311 {
1312 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1313 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001314 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001315 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001316 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001317 " }\n"
1318 " else\n"
1319 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001320 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001321 " }\n"
1322 "}\n"
1323 "\n";
1324 }
1325
1326 if (mUsesFaceforward4)
1327 {
1328 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1329 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001330 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001331 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001332 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001333 " }\n"
1334 " else\n"
1335 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001336 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001337 " }\n"
1338 "}\n"
1339 "\n";
1340 }
1341
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001342 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001343 {
1344 out << "float atanyx(float y, float x)\n"
1345 "{\n"
1346 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1347 " return atan2(y, x);\n"
1348 "}\n";
1349 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001350
1351 if (mUsesAtan2_2)
1352 {
1353 out << "float2 atanyx(float2 y, float2 x)\n"
1354 "{\n"
1355 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1356 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1357 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1358 "}\n";
1359 }
1360
1361 if (mUsesAtan2_3)
1362 {
1363 out << "float3 atanyx(float3 y, float3 x)\n"
1364 "{\n"
1365 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1366 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1367 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1368 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1369 "}\n";
1370 }
1371
1372 if (mUsesAtan2_4)
1373 {
1374 out << "float4 atanyx(float4 y, float4 x)\n"
1375 "{\n"
1376 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1377 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1378 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1379 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1380 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1381 "}\n";
1382 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001383}
1384
1385void OutputHLSL::visitSymbol(TIntermSymbol *node)
1386{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001387 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001388
Jamie Madill570e04d2013-06-21 09:15:33 -04001389 // Handle accessing std140 structs by value
1390 if (mFlaggedStructMappedNames.count(node) > 0)
1391 {
1392 out << mFlaggedStructMappedNames[node];
1393 return;
1394 }
1395
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001396 TString name = node->getSymbol();
1397
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001398 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001399 {
1400 mUsesDepthRange = true;
1401 out << name;
1402 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001403 else
1404 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001405 TQualifier qualifier = node->getQualifier();
1406
1407 if (qualifier == EvqUniform)
1408 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001409 const TType& nodeType = node->getType();
1410 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1411
1412 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001413 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001414 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001415 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001416 else
1417 {
1418 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001419 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001420
Jamie Madill033dae62014-06-18 12:56:28 -04001421 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001422 }
Jamie Madill19571812013-08-12 15:26:34 -07001423 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001424 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001425 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001426 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001427 }
Jamie Madill033dae62014-06-18 12:56:28 -04001428 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001429 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001430 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001431 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001432 }
Jamie Madill19571812013-08-12 15:26:34 -07001433 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001434 {
1435 mReferencedOutputVariables[name] = node;
1436 out << "out_" << name;
1437 }
1438 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001439 {
1440 out << "gl_Color[0]";
1441 mUsesFragColor = true;
1442 }
1443 else if (qualifier == EvqFragData)
1444 {
1445 out << "gl_Color";
1446 mUsesFragData = true;
1447 }
1448 else if (qualifier == EvqFragCoord)
1449 {
1450 mUsesFragCoord = true;
1451 out << name;
1452 }
1453 else if (qualifier == EvqPointCoord)
1454 {
1455 mUsesPointCoord = true;
1456 out << name;
1457 }
1458 else if (qualifier == EvqFrontFacing)
1459 {
1460 mUsesFrontFacing = true;
1461 out << name;
1462 }
1463 else if (qualifier == EvqPointSize)
1464 {
1465 mUsesPointSize = true;
1466 out << name;
1467 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001468 else if (name == "gl_FragDepthEXT")
1469 {
1470 mUsesFragDepth = true;
1471 out << "gl_Depth";
1472 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001473 else if (qualifier == EvqInternal)
1474 {
1475 out << name;
1476 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001477 else
1478 {
Jamie Madill033dae62014-06-18 12:56:28 -04001479 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001480 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001481 }
1482}
1483
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001484void OutputHLSL::visitRaw(TIntermRaw *node)
1485{
1486 mBody << node->getRawText();
1487}
1488
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001489bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1490{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001491 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001492
Jamie Madill570e04d2013-06-21 09:15:33 -04001493 // Handle accessing std140 structs by value
1494 if (mFlaggedStructMappedNames.count(node) > 0)
1495 {
1496 out << mFlaggedStructMappedNames[node];
1497 return false;
1498 }
1499
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001500 switch (node->getOp())
1501 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001502 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001503 case EOpInitialize:
1504 if (visit == PreVisit)
1505 {
1506 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1507 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1508 // new variable is created before the assignment is evaluated), so we need to convert
1509 // this to "float t = x, x = t;".
1510
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001511 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1512 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001513
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001514 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1515 expression->traverse(&searchSymbol);
1516 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001517
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001518 if (sameSymbol)
1519 {
1520 // Type already printed
1521 out << "t" + str(mUniqueIndex) + " = ";
1522 expression->traverse(this);
1523 out << ", ";
1524 symbolNode->traverse(this);
1525 out << " = t" + str(mUniqueIndex);
1526
1527 mUniqueIndex++;
1528 return false;
1529 }
1530 }
1531 else if (visit == InVisit)
1532 {
1533 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001534 }
1535 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001536 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1537 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1538 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1539 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1540 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1541 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001542 if (visit == PreVisit)
1543 {
1544 out << "(";
1545 }
1546 else if (visit == InVisit)
1547 {
1548 out << " = mul(";
1549 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001550 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001551 }
1552 else
1553 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001554 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001555 }
1556 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001557 case EOpMatrixTimesMatrixAssign:
1558 if (visit == PreVisit)
1559 {
1560 out << "(";
1561 }
1562 else if (visit == InVisit)
1563 {
1564 out << " = mul(";
1565 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001566 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001567 }
1568 else
1569 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001570 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001571 }
1572 break;
1573 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001574 case EOpModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001575 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001576 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001577 const TType& leftType = node->getLeft()->getType();
1578 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001579 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001580 if (visit == PreVisit)
1581 {
1582 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1583 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001584 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001585 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001586 return false;
1587 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001588 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001589 else
1590 {
1591 outputTriplet(visit, "", "[", "]");
1592 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001593 }
1594 break;
1595 case EOpIndexIndirect:
1596 // We do not currently support indirect references to interface blocks
1597 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1598 outputTriplet(visit, "", "[", "]");
1599 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001600 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001601 if (visit == InVisit)
1602 {
1603 const TStructure* structure = node->getLeft()->getType().getStruct();
1604 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1605 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001606 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001607
1608 return false;
1609 }
1610 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001611 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001612 if (visit == InVisit)
1613 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001614 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1615 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1616 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001617 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001618
1619 return false;
1620 }
1621 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001622 case EOpVectorSwizzle:
1623 if (visit == InVisit)
1624 {
1625 out << ".";
1626
1627 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1628
1629 if (swizzle)
1630 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001631 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001633 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634 {
1635 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1636
1637 if (element)
1638 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001639 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001640
1641 switch (i)
1642 {
1643 case 0: out << "x"; break;
1644 case 1: out << "y"; break;
1645 case 2: out << "z"; break;
1646 case 3: out << "w"; break;
1647 default: UNREACHABLE();
1648 }
1649 }
1650 else UNREACHABLE();
1651 }
1652 }
1653 else UNREACHABLE();
1654
1655 return false; // Fully processed
1656 }
1657 break;
1658 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1659 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1660 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1661 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001662 case EOpMod: outputTriplet(visit, "(", " % ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001663 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001664 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001665 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001666 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001667 if (node->getOp() == EOpEqual)
1668 {
1669 outputTriplet(visit, "(", " == ", ")");
1670 }
1671 else
1672 {
1673 outputTriplet(visit, "(", " != ", ")");
1674 }
1675 }
1676 else if (node->getLeft()->getBasicType() == EbtStruct)
1677 {
1678 if (node->getOp() == EOpEqual)
1679 {
1680 out << "(";
1681 }
1682 else
1683 {
1684 out << "!(";
1685 }
1686
Jamie Madill98493dd2013-07-08 14:39:03 -04001687 const TStructure &structure = *node->getLeft()->getType().getStruct();
1688 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001689
Jamie Madill98493dd2013-07-08 14:39:03 -04001690 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001691 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001692 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001693
1694 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001695 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001696 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001697 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001698
Jamie Madill98493dd2013-07-08 14:39:03 -04001699 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001700 {
1701 out << " && ";
1702 }
1703 }
1704
1705 out << ")";
1706
1707 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001708 }
1709 else
1710 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001711 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001712
1713 if (node->getOp() == EOpEqual)
1714 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001715 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001716 }
1717 else
1718 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001719 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001720 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001721 }
1722 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1724 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1725 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1726 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1727 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001728 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001729 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1730 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001731 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001732 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001733 if (node->getRight()->hasSideEffects())
1734 {
1735 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1736 return false;
1737 }
1738 else
1739 {
1740 outputTriplet(visit, "(", " || ", ")");
1741 return true;
1742 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001743 case EOpLogicalXor:
1744 mUsesXor = true;
1745 outputTriplet(visit, "xor(", ", ", ")");
1746 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001747 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001748 if (node->getRight()->hasSideEffects())
1749 {
1750 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1751 return false;
1752 }
1753 else
1754 {
1755 outputTriplet(visit, "(", " && ", ")");
1756 return true;
1757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758 default: UNREACHABLE();
1759 }
1760
1761 return true;
1762}
1763
1764bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1765{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 switch (node->getOp())
1767 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001768 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001769 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001770 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1771 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1772 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1773 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1774 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1775 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001776 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1777 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1778 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1779 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1780 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1781 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1782 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1783 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1784 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1785 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1786 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1787 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1788 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1789 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1790 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1791 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1792 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1793 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1794 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001795 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1796 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1797 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1798 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001799 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1800 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001801 case EOpDFdx:
1802 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1803 {
1804 outputTriplet(visit, "(", "", ", 0.0)");
1805 }
1806 else
1807 {
1808 outputTriplet(visit, "ddx(", "", ")");
1809 }
1810 break;
1811 case EOpDFdy:
1812 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1813 {
1814 outputTriplet(visit, "(", "", ", 0.0)");
1815 }
1816 else
1817 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001818 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001819 }
1820 break;
1821 case EOpFwidth:
1822 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1823 {
1824 outputTriplet(visit, "(", "", ", 0.0)");
1825 }
1826 else
1827 {
1828 outputTriplet(visit, "fwidth(", "", ")");
1829 }
1830 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001831 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1832 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833 default: UNREACHABLE();
1834 }
1835
1836 return true;
1837}
1838
1839bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1840{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001841 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843 switch (node->getOp())
1844 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001845 case EOpSequence:
1846 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001847 if (mInsideFunction)
1848 {
Jamie Madill075edd82013-07-08 13:30:19 -04001849 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001850 out << "{\n";
1851 }
1852
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001853 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001854 {
Jamie Madill075edd82013-07-08 13:30:19 -04001855 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001856
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001857 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001858
1859 out << ";\n";
1860 }
1861
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001862 if (mInsideFunction)
1863 {
Jamie Madill075edd82013-07-08 13:30:19 -04001864 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001865 out << "}\n";
1866 }
1867
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001868 return false;
1869 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 case EOpDeclaration:
1871 if (visit == PreVisit)
1872 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001873 TIntermSequence *sequence = node->getSequence();
1874 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001876 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001878 TStructure *structure = variable->getType().getStruct();
1879
1880 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001881 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001882 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001883 }
1884
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001885 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001886 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001887 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888 {
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001889 if (isSingleStatement(*sit))
1890 {
1891 mUnfoldShortCircuit->traverse(*sit);
1892 }
1893
Nicolas Capensd974db42014-10-07 10:50:19 -04001894 if (!mInsideFunction)
1895 {
1896 out << "static ";
1897 }
1898
1899 out << TypeString(variable->getType()) + " ";
1900
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001901 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001902
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001903 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001905 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001906 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001907 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001908 }
1909 else
1910 {
1911 (*sit)->traverse(this);
1912 }
1913
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001914 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001915 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001916 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917 }
1918 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001920 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1921 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001922 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001923 }
1924 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925 }
Jamie Madill033dae62014-06-18 12:56:28 -04001926 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001927 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001928 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001929 {
1930 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1931
1932 if (symbol)
1933 {
1934 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1935 mReferencedVaryings[symbol->getSymbol()] = symbol;
1936 }
1937 else
1938 {
1939 (*sit)->traverse(this);
1940 }
1941 }
1942 }
1943
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944 return false;
1945 }
1946 else if (visit == InVisit)
1947 {
1948 out << ", ";
1949 }
1950 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001951 case EOpInvariantDeclaration:
1952 // Do not do any translation
1953 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001954 case EOpPrototype:
1955 if (visit == PreVisit)
1956 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001957 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001958
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001959 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001960
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001961 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001962 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001963 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001964
1965 if (symbol)
1966 {
1967 out << argumentString(symbol);
1968
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001969 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001970 {
1971 out << ", ";
1972 }
1973 }
1974 else UNREACHABLE();
1975 }
1976
1977 out << ");\n";
1978
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001979 // Also prototype the Lod0 variant if needed
1980 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1981 {
1982 mOutputLod0Function = true;
1983 node->traverse(this);
1984 mOutputLod0Function = false;
1985 }
1986
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001987 return false;
1988 }
1989 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001990 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991 case EOpFunction:
1992 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001993 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994
Jamie Madill033dae62014-06-18 12:56:28 -04001995 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001996
1997 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001999 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002001 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002002 {
Jamie Madill033dae62014-06-18 12:56:28 -04002003 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002004 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002005
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002006 TIntermSequence *sequence = node->getSequence();
2007 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002008
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002009 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002010 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002011 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002012
2013 if (symbol)
2014 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002015 TStructure *structure = symbol->getType().getStruct();
2016
2017 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002018 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002019 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002020 }
2021
2022 out << argumentString(symbol);
2023
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002024 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002025 {
2026 out << ", ";
2027 }
2028 }
2029 else UNREACHABLE();
2030 }
2031
2032 out << ")\n"
2033 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002034
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002035 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002036 {
2037 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002038 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002039 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002041
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002042 out << "}\n";
2043
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002044 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2045 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002046 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002047 {
2048 mOutputLod0Function = true;
2049 node->traverse(this);
2050 mOutputLod0Function = false;
2051 }
2052 }
2053
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002054 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055 }
2056 break;
2057 case EOpFunctionCall:
2058 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002059 TString name = TFunction::unmangleName(node->getName());
2060 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002061 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002062
2063 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064 {
Jamie Madill033dae62014-06-18 12:56:28 -04002065 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066 }
2067 else
2068 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002069 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002070
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002071 TextureFunction textureFunction;
2072 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002073 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002074 textureFunction.method = TextureFunction::IMPLICIT;
2075 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002076 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002077
2078 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002079 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002080 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002081 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002082 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002083 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002084 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002085 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002086 }
Nicolas Capens46485082014-04-15 13:12:50 -04002087 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2088 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002089 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002090 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002091 }
Nicolas Capens46485082014-04-15 13:12:50 -04002092 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002093 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002094 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002095 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002096 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002097 else if (name == "textureSize")
2098 {
2099 textureFunction.method = TextureFunction::SIZE;
2100 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002101 else if (name == "textureOffset")
2102 {
2103 textureFunction.method = TextureFunction::IMPLICIT;
2104 textureFunction.offset = true;
2105 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002106 else if (name == "textureProjOffset")
2107 {
2108 textureFunction.method = TextureFunction::IMPLICIT;
2109 textureFunction.offset = true;
2110 textureFunction.proj = true;
2111 }
2112 else if (name == "textureLodOffset")
2113 {
2114 textureFunction.method = TextureFunction::LOD;
2115 textureFunction.offset = true;
2116 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002117 else if (name == "textureProjLodOffset")
2118 {
2119 textureFunction.method = TextureFunction::LOD;
2120 textureFunction.proj = true;
2121 textureFunction.offset = true;
2122 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002123 else if (name == "texelFetch")
2124 {
2125 textureFunction.method = TextureFunction::FETCH;
2126 }
2127 else if (name == "texelFetchOffset")
2128 {
2129 textureFunction.method = TextureFunction::FETCH;
2130 textureFunction.offset = true;
2131 }
Nicolas Capens46485082014-04-15 13:12:50 -04002132 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002133 {
2134 textureFunction.method = TextureFunction::GRAD;
2135 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002136 else if (name == "textureGradOffset")
2137 {
2138 textureFunction.method = TextureFunction::GRAD;
2139 textureFunction.offset = true;
2140 }
Nicolas Capens46485082014-04-15 13:12:50 -04002141 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002142 {
2143 textureFunction.method = TextureFunction::GRAD;
2144 textureFunction.proj = true;
2145 }
2146 else if (name == "textureProjGradOffset")
2147 {
2148 textureFunction.method = TextureFunction::GRAD;
2149 textureFunction.proj = true;
2150 textureFunction.offset = true;
2151 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002152 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002153
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002154 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002155 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002156 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2157
2158 if (textureFunction.offset)
2159 {
2160 mandatoryArgumentCount++;
2161 }
2162
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002163 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002164
Jamie Madill183bde52014-07-02 15:31:19 -04002165 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002166 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002167 if (bias)
2168 {
2169 textureFunction.method = TextureFunction::LOD0BIAS;
2170 }
2171 else
2172 {
2173 textureFunction.method = TextureFunction::LOD0;
2174 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002175 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002176 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002177 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002178 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002179 }
2180 }
2181
2182 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002183
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002184 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002186
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002187 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002188 {
2189 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2190 {
2191 out << "texture_";
2192 (*arg)->traverse(this);
2193 out << ", sampler_";
2194 }
2195
2196 (*arg)->traverse(this);
2197
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002198 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002199 {
2200 out << ", ";
2201 }
2202 }
2203
2204 out << ")";
2205
2206 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002207 }
2208 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002209 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002210 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2211 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2212 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2213 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2214 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2215 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2216 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2217 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2218 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2219 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2220 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2221 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2222 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2223 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2224 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2225 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2226 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2227 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2228 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002229 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002230 {
Jamie Madill033dae62014-06-18 12:56:28 -04002231 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002232 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002233 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2234 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002235 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002236 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2237 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2238 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2239 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2240 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2241 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002242 case EOpMod:
2243 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002244 // We need to look at the number of components in both arguments
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002245 const int modValue = (*node->getSequence())[0]->getAsTyped()->getNominalSize() * 10 +
2246 (*node->getSequence())[1]->getAsTyped()->getNominalSize();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002247 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002248 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002249 case 11: mUsesMod1 = true; break;
2250 case 22: mUsesMod2v = true; break;
2251 case 21: mUsesMod2f = true; break;
2252 case 33: mUsesMod3v = true; break;
2253 case 31: mUsesMod3f = true; break;
2254 case 44: mUsesMod4v = true; break;
2255 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002256 default: UNREACHABLE();
2257 }
2258
2259 outputTriplet(visit, "mod(", ", ", ")");
2260 }
2261 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002262 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002264 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2265 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize())
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002266 {
2267 case 1: mUsesAtan2_1 = true; break;
2268 case 2: mUsesAtan2_2 = true; break;
2269 case 3: mUsesAtan2_3 = true; break;
2270 case 4: mUsesAtan2_4 = true; break;
2271 default: UNREACHABLE();
2272 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002273 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 break;
2275 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2276 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2277 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2278 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2279 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2280 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2281 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2282 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2283 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002284 case EOpFaceForward:
2285 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002286 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002287 {
2288 case 1: mUsesFaceforward1 = true; break;
2289 case 2: mUsesFaceforward2 = true; break;
2290 case 3: mUsesFaceforward3 = true; break;
2291 case 4: mUsesFaceforward4 = true; break;
2292 default: UNREACHABLE();
2293 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002294
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002295 outputTriplet(visit, "faceforward(", ", ", ")");
2296 }
2297 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2299 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2300 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301 default: UNREACHABLE();
2302 }
2303
2304 return true;
2305}
2306
2307bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2308{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002309 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002311 if (node->usesTernaryOperator())
2312 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002313 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002314 }
2315 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002317 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002318
Corentin Wallez80bacde2014-11-10 12:07:37 -08002319 // D3D errors when there is a gradient operation in a loop in an unflattened if
2320 // however flattening all the ifs in branch heavy shaders made D3D error too.
2321 // As a temporary workaround we flatten the ifs only if there is at least a loop
2322 // present somewhere in the shader.
2323 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2324 {
2325 out << "FLATTEN ";
2326 }
2327
2328 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002329
2330 node->getCondition()->traverse(this);
2331
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002332 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002333
Jamie Madill075edd82013-07-08 13:30:19 -04002334 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002335 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002337 bool discard = false;
2338
daniel@transgaming.combb885322010-04-15 20:45:24 +00002339 if (node->getTrueBlock())
2340 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002341 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002342
2343 // Detect true discard
2344 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002345 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002346
Jamie Madill075edd82013-07-08 13:30:19 -04002347 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002348 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002349
2350 if (node->getFalseBlock())
2351 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002352 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002353
Jamie Madill075edd82013-07-08 13:30:19 -04002354 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002355 out << "{\n";
2356
Jamie Madill075edd82013-07-08 13:30:19 -04002357 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002358 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002359
Jamie Madill075edd82013-07-08 13:30:19 -04002360 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002361 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002362
2363 // Detect false discard
2364 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2365 }
2366
2367 // ANGLE issue 486: Detect problematic conditional discard
2368 if (discard && FindSideEffectRewriting::search(node))
2369 {
2370 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002371 }
2372 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373
2374 return false;
2375}
2376
2377void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2378{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002379 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380}
2381
2382bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2383{
Nicolas Capens655fe362014-04-11 13:12:34 -04002384 mNestedLoopDepth++;
2385
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002386 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2387
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002388 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002389 {
2390 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2391 }
2392
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002393 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002394 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002395 if (handleExcessiveLoop(node))
2396 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002397 mInsideDiscontinuousLoop = wasDiscontinuous;
2398 mNestedLoopDepth--;
2399
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002400 return false;
2401 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002402 }
2403
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002404 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405
alokp@chromium.org52813552010-11-16 18:36:09 +00002406 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002408 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002409
Jamie Madill075edd82013-07-08 13:30:19 -04002410 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002411 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002412 }
2413 else
2414 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002415 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002416
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417 if (node->getInit())
2418 {
2419 node->getInit()->traverse(this);
2420 }
2421
2422 out << "; ";
2423
alokp@chromium.org52813552010-11-16 18:36:09 +00002424 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002426 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 }
2428
2429 out << "; ";
2430
alokp@chromium.org52813552010-11-16 18:36:09 +00002431 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002433 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 }
2435
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002436 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002437
Jamie Madill075edd82013-07-08 13:30:19 -04002438 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002439 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002440 }
2441
2442 if (node->getBody())
2443 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002444 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445 }
2446
Jamie Madill075edd82013-07-08 13:30:19 -04002447 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002448 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449
alokp@chromium.org52813552010-11-16 18:36:09 +00002450 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002451 {
Jamie Madill075edd82013-07-08 13:30:19 -04002452 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002453 out << "while(\n";
2454
alokp@chromium.org52813552010-11-16 18:36:09 +00002455 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456
daniel@transgaming.com73536982012-03-21 20:45:49 +00002457 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458 }
2459
daniel@transgaming.com73536982012-03-21 20:45:49 +00002460 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002462 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002463 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002464
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 return false;
2466}
2467
2468bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2469{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002470 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471
2472 switch (node->getFlowOp())
2473 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002474 case EOpKill:
2475 outputTriplet(visit, "discard;\n", "", "");
2476 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002477 case EOpBreak:
2478 if (visit == PreVisit)
2479 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002480 if (mNestedLoopDepth > 1)
2481 {
2482 mUsesNestedBreak = true;
2483 }
2484
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002485 if (mExcessiveLoopIndex)
2486 {
2487 out << "{Break";
2488 mExcessiveLoopIndex->traverse(this);
2489 out << " = true; break;}\n";
2490 }
2491 else
2492 {
2493 out << "break;\n";
2494 }
2495 }
2496 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002497 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002498 case EOpReturn:
2499 if (visit == PreVisit)
2500 {
2501 if (node->getExpression())
2502 {
2503 out << "return ";
2504 }
2505 else
2506 {
2507 out << "return;\n";
2508 }
2509 }
2510 else if (visit == PostVisit)
2511 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002512 if (node->getExpression())
2513 {
2514 out << ";\n";
2515 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516 }
2517 break;
2518 default: UNREACHABLE();
2519 }
2520
2521 return true;
2522}
2523
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002524void OutputHLSL::traverseStatements(TIntermNode *node)
2525{
2526 if (isSingleStatement(node))
2527 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002528 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002529 }
2530
2531 node->traverse(this);
2532}
2533
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002534bool OutputHLSL::isSingleStatement(TIntermNode *node)
2535{
2536 TIntermAggregate *aggregate = node->getAsAggregate();
2537
2538 if (aggregate)
2539 {
2540 if (aggregate->getOp() == EOpSequence)
2541 {
2542 return false;
2543 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002544 else if (aggregate->getOp() == EOpDeclaration)
2545 {
2546 // Declaring multiple comma-separated variables must be considered multiple statements
2547 // because each individual declaration has side effects which are visible in the next.
2548 return false;
2549 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002550 else
2551 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002552 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002553 {
2554 if (!isSingleStatement(*sit))
2555 {
2556 return false;
2557 }
2558 }
2559
2560 return true;
2561 }
2562 }
2563
2564 return true;
2565}
2566
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002567// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2568// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002569bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2570{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002571 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002572 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002573
2574 // Parse loops of the form:
2575 // for(int index = initial; index [comparator] limit; index += increment)
2576 TIntermSymbol *index = NULL;
2577 TOperator comparator = EOpNull;
2578 int initial = 0;
2579 int limit = 0;
2580 int increment = 0;
2581
2582 // Parse index name and intial value
2583 if (node->getInit())
2584 {
2585 TIntermAggregate *init = node->getInit()->getAsAggregate();
2586
2587 if (init)
2588 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002589 TIntermSequence *sequence = init->getSequence();
2590 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002591
2592 if (variable && variable->getQualifier() == EvqTemporary)
2593 {
2594 TIntermBinary *assign = variable->getAsBinaryNode();
2595
2596 if (assign->getOp() == EOpInitialize)
2597 {
2598 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2599 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2600
2601 if (symbol && constant)
2602 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002603 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002604 {
2605 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002606 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002607 }
2608 }
2609 }
2610 }
2611 }
2612 }
2613
2614 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002615 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002616 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002617 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002618
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002619 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2620 {
2621 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2622
2623 if (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 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002628 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002629 }
2630 }
2631 }
2632 }
2633
2634 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002635 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002636 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002637 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2638 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002639
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002640 if (binaryTerminal)
2641 {
2642 TOperator op = binaryTerminal->getOp();
2643 TIntermConstantUnion *constant = binaryTerminal->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 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002649 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002650
2651 switch (op)
2652 {
2653 case EOpAddAssign: increment = value; break;
2654 case EOpSubAssign: increment = -value; break;
2655 default: UNIMPLEMENTED();
2656 }
2657 }
2658 }
2659 }
2660 else if (unaryTerminal)
2661 {
2662 TOperator op = unaryTerminal->getOp();
2663
2664 switch (op)
2665 {
2666 case EOpPostIncrement: increment = 1; break;
2667 case EOpPostDecrement: increment = -1; break;
2668 case EOpPreIncrement: increment = 1; break;
2669 case EOpPreDecrement: increment = -1; break;
2670 default: UNIMPLEMENTED();
2671 }
2672 }
2673 }
2674
2675 if (index != NULL && comparator != EOpNull && increment != 0)
2676 {
2677 if (comparator == EOpLessThanEqual)
2678 {
2679 comparator = EOpLessThan;
2680 limit += 1;
2681 }
2682
2683 if (comparator == EOpLessThan)
2684 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002685 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002686
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002687 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002688 {
2689 return false; // Not an excessive loop
2690 }
2691
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002692 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2693 mExcessiveLoopIndex = index;
2694
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002695 out << "{int ";
2696 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002697 out << ";\n"
2698 "bool Break";
2699 index->traverse(this);
2700 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002701
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002702 bool firstLoopFragment = true;
2703
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002704 while (iterations > 0)
2705 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002706 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002707
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002708 if (!firstLoopFragment)
2709 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002710 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002711 index->traverse(this);
2712 out << ") {\n";
2713 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002714
2715 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2716 {
2717 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2718 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002719
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002720 // for(int index = initial; index < clampedLimit; index += increment)
2721
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002722 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002723 index->traverse(this);
2724 out << " = ";
2725 out << initial;
2726
2727 out << "; ";
2728 index->traverse(this);
2729 out << " < ";
2730 out << clampedLimit;
2731
2732 out << "; ";
2733 index->traverse(this);
2734 out << " += ";
2735 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002736 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002737
Jamie Madill075edd82013-07-08 13:30:19 -04002738 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002739 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002740
2741 if (node->getBody())
2742 {
2743 node->getBody()->traverse(this);
2744 }
2745
Jamie Madill075edd82013-07-08 13:30:19 -04002746 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002747 out << ";}\n";
2748
2749 if (!firstLoopFragment)
2750 {
2751 out << "}\n";
2752 }
2753
2754 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002755
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002756 initial += MAX_LOOP_ITERATIONS * increment;
2757 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002758 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002759
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002760 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002761
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002762 mExcessiveLoopIndex = restoreIndex;
2763
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002764 return true;
2765 }
2766 else UNIMPLEMENTED();
2767 }
2768
2769 return false; // Not handled as an excessive loop
2770}
2771
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002772void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002773{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002774 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002776 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002777 {
2778 out << preString;
2779 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002780 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002781 {
2782 out << inString;
2783 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002784 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002785 {
2786 out << postString;
2787 }
2788}
2789
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002790void OutputHLSL::outputLineDirective(int line)
2791{
2792 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2793 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002794 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002795 mBody << "#line " << line;
2796
2797 if (mContext.sourcePath)
2798 {
2799 mBody << " \"" << mContext.sourcePath << "\"";
2800 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002801
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002802 mBody << "\n";
2803 }
2804}
2805
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002806TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2807{
2808 TQualifier qualifier = symbol->getQualifier();
2809 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002810 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002811
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002812 if (name.empty()) // HLSL demands named arguments, also for prototypes
2813 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002814 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002815 }
2816 else
2817 {
Jamie Madill033dae62014-06-18 12:56:28 -04002818 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002819 }
2820
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002821 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2822 {
Jamie Madill033dae62014-06-18 12:56:28 -04002823 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002824 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002825 }
2826
Jamie Madill033dae62014-06-18 12:56:28 -04002827 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002828}
2829
2830TString OutputHLSL::initializer(const TType &type)
2831{
2832 TString string;
2833
Jamie Madill94bf7f22013-07-08 13:31:15 -04002834 size_t size = type.getObjectSize();
2835 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002836 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002837 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002838
Jamie Madill94bf7f22013-07-08 13:31:15 -04002839 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002840 {
2841 string += ", ";
2842 }
2843 }
2844
daniel@transgaming.comead23042010-04-29 03:35:36 +00002845 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002846}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002847
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002848void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2849{
2850 TInfoSinkBase &out = mBody;
2851
2852 if (visit == PreVisit)
2853 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002854 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002855
2856 out << name + "(";
2857 }
2858 else if (visit == InVisit)
2859 {
2860 out << ", ";
2861 }
2862 else if (visit == PostVisit)
2863 {
2864 out << ")";
2865 }
2866}
2867
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002868const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2869{
2870 TInfoSinkBase &out = mBody;
2871
Jamie Madill98493dd2013-07-08 14:39:03 -04002872 const TStructure* structure = type.getStruct();
2873 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002874 {
Jamie Madill033dae62014-06-18 12:56:28 -04002875 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002876
Jamie Madill98493dd2013-07-08 14:39:03 -04002877 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002878
Jamie Madill98493dd2013-07-08 14:39:03 -04002879 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002880 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002881 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002882 constUnion = writeConstantUnion(*fieldType, constUnion);
2883
Jamie Madill98493dd2013-07-08 14:39:03 -04002884 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002885 {
2886 out << ", ";
2887 }
2888 }
2889
2890 out << ")";
2891 }
2892 else
2893 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002894 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002895 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002896
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002897 if (writeType)
2898 {
Jamie Madill033dae62014-06-18 12:56:28 -04002899 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002900 }
2901
Jamie Madill94bf7f22013-07-08 13:31:15 -04002902 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002903 {
2904 switch (constUnion->getType())
2905 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002906 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002907 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002908 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002909 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002910 default: UNREACHABLE();
2911 }
2912
2913 if (i != size - 1)
2914 {
2915 out << ", ";
2916 }
2917 }
2918
2919 if (writeType)
2920 {
2921 out << ")";
2922 }
2923 }
2924
2925 return constUnion;
2926}
2927
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002928}