blob: 488f2e330240badf6ec20bb0c40687595bf6fb44 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000010#include "common/utilities.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040011#include "common/blocklayout.h"
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/compilerdebug.h"
13#include "compiler/translator/InfoSink.h"
14#include "compiler/translator/DetectDiscontinuity.h"
15#include "compiler/translator/SearchSymbol.h"
16#include "compiler/translator/UnfoldShortCircuit.h"
Geoff Lang17732822013-08-29 13:46:49 -040017#include "compiler/translator/FlagStd140Structs.h"
Jamie Madill3c9eeb92013-11-04 11:09:26 -050018#include "compiler/translator/NodeSearch.h"
Jamie Madille53c98b2014-02-03 11:57:13 -050019#include "compiler/translator/RewriteElseBlocks.h"
Jamie Madill033dae62014-06-18 12:56:28 -040020#include "compiler/translator/UtilsHLSL.h"
21#include "compiler/translator/util.h"
Jamie Madillf91ce812014-06-13 10:04:34 -040022#include "compiler/translator/UniformHLSL.h"
Jamie Madill8daaba12014-06-13 10:04:33 -040023#include "compiler/translator/StructureHLSL.h"
Jamie Madill54ad4f82014-09-03 09:40:46 -040024#include "compiler/translator/TranslatorHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000026#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000027#include <cfloat>
28#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030namespace sh
31{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000032
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033TString OutputHLSL::TextureFunction::name() const
34{
35 TString name = "gl_texture";
36
Nicolas Capens6d232bb2013-07-08 15:56:38 -040037 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040038 {
39 name += "2D";
40 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040041 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040042 {
43 name += "3D";
44 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040045 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040046 {
47 name += "Cube";
48 }
49 else UNREACHABLE();
50
51 if (proj)
52 {
53 name += "Proj";
54 }
55
Nicolas Capensb1f45b72013-12-19 17:37:19 -050056 if (offset)
57 {
58 name += "Offset";
59 }
60
Nicolas Capens75fb4752013-07-10 15:14:47 -040061 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040062 {
Nicolas Capensfc014542014-02-18 14:47:13 -050063 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040064 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050065 case LOD: name += "Lod"; break;
66 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040067 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050068 case SIZE: name += "Size"; break;
69 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050070 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040071 default: UNREACHABLE();
72 }
73
74 return name + "(";
75}
76
77bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
78{
79 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040080 if (sampler > rhs.sampler) return false;
81
Nicolas Capense0ba27a2013-06-24 16:10:52 -040082 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040083 if (coords > rhs.coords) return false;
84
Nicolas Capense0ba27a2013-06-24 16:10:52 -040085 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040086 if (proj && !rhs.proj) return false;
87
88 if (!offset && rhs.offset) return true;
89 if (offset && !rhs.offset) return false;
90
Nicolas Capens75fb4752013-07-10 15:14:47 -040091 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040092 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040093
94 return false;
95}
96
Jamie Madill54ad4f82014-09-03 09:40:46 -040097OutputHLSL::OutputHLSL(TParseContext &context, TranslatorHLSL *parentTranslator)
98 : TIntermTraverser(true, true, true),
99 mContext(context),
100 mOutputType(parentTranslator->getOutputType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000102 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000103 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000104
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000105 mUsesFragColor = false;
106 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000107 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000108 mUsesFragCoord = false;
109 mUsesPointCoord = false;
110 mUsesFrontFacing = false;
111 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400112 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000113 mUsesXor = false;
114 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000115 mUsesMod2v = false;
116 mUsesMod2f = false;
117 mUsesMod3v = false;
118 mUsesMod3f = false;
119 mUsesMod4v = false;
120 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000121 mUsesFaceforward1 = false;
122 mUsesFaceforward2 = false;
123 mUsesFaceforward3 = false;
124 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000125 mUsesAtan2_1 = false;
126 mUsesAtan2_2 = false;
127 mUsesAtan2_3 = false;
128 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500129 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400130 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000131
Jamie Madill54ad4f82014-09-03 09:40:46 -0400132 const ShBuiltInResources &resources = parentTranslator->getResources();
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000133 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
134
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000135 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000136
137 mContainsLoopDiscontinuity = false;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800138 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000139 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000140 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400141 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000142
143 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000144
Jamie Madill8daaba12014-06-13 10:04:33 -0400145 mStructureHLSL = new StructureHLSL;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400146 mUniformHLSL = new UniformHLSL(mStructureHLSL, parentTranslator);
Jamie Madill8daaba12014-06-13 10:04:33 -0400147
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000148 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000149 {
Jamie Madill183bde52014-07-02 15:31:19 -0400150 if (mContext.shaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000151 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400152 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
153 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000154 }
155 else
156 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400157 // Reserve registers for dx_DepthRange and dx_ViewAdjust
158 mUniformHLSL->reserveUniformRegisters(2);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000159 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000160 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000161
Jamie Madillf91ce812014-06-13 10:04:34 -0400162 // Reserve registers for the default uniform block and driver constants
163 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164}
165
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000166OutputHLSL::~OutputHLSL()
167{
Jamie Madill8daaba12014-06-13 10:04:33 -0400168 SafeDelete(mUnfoldShortCircuit);
169 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400170 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000171}
172
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000173void OutputHLSL::output()
174{
Jamie Madill183bde52014-07-02 15:31:19 -0400175 mContainsLoopDiscontinuity = mContext.shaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Corentin Wallez80bacde2014-11-10 12:07:37 -0800176 mContainsAnyLoop = containsAnyLoop(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400177 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
178 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000179
Jamie Madille53c98b2014-02-03 11:57:13 -0500180 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
181 // use a vertex attribute as a condition, and some related computation in the else block.
Jamie Madill183bde52014-07-02 15:31:19 -0400182 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500183 {
184 RewriteElseBlocks(mContext.treeRoot);
185 }
186
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000187 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000188 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000189
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000190 mContext.infoSink().obj << mHeader.c_str();
191 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000192}
193
Jamie Madill570e04d2013-06-21 09:15:33 -0400194void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
195{
196 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
197 {
198 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
199
200 // This will mark the necessary block elements as referenced
201 flaggedNode->traverse(this);
202 TString structName(mBody.c_str());
203 mBody.erase();
204
205 mFlaggedStructOriginalNames[flaggedNode] = structName;
206
207 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
208 {
209 structName.erase(pos, 1);
210 }
211
212 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
213 }
214}
215
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000216TInfoSinkBase &OutputHLSL::getBodyStream()
217{
218 return mBody;
219}
220
Jamie Madill4e1fd412014-07-10 17:50:10 -0400221const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
222{
223 return mUniformHLSL->getInterfaceBlockRegisterMap();
224}
225
Jamie Madill9fe25e92014-07-18 10:33:08 -0400226const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
227{
228 return mUniformHLSL->getUniformRegisterMap();
229}
230
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000231int OutputHLSL::vectorSize(const TType &type) const
232{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000233 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000234 int arraySize = type.isArray() ? type.getArraySize() : 1;
235
236 return elementSize * arraySize;
237}
238
Jamie Madill98493dd2013-07-08 14:39:03 -0400239TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400240{
241 TString init;
242
243 TString preIndentString;
244 TString fullIndentString;
245
246 for (int spaces = 0; spaces < (indent * 4); spaces++)
247 {
248 preIndentString += ' ';
249 }
250
251 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
252 {
253 fullIndentString += ' ';
254 }
255
256 init += preIndentString + "{\n";
257
Jamie Madill98493dd2013-07-08 14:39:03 -0400258 const TFieldList &fields = structure.fields();
259 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400260 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400261 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400262 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400264
Jamie Madill98493dd2013-07-08 14:39:03 -0400265 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400266 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400268 }
269 else
270 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400271 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400272 }
273 }
274
275 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
276
277 return init;
278}
279
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280void OutputHLSL::header()
281{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000282 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000284 TString varyings;
285 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000287
Jamie Madill829f59e2013-11-13 19:40:54 -0500288 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400289 {
290 TIntermTyped *structNode = flaggedStructIt->first;
291 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400292 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400293 const TString &originalName = mFlaggedStructOriginalNames[structNode];
294
Jamie Madill033dae62014-06-18 12:56:28 -0400295 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400296 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400297 flaggedStructs += "\n";
298 }
299
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000300 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
301 {
302 const TType &type = varying->second->getType();
303 const TString &name = varying->second->getSymbol();
304
305 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400306 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
307 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000308 }
309
310 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
311 {
312 const TType &type = attribute->second->getType();
313 const TString &name = attribute->second->getSymbol();
314
Jamie Madill033dae62014-06-18 12:56:28 -0400315 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000316 }
317
Jamie Madill8daaba12014-06-13 10:04:33 -0400318 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400319
Jamie Madillf91ce812014-06-13 10:04:34 -0400320 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
321 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
322
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500323 if (mUsesDiscardRewriting)
324 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400325 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500326 }
327
Nicolas Capens655fe362014-04-11 13:12:34 -0400328 if (mUsesNestedBreak)
329 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400330 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400331 }
332
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400333 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
334 "#define LOOP [loop]\n"
335 "#define FLATTEN [flatten]\n"
336 "#else\n"
337 "#define LOOP\n"
338 "#define FLATTEN\n"
339 "#endif\n";
340
Jamie Madill183bde52014-07-02 15:31:19 -0400341 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000343 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000344 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000345
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000346 out << "// Varyings\n";
347 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400348 out << "\n";
349
350 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000351 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500352 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000353 {
Jamie Madill46131a32013-06-20 11:55:50 -0400354 const TString &variableName = outputVariableIt->first;
355 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400356
Jamie Madill033dae62014-06-18 12:56:28 -0400357 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400358 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000359 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000360 }
Jamie Madill46131a32013-06-20 11:55:50 -0400361 else
362 {
363 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
364
365 out << "static float4 gl_Color[" << numColorValues << "] =\n"
366 "{\n";
367 for (unsigned int i = 0; i < numColorValues; i++)
368 {
369 out << " float4(0, 0, 0, 0)";
370 if (i + 1 != numColorValues)
371 {
372 out << ",";
373 }
374 out << "\n";
375 }
376
377 out << "};\n";
378 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000379
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400380 if (mUsesFragDepth)
381 {
382 out << "static float gl_Depth = 0.0;\n";
383 }
384
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000385 if (mUsesFragCoord)
386 {
387 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
388 }
389
390 if (mUsesPointCoord)
391 {
392 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
393 }
394
395 if (mUsesFrontFacing)
396 {
397 out << "static bool gl_FrontFacing = false;\n";
398 }
399
400 out << "\n";
401
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000402 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000403 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000404 out << "struct gl_DepthRangeParameters\n"
405 "{\n"
406 " float near;\n"
407 " float far;\n"
408 " float diff;\n"
409 "};\n"
410 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000411 }
412
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000413 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000414 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000415 out << "cbuffer DriverConstants : register(b1)\n"
416 "{\n";
417
418 if (mUsesDepthRange)
419 {
420 out << " float3 dx_DepthRange : packoffset(c0);\n";
421 }
422
423 if (mUsesFragCoord)
424 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000425 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000426 }
427
428 if (mUsesFragCoord || mUsesFrontFacing)
429 {
430 out << " float3 dx_DepthFront : packoffset(c2);\n";
431 }
432
433 out << "};\n";
434 }
435 else
436 {
437 if (mUsesDepthRange)
438 {
439 out << "uniform float3 dx_DepthRange : register(c0);";
440 }
441
442 if (mUsesFragCoord)
443 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000444 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000445 }
446
447 if (mUsesFragCoord || mUsesFrontFacing)
448 {
449 out << "uniform float3 dx_DepthFront : register(c2);\n";
450 }
451 }
452
453 out << "\n";
454
455 if (mUsesDepthRange)
456 {
457 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
458 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000459 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000460
Jamie Madillf91ce812014-06-13 10:04:34 -0400461 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000462 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400463 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000464 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400465 out << flaggedStructs;
466 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000467 }
468
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000469 if (usingMRTExtension && mNumRenderTargets > 1)
470 {
471 out << "#define GL_USES_MRT\n";
472 }
473
474 if (mUsesFragColor)
475 {
476 out << "#define GL_USES_FRAG_COLOR\n";
477 }
478
479 if (mUsesFragData)
480 {
481 out << "#define GL_USES_FRAG_DATA\n";
482 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000484 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000486 out << "// Attributes\n";
487 out << attributes;
488 out << "\n"
489 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400490
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000491 if (mUsesPointSize)
492 {
493 out << "static float gl_PointSize = float(1);\n";
494 }
495
496 out << "\n"
497 "// Varyings\n";
498 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000499 out << "\n";
500
501 if (mUsesDepthRange)
502 {
503 out << "struct gl_DepthRangeParameters\n"
504 "{\n"
505 " float near;\n"
506 " float far;\n"
507 " float diff;\n"
508 "};\n"
509 "\n";
510 }
511
512 if (mOutputType == SH_HLSL11_OUTPUT)
513 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000514 if (mUsesDepthRange)
515 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000516 out << "cbuffer DriverConstants : register(b1)\n"
517 "{\n"
518 " float3 dx_DepthRange : packoffset(c0);\n"
519 "};\n"
520 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000521 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000522 }
523 else
524 {
525 if (mUsesDepthRange)
526 {
527 out << "uniform float3 dx_DepthRange : register(c0);\n";
528 }
529
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000530 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000531 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000532 }
533
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000534 if (mUsesDepthRange)
535 {
536 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
537 "\n";
538 }
539
Jamie Madillf91ce812014-06-13 10:04:34 -0400540 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000541 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400542 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000543 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400544 out << flaggedStructs;
545 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000546 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400547 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000548
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400549 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
550 {
551 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400552 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000553 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400554 switch(textureFunction->sampler)
555 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400556 case EbtSampler2D: out << "int2 "; break;
557 case EbtSampler3D: out << "int3 "; break;
558 case EbtSamplerCube: out << "int2 "; break;
559 case EbtSampler2DArray: out << "int3 "; break;
560 case EbtISampler2D: out << "int2 "; break;
561 case EbtISampler3D: out << "int3 "; break;
562 case EbtISamplerCube: out << "int2 "; break;
563 case EbtISampler2DArray: out << "int3 "; break;
564 case EbtUSampler2D: out << "int2 "; break;
565 case EbtUSampler3D: out << "int3 "; break;
566 case EbtUSamplerCube: out << "int2 "; break;
567 case EbtUSampler2DArray: out << "int3 "; break;
568 case EbtSampler2DShadow: out << "int2 "; break;
569 case EbtSamplerCubeShadow: out << "int2 "; break;
570 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400571 default: UNREACHABLE();
572 }
573 }
574 else // Sampling function
575 {
576 switch(textureFunction->sampler)
577 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400578 case EbtSampler2D: out << "float4 "; break;
579 case EbtSampler3D: out << "float4 "; break;
580 case EbtSamplerCube: out << "float4 "; break;
581 case EbtSampler2DArray: out << "float4 "; break;
582 case EbtISampler2D: out << "int4 "; break;
583 case EbtISampler3D: out << "int4 "; break;
584 case EbtISamplerCube: out << "int4 "; break;
585 case EbtISampler2DArray: out << "int4 "; break;
586 case EbtUSampler2D: out << "uint4 "; break;
587 case EbtUSampler3D: out << "uint4 "; break;
588 case EbtUSamplerCube: out << "uint4 "; break;
589 case EbtUSampler2DArray: out << "uint4 "; break;
590 case EbtSampler2DShadow: out << "float "; break;
591 case EbtSamplerCubeShadow: out << "float "; break;
592 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400593 default: UNREACHABLE();
594 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000595 }
596
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400597 // Function name
598 out << textureFunction->name();
599
600 // Argument list
601 int hlslCoords = 4;
602
603 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000604 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400605 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000606 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400607 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
608 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
609 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000610 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400611
Nicolas Capens75fb4752013-07-10 15:14:47 -0400612 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000613 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400614 case TextureFunction::IMPLICIT: break;
615 case TextureFunction::BIAS: hlslCoords = 4; break;
616 case TextureFunction::LOD: hlslCoords = 4; break;
617 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400618 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400619 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000620 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400621 }
622 else if (mOutputType == SH_HLSL11_OUTPUT)
623 {
624 switch(textureFunction->sampler)
625 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400626 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
627 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
628 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
629 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
630 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
631 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500632 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400633 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
634 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
635 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500636 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400637 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
638 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
639 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
640 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400641 default: UNREACHABLE();
642 }
643 }
644 else UNREACHABLE();
645
Nicolas Capensfc014542014-02-18 14:47:13 -0500646 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400647 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500648 switch(textureFunction->coords)
649 {
650 case 2: out << ", int2 t"; break;
651 case 3: out << ", int3 t"; break;
652 default: UNREACHABLE();
653 }
654 }
655 else // Floating-point coordinates (except textureSize)
656 {
657 switch(textureFunction->coords)
658 {
659 case 1: out << ", int lod"; break; // textureSize()
660 case 2: out << ", float2 t"; break;
661 case 3: out << ", float3 t"; break;
662 case 4: out << ", float4 t"; break;
663 default: UNREACHABLE();
664 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000665 }
666
Nicolas Capensd11d5492014-02-19 17:06:10 -0500667 if (textureFunction->method == TextureFunction::GRAD)
668 {
669 switch(textureFunction->sampler)
670 {
671 case EbtSampler2D:
672 case EbtISampler2D:
673 case EbtUSampler2D:
674 case EbtSampler2DArray:
675 case EbtISampler2DArray:
676 case EbtUSampler2DArray:
677 case EbtSampler2DShadow:
678 case EbtSampler2DArrayShadow:
679 out << ", float2 ddx, float2 ddy";
680 break;
681 case EbtSampler3D:
682 case EbtISampler3D:
683 case EbtUSampler3D:
684 case EbtSamplerCube:
685 case EbtISamplerCube:
686 case EbtUSamplerCube:
687 case EbtSamplerCubeShadow:
688 out << ", float3 ddx, float3 ddy";
689 break;
690 default: UNREACHABLE();
691 }
692 }
693
Nicolas Capens75fb4752013-07-10 15:14:47 -0400694 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000695 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400696 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400697 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400698 case TextureFunction::LOD: out << ", float lod"; break;
699 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400700 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400701 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500702 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500703 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400704 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000705 }
706
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500707 if (textureFunction->offset)
708 {
709 switch(textureFunction->sampler)
710 {
711 case EbtSampler2D: out << ", int2 offset"; break;
712 case EbtSampler3D: out << ", int3 offset"; break;
713 case EbtSampler2DArray: out << ", int2 offset"; break;
714 case EbtISampler2D: out << ", int2 offset"; break;
715 case EbtISampler3D: out << ", int3 offset"; break;
716 case EbtISampler2DArray: out << ", int2 offset"; break;
717 case EbtUSampler2D: out << ", int2 offset"; break;
718 case EbtUSampler3D: out << ", int3 offset"; break;
719 case EbtUSampler2DArray: out << ", int2 offset"; break;
720 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500721 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500722 default: UNREACHABLE();
723 }
724 }
725
Nicolas Capens84cfa122014-04-14 13:48:45 -0400726 if (textureFunction->method == TextureFunction::BIAS ||
727 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500728 {
729 out << ", float bias";
730 }
731
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400732 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400733 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400734
Nicolas Capens75fb4752013-07-10 15:14:47 -0400735 if (textureFunction->method == TextureFunction::SIZE)
736 {
737 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
738 {
739 if (IsSamplerArray(textureFunction->sampler))
740 {
741 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
742 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
743 }
744 else
745 {
746 out << " uint width; uint height; uint numberOfLevels;\n"
747 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
748 }
749 }
750 else if (IsSampler3D(textureFunction->sampler))
751 {
752 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
753 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
754 }
755 else UNREACHABLE();
756
757 switch(textureFunction->sampler)
758 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400759 case EbtSampler2D: out << " return int2(width, height);"; break;
760 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
761 case EbtSamplerCube: out << " return int2(width, height);"; break;
762 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
763 case EbtISampler2D: out << " return int2(width, height);"; break;
764 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
765 case EbtISamplerCube: out << " return int2(width, height);"; break;
766 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
767 case EbtUSampler2D: out << " return int2(width, height);"; break;
768 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
769 case EbtUSamplerCube: out << " return int2(width, height);"; break;
770 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
771 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
772 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
773 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400774 default: UNREACHABLE();
775 }
776 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400777 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400778 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500779 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
780 {
781 out << " float width; float height; float layers; float levels;\n";
782
783 out << " uint mip = 0;\n";
784
785 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
786
787 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
788 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
789 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
790 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
791
792 // FACE_POSITIVE_X = 000b
793 // FACE_NEGATIVE_X = 001b
794 // FACE_POSITIVE_Y = 010b
795 // FACE_NEGATIVE_Y = 011b
796 // FACE_POSITIVE_Z = 100b
797 // FACE_NEGATIVE_Z = 101b
798 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
799
800 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
801 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
802 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
803
804 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
805 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
806 }
807 else if (IsIntegerSampler(textureFunction->sampler) &&
808 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400809 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400810 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400811 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400812 if (IsSamplerArray(textureFunction->sampler))
813 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400814 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400815
Nicolas Capens9edebd62013-08-06 10:59:10 -0400816 if (textureFunction->method == TextureFunction::LOD0)
817 {
818 out << " uint mip = 0;\n";
819 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400820 else if (textureFunction->method == TextureFunction::LOD0BIAS)
821 {
822 out << " uint mip = bias;\n";
823 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400824 else
825 {
826 if (textureFunction->method == TextureFunction::IMPLICIT ||
827 textureFunction->method == TextureFunction::BIAS)
828 {
829 out << " x.GetDimensions(0, width, height, layers, levels);\n"
830 " float2 tSized = float2(t.x * width, t.y * height);\n"
831 " float dx = length(ddx(tSized));\n"
832 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500833 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400834
835 if (textureFunction->method == TextureFunction::BIAS)
836 {
837 out << " lod += bias;\n";
838 }
839 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500840 else if (textureFunction->method == TextureFunction::GRAD)
841 {
842 out << " x.GetDimensions(0, width, height, layers, levels);\n"
843 " float lod = log2(max(length(ddx), length(ddy)));\n";
844 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400845
846 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
847 }
848
849 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400850 }
851 else
852 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400853 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400854
Nicolas Capens9edebd62013-08-06 10:59:10 -0400855 if (textureFunction->method == TextureFunction::LOD0)
856 {
857 out << " uint mip = 0;\n";
858 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400859 else if (textureFunction->method == TextureFunction::LOD0BIAS)
860 {
861 out << " uint mip = bias;\n";
862 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400863 else
864 {
865 if (textureFunction->method == TextureFunction::IMPLICIT ||
866 textureFunction->method == TextureFunction::BIAS)
867 {
868 out << " x.GetDimensions(0, width, height, levels);\n"
869 " float2 tSized = float2(t.x * width, t.y * height);\n"
870 " float dx = length(ddx(tSized));\n"
871 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500872 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400873
874 if (textureFunction->method == TextureFunction::BIAS)
875 {
876 out << " lod += bias;\n";
877 }
878 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500879 else if (textureFunction->method == TextureFunction::LOD)
880 {
881 out << " x.GetDimensions(0, width, height, levels);\n";
882 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500883 else if (textureFunction->method == TextureFunction::GRAD)
884 {
885 out << " x.GetDimensions(0, width, height, levels);\n"
886 " float lod = log2(max(length(ddx), length(ddy)));\n";
887 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400888
889 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
890 }
891
892 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400893 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400894 }
895 else if (IsSampler3D(textureFunction->sampler))
896 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400897 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400898
Nicolas Capens9edebd62013-08-06 10:59:10 -0400899 if (textureFunction->method == TextureFunction::LOD0)
900 {
901 out << " uint mip = 0;\n";
902 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400903 else if (textureFunction->method == TextureFunction::LOD0BIAS)
904 {
905 out << " uint mip = bias;\n";
906 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400907 else
908 {
909 if (textureFunction->method == TextureFunction::IMPLICIT ||
910 textureFunction->method == TextureFunction::BIAS)
911 {
912 out << " x.GetDimensions(0, width, height, depth, levels);\n"
913 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
914 " float dx = length(ddx(tSized));\n"
915 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500916 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400917
918 if (textureFunction->method == TextureFunction::BIAS)
919 {
920 out << " lod += bias;\n";
921 }
922 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500923 else if (textureFunction->method == TextureFunction::GRAD)
924 {
925 out << " x.GetDimensions(0, width, height, depth, levels);\n"
926 " float lod = log2(max(length(ddx), length(ddy)));\n";
927 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400928
929 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
930 }
931
932 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400933 }
934 else UNREACHABLE();
935 }
936
937 out << " return ";
938
939 // HLSL intrinsic
940 if (mOutputType == SH_HLSL9_OUTPUT)
941 {
942 switch(textureFunction->sampler)
943 {
944 case EbtSampler2D: out << "tex2D"; break;
945 case EbtSamplerCube: out << "texCUBE"; break;
946 default: UNREACHABLE();
947 }
948
Nicolas Capens75fb4752013-07-10 15:14:47 -0400949 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400950 {
951 case TextureFunction::IMPLICIT: out << "(s, "; break;
952 case TextureFunction::BIAS: out << "bias(s, "; break;
953 case TextureFunction::LOD: out << "lod(s, "; break;
954 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400955 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400956 default: UNREACHABLE();
957 }
958 }
959 else if (mOutputType == SH_HLSL11_OUTPUT)
960 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500961 if (textureFunction->method == TextureFunction::GRAD)
962 {
963 if (IsIntegerSampler(textureFunction->sampler))
964 {
965 out << "x.Load(";
966 }
967 else if (IsShadowSampler(textureFunction->sampler))
968 {
969 out << "x.SampleCmpLevelZero(s, ";
970 }
971 else
972 {
973 out << "x.SampleGrad(s, ";
974 }
975 }
976 else if (IsIntegerSampler(textureFunction->sampler) ||
977 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400978 {
979 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400980 }
Nicolas Capenscb127d32013-07-15 17:26:18 -0400981 else if (IsShadowSampler(textureFunction->sampler))
982 {
983 out << "x.SampleCmp(s, ";
984 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400985 else
986 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400987 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400988 {
989 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
990 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
991 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
992 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400993 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400994 default: UNREACHABLE();
995 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400996 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400997 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400998 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400999
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001000 // Integer sampling requires integer addresses
1001 TString addressx = "";
1002 TString addressy = "";
1003 TString addressz = "";
1004 TString close = "";
1005
Nicolas Capensfc014542014-02-18 14:47:13 -05001006 if (IsIntegerSampler(textureFunction->sampler) ||
1007 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001008 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001009 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001010 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001011 case 2: out << "int3("; break;
1012 case 3: out << "int4("; break;
1013 default: UNREACHABLE();
1014 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001015
Nicolas Capensfc014542014-02-18 14:47:13 -05001016 // Convert from normalized floating-point to integer
1017 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001018 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001019 addressx = "int(floor(width * frac((";
1020 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001021
Nicolas Capensfc014542014-02-18 14:47:13 -05001022 if (IsSamplerArray(textureFunction->sampler))
1023 {
1024 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1025 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001026 else if (IsSamplerCube(textureFunction->sampler))
1027 {
1028 addressz = "((((";
1029 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001030 else
1031 {
1032 addressz = "int(floor(depth * frac((";
1033 }
1034
1035 close = "))))";
1036 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001037 }
1038 else
1039 {
1040 switch(hlslCoords)
1041 {
1042 case 2: out << "float2("; break;
1043 case 3: out << "float3("; break;
1044 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001045 default: UNREACHABLE();
1046 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001047 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001048
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001049 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001050
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001051 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001052 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001053 switch(textureFunction->coords)
1054 {
1055 case 3: proj = " / t.z"; break;
1056 case 4: proj = " / t.w"; break;
1057 default: UNREACHABLE();
1058 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001059 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001060
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001061 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001062
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001063 if (mOutputType == SH_HLSL9_OUTPUT)
1064 {
1065 if (hlslCoords >= 3)
1066 {
1067 if (textureFunction->coords < 3)
1068 {
1069 out << ", 0";
1070 }
1071 else
1072 {
1073 out << ", t.z" + proj;
1074 }
1075 }
1076
1077 if (hlslCoords == 4)
1078 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001079 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001080 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001081 case TextureFunction::BIAS: out << ", bias"; break;
1082 case TextureFunction::LOD: out << ", lod"; break;
1083 case TextureFunction::LOD0: out << ", 0"; break;
1084 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001085 default: UNREACHABLE();
1086 }
1087 }
1088
1089 out << "));\n";
1090 }
1091 else if (mOutputType == SH_HLSL11_OUTPUT)
1092 {
1093 if (hlslCoords >= 3)
1094 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001095 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1096 {
1097 out << ", face";
1098 }
1099 else
1100 {
1101 out << ", " + addressz + ("t.z" + proj) + close;
1102 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001103 }
1104
Nicolas Capensd11d5492014-02-19 17:06:10 -05001105 if (textureFunction->method == TextureFunction::GRAD)
1106 {
1107 if (IsIntegerSampler(textureFunction->sampler))
1108 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001109 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001110 }
1111 else if (IsShadowSampler(textureFunction->sampler))
1112 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001113 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001114 switch(textureFunction->coords)
1115 {
1116 case 3: out << "), t.z"; break;
1117 case 4: out << "), t.w"; break;
1118 default: UNREACHABLE();
1119 }
1120 }
1121 else
1122 {
1123 out << "), ddx, ddy";
1124 }
1125 }
1126 else if (IsIntegerSampler(textureFunction->sampler) ||
1127 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001128 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001129 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001130 }
1131 else if (IsShadowSampler(textureFunction->sampler))
1132 {
1133 // Compare value
1134 switch(textureFunction->coords)
1135 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001136 case 3: out << "), t.z"; break;
1137 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001138 default: UNREACHABLE();
1139 }
1140 }
1141 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001142 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001143 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001144 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001145 case TextureFunction::IMPLICIT: out << ")"; break;
1146 case TextureFunction::BIAS: out << "), bias"; break;
1147 case TextureFunction::LOD: out << "), lod"; break;
1148 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001149 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001150 default: UNREACHABLE();
1151 }
1152 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001153
1154 if (textureFunction->offset)
1155 {
1156 out << ", offset";
1157 }
1158
1159 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001160 }
1161 else UNREACHABLE();
1162 }
1163
1164 out << "\n"
1165 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001166 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001167 }
1168
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001169 if (mUsesFragCoord)
1170 {
1171 out << "#define GL_USES_FRAG_COORD\n";
1172 }
1173
1174 if (mUsesPointCoord)
1175 {
1176 out << "#define GL_USES_POINT_COORD\n";
1177 }
1178
1179 if (mUsesFrontFacing)
1180 {
1181 out << "#define GL_USES_FRONT_FACING\n";
1182 }
1183
1184 if (mUsesPointSize)
1185 {
1186 out << "#define GL_USES_POINT_SIZE\n";
1187 }
1188
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001189 if (mUsesFragDepth)
1190 {
1191 out << "#define GL_USES_FRAG_DEPTH\n";
1192 }
1193
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001194 if (mUsesDepthRange)
1195 {
1196 out << "#define GL_USES_DEPTH_RANGE\n";
1197 }
1198
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001199 if (mUsesXor)
1200 {
1201 out << "bool xor(bool p, bool q)\n"
1202 "{\n"
1203 " return (p || q) && !(p && q);\n"
1204 "}\n"
1205 "\n";
1206 }
1207
1208 if (mUsesMod1)
1209 {
1210 out << "float mod(float x, float y)\n"
1211 "{\n"
1212 " return x - y * floor(x / y);\n"
1213 "}\n"
1214 "\n";
1215 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001216
1217 if (mUsesMod2v)
1218 {
1219 out << "float2 mod(float2 x, float2 y)\n"
1220 "{\n"
1221 " return x - y * floor(x / y);\n"
1222 "}\n"
1223 "\n";
1224 }
1225
1226 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001227 {
1228 out << "float2 mod(float2 x, float y)\n"
1229 "{\n"
1230 " return x - y * floor(x / y);\n"
1231 "}\n"
1232 "\n";
1233 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001234
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001235 if (mUsesMod3v)
1236 {
1237 out << "float3 mod(float3 x, float3 y)\n"
1238 "{\n"
1239 " return x - y * floor(x / y);\n"
1240 "}\n"
1241 "\n";
1242 }
1243
1244 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001245 {
1246 out << "float3 mod(float3 x, float y)\n"
1247 "{\n"
1248 " return x - y * floor(x / y);\n"
1249 "}\n"
1250 "\n";
1251 }
1252
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001253 if (mUsesMod4v)
1254 {
1255 out << "float4 mod(float4 x, float4 y)\n"
1256 "{\n"
1257 " return x - y * floor(x / y);\n"
1258 "}\n"
1259 "\n";
1260 }
1261
1262 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001263 {
1264 out << "float4 mod(float4 x, float y)\n"
1265 "{\n"
1266 " return x - y * floor(x / y);\n"
1267 "}\n"
1268 "\n";
1269 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001270
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001271 if (mUsesFaceforward1)
1272 {
1273 out << "float faceforward(float N, float I, float Nref)\n"
1274 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001275 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001276 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001277 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001278 " }\n"
1279 " else\n"
1280 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001281 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001282 " }\n"
1283 "}\n"
1284 "\n";
1285 }
1286
1287 if (mUsesFaceforward2)
1288 {
1289 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1290 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001291 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001292 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001293 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001294 " }\n"
1295 " else\n"
1296 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001297 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001298 " }\n"
1299 "}\n"
1300 "\n";
1301 }
1302
1303 if (mUsesFaceforward3)
1304 {
1305 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1306 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001307 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001308 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001309 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001310 " }\n"
1311 " else\n"
1312 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001313 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001314 " }\n"
1315 "}\n"
1316 "\n";
1317 }
1318
1319 if (mUsesFaceforward4)
1320 {
1321 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1322 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001323 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001324 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001325 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001326 " }\n"
1327 " else\n"
1328 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001329 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001330 " }\n"
1331 "}\n"
1332 "\n";
1333 }
1334
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001335 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001336 {
1337 out << "float atanyx(float y, float x)\n"
1338 "{\n"
1339 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1340 " return atan2(y, x);\n"
1341 "}\n";
1342 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001343
1344 if (mUsesAtan2_2)
1345 {
1346 out << "float2 atanyx(float2 y, float2 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 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1351 "}\n";
1352 }
1353
1354 if (mUsesAtan2_3)
1355 {
1356 out << "float3 atanyx(float3 y, float3 x)\n"
1357 "{\n"
1358 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1359 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1360 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1361 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1362 "}\n";
1363 }
1364
1365 if (mUsesAtan2_4)
1366 {
1367 out << "float4 atanyx(float4 y, float4 x)\n"
1368 "{\n"
1369 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1370 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1371 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1372 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1373 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1374 "}\n";
1375 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376}
1377
1378void OutputHLSL::visitSymbol(TIntermSymbol *node)
1379{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001380 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001381
Jamie Madill570e04d2013-06-21 09:15:33 -04001382 // Handle accessing std140 structs by value
1383 if (mFlaggedStructMappedNames.count(node) > 0)
1384 {
1385 out << mFlaggedStructMappedNames[node];
1386 return;
1387 }
1388
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001389 TString name = node->getSymbol();
1390
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001391 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001392 {
1393 mUsesDepthRange = true;
1394 out << name;
1395 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001396 else
1397 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001398 TQualifier qualifier = node->getQualifier();
1399
1400 if (qualifier == EvqUniform)
1401 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001402 const TType& nodeType = node->getType();
1403 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1404
1405 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001406 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001407 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001408 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001409 else
1410 {
1411 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001412 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001413
Jamie Madill033dae62014-06-18 12:56:28 -04001414 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001415 }
Jamie Madill19571812013-08-12 15:26:34 -07001416 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001417 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001418 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001419 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001420 }
Jamie Madill033dae62014-06-18 12:56:28 -04001421 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001422 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001423 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001424 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001425 }
Jamie Madill19571812013-08-12 15:26:34 -07001426 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001427 {
1428 mReferencedOutputVariables[name] = node;
1429 out << "out_" << name;
1430 }
1431 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001432 {
1433 out << "gl_Color[0]";
1434 mUsesFragColor = true;
1435 }
1436 else if (qualifier == EvqFragData)
1437 {
1438 out << "gl_Color";
1439 mUsesFragData = true;
1440 }
1441 else if (qualifier == EvqFragCoord)
1442 {
1443 mUsesFragCoord = true;
1444 out << name;
1445 }
1446 else if (qualifier == EvqPointCoord)
1447 {
1448 mUsesPointCoord = true;
1449 out << name;
1450 }
1451 else if (qualifier == EvqFrontFacing)
1452 {
1453 mUsesFrontFacing = true;
1454 out << name;
1455 }
1456 else if (qualifier == EvqPointSize)
1457 {
1458 mUsesPointSize = true;
1459 out << name;
1460 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001461 else if (name == "gl_FragDepthEXT")
1462 {
1463 mUsesFragDepth = true;
1464 out << "gl_Depth";
1465 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001466 else if (qualifier == EvqInternal)
1467 {
1468 out << name;
1469 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001470 else
1471 {
Jamie Madill033dae62014-06-18 12:56:28 -04001472 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001473 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001474 }
1475}
1476
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001477void OutputHLSL::visitRaw(TIntermRaw *node)
1478{
1479 mBody << node->getRawText();
1480}
1481
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001482bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1483{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001484 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001485
Jamie Madill570e04d2013-06-21 09:15:33 -04001486 // Handle accessing std140 structs by value
1487 if (mFlaggedStructMappedNames.count(node) > 0)
1488 {
1489 out << mFlaggedStructMappedNames[node];
1490 return false;
1491 }
1492
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001493 switch (node->getOp())
1494 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001495 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001496 case EOpInitialize:
1497 if (visit == PreVisit)
1498 {
1499 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1500 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1501 // new variable is created before the assignment is evaluated), so we need to convert
1502 // this to "float t = x, x = t;".
1503
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001504 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1505 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001506
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001507 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1508 expression->traverse(&searchSymbol);
1509 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001510
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001511 if (sameSymbol)
1512 {
1513 // Type already printed
1514 out << "t" + str(mUniqueIndex) + " = ";
1515 expression->traverse(this);
1516 out << ", ";
1517 symbolNode->traverse(this);
1518 out << " = t" + str(mUniqueIndex);
1519
1520 mUniqueIndex++;
1521 return false;
1522 }
1523 }
1524 else if (visit == InVisit)
1525 {
1526 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001527 }
1528 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001529 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1530 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1531 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1532 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1533 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1534 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001535 if (visit == PreVisit)
1536 {
1537 out << "(";
1538 }
1539 else if (visit == InVisit)
1540 {
1541 out << " = mul(";
1542 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001543 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001544 }
1545 else
1546 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001547 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001548 }
1549 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001550 case EOpMatrixTimesMatrixAssign:
1551 if (visit == PreVisit)
1552 {
1553 out << "(";
1554 }
1555 else if (visit == InVisit)
1556 {
1557 out << " = mul(";
1558 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001559 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001560 }
1561 else
1562 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001563 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001564 }
1565 break;
1566 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001567 case EOpModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001568 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001569 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001570 const TType& leftType = node->getLeft()->getType();
1571 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001572 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001573 if (visit == PreVisit)
1574 {
1575 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1576 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001577 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001578 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001579 return false;
1580 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001581 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001582 else
1583 {
1584 outputTriplet(visit, "", "[", "]");
1585 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001586 }
1587 break;
1588 case EOpIndexIndirect:
1589 // We do not currently support indirect references to interface blocks
1590 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1591 outputTriplet(visit, "", "[", "]");
1592 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001593 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001594 if (visit == InVisit)
1595 {
1596 const TStructure* structure = node->getLeft()->getType().getStruct();
1597 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1598 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001599 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001600
1601 return false;
1602 }
1603 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001604 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001605 if (visit == InVisit)
1606 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001607 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1608 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1609 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001610 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001611
1612 return false;
1613 }
1614 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001615 case EOpVectorSwizzle:
1616 if (visit == InVisit)
1617 {
1618 out << ".";
1619
1620 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1621
1622 if (swizzle)
1623 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001624 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001625
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001626 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001627 {
1628 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1629
1630 if (element)
1631 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001632 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001633
1634 switch (i)
1635 {
1636 case 0: out << "x"; break;
1637 case 1: out << "y"; break;
1638 case 2: out << "z"; break;
1639 case 3: out << "w"; break;
1640 default: UNREACHABLE();
1641 }
1642 }
1643 else UNREACHABLE();
1644 }
1645 }
1646 else UNREACHABLE();
1647
1648 return false; // Fully processed
1649 }
1650 break;
1651 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1652 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1653 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1654 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001655 case EOpMod: outputTriplet(visit, "(", " % ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001656 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001657 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001658 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001659 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001660 if (node->getOp() == EOpEqual)
1661 {
1662 outputTriplet(visit, "(", " == ", ")");
1663 }
1664 else
1665 {
1666 outputTriplet(visit, "(", " != ", ")");
1667 }
1668 }
1669 else if (node->getLeft()->getBasicType() == EbtStruct)
1670 {
1671 if (node->getOp() == EOpEqual)
1672 {
1673 out << "(";
1674 }
1675 else
1676 {
1677 out << "!(";
1678 }
1679
Jamie Madill98493dd2013-07-08 14:39:03 -04001680 const TStructure &structure = *node->getLeft()->getType().getStruct();
1681 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001682
Jamie Madill98493dd2013-07-08 14:39:03 -04001683 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001684 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001685 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001686
1687 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001688 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001689 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001690 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001691
Jamie Madill98493dd2013-07-08 14:39:03 -04001692 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001693 {
1694 out << " && ";
1695 }
1696 }
1697
1698 out << ")";
1699
1700 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001701 }
1702 else
1703 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001704 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001705
1706 if (node->getOp() == EOpEqual)
1707 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001708 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001709 }
1710 else
1711 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001712 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001713 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001714 }
1715 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1717 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1718 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1719 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1720 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001721 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001722 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1723 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001724 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001725 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001726 if (node->getRight()->hasSideEffects())
1727 {
1728 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1729 return false;
1730 }
1731 else
1732 {
1733 outputTriplet(visit, "(", " || ", ")");
1734 return true;
1735 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001736 case EOpLogicalXor:
1737 mUsesXor = true;
1738 outputTriplet(visit, "xor(", ", ", ")");
1739 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001740 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001741 if (node->getRight()->hasSideEffects())
1742 {
1743 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1744 return false;
1745 }
1746 else
1747 {
1748 outputTriplet(visit, "(", " && ", ")");
1749 return true;
1750 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 default: UNREACHABLE();
1752 }
1753
1754 return true;
1755}
1756
1757bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1758{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 switch (node->getOp())
1760 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001761 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001762 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001763 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1764 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1765 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1766 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1767 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1768 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001769 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1770 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1771 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1772 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1773 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1774 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1775 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1776 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1777 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1778 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1779 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1780 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1781 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1782 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1783 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1784 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1785 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1786 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1787 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1788 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1789 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001790 case EOpDFdx:
1791 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1792 {
1793 outputTriplet(visit, "(", "", ", 0.0)");
1794 }
1795 else
1796 {
1797 outputTriplet(visit, "ddx(", "", ")");
1798 }
1799 break;
1800 case EOpDFdy:
1801 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1802 {
1803 outputTriplet(visit, "(", "", ", 0.0)");
1804 }
1805 else
1806 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001807 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001808 }
1809 break;
1810 case EOpFwidth:
1811 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1812 {
1813 outputTriplet(visit, "(", "", ", 0.0)");
1814 }
1815 else
1816 {
1817 outputTriplet(visit, "fwidth(", "", ")");
1818 }
1819 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001820 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1821 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822 default: UNREACHABLE();
1823 }
1824
1825 return true;
1826}
1827
1828bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1829{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001830 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832 switch (node->getOp())
1833 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001834 case EOpSequence:
1835 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001836 if (mInsideFunction)
1837 {
Jamie Madill075edd82013-07-08 13:30:19 -04001838 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001839 out << "{\n";
1840 }
1841
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001842 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001843 {
Jamie Madill075edd82013-07-08 13:30:19 -04001844 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001845
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001846 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001847
1848 out << ";\n";
1849 }
1850
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001851 if (mInsideFunction)
1852 {
Jamie Madill075edd82013-07-08 13:30:19 -04001853 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001854 out << "}\n";
1855 }
1856
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001857 return false;
1858 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001859 case EOpDeclaration:
1860 if (visit == PreVisit)
1861 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001862 TIntermSequence *sequence = node->getSequence();
1863 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001865 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001867 TStructure *structure = variable->getType().getStruct();
1868
1869 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001870 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001871 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001872 }
1873
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001874 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001876 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877 {
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001878 if (isSingleStatement(*sit))
1879 {
1880 mUnfoldShortCircuit->traverse(*sit);
1881 }
1882
Nicolas Capensd974db42014-10-07 10:50:19 -04001883 if (!mInsideFunction)
1884 {
1885 out << "static ";
1886 }
1887
1888 out << TypeString(variable->getType()) + " ";
1889
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001890 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001892 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001894 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001895 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001896 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001897 }
1898 else
1899 {
1900 (*sit)->traverse(this);
1901 }
1902
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001903 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001904 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001905 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001906 }
1907 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001909 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1910 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001911 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001912 }
1913 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914 }
Jamie Madill033dae62014-06-18 12:56:28 -04001915 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001916 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001917 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001918 {
1919 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1920
1921 if (symbol)
1922 {
1923 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1924 mReferencedVaryings[symbol->getSymbol()] = symbol;
1925 }
1926 else
1927 {
1928 (*sit)->traverse(this);
1929 }
1930 }
1931 }
1932
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933 return false;
1934 }
1935 else if (visit == InVisit)
1936 {
1937 out << ", ";
1938 }
1939 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001940 case EOpInvariantDeclaration:
1941 // Do not do any translation
1942 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001943 case EOpPrototype:
1944 if (visit == PreVisit)
1945 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001946 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001947
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001948 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001949
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001950 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001951 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001952 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001953
1954 if (symbol)
1955 {
1956 out << argumentString(symbol);
1957
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001958 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001959 {
1960 out << ", ";
1961 }
1962 }
1963 else UNREACHABLE();
1964 }
1965
1966 out << ");\n";
1967
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001968 // Also prototype the Lod0 variant if needed
1969 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1970 {
1971 mOutputLod0Function = true;
1972 node->traverse(this);
1973 mOutputLod0Function = false;
1974 }
1975
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001976 return false;
1977 }
1978 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001979 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980 case EOpFunction:
1981 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001982 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001983
Jamie Madill033dae62014-06-18 12:56:28 -04001984 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001985
1986 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001988 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001990 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991 {
Jamie Madill033dae62014-06-18 12:56:28 -04001992 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001993 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001994
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001995 TIntermSequence *sequence = node->getSequence();
1996 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001997
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001998 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001999 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002000 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002001
2002 if (symbol)
2003 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002004 TStructure *structure = symbol->getType().getStruct();
2005
2006 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002007 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002008 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002009 }
2010
2011 out << argumentString(symbol);
2012
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002013 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002014 {
2015 out << ", ";
2016 }
2017 }
2018 else UNREACHABLE();
2019 }
2020
2021 out << ")\n"
2022 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002023
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002024 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002025 {
2026 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002027 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002028 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002030
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002031 out << "}\n";
2032
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002033 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2034 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002035 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002036 {
2037 mOutputLod0Function = true;
2038 node->traverse(this);
2039 mOutputLod0Function = false;
2040 }
2041 }
2042
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002043 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002044 }
2045 break;
2046 case EOpFunctionCall:
2047 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002048 TString name = TFunction::unmangleName(node->getName());
2049 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002050 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002051
2052 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053 {
Jamie Madill033dae62014-06-18 12:56:28 -04002054 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055 }
2056 else
2057 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002058 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002059
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002060 TextureFunction textureFunction;
2061 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002062 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002063 textureFunction.method = TextureFunction::IMPLICIT;
2064 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002065 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002066
2067 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002068 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002069 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002070 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002071 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002072 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002073 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002074 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002075 }
Nicolas Capens46485082014-04-15 13:12:50 -04002076 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2077 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002078 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002079 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002080 }
Nicolas Capens46485082014-04-15 13:12:50 -04002081 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002082 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002083 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002084 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002085 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002086 else if (name == "textureSize")
2087 {
2088 textureFunction.method = TextureFunction::SIZE;
2089 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002090 else if (name == "textureOffset")
2091 {
2092 textureFunction.method = TextureFunction::IMPLICIT;
2093 textureFunction.offset = true;
2094 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002095 else if (name == "textureProjOffset")
2096 {
2097 textureFunction.method = TextureFunction::IMPLICIT;
2098 textureFunction.offset = true;
2099 textureFunction.proj = true;
2100 }
2101 else if (name == "textureLodOffset")
2102 {
2103 textureFunction.method = TextureFunction::LOD;
2104 textureFunction.offset = true;
2105 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002106 else if (name == "textureProjLodOffset")
2107 {
2108 textureFunction.method = TextureFunction::LOD;
2109 textureFunction.proj = true;
2110 textureFunction.offset = true;
2111 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002112 else if (name == "texelFetch")
2113 {
2114 textureFunction.method = TextureFunction::FETCH;
2115 }
2116 else if (name == "texelFetchOffset")
2117 {
2118 textureFunction.method = TextureFunction::FETCH;
2119 textureFunction.offset = true;
2120 }
Nicolas Capens46485082014-04-15 13:12:50 -04002121 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002122 {
2123 textureFunction.method = TextureFunction::GRAD;
2124 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002125 else if (name == "textureGradOffset")
2126 {
2127 textureFunction.method = TextureFunction::GRAD;
2128 textureFunction.offset = true;
2129 }
Nicolas Capens46485082014-04-15 13:12:50 -04002130 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002131 {
2132 textureFunction.method = TextureFunction::GRAD;
2133 textureFunction.proj = true;
2134 }
2135 else if (name == "textureProjGradOffset")
2136 {
2137 textureFunction.method = TextureFunction::GRAD;
2138 textureFunction.proj = true;
2139 textureFunction.offset = true;
2140 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002141 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002142
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002143 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002144 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002145 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2146
2147 if (textureFunction.offset)
2148 {
2149 mandatoryArgumentCount++;
2150 }
2151
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002152 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002153
Jamie Madill183bde52014-07-02 15:31:19 -04002154 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002155 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002156 if (bias)
2157 {
2158 textureFunction.method = TextureFunction::LOD0BIAS;
2159 }
2160 else
2161 {
2162 textureFunction.method = TextureFunction::LOD0;
2163 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002164 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002165 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002166 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002167 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002168 }
2169 }
2170
2171 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002172
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002173 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002175
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002176 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002177 {
2178 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2179 {
2180 out << "texture_";
2181 (*arg)->traverse(this);
2182 out << ", sampler_";
2183 }
2184
2185 (*arg)->traverse(this);
2186
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002187 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002188 {
2189 out << ", ";
2190 }
2191 }
2192
2193 out << ")";
2194
2195 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002196 }
2197 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002198 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002199 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2200 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2201 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2202 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2203 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2204 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2205 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2206 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2207 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2208 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2209 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2210 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2211 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2212 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2213 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2214 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2215 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2216 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2217 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002218 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002219 {
Jamie Madill033dae62014-06-18 12:56:28 -04002220 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002221 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002222 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2223 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002224 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002225 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2226 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2227 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2228 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2229 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2230 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002231 case EOpMod:
2232 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002233 // We need to look at the number of components in both arguments
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002234 const int modValue = (*node->getSequence())[0]->getAsTyped()->getNominalSize() * 10 +
2235 (*node->getSequence())[1]->getAsTyped()->getNominalSize();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002236 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002237 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002238 case 11: mUsesMod1 = true; break;
2239 case 22: mUsesMod2v = true; break;
2240 case 21: mUsesMod2f = true; break;
2241 case 33: mUsesMod3v = true; break;
2242 case 31: mUsesMod3f = true; break;
2243 case 44: mUsesMod4v = true; break;
2244 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002245 default: UNREACHABLE();
2246 }
2247
2248 outputTriplet(visit, "mod(", ", ", ")");
2249 }
2250 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002251 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002253 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2254 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize())
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002255 {
2256 case 1: mUsesAtan2_1 = true; break;
2257 case 2: mUsesAtan2_2 = true; break;
2258 case 3: mUsesAtan2_3 = true; break;
2259 case 4: mUsesAtan2_4 = true; break;
2260 default: UNREACHABLE();
2261 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002262 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263 break;
2264 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2265 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2266 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2267 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2268 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2269 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2270 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2271 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2272 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002273 case EOpFaceForward:
2274 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002275 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002276 {
2277 case 1: mUsesFaceforward1 = true; break;
2278 case 2: mUsesFaceforward2 = true; break;
2279 case 3: mUsesFaceforward3 = true; break;
2280 case 4: mUsesFaceforward4 = true; break;
2281 default: UNREACHABLE();
2282 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002283
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002284 outputTriplet(visit, "faceforward(", ", ", ")");
2285 }
2286 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2288 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2289 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290 default: UNREACHABLE();
2291 }
2292
2293 return true;
2294}
2295
2296bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2297{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002298 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002300 if (node->usesTernaryOperator())
2301 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002302 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002303 }
2304 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002306 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002307
Corentin Wallez80bacde2014-11-10 12:07:37 -08002308 // D3D errors when there is a gradient operation in a loop in an unflattened if
2309 // however flattening all the ifs in branch heavy shaders made D3D error too.
2310 // As a temporary workaround we flatten the ifs only if there is at least a loop
2311 // present somewhere in the shader.
2312 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2313 {
2314 out << "FLATTEN ";
2315 }
2316
2317 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002318
2319 node->getCondition()->traverse(this);
2320
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002321 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002322
Jamie Madill075edd82013-07-08 13:30:19 -04002323 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002324 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002326 bool discard = false;
2327
daniel@transgaming.combb885322010-04-15 20:45:24 +00002328 if (node->getTrueBlock())
2329 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002330 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002331
2332 // Detect true discard
2333 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002334 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335
Jamie Madill075edd82013-07-08 13:30:19 -04002336 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002337 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002338
2339 if (node->getFalseBlock())
2340 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002341 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002342
Jamie Madill075edd82013-07-08 13:30:19 -04002343 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002344 out << "{\n";
2345
Jamie Madill075edd82013-07-08 13:30:19 -04002346 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002347 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002348
Jamie Madill075edd82013-07-08 13:30:19 -04002349 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002350 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002351
2352 // Detect false discard
2353 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2354 }
2355
2356 // ANGLE issue 486: Detect problematic conditional discard
2357 if (discard && FindSideEffectRewriting::search(node))
2358 {
2359 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002360 }
2361 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362
2363 return false;
2364}
2365
2366void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2367{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002368 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002369}
2370
2371bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2372{
Nicolas Capens655fe362014-04-11 13:12:34 -04002373 mNestedLoopDepth++;
2374
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002375 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2376
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002377 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002378 {
2379 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2380 }
2381
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002382 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002383 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002384 if (handleExcessiveLoop(node))
2385 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002386 mInsideDiscontinuousLoop = wasDiscontinuous;
2387 mNestedLoopDepth--;
2388
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002389 return false;
2390 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002391 }
2392
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002393 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394
alokp@chromium.org52813552010-11-16 18:36:09 +00002395 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002397 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002398
Jamie Madill075edd82013-07-08 13:30:19 -04002399 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002400 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002401 }
2402 else
2403 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002404 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002405
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406 if (node->getInit())
2407 {
2408 node->getInit()->traverse(this);
2409 }
2410
2411 out << "; ";
2412
alokp@chromium.org52813552010-11-16 18:36:09 +00002413 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002415 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416 }
2417
2418 out << "; ";
2419
alokp@chromium.org52813552010-11-16 18:36:09 +00002420 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002422 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423 }
2424
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002425 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002426
Jamie Madill075edd82013-07-08 13:30:19 -04002427 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002428 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 }
2430
2431 if (node->getBody())
2432 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002433 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 }
2435
Jamie Madill075edd82013-07-08 13:30:19 -04002436 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002437 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438
alokp@chromium.org52813552010-11-16 18:36:09 +00002439 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002440 {
Jamie Madill075edd82013-07-08 13:30:19 -04002441 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442 out << "while(\n";
2443
alokp@chromium.org52813552010-11-16 18:36:09 +00002444 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445
daniel@transgaming.com73536982012-03-21 20:45:49 +00002446 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447 }
2448
daniel@transgaming.com73536982012-03-21 20:45:49 +00002449 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002451 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002452 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002453
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 return false;
2455}
2456
2457bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2458{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002459 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460
2461 switch (node->getFlowOp())
2462 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002463 case EOpKill:
2464 outputTriplet(visit, "discard;\n", "", "");
2465 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002466 case EOpBreak:
2467 if (visit == PreVisit)
2468 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002469 if (mNestedLoopDepth > 1)
2470 {
2471 mUsesNestedBreak = true;
2472 }
2473
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002474 if (mExcessiveLoopIndex)
2475 {
2476 out << "{Break";
2477 mExcessiveLoopIndex->traverse(this);
2478 out << " = true; break;}\n";
2479 }
2480 else
2481 {
2482 out << "break;\n";
2483 }
2484 }
2485 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002486 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487 case EOpReturn:
2488 if (visit == PreVisit)
2489 {
2490 if (node->getExpression())
2491 {
2492 out << "return ";
2493 }
2494 else
2495 {
2496 out << "return;\n";
2497 }
2498 }
2499 else if (visit == PostVisit)
2500 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002501 if (node->getExpression())
2502 {
2503 out << ";\n";
2504 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 }
2506 break;
2507 default: UNREACHABLE();
2508 }
2509
2510 return true;
2511}
2512
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002513void OutputHLSL::traverseStatements(TIntermNode *node)
2514{
2515 if (isSingleStatement(node))
2516 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002517 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002518 }
2519
2520 node->traverse(this);
2521}
2522
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002523bool OutputHLSL::isSingleStatement(TIntermNode *node)
2524{
2525 TIntermAggregate *aggregate = node->getAsAggregate();
2526
2527 if (aggregate)
2528 {
2529 if (aggregate->getOp() == EOpSequence)
2530 {
2531 return false;
2532 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002533 else if (aggregate->getOp() == EOpDeclaration)
2534 {
2535 // Declaring multiple comma-separated variables must be considered multiple statements
2536 // because each individual declaration has side effects which are visible in the next.
2537 return false;
2538 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002539 else
2540 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002541 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002542 {
2543 if (!isSingleStatement(*sit))
2544 {
2545 return false;
2546 }
2547 }
2548
2549 return true;
2550 }
2551 }
2552
2553 return true;
2554}
2555
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002556// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2557// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002558bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2559{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002560 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002561 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002562
2563 // Parse loops of the form:
2564 // for(int index = initial; index [comparator] limit; index += increment)
2565 TIntermSymbol *index = NULL;
2566 TOperator comparator = EOpNull;
2567 int initial = 0;
2568 int limit = 0;
2569 int increment = 0;
2570
2571 // Parse index name and intial value
2572 if (node->getInit())
2573 {
2574 TIntermAggregate *init = node->getInit()->getAsAggregate();
2575
2576 if (init)
2577 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002578 TIntermSequence *sequence = init->getSequence();
2579 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002580
2581 if (variable && variable->getQualifier() == EvqTemporary)
2582 {
2583 TIntermBinary *assign = variable->getAsBinaryNode();
2584
2585 if (assign->getOp() == EOpInitialize)
2586 {
2587 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2588 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2589
2590 if (symbol && constant)
2591 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002592 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002593 {
2594 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002595 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002596 }
2597 }
2598 }
2599 }
2600 }
2601 }
2602
2603 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002604 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002605 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002606 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002607
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002608 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2609 {
2610 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2611
2612 if (constant)
2613 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002614 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002615 {
2616 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002617 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002618 }
2619 }
2620 }
2621 }
2622
2623 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002624 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002625 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002626 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2627 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002628
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002629 if (binaryTerminal)
2630 {
2631 TOperator op = binaryTerminal->getOp();
2632 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2633
2634 if (constant)
2635 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002636 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002637 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002638 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002639
2640 switch (op)
2641 {
2642 case EOpAddAssign: increment = value; break;
2643 case EOpSubAssign: increment = -value; break;
2644 default: UNIMPLEMENTED();
2645 }
2646 }
2647 }
2648 }
2649 else if (unaryTerminal)
2650 {
2651 TOperator op = unaryTerminal->getOp();
2652
2653 switch (op)
2654 {
2655 case EOpPostIncrement: increment = 1; break;
2656 case EOpPostDecrement: increment = -1; break;
2657 case EOpPreIncrement: increment = 1; break;
2658 case EOpPreDecrement: increment = -1; break;
2659 default: UNIMPLEMENTED();
2660 }
2661 }
2662 }
2663
2664 if (index != NULL && comparator != EOpNull && increment != 0)
2665 {
2666 if (comparator == EOpLessThanEqual)
2667 {
2668 comparator = EOpLessThan;
2669 limit += 1;
2670 }
2671
2672 if (comparator == EOpLessThan)
2673 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002674 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002675
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002676 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002677 {
2678 return false; // Not an excessive loop
2679 }
2680
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002681 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2682 mExcessiveLoopIndex = index;
2683
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002684 out << "{int ";
2685 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002686 out << ";\n"
2687 "bool Break";
2688 index->traverse(this);
2689 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002690
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002691 bool firstLoopFragment = true;
2692
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002693 while (iterations > 0)
2694 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002695 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002696
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002697 if (!firstLoopFragment)
2698 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002699 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002700 index->traverse(this);
2701 out << ") {\n";
2702 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002703
2704 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2705 {
2706 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2707 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002708
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002709 // for(int index = initial; index < clampedLimit; index += increment)
2710
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002711 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002712 index->traverse(this);
2713 out << " = ";
2714 out << initial;
2715
2716 out << "; ";
2717 index->traverse(this);
2718 out << " < ";
2719 out << clampedLimit;
2720
2721 out << "; ";
2722 index->traverse(this);
2723 out << " += ";
2724 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002725 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002726
Jamie Madill075edd82013-07-08 13:30:19 -04002727 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002728 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002729
2730 if (node->getBody())
2731 {
2732 node->getBody()->traverse(this);
2733 }
2734
Jamie Madill075edd82013-07-08 13:30:19 -04002735 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002736 out << ";}\n";
2737
2738 if (!firstLoopFragment)
2739 {
2740 out << "}\n";
2741 }
2742
2743 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002744
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002745 initial += MAX_LOOP_ITERATIONS * increment;
2746 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002747 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002748
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002749 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002750
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002751 mExcessiveLoopIndex = restoreIndex;
2752
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002753 return true;
2754 }
2755 else UNIMPLEMENTED();
2756 }
2757
2758 return false; // Not handled as an excessive loop
2759}
2760
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002761void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002762{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002763 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002765 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766 {
2767 out << preString;
2768 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002769 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002770 {
2771 out << inString;
2772 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002773 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002774 {
2775 out << postString;
2776 }
2777}
2778
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002779void OutputHLSL::outputLineDirective(int line)
2780{
2781 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2782 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002783 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002784 mBody << "#line " << line;
2785
2786 if (mContext.sourcePath)
2787 {
2788 mBody << " \"" << mContext.sourcePath << "\"";
2789 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002790
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002791 mBody << "\n";
2792 }
2793}
2794
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002795TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2796{
2797 TQualifier qualifier = symbol->getQualifier();
2798 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002799 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002800
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002801 if (name.empty()) // HLSL demands named arguments, also for prototypes
2802 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002803 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002804 }
2805 else
2806 {
Jamie Madill033dae62014-06-18 12:56:28 -04002807 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002808 }
2809
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002810 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2811 {
Jamie Madill033dae62014-06-18 12:56:28 -04002812 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002813 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002814 }
2815
Jamie Madill033dae62014-06-18 12:56:28 -04002816 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002817}
2818
2819TString OutputHLSL::initializer(const TType &type)
2820{
2821 TString string;
2822
Jamie Madill94bf7f22013-07-08 13:31:15 -04002823 size_t size = type.getObjectSize();
2824 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002825 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002826 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002827
Jamie Madill94bf7f22013-07-08 13:31:15 -04002828 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002829 {
2830 string += ", ";
2831 }
2832 }
2833
daniel@transgaming.comead23042010-04-29 03:35:36 +00002834 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002835}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002836
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002837void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2838{
2839 TInfoSinkBase &out = mBody;
2840
2841 if (visit == PreVisit)
2842 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002843 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002844
2845 out << name + "(";
2846 }
2847 else if (visit == InVisit)
2848 {
2849 out << ", ";
2850 }
2851 else if (visit == PostVisit)
2852 {
2853 out << ")";
2854 }
2855}
2856
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002857const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2858{
2859 TInfoSinkBase &out = mBody;
2860
Jamie Madill98493dd2013-07-08 14:39:03 -04002861 const TStructure* structure = type.getStruct();
2862 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002863 {
Jamie Madill033dae62014-06-18 12:56:28 -04002864 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002865
Jamie Madill98493dd2013-07-08 14:39:03 -04002866 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002867
Jamie Madill98493dd2013-07-08 14:39:03 -04002868 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002869 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002870 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002871 constUnion = writeConstantUnion(*fieldType, constUnion);
2872
Jamie Madill98493dd2013-07-08 14:39:03 -04002873 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002874 {
2875 out << ", ";
2876 }
2877 }
2878
2879 out << ")";
2880 }
2881 else
2882 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002883 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002884 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002885
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002886 if (writeType)
2887 {
Jamie Madill033dae62014-06-18 12:56:28 -04002888 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002889 }
2890
Jamie Madill94bf7f22013-07-08 13:31:15 -04002891 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002892 {
2893 switch (constUnion->getType())
2894 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002895 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002896 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002897 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002898 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002899 default: UNREACHABLE();
2900 }
2901
2902 if (i != size - 1)
2903 {
2904 out << ", ";
2905 }
2906 }
2907
2908 if (writeType)
2909 {
2910 out << ")";
2911 }
2912 }
2913
2914 return constUnion;
2915}
2916
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002917}