blob: 9445b09035d3e72df000a518e2abc9ca5355be94 [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;
1795 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1796 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001797 case EOpDFdx:
1798 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1799 {
1800 outputTriplet(visit, "(", "", ", 0.0)");
1801 }
1802 else
1803 {
1804 outputTriplet(visit, "ddx(", "", ")");
1805 }
1806 break;
1807 case EOpDFdy:
1808 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1809 {
1810 outputTriplet(visit, "(", "", ", 0.0)");
1811 }
1812 else
1813 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001814 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001815 }
1816 break;
1817 case EOpFwidth:
1818 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1819 {
1820 outputTriplet(visit, "(", "", ", 0.0)");
1821 }
1822 else
1823 {
1824 outputTriplet(visit, "fwidth(", "", ")");
1825 }
1826 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001827 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1828 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829 default: UNREACHABLE();
1830 }
1831
1832 return true;
1833}
1834
1835bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1836{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001837 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839 switch (node->getOp())
1840 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001841 case EOpSequence:
1842 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001843 if (mInsideFunction)
1844 {
Jamie Madill075edd82013-07-08 13:30:19 -04001845 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001846 out << "{\n";
1847 }
1848
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001849 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001850 {
Jamie Madill075edd82013-07-08 13:30:19 -04001851 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001852
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001853 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001854
1855 out << ";\n";
1856 }
1857
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001858 if (mInsideFunction)
1859 {
Jamie Madill075edd82013-07-08 13:30:19 -04001860 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001861 out << "}\n";
1862 }
1863
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001864 return false;
1865 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866 case EOpDeclaration:
1867 if (visit == PreVisit)
1868 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001869 TIntermSequence *sequence = node->getSequence();
1870 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001872 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001874 TStructure *structure = variable->getType().getStruct();
1875
1876 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001877 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001878 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001879 }
1880
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001881 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001882 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001883 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001884 {
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001885 if (isSingleStatement(*sit))
1886 {
1887 mUnfoldShortCircuit->traverse(*sit);
1888 }
1889
Nicolas Capensd974db42014-10-07 10:50:19 -04001890 if (!mInsideFunction)
1891 {
1892 out << "static ";
1893 }
1894
1895 out << TypeString(variable->getType()) + " ";
1896
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001897 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001899 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001901 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001902 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001903 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001904 }
1905 else
1906 {
1907 (*sit)->traverse(this);
1908 }
1909
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001910 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001911 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001912 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913 }
1914 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001916 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1917 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001918 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001919 }
1920 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921 }
Jamie Madill033dae62014-06-18 12:56:28 -04001922 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001923 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001924 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001925 {
1926 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1927
1928 if (symbol)
1929 {
1930 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1931 mReferencedVaryings[symbol->getSymbol()] = symbol;
1932 }
1933 else
1934 {
1935 (*sit)->traverse(this);
1936 }
1937 }
1938 }
1939
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001940 return false;
1941 }
1942 else if (visit == InVisit)
1943 {
1944 out << ", ";
1945 }
1946 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001947 case EOpInvariantDeclaration:
1948 // Do not do any translation
1949 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001950 case EOpPrototype:
1951 if (visit == PreVisit)
1952 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001953 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001954
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001955 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001956
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001957 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001958 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001959 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001960
1961 if (symbol)
1962 {
1963 out << argumentString(symbol);
1964
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001965 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001966 {
1967 out << ", ";
1968 }
1969 }
1970 else UNREACHABLE();
1971 }
1972
1973 out << ");\n";
1974
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001975 // Also prototype the Lod0 variant if needed
1976 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1977 {
1978 mOutputLod0Function = true;
1979 node->traverse(this);
1980 mOutputLod0Function = false;
1981 }
1982
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001983 return false;
1984 }
1985 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001986 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987 case EOpFunction:
1988 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001989 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990
Jamie Madill033dae62014-06-18 12:56:28 -04001991 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001992
1993 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001995 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001996 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001997 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998 {
Jamie Madill033dae62014-06-18 12:56:28 -04001999 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002000 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002001
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002002 TIntermSequence *sequence = node->getSequence();
2003 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002004
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002005 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002006 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002007 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002008
2009 if (symbol)
2010 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002011 TStructure *structure = symbol->getType().getStruct();
2012
2013 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002014 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002015 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002016 }
2017
2018 out << argumentString(symbol);
2019
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002020 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002021 {
2022 out << ", ";
2023 }
2024 }
2025 else UNREACHABLE();
2026 }
2027
2028 out << ")\n"
2029 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002030
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002031 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002032 {
2033 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002034 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002035 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002036 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002037
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002038 out << "}\n";
2039
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002040 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2041 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002042 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002043 {
2044 mOutputLod0Function = true;
2045 node->traverse(this);
2046 mOutputLod0Function = false;
2047 }
2048 }
2049
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002050 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051 }
2052 break;
2053 case EOpFunctionCall:
2054 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002055 TString name = TFunction::unmangleName(node->getName());
2056 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002057 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002058
2059 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002060 {
Jamie Madill033dae62014-06-18 12:56:28 -04002061 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 }
2063 else
2064 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002065 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002066
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002067 TextureFunction textureFunction;
2068 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002069 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002070 textureFunction.method = TextureFunction::IMPLICIT;
2071 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002072 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002073
2074 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002075 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002076 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002077 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002078 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002079 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002080 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002081 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002082 }
Nicolas Capens46485082014-04-15 13:12:50 -04002083 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2084 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002085 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002086 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002087 }
Nicolas Capens46485082014-04-15 13:12:50 -04002088 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002089 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002090 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002091 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002092 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002093 else if (name == "textureSize")
2094 {
2095 textureFunction.method = TextureFunction::SIZE;
2096 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002097 else if (name == "textureOffset")
2098 {
2099 textureFunction.method = TextureFunction::IMPLICIT;
2100 textureFunction.offset = true;
2101 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002102 else if (name == "textureProjOffset")
2103 {
2104 textureFunction.method = TextureFunction::IMPLICIT;
2105 textureFunction.offset = true;
2106 textureFunction.proj = true;
2107 }
2108 else if (name == "textureLodOffset")
2109 {
2110 textureFunction.method = TextureFunction::LOD;
2111 textureFunction.offset = true;
2112 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002113 else if (name == "textureProjLodOffset")
2114 {
2115 textureFunction.method = TextureFunction::LOD;
2116 textureFunction.proj = true;
2117 textureFunction.offset = true;
2118 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002119 else if (name == "texelFetch")
2120 {
2121 textureFunction.method = TextureFunction::FETCH;
2122 }
2123 else if (name == "texelFetchOffset")
2124 {
2125 textureFunction.method = TextureFunction::FETCH;
2126 textureFunction.offset = true;
2127 }
Nicolas Capens46485082014-04-15 13:12:50 -04002128 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002129 {
2130 textureFunction.method = TextureFunction::GRAD;
2131 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002132 else if (name == "textureGradOffset")
2133 {
2134 textureFunction.method = TextureFunction::GRAD;
2135 textureFunction.offset = true;
2136 }
Nicolas Capens46485082014-04-15 13:12:50 -04002137 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002138 {
2139 textureFunction.method = TextureFunction::GRAD;
2140 textureFunction.proj = true;
2141 }
2142 else if (name == "textureProjGradOffset")
2143 {
2144 textureFunction.method = TextureFunction::GRAD;
2145 textureFunction.proj = true;
2146 textureFunction.offset = true;
2147 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002148 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002149
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002150 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002151 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002152 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2153
2154 if (textureFunction.offset)
2155 {
2156 mandatoryArgumentCount++;
2157 }
2158
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002159 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002160
Jamie Madill183bde52014-07-02 15:31:19 -04002161 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002162 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002163 if (bias)
2164 {
2165 textureFunction.method = TextureFunction::LOD0BIAS;
2166 }
2167 else
2168 {
2169 textureFunction.method = TextureFunction::LOD0;
2170 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002171 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002172 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002173 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002174 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002175 }
2176 }
2177
2178 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002179
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002180 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002182
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002183 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002184 {
2185 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2186 {
2187 out << "texture_";
2188 (*arg)->traverse(this);
2189 out << ", sampler_";
2190 }
2191
2192 (*arg)->traverse(this);
2193
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002194 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002195 {
2196 out << ", ";
2197 }
2198 }
2199
2200 out << ")";
2201
2202 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002203 }
2204 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002205 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002206 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2207 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2208 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2209 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2210 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2211 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2212 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2213 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2214 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2215 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2216 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2217 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2218 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2219 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2220 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2221 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2222 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2223 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2224 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002225 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002226 {
Jamie Madill033dae62014-06-18 12:56:28 -04002227 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002228 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002229 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2230 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002231 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002232 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2233 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2234 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2235 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2236 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2237 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002238 case EOpMod:
2239 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002240 // We need to look at the number of components in both arguments
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002241 const int modValue = (*node->getSequence())[0]->getAsTyped()->getNominalSize() * 10 +
2242 (*node->getSequence())[1]->getAsTyped()->getNominalSize();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002243 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002244 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002245 case 11: mUsesMod1 = true; break;
2246 case 22: mUsesMod2v = true; break;
2247 case 21: mUsesMod2f = true; break;
2248 case 33: mUsesMod3v = true; break;
2249 case 31: mUsesMod3f = true; break;
2250 case 44: mUsesMod4v = true; break;
2251 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002252 default: UNREACHABLE();
2253 }
2254
2255 outputTriplet(visit, "mod(", ", ", ")");
2256 }
2257 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002258 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002260 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2261 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize())
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002262 {
2263 case 1: mUsesAtan2_1 = true; break;
2264 case 2: mUsesAtan2_2 = true; break;
2265 case 3: mUsesAtan2_3 = true; break;
2266 case 4: mUsesAtan2_4 = true; break;
2267 default: UNREACHABLE();
2268 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002269 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002270 break;
2271 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2272 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2273 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2274 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2275 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2276 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2277 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2278 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2279 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002280 case EOpFaceForward:
2281 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002282 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002283 {
2284 case 1: mUsesFaceforward1 = true; break;
2285 case 2: mUsesFaceforward2 = true; break;
2286 case 3: mUsesFaceforward3 = true; break;
2287 case 4: mUsesFaceforward4 = true; break;
2288 default: UNREACHABLE();
2289 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002290
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002291 outputTriplet(visit, "faceforward(", ", ", ")");
2292 }
2293 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2295 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2296 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297 default: UNREACHABLE();
2298 }
2299
2300 return true;
2301}
2302
2303bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2304{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002305 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002307 if (node->usesTernaryOperator())
2308 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002309 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002310 }
2311 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002313 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002314
Corentin Wallez80bacde2014-11-10 12:07:37 -08002315 // D3D errors when there is a gradient operation in a loop in an unflattened if
2316 // however flattening all the ifs in branch heavy shaders made D3D error too.
2317 // As a temporary workaround we flatten the ifs only if there is at least a loop
2318 // present somewhere in the shader.
2319 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2320 {
2321 out << "FLATTEN ";
2322 }
2323
2324 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002325
2326 node->getCondition()->traverse(this);
2327
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002328 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002329
Jamie Madill075edd82013-07-08 13:30:19 -04002330 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002331 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002333 bool discard = false;
2334
daniel@transgaming.combb885322010-04-15 20:45:24 +00002335 if (node->getTrueBlock())
2336 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002337 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002338
2339 // Detect true discard
2340 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002341 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002342
Jamie Madill075edd82013-07-08 13:30:19 -04002343 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002344 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002345
2346 if (node->getFalseBlock())
2347 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002348 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002349
Jamie Madill075edd82013-07-08 13:30:19 -04002350 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002351 out << "{\n";
2352
Jamie Madill075edd82013-07-08 13:30:19 -04002353 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002354 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002355
Jamie Madill075edd82013-07-08 13:30:19 -04002356 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002357 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002358
2359 // Detect false discard
2360 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2361 }
2362
2363 // ANGLE issue 486: Detect problematic conditional discard
2364 if (discard && FindSideEffectRewriting::search(node))
2365 {
2366 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002367 }
2368 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002369
2370 return false;
2371}
2372
2373void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2374{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002375 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376}
2377
2378bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2379{
Nicolas Capens655fe362014-04-11 13:12:34 -04002380 mNestedLoopDepth++;
2381
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002382 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2383
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002384 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002385 {
2386 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2387 }
2388
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002389 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002390 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002391 if (handleExcessiveLoop(node))
2392 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002393 mInsideDiscontinuousLoop = wasDiscontinuous;
2394 mNestedLoopDepth--;
2395
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002396 return false;
2397 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002398 }
2399
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002400 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002401
alokp@chromium.org52813552010-11-16 18:36:09 +00002402 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002404 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002405
Jamie Madill075edd82013-07-08 13:30:19 -04002406 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002407 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 }
2409 else
2410 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002411 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002412
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 if (node->getInit())
2414 {
2415 node->getInit()->traverse(this);
2416 }
2417
2418 out << "; ";
2419
alokp@chromium.org52813552010-11-16 18:36:09 +00002420 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002422 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423 }
2424
2425 out << "; ";
2426
alokp@chromium.org52813552010-11-16 18:36:09 +00002427 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002429 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 }
2431
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002432 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002433
Jamie Madill075edd82013-07-08 13:30:19 -04002434 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002435 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436 }
2437
2438 if (node->getBody())
2439 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002440 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 }
2442
Jamie Madill075edd82013-07-08 13:30:19 -04002443 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002444 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445
alokp@chromium.org52813552010-11-16 18:36:09 +00002446 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447 {
Jamie Madill075edd82013-07-08 13:30:19 -04002448 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 out << "while(\n";
2450
alokp@chromium.org52813552010-11-16 18:36:09 +00002451 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452
daniel@transgaming.com73536982012-03-21 20:45:49 +00002453 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 }
2455
daniel@transgaming.com73536982012-03-21 20:45:49 +00002456 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002457
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002458 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002459 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002460
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461 return false;
2462}
2463
2464bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2465{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002466 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467
2468 switch (node->getFlowOp())
2469 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002470 case EOpKill:
2471 outputTriplet(visit, "discard;\n", "", "");
2472 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002473 case EOpBreak:
2474 if (visit == PreVisit)
2475 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002476 if (mNestedLoopDepth > 1)
2477 {
2478 mUsesNestedBreak = true;
2479 }
2480
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002481 if (mExcessiveLoopIndex)
2482 {
2483 out << "{Break";
2484 mExcessiveLoopIndex->traverse(this);
2485 out << " = true; break;}\n";
2486 }
2487 else
2488 {
2489 out << "break;\n";
2490 }
2491 }
2492 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002493 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002494 case EOpReturn:
2495 if (visit == PreVisit)
2496 {
2497 if (node->getExpression())
2498 {
2499 out << "return ";
2500 }
2501 else
2502 {
2503 out << "return;\n";
2504 }
2505 }
2506 else if (visit == PostVisit)
2507 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002508 if (node->getExpression())
2509 {
2510 out << ";\n";
2511 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512 }
2513 break;
2514 default: UNREACHABLE();
2515 }
2516
2517 return true;
2518}
2519
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002520void OutputHLSL::traverseStatements(TIntermNode *node)
2521{
2522 if (isSingleStatement(node))
2523 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002524 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002525 }
2526
2527 node->traverse(this);
2528}
2529
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002530bool OutputHLSL::isSingleStatement(TIntermNode *node)
2531{
2532 TIntermAggregate *aggregate = node->getAsAggregate();
2533
2534 if (aggregate)
2535 {
2536 if (aggregate->getOp() == EOpSequence)
2537 {
2538 return false;
2539 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002540 else if (aggregate->getOp() == EOpDeclaration)
2541 {
2542 // Declaring multiple comma-separated variables must be considered multiple statements
2543 // because each individual declaration has side effects which are visible in the next.
2544 return false;
2545 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002546 else
2547 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002548 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002549 {
2550 if (!isSingleStatement(*sit))
2551 {
2552 return false;
2553 }
2554 }
2555
2556 return true;
2557 }
2558 }
2559
2560 return true;
2561}
2562
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002563// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2564// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002565bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2566{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002567 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002568 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002569
2570 // Parse loops of the form:
2571 // for(int index = initial; index [comparator] limit; index += increment)
2572 TIntermSymbol *index = NULL;
2573 TOperator comparator = EOpNull;
2574 int initial = 0;
2575 int limit = 0;
2576 int increment = 0;
2577
2578 // Parse index name and intial value
2579 if (node->getInit())
2580 {
2581 TIntermAggregate *init = node->getInit()->getAsAggregate();
2582
2583 if (init)
2584 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002585 TIntermSequence *sequence = init->getSequence();
2586 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002587
2588 if (variable && variable->getQualifier() == EvqTemporary)
2589 {
2590 TIntermBinary *assign = variable->getAsBinaryNode();
2591
2592 if (assign->getOp() == EOpInitialize)
2593 {
2594 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2595 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2596
2597 if (symbol && constant)
2598 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002599 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002600 {
2601 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002602 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002603 }
2604 }
2605 }
2606 }
2607 }
2608 }
2609
2610 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002611 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002612 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002613 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002614
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002615 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2616 {
2617 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2618
2619 if (constant)
2620 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002621 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002622 {
2623 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002624 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002625 }
2626 }
2627 }
2628 }
2629
2630 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002631 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002632 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002633 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2634 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002635
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002636 if (binaryTerminal)
2637 {
2638 TOperator op = binaryTerminal->getOp();
2639 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2640
2641 if (constant)
2642 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002643 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002644 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002645 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002646
2647 switch (op)
2648 {
2649 case EOpAddAssign: increment = value; break;
2650 case EOpSubAssign: increment = -value; break;
2651 default: UNIMPLEMENTED();
2652 }
2653 }
2654 }
2655 }
2656 else if (unaryTerminal)
2657 {
2658 TOperator op = unaryTerminal->getOp();
2659
2660 switch (op)
2661 {
2662 case EOpPostIncrement: increment = 1; break;
2663 case EOpPostDecrement: increment = -1; break;
2664 case EOpPreIncrement: increment = 1; break;
2665 case EOpPreDecrement: increment = -1; break;
2666 default: UNIMPLEMENTED();
2667 }
2668 }
2669 }
2670
2671 if (index != NULL && comparator != EOpNull && increment != 0)
2672 {
2673 if (comparator == EOpLessThanEqual)
2674 {
2675 comparator = EOpLessThan;
2676 limit += 1;
2677 }
2678
2679 if (comparator == EOpLessThan)
2680 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002681 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002682
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002683 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002684 {
2685 return false; // Not an excessive loop
2686 }
2687
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002688 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2689 mExcessiveLoopIndex = index;
2690
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002691 out << "{int ";
2692 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002693 out << ";\n"
2694 "bool Break";
2695 index->traverse(this);
2696 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002697
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002698 bool firstLoopFragment = true;
2699
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002700 while (iterations > 0)
2701 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002702 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002703
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002704 if (!firstLoopFragment)
2705 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002706 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002707 index->traverse(this);
2708 out << ") {\n";
2709 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002710
2711 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2712 {
2713 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2714 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002715
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002716 // for(int index = initial; index < clampedLimit; index += increment)
2717
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002718 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002719 index->traverse(this);
2720 out << " = ";
2721 out << initial;
2722
2723 out << "; ";
2724 index->traverse(this);
2725 out << " < ";
2726 out << clampedLimit;
2727
2728 out << "; ";
2729 index->traverse(this);
2730 out << " += ";
2731 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002732 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002733
Jamie Madill075edd82013-07-08 13:30:19 -04002734 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002735 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002736
2737 if (node->getBody())
2738 {
2739 node->getBody()->traverse(this);
2740 }
2741
Jamie Madill075edd82013-07-08 13:30:19 -04002742 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002743 out << ";}\n";
2744
2745 if (!firstLoopFragment)
2746 {
2747 out << "}\n";
2748 }
2749
2750 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002751
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002752 initial += MAX_LOOP_ITERATIONS * increment;
2753 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002754 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002755
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002756 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002757
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002758 mExcessiveLoopIndex = restoreIndex;
2759
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002760 return true;
2761 }
2762 else UNIMPLEMENTED();
2763 }
2764
2765 return false; // Not handled as an excessive loop
2766}
2767
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002768void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002769{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002770 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002771
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002772 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002773 {
2774 out << preString;
2775 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002776 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002777 {
2778 out << inString;
2779 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002780 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002781 {
2782 out << postString;
2783 }
2784}
2785
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002786void OutputHLSL::outputLineDirective(int line)
2787{
2788 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2789 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002790 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002791 mBody << "#line " << line;
2792
2793 if (mContext.sourcePath)
2794 {
2795 mBody << " \"" << mContext.sourcePath << "\"";
2796 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002797
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002798 mBody << "\n";
2799 }
2800}
2801
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002802TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2803{
2804 TQualifier qualifier = symbol->getQualifier();
2805 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002806 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002807
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002808 if (name.empty()) // HLSL demands named arguments, also for prototypes
2809 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002810 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002811 }
2812 else
2813 {
Jamie Madill033dae62014-06-18 12:56:28 -04002814 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002815 }
2816
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002817 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2818 {
Jamie Madill033dae62014-06-18 12:56:28 -04002819 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002820 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002821 }
2822
Jamie Madill033dae62014-06-18 12:56:28 -04002823 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002824}
2825
2826TString OutputHLSL::initializer(const TType &type)
2827{
2828 TString string;
2829
Jamie Madill94bf7f22013-07-08 13:31:15 -04002830 size_t size = type.getObjectSize();
2831 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002832 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002833 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002834
Jamie Madill94bf7f22013-07-08 13:31:15 -04002835 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002836 {
2837 string += ", ";
2838 }
2839 }
2840
daniel@transgaming.comead23042010-04-29 03:35:36 +00002841 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002842}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002843
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002844void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2845{
2846 TInfoSinkBase &out = mBody;
2847
2848 if (visit == PreVisit)
2849 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002850 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002851
2852 out << name + "(";
2853 }
2854 else if (visit == InVisit)
2855 {
2856 out << ", ";
2857 }
2858 else if (visit == PostVisit)
2859 {
2860 out << ")";
2861 }
2862}
2863
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002864const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2865{
2866 TInfoSinkBase &out = mBody;
2867
Jamie Madill98493dd2013-07-08 14:39:03 -04002868 const TStructure* structure = type.getStruct();
2869 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002870 {
Jamie Madill033dae62014-06-18 12:56:28 -04002871 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002872
Jamie Madill98493dd2013-07-08 14:39:03 -04002873 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002874
Jamie Madill98493dd2013-07-08 14:39:03 -04002875 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002876 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002877 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002878 constUnion = writeConstantUnion(*fieldType, constUnion);
2879
Jamie Madill98493dd2013-07-08 14:39:03 -04002880 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002881 {
2882 out << ", ";
2883 }
2884 }
2885
2886 out << ")";
2887 }
2888 else
2889 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002890 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002891 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002892
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002893 if (writeType)
2894 {
Jamie Madill033dae62014-06-18 12:56:28 -04002895 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002896 }
2897
Jamie Madill94bf7f22013-07-08 13:31:15 -04002898 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002899 {
2900 switch (constUnion->getType())
2901 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002902 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002903 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002904 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002905 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002906 default: UNREACHABLE();
2907 }
2908
2909 if (i != size - 1)
2910 {
2911 out << ", ";
2912 }
2913 }
2914
2915 if (writeType)
2916 {
2917 out << ")";
2918 }
2919 }
2920
2921 return constUnion;
2922}
2923
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002924}