blob: bded1fa9b9e3cabccb757b1d48a4322426409777 [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;
138 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000139 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400140 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000141
142 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000143
Jamie Madill8daaba12014-06-13 10:04:33 -0400144 mStructureHLSL = new StructureHLSL;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400145 mUniformHLSL = new UniformHLSL(mStructureHLSL, parentTranslator);
Jamie Madill8daaba12014-06-13 10:04:33 -0400146
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000147 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000148 {
Jamie Madill183bde52014-07-02 15:31:19 -0400149 if (mContext.shaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000150 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400151 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
152 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000153 }
154 else
155 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400156 // Reserve registers for dx_DepthRange and dx_ViewAdjust
157 mUniformHLSL->reserveUniformRegisters(2);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000158 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000159 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000160
Jamie Madillf91ce812014-06-13 10:04:34 -0400161 // Reserve registers for the default uniform block and driver constants
162 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000163}
164
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000165OutputHLSL::~OutputHLSL()
166{
Jamie Madill8daaba12014-06-13 10:04:33 -0400167 SafeDelete(mUnfoldShortCircuit);
168 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400169 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000170}
171
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000172void OutputHLSL::output()
173{
Jamie Madill183bde52014-07-02 15:31:19 -0400174 mContainsLoopDiscontinuity = mContext.shaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400175 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
176 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000177
Jamie Madille53c98b2014-02-03 11:57:13 -0500178 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
179 // use a vertex attribute as a condition, and some related computation in the else block.
Jamie Madill183bde52014-07-02 15:31:19 -0400180 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500181 {
182 RewriteElseBlocks(mContext.treeRoot);
183 }
184
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000185 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 +0000186 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000187
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000188 mContext.infoSink().obj << mHeader.c_str();
189 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000190}
191
Jamie Madill570e04d2013-06-21 09:15:33 -0400192void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
193{
194 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
195 {
196 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
197
198 // This will mark the necessary block elements as referenced
199 flaggedNode->traverse(this);
200 TString structName(mBody.c_str());
201 mBody.erase();
202
203 mFlaggedStructOriginalNames[flaggedNode] = structName;
204
205 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
206 {
207 structName.erase(pos, 1);
208 }
209
210 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
211 }
212}
213
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000214TInfoSinkBase &OutputHLSL::getBodyStream()
215{
216 return mBody;
217}
218
Jamie Madill4e1fd412014-07-10 17:50:10 -0400219const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
220{
221 return mUniformHLSL->getInterfaceBlockRegisterMap();
222}
223
Jamie Madill9fe25e92014-07-18 10:33:08 -0400224const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
225{
226 return mUniformHLSL->getUniformRegisterMap();
227}
228
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000229int OutputHLSL::vectorSize(const TType &type) const
230{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000231 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000232 int arraySize = type.isArray() ? type.getArraySize() : 1;
233
234 return elementSize * arraySize;
235}
236
Jamie Madill98493dd2013-07-08 14:39:03 -0400237TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400238{
239 TString init;
240
241 TString preIndentString;
242 TString fullIndentString;
243
244 for (int spaces = 0; spaces < (indent * 4); spaces++)
245 {
246 preIndentString += ' ';
247 }
248
249 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
250 {
251 fullIndentString += ' ';
252 }
253
254 init += preIndentString + "{\n";
255
Jamie Madill98493dd2013-07-08 14:39:03 -0400256 const TFieldList &fields = structure.fields();
257 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400258 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400259 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400260 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400261 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400262
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400264 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400265 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400266 }
267 else
268 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400270 }
271 }
272
273 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
274
275 return init;
276}
277
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278void OutputHLSL::header()
279{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000280 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000282 TString varyings;
283 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400284 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000285
Jamie Madill829f59e2013-11-13 19:40:54 -0500286 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400287 {
288 TIntermTyped *structNode = flaggedStructIt->first;
289 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400290 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400291 const TString &originalName = mFlaggedStructOriginalNames[structNode];
292
Jamie Madill033dae62014-06-18 12:56:28 -0400293 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400294 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400295 flaggedStructs += "\n";
296 }
297
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000298 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
299 {
300 const TType &type = varying->second->getType();
301 const TString &name = varying->second->getSymbol();
302
303 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400304 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
305 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000306 }
307
308 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
309 {
310 const TType &type = attribute->second->getType();
311 const TString &name = attribute->second->getSymbol();
312
Jamie Madill033dae62014-06-18 12:56:28 -0400313 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000314 }
315
Jamie Madill8daaba12014-06-13 10:04:33 -0400316 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400317
Jamie Madillf91ce812014-06-13 10:04:34 -0400318 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
319 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
320
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500321 if (mUsesDiscardRewriting)
322 {
323 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
324 }
325
Nicolas Capens655fe362014-04-11 13:12:34 -0400326 if (mUsesNestedBreak)
327 {
328 out << "#define ANGLE_USES_NESTED_BREAK" << "\n";
329 }
330
Jamie Madill183bde52014-07-02 15:31:19 -0400331 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000333 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000334 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000335
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000336 out << "// Varyings\n";
337 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400338 out << "\n";
339
340 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000341 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500342 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000343 {
Jamie Madill46131a32013-06-20 11:55:50 -0400344 const TString &variableName = outputVariableIt->first;
345 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400346
Jamie Madill033dae62014-06-18 12:56:28 -0400347 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400348 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000349 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000350 }
Jamie Madill46131a32013-06-20 11:55:50 -0400351 else
352 {
353 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
354
355 out << "static float4 gl_Color[" << numColorValues << "] =\n"
356 "{\n";
357 for (unsigned int i = 0; i < numColorValues; i++)
358 {
359 out << " float4(0, 0, 0, 0)";
360 if (i + 1 != numColorValues)
361 {
362 out << ",";
363 }
364 out << "\n";
365 }
366
367 out << "};\n";
368 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000369
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400370 if (mUsesFragDepth)
371 {
372 out << "static float gl_Depth = 0.0;\n";
373 }
374
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000375 if (mUsesFragCoord)
376 {
377 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
378 }
379
380 if (mUsesPointCoord)
381 {
382 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
383 }
384
385 if (mUsesFrontFacing)
386 {
387 out << "static bool gl_FrontFacing = false;\n";
388 }
389
390 out << "\n";
391
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000392 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000393 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000394 out << "struct gl_DepthRangeParameters\n"
395 "{\n"
396 " float near;\n"
397 " float far;\n"
398 " float diff;\n"
399 "};\n"
400 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000401 }
402
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000403 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000404 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000405 out << "cbuffer DriverConstants : register(b1)\n"
406 "{\n";
407
408 if (mUsesDepthRange)
409 {
410 out << " float3 dx_DepthRange : packoffset(c0);\n";
411 }
412
413 if (mUsesFragCoord)
414 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000415 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000416 }
417
418 if (mUsesFragCoord || mUsesFrontFacing)
419 {
420 out << " float3 dx_DepthFront : packoffset(c2);\n";
421 }
422
423 out << "};\n";
424 }
425 else
426 {
427 if (mUsesDepthRange)
428 {
429 out << "uniform float3 dx_DepthRange : register(c0);";
430 }
431
432 if (mUsesFragCoord)
433 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000434 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000435 }
436
437 if (mUsesFragCoord || mUsesFrontFacing)
438 {
439 out << "uniform float3 dx_DepthFront : register(c2);\n";
440 }
441 }
442
443 out << "\n";
444
445 if (mUsesDepthRange)
446 {
447 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
448 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000449 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000450
Jamie Madillf91ce812014-06-13 10:04:34 -0400451 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000452 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400453 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000454 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400455 out << flaggedStructs;
456 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000457 }
458
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000459 if (usingMRTExtension && mNumRenderTargets > 1)
460 {
461 out << "#define GL_USES_MRT\n";
462 }
463
464 if (mUsesFragColor)
465 {
466 out << "#define GL_USES_FRAG_COLOR\n";
467 }
468
469 if (mUsesFragData)
470 {
471 out << "#define GL_USES_FRAG_DATA\n";
472 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000474 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000476 out << "// Attributes\n";
477 out << attributes;
478 out << "\n"
479 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400480
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000481 if (mUsesPointSize)
482 {
483 out << "static float gl_PointSize = float(1);\n";
484 }
485
486 out << "\n"
487 "// Varyings\n";
488 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000489 out << "\n";
490
491 if (mUsesDepthRange)
492 {
493 out << "struct gl_DepthRangeParameters\n"
494 "{\n"
495 " float near;\n"
496 " float far;\n"
497 " float diff;\n"
498 "};\n"
499 "\n";
500 }
501
502 if (mOutputType == SH_HLSL11_OUTPUT)
503 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000504 if (mUsesDepthRange)
505 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000506 out << "cbuffer DriverConstants : register(b1)\n"
507 "{\n"
508 " float3 dx_DepthRange : packoffset(c0);\n"
509 "};\n"
510 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000511 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000512 }
513 else
514 {
515 if (mUsesDepthRange)
516 {
517 out << "uniform float3 dx_DepthRange : register(c0);\n";
518 }
519
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000520 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000521 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000522 }
523
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000524 if (mUsesDepthRange)
525 {
526 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
527 "\n";
528 }
529
Jamie Madillf91ce812014-06-13 10:04:34 -0400530 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000531 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400532 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000533 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400534 out << flaggedStructs;
535 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000536 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400537 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000538
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400539 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
540 {
541 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400542 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000543 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400544 switch(textureFunction->sampler)
545 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400546 case EbtSampler2D: out << "int2 "; break;
547 case EbtSampler3D: out << "int3 "; break;
548 case EbtSamplerCube: out << "int2 "; break;
549 case EbtSampler2DArray: out << "int3 "; break;
550 case EbtISampler2D: out << "int2 "; break;
551 case EbtISampler3D: out << "int3 "; break;
552 case EbtISamplerCube: out << "int2 "; break;
553 case EbtISampler2DArray: out << "int3 "; break;
554 case EbtUSampler2D: out << "int2 "; break;
555 case EbtUSampler3D: out << "int3 "; break;
556 case EbtUSamplerCube: out << "int2 "; break;
557 case EbtUSampler2DArray: out << "int3 "; break;
558 case EbtSampler2DShadow: out << "int2 "; break;
559 case EbtSamplerCubeShadow: out << "int2 "; break;
560 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400561 default: UNREACHABLE();
562 }
563 }
564 else // Sampling function
565 {
566 switch(textureFunction->sampler)
567 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400568 case EbtSampler2D: out << "float4 "; break;
569 case EbtSampler3D: out << "float4 "; break;
570 case EbtSamplerCube: out << "float4 "; break;
571 case EbtSampler2DArray: out << "float4 "; break;
572 case EbtISampler2D: out << "int4 "; break;
573 case EbtISampler3D: out << "int4 "; break;
574 case EbtISamplerCube: out << "int4 "; break;
575 case EbtISampler2DArray: out << "int4 "; break;
576 case EbtUSampler2D: out << "uint4 "; break;
577 case EbtUSampler3D: out << "uint4 "; break;
578 case EbtUSamplerCube: out << "uint4 "; break;
579 case EbtUSampler2DArray: out << "uint4 "; break;
580 case EbtSampler2DShadow: out << "float "; break;
581 case EbtSamplerCubeShadow: out << "float "; break;
582 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400583 default: UNREACHABLE();
584 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000585 }
586
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400587 // Function name
588 out << textureFunction->name();
589
590 // Argument list
591 int hlslCoords = 4;
592
593 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000594 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400595 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000596 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400597 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
598 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
599 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000600 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400601
Nicolas Capens75fb4752013-07-10 15:14:47 -0400602 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000603 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400604 case TextureFunction::IMPLICIT: break;
605 case TextureFunction::BIAS: hlslCoords = 4; break;
606 case TextureFunction::LOD: hlslCoords = 4; break;
607 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400608 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400609 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000610 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400611 }
612 else if (mOutputType == SH_HLSL11_OUTPUT)
613 {
614 switch(textureFunction->sampler)
615 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400616 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
617 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
618 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
619 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
620 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
621 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500622 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400623 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
624 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
625 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500626 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400627 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
628 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
629 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
630 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400631 default: UNREACHABLE();
632 }
633 }
634 else UNREACHABLE();
635
Nicolas Capensfc014542014-02-18 14:47:13 -0500636 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400637 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500638 switch(textureFunction->coords)
639 {
640 case 2: out << ", int2 t"; break;
641 case 3: out << ", int3 t"; break;
642 default: UNREACHABLE();
643 }
644 }
645 else // Floating-point coordinates (except textureSize)
646 {
647 switch(textureFunction->coords)
648 {
649 case 1: out << ", int lod"; break; // textureSize()
650 case 2: out << ", float2 t"; break;
651 case 3: out << ", float3 t"; break;
652 case 4: out << ", float4 t"; break;
653 default: UNREACHABLE();
654 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000655 }
656
Nicolas Capensd11d5492014-02-19 17:06:10 -0500657 if (textureFunction->method == TextureFunction::GRAD)
658 {
659 switch(textureFunction->sampler)
660 {
661 case EbtSampler2D:
662 case EbtISampler2D:
663 case EbtUSampler2D:
664 case EbtSampler2DArray:
665 case EbtISampler2DArray:
666 case EbtUSampler2DArray:
667 case EbtSampler2DShadow:
668 case EbtSampler2DArrayShadow:
669 out << ", float2 ddx, float2 ddy";
670 break;
671 case EbtSampler3D:
672 case EbtISampler3D:
673 case EbtUSampler3D:
674 case EbtSamplerCube:
675 case EbtISamplerCube:
676 case EbtUSamplerCube:
677 case EbtSamplerCubeShadow:
678 out << ", float3 ddx, float3 ddy";
679 break;
680 default: UNREACHABLE();
681 }
682 }
683
Nicolas Capens75fb4752013-07-10 15:14:47 -0400684 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000685 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400686 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400687 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400688 case TextureFunction::LOD: out << ", float lod"; break;
689 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400690 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400691 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500692 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500693 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400694 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000695 }
696
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500697 if (textureFunction->offset)
698 {
699 switch(textureFunction->sampler)
700 {
701 case EbtSampler2D: out << ", int2 offset"; break;
702 case EbtSampler3D: out << ", int3 offset"; break;
703 case EbtSampler2DArray: out << ", int2 offset"; break;
704 case EbtISampler2D: out << ", int2 offset"; break;
705 case EbtISampler3D: out << ", int3 offset"; break;
706 case EbtISampler2DArray: out << ", int2 offset"; break;
707 case EbtUSampler2D: out << ", int2 offset"; break;
708 case EbtUSampler3D: out << ", int3 offset"; break;
709 case EbtUSampler2DArray: out << ", int2 offset"; break;
710 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500711 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500712 default: UNREACHABLE();
713 }
714 }
715
Nicolas Capens84cfa122014-04-14 13:48:45 -0400716 if (textureFunction->method == TextureFunction::BIAS ||
717 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500718 {
719 out << ", float bias";
720 }
721
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400722 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400723 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400724
Nicolas Capens75fb4752013-07-10 15:14:47 -0400725 if (textureFunction->method == TextureFunction::SIZE)
726 {
727 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
728 {
729 if (IsSamplerArray(textureFunction->sampler))
730 {
731 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
732 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
733 }
734 else
735 {
736 out << " uint width; uint height; uint numberOfLevels;\n"
737 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
738 }
739 }
740 else if (IsSampler3D(textureFunction->sampler))
741 {
742 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
743 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
744 }
745 else UNREACHABLE();
746
747 switch(textureFunction->sampler)
748 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400749 case EbtSampler2D: out << " return int2(width, height);"; break;
750 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
751 case EbtSamplerCube: out << " return int2(width, height);"; break;
752 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
753 case EbtISampler2D: out << " return int2(width, height);"; break;
754 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
755 case EbtISamplerCube: out << " return int2(width, height);"; break;
756 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
757 case EbtUSampler2D: out << " return int2(width, height);"; break;
758 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
759 case EbtUSamplerCube: out << " return int2(width, height);"; break;
760 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
761 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
762 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
763 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400764 default: UNREACHABLE();
765 }
766 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400767 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400768 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500769 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
770 {
771 out << " float width; float height; float layers; float levels;\n";
772
773 out << " uint mip = 0;\n";
774
775 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
776
777 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
778 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
779 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
780 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
781
782 // FACE_POSITIVE_X = 000b
783 // FACE_NEGATIVE_X = 001b
784 // FACE_POSITIVE_Y = 010b
785 // FACE_NEGATIVE_Y = 011b
786 // FACE_POSITIVE_Z = 100b
787 // FACE_NEGATIVE_Z = 101b
788 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
789
790 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
791 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
792 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
793
794 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
795 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
796 }
797 else if (IsIntegerSampler(textureFunction->sampler) &&
798 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400799 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400800 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400801 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400802 if (IsSamplerArray(textureFunction->sampler))
803 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400804 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400805
Nicolas Capens9edebd62013-08-06 10:59:10 -0400806 if (textureFunction->method == TextureFunction::LOD0)
807 {
808 out << " uint mip = 0;\n";
809 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400810 else if (textureFunction->method == TextureFunction::LOD0BIAS)
811 {
812 out << " uint mip = bias;\n";
813 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400814 else
815 {
816 if (textureFunction->method == TextureFunction::IMPLICIT ||
817 textureFunction->method == TextureFunction::BIAS)
818 {
819 out << " x.GetDimensions(0, width, height, layers, levels);\n"
820 " float2 tSized = float2(t.x * width, t.y * height);\n"
821 " float dx = length(ddx(tSized));\n"
822 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500823 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400824
825 if (textureFunction->method == TextureFunction::BIAS)
826 {
827 out << " lod += bias;\n";
828 }
829 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500830 else if (textureFunction->method == TextureFunction::GRAD)
831 {
832 out << " x.GetDimensions(0, width, height, layers, levels);\n"
833 " float lod = log2(max(length(ddx), length(ddy)));\n";
834 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400835
836 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
837 }
838
839 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400840 }
841 else
842 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400843 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400844
Nicolas Capens9edebd62013-08-06 10:59:10 -0400845 if (textureFunction->method == TextureFunction::LOD0)
846 {
847 out << " uint mip = 0;\n";
848 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400849 else if (textureFunction->method == TextureFunction::LOD0BIAS)
850 {
851 out << " uint mip = bias;\n";
852 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400853 else
854 {
855 if (textureFunction->method == TextureFunction::IMPLICIT ||
856 textureFunction->method == TextureFunction::BIAS)
857 {
858 out << " x.GetDimensions(0, width, height, levels);\n"
859 " float2 tSized = float2(t.x * width, t.y * height);\n"
860 " float dx = length(ddx(tSized));\n"
861 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500862 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400863
864 if (textureFunction->method == TextureFunction::BIAS)
865 {
866 out << " lod += bias;\n";
867 }
868 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500869 else if (textureFunction->method == TextureFunction::LOD)
870 {
871 out << " x.GetDimensions(0, width, height, levels);\n";
872 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500873 else if (textureFunction->method == TextureFunction::GRAD)
874 {
875 out << " x.GetDimensions(0, width, height, levels);\n"
876 " float lod = log2(max(length(ddx), length(ddy)));\n";
877 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400878
879 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
880 }
881
882 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400883 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400884 }
885 else if (IsSampler3D(textureFunction->sampler))
886 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400887 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400888
Nicolas Capens9edebd62013-08-06 10:59:10 -0400889 if (textureFunction->method == TextureFunction::LOD0)
890 {
891 out << " uint mip = 0;\n";
892 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400893 else if (textureFunction->method == TextureFunction::LOD0BIAS)
894 {
895 out << " uint mip = bias;\n";
896 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400897 else
898 {
899 if (textureFunction->method == TextureFunction::IMPLICIT ||
900 textureFunction->method == TextureFunction::BIAS)
901 {
902 out << " x.GetDimensions(0, width, height, depth, levels);\n"
903 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
904 " float dx = length(ddx(tSized));\n"
905 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500906 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400907
908 if (textureFunction->method == TextureFunction::BIAS)
909 {
910 out << " lod += bias;\n";
911 }
912 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500913 else if (textureFunction->method == TextureFunction::GRAD)
914 {
915 out << " x.GetDimensions(0, width, height, depth, levels);\n"
916 " float lod = log2(max(length(ddx), length(ddy)));\n";
917 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400918
919 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
920 }
921
922 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400923 }
924 else UNREACHABLE();
925 }
926
927 out << " return ";
928
929 // HLSL intrinsic
930 if (mOutputType == SH_HLSL9_OUTPUT)
931 {
932 switch(textureFunction->sampler)
933 {
934 case EbtSampler2D: out << "tex2D"; break;
935 case EbtSamplerCube: out << "texCUBE"; break;
936 default: UNREACHABLE();
937 }
938
Nicolas Capens75fb4752013-07-10 15:14:47 -0400939 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400940 {
941 case TextureFunction::IMPLICIT: out << "(s, "; break;
942 case TextureFunction::BIAS: out << "bias(s, "; break;
943 case TextureFunction::LOD: out << "lod(s, "; break;
944 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400945 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400946 default: UNREACHABLE();
947 }
948 }
949 else if (mOutputType == SH_HLSL11_OUTPUT)
950 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500951 if (textureFunction->method == TextureFunction::GRAD)
952 {
953 if (IsIntegerSampler(textureFunction->sampler))
954 {
955 out << "x.Load(";
956 }
957 else if (IsShadowSampler(textureFunction->sampler))
958 {
959 out << "x.SampleCmpLevelZero(s, ";
960 }
961 else
962 {
963 out << "x.SampleGrad(s, ";
964 }
965 }
966 else if (IsIntegerSampler(textureFunction->sampler) ||
967 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400968 {
969 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400970 }
Nicolas Capenscb127d32013-07-15 17:26:18 -0400971 else if (IsShadowSampler(textureFunction->sampler))
972 {
973 out << "x.SampleCmp(s, ";
974 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400975 else
976 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400977 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400978 {
979 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
980 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
981 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
982 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400983 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400984 default: UNREACHABLE();
985 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400986 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400987 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400988 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400989
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400990 // Integer sampling requires integer addresses
991 TString addressx = "";
992 TString addressy = "";
993 TString addressz = "";
994 TString close = "";
995
Nicolas Capensfc014542014-02-18 14:47:13 -0500996 if (IsIntegerSampler(textureFunction->sampler) ||
997 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400998 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400999 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001000 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001001 case 2: out << "int3("; break;
1002 case 3: out << "int4("; break;
1003 default: UNREACHABLE();
1004 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001005
Nicolas Capensfc014542014-02-18 14:47:13 -05001006 // Convert from normalized floating-point to integer
1007 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001008 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001009 addressx = "int(floor(width * frac((";
1010 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001011
Nicolas Capensfc014542014-02-18 14:47:13 -05001012 if (IsSamplerArray(textureFunction->sampler))
1013 {
1014 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1015 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001016 else if (IsSamplerCube(textureFunction->sampler))
1017 {
1018 addressz = "((((";
1019 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001020 else
1021 {
1022 addressz = "int(floor(depth * frac((";
1023 }
1024
1025 close = "))))";
1026 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001027 }
1028 else
1029 {
1030 switch(hlslCoords)
1031 {
1032 case 2: out << "float2("; break;
1033 case 3: out << "float3("; break;
1034 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001035 default: UNREACHABLE();
1036 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001037 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001038
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001039 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001040
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001041 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001042 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001043 switch(textureFunction->coords)
1044 {
1045 case 3: proj = " / t.z"; break;
1046 case 4: proj = " / t.w"; break;
1047 default: UNREACHABLE();
1048 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001049 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001050
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001051 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001052
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001053 if (mOutputType == SH_HLSL9_OUTPUT)
1054 {
1055 if (hlslCoords >= 3)
1056 {
1057 if (textureFunction->coords < 3)
1058 {
1059 out << ", 0";
1060 }
1061 else
1062 {
1063 out << ", t.z" + proj;
1064 }
1065 }
1066
1067 if (hlslCoords == 4)
1068 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001069 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001070 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001071 case TextureFunction::BIAS: out << ", bias"; break;
1072 case TextureFunction::LOD: out << ", lod"; break;
1073 case TextureFunction::LOD0: out << ", 0"; break;
1074 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001075 default: UNREACHABLE();
1076 }
1077 }
1078
1079 out << "));\n";
1080 }
1081 else if (mOutputType == SH_HLSL11_OUTPUT)
1082 {
1083 if (hlslCoords >= 3)
1084 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001085 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1086 {
1087 out << ", face";
1088 }
1089 else
1090 {
1091 out << ", " + addressz + ("t.z" + proj) + close;
1092 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001093 }
1094
Nicolas Capensd11d5492014-02-19 17:06:10 -05001095 if (textureFunction->method == TextureFunction::GRAD)
1096 {
1097 if (IsIntegerSampler(textureFunction->sampler))
1098 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001099 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001100 }
1101 else if (IsShadowSampler(textureFunction->sampler))
1102 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001103 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001104 switch(textureFunction->coords)
1105 {
1106 case 3: out << "), t.z"; break;
1107 case 4: out << "), t.w"; break;
1108 default: UNREACHABLE();
1109 }
1110 }
1111 else
1112 {
1113 out << "), ddx, ddy";
1114 }
1115 }
1116 else if (IsIntegerSampler(textureFunction->sampler) ||
1117 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001118 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001119 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001120 }
1121 else if (IsShadowSampler(textureFunction->sampler))
1122 {
1123 // Compare value
1124 switch(textureFunction->coords)
1125 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001126 case 3: out << "), t.z"; break;
1127 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001128 default: UNREACHABLE();
1129 }
1130 }
1131 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001132 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001133 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001134 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001135 case TextureFunction::IMPLICIT: out << ")"; break;
1136 case TextureFunction::BIAS: out << "), bias"; break;
1137 case TextureFunction::LOD: out << "), lod"; break;
1138 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001139 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001140 default: UNREACHABLE();
1141 }
1142 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001143
1144 if (textureFunction->offset)
1145 {
1146 out << ", offset";
1147 }
1148
1149 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001150 }
1151 else UNREACHABLE();
1152 }
1153
1154 out << "\n"
1155 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001156 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001157 }
1158
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001159 if (mUsesFragCoord)
1160 {
1161 out << "#define GL_USES_FRAG_COORD\n";
1162 }
1163
1164 if (mUsesPointCoord)
1165 {
1166 out << "#define GL_USES_POINT_COORD\n";
1167 }
1168
1169 if (mUsesFrontFacing)
1170 {
1171 out << "#define GL_USES_FRONT_FACING\n";
1172 }
1173
1174 if (mUsesPointSize)
1175 {
1176 out << "#define GL_USES_POINT_SIZE\n";
1177 }
1178
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001179 if (mUsesFragDepth)
1180 {
1181 out << "#define GL_USES_FRAG_DEPTH\n";
1182 }
1183
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001184 if (mUsesDepthRange)
1185 {
1186 out << "#define GL_USES_DEPTH_RANGE\n";
1187 }
1188
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001189 if (mUsesXor)
1190 {
1191 out << "bool xor(bool p, bool q)\n"
1192 "{\n"
1193 " return (p || q) && !(p && q);\n"
1194 "}\n"
1195 "\n";
1196 }
1197
1198 if (mUsesMod1)
1199 {
1200 out << "float mod(float x, float y)\n"
1201 "{\n"
1202 " return x - y * floor(x / y);\n"
1203 "}\n"
1204 "\n";
1205 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001206
1207 if (mUsesMod2v)
1208 {
1209 out << "float2 mod(float2 x, float2 y)\n"
1210 "{\n"
1211 " return x - y * floor(x / y);\n"
1212 "}\n"
1213 "\n";
1214 }
1215
1216 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001217 {
1218 out << "float2 mod(float2 x, float y)\n"
1219 "{\n"
1220 " return x - y * floor(x / y);\n"
1221 "}\n"
1222 "\n";
1223 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001224
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001225 if (mUsesMod3v)
1226 {
1227 out << "float3 mod(float3 x, float3 y)\n"
1228 "{\n"
1229 " return x - y * floor(x / y);\n"
1230 "}\n"
1231 "\n";
1232 }
1233
1234 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001235 {
1236 out << "float3 mod(float3 x, float y)\n"
1237 "{\n"
1238 " return x - y * floor(x / y);\n"
1239 "}\n"
1240 "\n";
1241 }
1242
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001243 if (mUsesMod4v)
1244 {
1245 out << "float4 mod(float4 x, float4 y)\n"
1246 "{\n"
1247 " return x - y * floor(x / y);\n"
1248 "}\n"
1249 "\n";
1250 }
1251
1252 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001253 {
1254 out << "float4 mod(float4 x, float y)\n"
1255 "{\n"
1256 " return x - y * floor(x / y);\n"
1257 "}\n"
1258 "\n";
1259 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001260
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001261 if (mUsesFaceforward1)
1262 {
1263 out << "float faceforward(float N, float I, float Nref)\n"
1264 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001265 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001266 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001267 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001268 " }\n"
1269 " else\n"
1270 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001271 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001272 " }\n"
1273 "}\n"
1274 "\n";
1275 }
1276
1277 if (mUsesFaceforward2)
1278 {
1279 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1280 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001281 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001282 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001283 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001284 " }\n"
1285 " else\n"
1286 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001287 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001288 " }\n"
1289 "}\n"
1290 "\n";
1291 }
1292
1293 if (mUsesFaceforward3)
1294 {
1295 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1296 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001297 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001298 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001299 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001300 " }\n"
1301 " else\n"
1302 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001303 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001304 " }\n"
1305 "}\n"
1306 "\n";
1307 }
1308
1309 if (mUsesFaceforward4)
1310 {
1311 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1312 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001313 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001314 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001315 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001316 " }\n"
1317 " else\n"
1318 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001319 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001320 " }\n"
1321 "}\n"
1322 "\n";
1323 }
1324
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001325 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001326 {
1327 out << "float atanyx(float y, float x)\n"
1328 "{\n"
1329 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1330 " return atan2(y, x);\n"
1331 "}\n";
1332 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001333
1334 if (mUsesAtan2_2)
1335 {
1336 out << "float2 atanyx(float2 y, float2 x)\n"
1337 "{\n"
1338 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1339 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1340 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1341 "}\n";
1342 }
1343
1344 if (mUsesAtan2_3)
1345 {
1346 out << "float3 atanyx(float3 y, float3 x)\n"
1347 "{\n"
1348 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1349 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1350 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1351 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1352 "}\n";
1353 }
1354
1355 if (mUsesAtan2_4)
1356 {
1357 out << "float4 atanyx(float4 y, float4 x)\n"
1358 "{\n"
1359 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1360 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1361 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1362 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1363 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1364 "}\n";
1365 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001366}
1367
1368void OutputHLSL::visitSymbol(TIntermSymbol *node)
1369{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001370 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001371
Jamie Madill570e04d2013-06-21 09:15:33 -04001372 // Handle accessing std140 structs by value
1373 if (mFlaggedStructMappedNames.count(node) > 0)
1374 {
1375 out << mFlaggedStructMappedNames[node];
1376 return;
1377 }
1378
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001379 TString name = node->getSymbol();
1380
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001381 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001382 {
1383 mUsesDepthRange = true;
1384 out << name;
1385 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001386 else
1387 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001388 TQualifier qualifier = node->getQualifier();
1389
1390 if (qualifier == EvqUniform)
1391 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001392 const TType& nodeType = node->getType();
1393 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1394
1395 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001396 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001397 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001398 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001399 else
1400 {
1401 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001402 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001403
Jamie Madill033dae62014-06-18 12:56:28 -04001404 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001405 }
Jamie Madill19571812013-08-12 15:26:34 -07001406 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001407 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001408 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001409 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001410 }
Jamie Madill033dae62014-06-18 12:56:28 -04001411 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001412 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001413 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001414 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001415 }
Jamie Madill19571812013-08-12 15:26:34 -07001416 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001417 {
1418 mReferencedOutputVariables[name] = node;
1419 out << "out_" << name;
1420 }
1421 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001422 {
1423 out << "gl_Color[0]";
1424 mUsesFragColor = true;
1425 }
1426 else if (qualifier == EvqFragData)
1427 {
1428 out << "gl_Color";
1429 mUsesFragData = true;
1430 }
1431 else if (qualifier == EvqFragCoord)
1432 {
1433 mUsesFragCoord = true;
1434 out << name;
1435 }
1436 else if (qualifier == EvqPointCoord)
1437 {
1438 mUsesPointCoord = true;
1439 out << name;
1440 }
1441 else if (qualifier == EvqFrontFacing)
1442 {
1443 mUsesFrontFacing = true;
1444 out << name;
1445 }
1446 else if (qualifier == EvqPointSize)
1447 {
1448 mUsesPointSize = true;
1449 out << name;
1450 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001451 else if (name == "gl_FragDepthEXT")
1452 {
1453 mUsesFragDepth = true;
1454 out << "gl_Depth";
1455 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001456 else if (qualifier == EvqInternal)
1457 {
1458 out << name;
1459 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001460 else
1461 {
Jamie Madill033dae62014-06-18 12:56:28 -04001462 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001463 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001464 }
1465}
1466
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001467void OutputHLSL::visitRaw(TIntermRaw *node)
1468{
1469 mBody << node->getRawText();
1470}
1471
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001472bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1473{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001474 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001475
Jamie Madill570e04d2013-06-21 09:15:33 -04001476 // Handle accessing std140 structs by value
1477 if (mFlaggedStructMappedNames.count(node) > 0)
1478 {
1479 out << mFlaggedStructMappedNames[node];
1480 return false;
1481 }
1482
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001483 switch (node->getOp())
1484 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001485 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001486 case EOpInitialize:
1487 if (visit == PreVisit)
1488 {
1489 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1490 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1491 // new variable is created before the assignment is evaluated), so we need to convert
1492 // this to "float t = x, x = t;".
1493
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001494 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1495 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001496
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001497 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1498 expression->traverse(&searchSymbol);
1499 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001500
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001501 if (sameSymbol)
1502 {
1503 // Type already printed
1504 out << "t" + str(mUniqueIndex) + " = ";
1505 expression->traverse(this);
1506 out << ", ";
1507 symbolNode->traverse(this);
1508 out << " = t" + str(mUniqueIndex);
1509
1510 mUniqueIndex++;
1511 return false;
1512 }
1513 }
1514 else if (visit == InVisit)
1515 {
1516 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001517 }
1518 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001519 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1520 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1521 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1522 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1523 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1524 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001525 if (visit == PreVisit)
1526 {
1527 out << "(";
1528 }
1529 else if (visit == InVisit)
1530 {
1531 out << " = mul(";
1532 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001533 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001534 }
1535 else
1536 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001537 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001538 }
1539 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001540 case EOpMatrixTimesMatrixAssign:
1541 if (visit == PreVisit)
1542 {
1543 out << "(";
1544 }
1545 else if (visit == InVisit)
1546 {
1547 out << " = mul(";
1548 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001549 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001550 }
1551 else
1552 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001553 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001554 }
1555 break;
1556 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001557 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001558 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001559 const TType& leftType = node->getLeft()->getType();
1560 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001561 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001562 if (visit == PreVisit)
1563 {
1564 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1565 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001566 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001567 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001568 return false;
1569 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001570 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001571 else
1572 {
1573 outputTriplet(visit, "", "[", "]");
1574 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001575 }
1576 break;
1577 case EOpIndexIndirect:
1578 // We do not currently support indirect references to interface blocks
1579 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1580 outputTriplet(visit, "", "[", "]");
1581 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001582 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001583 if (visit == InVisit)
1584 {
1585 const TStructure* structure = node->getLeft()->getType().getStruct();
1586 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1587 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001588 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001589
1590 return false;
1591 }
1592 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001593 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001594 if (visit == InVisit)
1595 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001596 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1597 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1598 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001599 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001600
1601 return false;
1602 }
1603 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001604 case EOpVectorSwizzle:
1605 if (visit == InVisit)
1606 {
1607 out << ".";
1608
1609 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1610
1611 if (swizzle)
1612 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001613 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001614
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001615 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001616 {
1617 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1618
1619 if (element)
1620 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001621 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001622
1623 switch (i)
1624 {
1625 case 0: out << "x"; break;
1626 case 1: out << "y"; break;
1627 case 2: out << "z"; break;
1628 case 3: out << "w"; break;
1629 default: UNREACHABLE();
1630 }
1631 }
1632 else UNREACHABLE();
1633 }
1634 }
1635 else UNREACHABLE();
1636
1637 return false; // Fully processed
1638 }
1639 break;
1640 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1641 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1642 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1643 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001644 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001645 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001646 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001647 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001648 if (node->getOp() == EOpEqual)
1649 {
1650 outputTriplet(visit, "(", " == ", ")");
1651 }
1652 else
1653 {
1654 outputTriplet(visit, "(", " != ", ")");
1655 }
1656 }
1657 else if (node->getLeft()->getBasicType() == EbtStruct)
1658 {
1659 if (node->getOp() == EOpEqual)
1660 {
1661 out << "(";
1662 }
1663 else
1664 {
1665 out << "!(";
1666 }
1667
Jamie Madill98493dd2013-07-08 14:39:03 -04001668 const TStructure &structure = *node->getLeft()->getType().getStruct();
1669 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001670
Jamie Madill98493dd2013-07-08 14:39:03 -04001671 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001672 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001673 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001674
1675 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001676 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001677 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001678 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001679
Jamie Madill98493dd2013-07-08 14:39:03 -04001680 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001681 {
1682 out << " && ";
1683 }
1684 }
1685
1686 out << ")";
1687
1688 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001689 }
1690 else
1691 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001692 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001693
1694 if (node->getOp() == EOpEqual)
1695 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001696 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001697 }
1698 else
1699 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001700 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001701 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001702 }
1703 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1705 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1706 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1707 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1708 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001709 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001710 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1711 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001712 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001713 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001714 if (node->getRight()->hasSideEffects())
1715 {
1716 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1717 return false;
1718 }
1719 else
1720 {
1721 outputTriplet(visit, "(", " || ", ")");
1722 return true;
1723 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001724 case EOpLogicalXor:
1725 mUsesXor = true;
1726 outputTriplet(visit, "xor(", ", ", ")");
1727 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001728 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001729 if (node->getRight()->hasSideEffects())
1730 {
1731 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1732 return false;
1733 }
1734 else
1735 {
1736 outputTriplet(visit, "(", " && ", ")");
1737 return true;
1738 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 default: UNREACHABLE();
1740 }
1741
1742 return true;
1743}
1744
1745bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1746{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747 switch (node->getOp())
1748 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001749 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001750 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001751 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1752 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1753 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1754 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1755 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1756 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001757 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1758 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1759 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1760 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1761 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1762 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1763 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1764 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1765 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1766 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1767 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1768 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1769 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1770 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1771 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1772 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1773 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1774 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1775 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1776 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1777 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001778 case EOpDFdx:
1779 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1780 {
1781 outputTriplet(visit, "(", "", ", 0.0)");
1782 }
1783 else
1784 {
1785 outputTriplet(visit, "ddx(", "", ")");
1786 }
1787 break;
1788 case EOpDFdy:
1789 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1790 {
1791 outputTriplet(visit, "(", "", ", 0.0)");
1792 }
1793 else
1794 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001795 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001796 }
1797 break;
1798 case EOpFwidth:
1799 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1800 {
1801 outputTriplet(visit, "(", "", ", 0.0)");
1802 }
1803 else
1804 {
1805 outputTriplet(visit, "fwidth(", "", ")");
1806 }
1807 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001808 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1809 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810 default: UNREACHABLE();
1811 }
1812
1813 return true;
1814}
1815
1816bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1817{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001818 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820 switch (node->getOp())
1821 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001822 case EOpSequence:
1823 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001824 if (mInsideFunction)
1825 {
Jamie Madill075edd82013-07-08 13:30:19 -04001826 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001827 out << "{\n";
1828 }
1829
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001830 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001831 {
Jamie Madill075edd82013-07-08 13:30:19 -04001832 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001833
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001834 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001835
1836 out << ";\n";
1837 }
1838
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001839 if (mInsideFunction)
1840 {
Jamie Madill075edd82013-07-08 13:30:19 -04001841 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001842 out << "}\n";
1843 }
1844
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001845 return false;
1846 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001847 case EOpDeclaration:
1848 if (visit == PreVisit)
1849 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001850 TIntermSequence *sequence = node->getSequence();
1851 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001852
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001853 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001854 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001855 TStructure *structure = variable->getType().getStruct();
1856
1857 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001858 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001859 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001860 }
1861
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001862 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001863 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001864 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001865 {
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001866 if (isSingleStatement(*sit))
1867 {
1868 mUnfoldShortCircuit->traverse(*sit);
1869 }
1870
Nicolas Capensd974db42014-10-07 10:50:19 -04001871 if (!mInsideFunction)
1872 {
1873 out << "static ";
1874 }
1875
1876 out << TypeString(variable->getType()) + " ";
1877
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001878 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001880 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001882 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001883 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001884 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001885 }
1886 else
1887 {
1888 (*sit)->traverse(this);
1889 }
1890
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001891 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001892 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001893 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894 }
1895 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001897 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1898 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001899 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001900 }
1901 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001902 }
Jamie Madill033dae62014-06-18 12:56:28 -04001903 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001904 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001905 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001906 {
1907 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1908
1909 if (symbol)
1910 {
1911 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1912 mReferencedVaryings[symbol->getSymbol()] = symbol;
1913 }
1914 else
1915 {
1916 (*sit)->traverse(this);
1917 }
1918 }
1919 }
1920
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921 return false;
1922 }
1923 else if (visit == InVisit)
1924 {
1925 out << ", ";
1926 }
1927 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001928 case EOpInvariantDeclaration:
1929 // Do not do any translation
1930 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001931 case EOpPrototype:
1932 if (visit == PreVisit)
1933 {
Jamie Madill033dae62014-06-18 12:56:28 -04001934 out << TypeString(node->getType()) << " " << Decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001935
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001936 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001937
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001938 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001939 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001940 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001941
1942 if (symbol)
1943 {
1944 out << argumentString(symbol);
1945
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001946 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001947 {
1948 out << ", ";
1949 }
1950 }
1951 else UNREACHABLE();
1952 }
1953
1954 out << ");\n";
1955
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001956 // Also prototype the Lod0 variant if needed
1957 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1958 {
1959 mOutputLod0Function = true;
1960 node->traverse(this);
1961 mOutputLod0Function = false;
1962 }
1963
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001964 return false;
1965 }
1966 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001967 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968 case EOpFunction:
1969 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001970 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971
Jamie Madill033dae62014-06-18 12:56:28 -04001972 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001973
1974 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001975 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001976 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001977 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001978 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001979 {
Jamie Madill033dae62014-06-18 12:56:28 -04001980 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001981 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001982
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001983 TIntermSequence *sequence = node->getSequence();
1984 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001985
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001986 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001987 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001988 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001989
1990 if (symbol)
1991 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001992 TStructure *structure = symbol->getType().getStruct();
1993
1994 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001995 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001996 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001997 }
1998
1999 out << argumentString(symbol);
2000
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002001 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002002 {
2003 out << ", ";
2004 }
2005 }
2006 else UNREACHABLE();
2007 }
2008
2009 out << ")\n"
2010 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002011
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002012 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002013 {
2014 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002015 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002016 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002017 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002018
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002019 out << "}\n";
2020
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002021 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2022 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002023 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002024 {
2025 mOutputLod0Function = true;
2026 node->traverse(this);
2027 mOutputLod0Function = false;
2028 }
2029 }
2030
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002031 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032 }
2033 break;
2034 case EOpFunctionCall:
2035 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002036 TString name = TFunction::unmangleName(node->getName());
2037 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002038 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002039
2040 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002041 {
Jamie Madill033dae62014-06-18 12:56:28 -04002042 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002043 }
2044 else
2045 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002046 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002047
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002048 TextureFunction textureFunction;
2049 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002050 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002051 textureFunction.method = TextureFunction::IMPLICIT;
2052 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002053 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002054
2055 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002056 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002057 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002058 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002059 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002060 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002061 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002062 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002063 }
Nicolas Capens46485082014-04-15 13:12:50 -04002064 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2065 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002066 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002067 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002068 }
Nicolas Capens46485082014-04-15 13:12:50 -04002069 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002070 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002071 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002072 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002073 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002074 else if (name == "textureSize")
2075 {
2076 textureFunction.method = TextureFunction::SIZE;
2077 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002078 else if (name == "textureOffset")
2079 {
2080 textureFunction.method = TextureFunction::IMPLICIT;
2081 textureFunction.offset = true;
2082 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002083 else if (name == "textureProjOffset")
2084 {
2085 textureFunction.method = TextureFunction::IMPLICIT;
2086 textureFunction.offset = true;
2087 textureFunction.proj = true;
2088 }
2089 else if (name == "textureLodOffset")
2090 {
2091 textureFunction.method = TextureFunction::LOD;
2092 textureFunction.offset = true;
2093 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002094 else if (name == "textureProjLodOffset")
2095 {
2096 textureFunction.method = TextureFunction::LOD;
2097 textureFunction.proj = true;
2098 textureFunction.offset = true;
2099 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002100 else if (name == "texelFetch")
2101 {
2102 textureFunction.method = TextureFunction::FETCH;
2103 }
2104 else if (name == "texelFetchOffset")
2105 {
2106 textureFunction.method = TextureFunction::FETCH;
2107 textureFunction.offset = true;
2108 }
Nicolas Capens46485082014-04-15 13:12:50 -04002109 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002110 {
2111 textureFunction.method = TextureFunction::GRAD;
2112 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002113 else if (name == "textureGradOffset")
2114 {
2115 textureFunction.method = TextureFunction::GRAD;
2116 textureFunction.offset = true;
2117 }
Nicolas Capens46485082014-04-15 13:12:50 -04002118 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002119 {
2120 textureFunction.method = TextureFunction::GRAD;
2121 textureFunction.proj = true;
2122 }
2123 else if (name == "textureProjGradOffset")
2124 {
2125 textureFunction.method = TextureFunction::GRAD;
2126 textureFunction.proj = true;
2127 textureFunction.offset = true;
2128 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002129 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002130
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002131 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002132 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002133 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2134
2135 if (textureFunction.offset)
2136 {
2137 mandatoryArgumentCount++;
2138 }
2139
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002140 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002141
Jamie Madill183bde52014-07-02 15:31:19 -04002142 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002143 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002144 if (bias)
2145 {
2146 textureFunction.method = TextureFunction::LOD0BIAS;
2147 }
2148 else
2149 {
2150 textureFunction.method = TextureFunction::LOD0;
2151 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002152 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002153 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002154 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002155 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002156 }
2157 }
2158
2159 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002160
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002161 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002163
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002164 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002165 {
2166 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2167 {
2168 out << "texture_";
2169 (*arg)->traverse(this);
2170 out << ", sampler_";
2171 }
2172
2173 (*arg)->traverse(this);
2174
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002175 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002176 {
2177 out << ", ";
2178 }
2179 }
2180
2181 out << ")";
2182
2183 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002184 }
2185 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002186 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002187 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2188 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2189 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2190 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2191 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2192 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2193 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2194 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2195 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2196 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2197 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2198 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2199 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2200 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2201 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2202 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2203 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2204 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2205 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002206 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002207 {
Jamie Madill033dae62014-06-18 12:56:28 -04002208 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002209 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002210 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2211 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002212 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002213 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2214 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2215 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2216 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2217 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2218 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002219 case EOpMod:
2220 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002221 // We need to look at the number of components in both arguments
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002222 const int modValue = (*node->getSequence())[0]->getAsTyped()->getNominalSize() * 10 +
2223 (*node->getSequence())[1]->getAsTyped()->getNominalSize();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002224 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002225 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002226 case 11: mUsesMod1 = true; break;
2227 case 22: mUsesMod2v = true; break;
2228 case 21: mUsesMod2f = true; break;
2229 case 33: mUsesMod3v = true; break;
2230 case 31: mUsesMod3f = true; break;
2231 case 44: mUsesMod4v = true; break;
2232 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002233 default: UNREACHABLE();
2234 }
2235
2236 outputTriplet(visit, "mod(", ", ", ")");
2237 }
2238 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002239 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002241 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2242 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize())
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002243 {
2244 case 1: mUsesAtan2_1 = true; break;
2245 case 2: mUsesAtan2_2 = true; break;
2246 case 3: mUsesAtan2_3 = true; break;
2247 case 4: mUsesAtan2_4 = true; break;
2248 default: UNREACHABLE();
2249 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002250 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251 break;
2252 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2253 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2254 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2255 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2256 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2257 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2258 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2259 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2260 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002261 case EOpFaceForward:
2262 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002263 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002264 {
2265 case 1: mUsesFaceforward1 = true; break;
2266 case 2: mUsesFaceforward2 = true; break;
2267 case 3: mUsesFaceforward3 = true; break;
2268 case 4: mUsesFaceforward4 = true; break;
2269 default: UNREACHABLE();
2270 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002271
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002272 outputTriplet(visit, "faceforward(", ", ", ")");
2273 }
2274 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002275 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2276 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2277 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278 default: UNREACHABLE();
2279 }
2280
2281 return true;
2282}
2283
2284bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2285{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002286 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002288 if (node->usesTernaryOperator())
2289 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002290 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002291 }
2292 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002294 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002295
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002296 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002297
2298 node->getCondition()->traverse(this);
2299
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002300 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002301
Jamie Madill075edd82013-07-08 13:30:19 -04002302 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002303 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002305 bool discard = false;
2306
daniel@transgaming.combb885322010-04-15 20:45:24 +00002307 if (node->getTrueBlock())
2308 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002309 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002310
2311 // Detect true discard
2312 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002313 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002314
Jamie Madill075edd82013-07-08 13:30:19 -04002315 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002316 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002317
2318 if (node->getFalseBlock())
2319 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002320 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002321
Jamie Madill075edd82013-07-08 13:30:19 -04002322 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002323 out << "{\n";
2324
Jamie Madill075edd82013-07-08 13:30:19 -04002325 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002326 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002327
Jamie Madill075edd82013-07-08 13:30:19 -04002328 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002329 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002330
2331 // Detect false discard
2332 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2333 }
2334
2335 // ANGLE issue 486: Detect problematic conditional discard
2336 if (discard && FindSideEffectRewriting::search(node))
2337 {
2338 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002339 }
2340 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341
2342 return false;
2343}
2344
2345void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2346{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002347 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348}
2349
2350bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2351{
Nicolas Capens655fe362014-04-11 13:12:34 -04002352 mNestedLoopDepth++;
2353
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002354 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2355
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002356 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002357 {
2358 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2359 }
2360
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002361 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002362 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002363 if (handleExcessiveLoop(node))
2364 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002365 mInsideDiscontinuousLoop = wasDiscontinuous;
2366 mNestedLoopDepth--;
2367
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002368 return false;
2369 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002370 }
2371
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002372 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373
alokp@chromium.org52813552010-11-16 18:36:09 +00002374 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002376 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002377
Jamie Madill075edd82013-07-08 13:30:19 -04002378 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002379 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380 }
2381 else
2382 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002383 out << "{for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002384
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385 if (node->getInit())
2386 {
2387 node->getInit()->traverse(this);
2388 }
2389
2390 out << "; ";
2391
alokp@chromium.org52813552010-11-16 18:36:09 +00002392 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002394 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395 }
2396
2397 out << "; ";
2398
alokp@chromium.org52813552010-11-16 18:36:09 +00002399 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002401 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402 }
2403
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002404 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002405
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
2410 if (node->getBody())
2411 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002412 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 }
2414
Jamie Madill075edd82013-07-08 13:30:19 -04002415 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002416 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417
alokp@chromium.org52813552010-11-16 18:36:09 +00002418 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419 {
Jamie Madill075edd82013-07-08 13:30:19 -04002420 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 out << "while(\n";
2422
alokp@chromium.org52813552010-11-16 18:36:09 +00002423 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424
daniel@transgaming.com73536982012-03-21 20:45:49 +00002425 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 }
2427
daniel@transgaming.com73536982012-03-21 20:45:49 +00002428 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002430 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002431 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002432
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 return false;
2434}
2435
2436bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2437{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002438 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439
2440 switch (node->getFlowOp())
2441 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002442 case EOpKill:
2443 outputTriplet(visit, "discard;\n", "", "");
2444 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002445 case EOpBreak:
2446 if (visit == PreVisit)
2447 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002448 if (mNestedLoopDepth > 1)
2449 {
2450 mUsesNestedBreak = true;
2451 }
2452
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002453 if (mExcessiveLoopIndex)
2454 {
2455 out << "{Break";
2456 mExcessiveLoopIndex->traverse(this);
2457 out << " = true; break;}\n";
2458 }
2459 else
2460 {
2461 out << "break;\n";
2462 }
2463 }
2464 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002465 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466 case EOpReturn:
2467 if (visit == PreVisit)
2468 {
2469 if (node->getExpression())
2470 {
2471 out << "return ";
2472 }
2473 else
2474 {
2475 out << "return;\n";
2476 }
2477 }
2478 else if (visit == PostVisit)
2479 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002480 if (node->getExpression())
2481 {
2482 out << ";\n";
2483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002484 }
2485 break;
2486 default: UNREACHABLE();
2487 }
2488
2489 return true;
2490}
2491
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002492void OutputHLSL::traverseStatements(TIntermNode *node)
2493{
2494 if (isSingleStatement(node))
2495 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002496 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002497 }
2498
2499 node->traverse(this);
2500}
2501
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002502bool OutputHLSL::isSingleStatement(TIntermNode *node)
2503{
2504 TIntermAggregate *aggregate = node->getAsAggregate();
2505
2506 if (aggregate)
2507 {
2508 if (aggregate->getOp() == EOpSequence)
2509 {
2510 return false;
2511 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002512 else if (aggregate->getOp() == EOpDeclaration)
2513 {
2514 // Declaring multiple comma-separated variables must be considered multiple statements
2515 // because each individual declaration has side effects which are visible in the next.
2516 return false;
2517 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002518 else
2519 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002520 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002521 {
2522 if (!isSingleStatement(*sit))
2523 {
2524 return false;
2525 }
2526 }
2527
2528 return true;
2529 }
2530 }
2531
2532 return true;
2533}
2534
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002535// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2536// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002537bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2538{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002539 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002540 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002541
2542 // Parse loops of the form:
2543 // for(int index = initial; index [comparator] limit; index += increment)
2544 TIntermSymbol *index = NULL;
2545 TOperator comparator = EOpNull;
2546 int initial = 0;
2547 int limit = 0;
2548 int increment = 0;
2549
2550 // Parse index name and intial value
2551 if (node->getInit())
2552 {
2553 TIntermAggregate *init = node->getInit()->getAsAggregate();
2554
2555 if (init)
2556 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002557 TIntermSequence *sequence = init->getSequence();
2558 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002559
2560 if (variable && variable->getQualifier() == EvqTemporary)
2561 {
2562 TIntermBinary *assign = variable->getAsBinaryNode();
2563
2564 if (assign->getOp() == EOpInitialize)
2565 {
2566 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2567 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2568
2569 if (symbol && constant)
2570 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002571 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002572 {
2573 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002574 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002575 }
2576 }
2577 }
2578 }
2579 }
2580 }
2581
2582 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002583 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002584 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002585 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002586
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002587 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2588 {
2589 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2590
2591 if (constant)
2592 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002593 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002594 {
2595 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002596 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002597 }
2598 }
2599 }
2600 }
2601
2602 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002603 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002604 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002605 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2606 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002607
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002608 if (binaryTerminal)
2609 {
2610 TOperator op = binaryTerminal->getOp();
2611 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2612
2613 if (constant)
2614 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002615 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002616 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002617 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002618
2619 switch (op)
2620 {
2621 case EOpAddAssign: increment = value; break;
2622 case EOpSubAssign: increment = -value; break;
2623 default: UNIMPLEMENTED();
2624 }
2625 }
2626 }
2627 }
2628 else if (unaryTerminal)
2629 {
2630 TOperator op = unaryTerminal->getOp();
2631
2632 switch (op)
2633 {
2634 case EOpPostIncrement: increment = 1; break;
2635 case EOpPostDecrement: increment = -1; break;
2636 case EOpPreIncrement: increment = 1; break;
2637 case EOpPreDecrement: increment = -1; break;
2638 default: UNIMPLEMENTED();
2639 }
2640 }
2641 }
2642
2643 if (index != NULL && comparator != EOpNull && increment != 0)
2644 {
2645 if (comparator == EOpLessThanEqual)
2646 {
2647 comparator = EOpLessThan;
2648 limit += 1;
2649 }
2650
2651 if (comparator == EOpLessThan)
2652 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002653 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002654
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002655 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002656 {
2657 return false; // Not an excessive loop
2658 }
2659
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002660 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2661 mExcessiveLoopIndex = index;
2662
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002663 out << "{int ";
2664 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002665 out << ";\n"
2666 "bool Break";
2667 index->traverse(this);
2668 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002669
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002670 bool firstLoopFragment = true;
2671
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002672 while (iterations > 0)
2673 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002674 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002675
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002676 if (!firstLoopFragment)
2677 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002678 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002679 index->traverse(this);
2680 out << ") {\n";
2681 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002682
2683 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2684 {
2685 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2686 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002687
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002688 // for(int index = initial; index < clampedLimit; index += increment)
2689
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002690 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002691 index->traverse(this);
2692 out << " = ";
2693 out << initial;
2694
2695 out << "; ";
2696 index->traverse(this);
2697 out << " < ";
2698 out << clampedLimit;
2699
2700 out << "; ";
2701 index->traverse(this);
2702 out << " += ";
2703 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002704 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002705
Jamie Madill075edd82013-07-08 13:30:19 -04002706 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002707 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002708
2709 if (node->getBody())
2710 {
2711 node->getBody()->traverse(this);
2712 }
2713
Jamie Madill075edd82013-07-08 13:30:19 -04002714 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002715 out << ";}\n";
2716
2717 if (!firstLoopFragment)
2718 {
2719 out << "}\n";
2720 }
2721
2722 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002723
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002724 initial += MAX_LOOP_ITERATIONS * increment;
2725 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002726 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002727
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002728 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002729
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002730 mExcessiveLoopIndex = restoreIndex;
2731
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002732 return true;
2733 }
2734 else UNIMPLEMENTED();
2735 }
2736
2737 return false; // Not handled as an excessive loop
2738}
2739
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002740void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002741{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002742 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002743
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002744 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002745 {
2746 out << preString;
2747 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002748 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002749 {
2750 out << inString;
2751 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002752 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002753 {
2754 out << postString;
2755 }
2756}
2757
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002758void OutputHLSL::outputLineDirective(int line)
2759{
2760 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2761 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002762 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002763 mBody << "#line " << line;
2764
2765 if (mContext.sourcePath)
2766 {
2767 mBody << " \"" << mContext.sourcePath << "\"";
2768 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002769
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002770 mBody << "\n";
2771 }
2772}
2773
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002774TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2775{
2776 TQualifier qualifier = symbol->getQualifier();
2777 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002778 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002779
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002780 if (name.empty()) // HLSL demands named arguments, also for prototypes
2781 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002782 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002783 }
2784 else
2785 {
Jamie Madill033dae62014-06-18 12:56:28 -04002786 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002787 }
2788
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002789 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2790 {
Jamie Madill033dae62014-06-18 12:56:28 -04002791 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002792 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002793 }
2794
Jamie Madill033dae62014-06-18 12:56:28 -04002795 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796}
2797
2798TString OutputHLSL::initializer(const TType &type)
2799{
2800 TString string;
2801
Jamie Madill94bf7f22013-07-08 13:31:15 -04002802 size_t size = type.getObjectSize();
2803 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002805 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002806
Jamie Madill94bf7f22013-07-08 13:31:15 -04002807 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808 {
2809 string += ", ";
2810 }
2811 }
2812
daniel@transgaming.comead23042010-04-29 03:35:36 +00002813 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002814}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002815
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002816void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2817{
2818 TInfoSinkBase &out = mBody;
2819
2820 if (visit == PreVisit)
2821 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002822 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002823
2824 out << name + "(";
2825 }
2826 else if (visit == InVisit)
2827 {
2828 out << ", ";
2829 }
2830 else if (visit == PostVisit)
2831 {
2832 out << ")";
2833 }
2834}
2835
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002836const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2837{
2838 TInfoSinkBase &out = mBody;
2839
Jamie Madill98493dd2013-07-08 14:39:03 -04002840 const TStructure* structure = type.getStruct();
2841 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002842 {
Jamie Madill033dae62014-06-18 12:56:28 -04002843 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002844
Jamie Madill98493dd2013-07-08 14:39:03 -04002845 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002846
Jamie Madill98493dd2013-07-08 14:39:03 -04002847 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002848 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002849 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002850 constUnion = writeConstantUnion(*fieldType, constUnion);
2851
Jamie Madill98493dd2013-07-08 14:39:03 -04002852 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002853 {
2854 out << ", ";
2855 }
2856 }
2857
2858 out << ")";
2859 }
2860 else
2861 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002862 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002863 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002864
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002865 if (writeType)
2866 {
Jamie Madill033dae62014-06-18 12:56:28 -04002867 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002868 }
2869
Jamie Madill94bf7f22013-07-08 13:31:15 -04002870 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002871 {
2872 switch (constUnion->getType())
2873 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002874 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002875 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002876 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002877 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002878 default: UNREACHABLE();
2879 }
2880
2881 if (i != size - 1)
2882 {
2883 out << ", ";
2884 }
2885 }
2886
2887 if (writeType)
2888 {
2889 out << ")";
2890 }
2891 }
2892
2893 return constUnion;
2894}
2895
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002896}