blob: 7d140707fc141329118a929dc4da8d2a6a6a33b9 [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"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000025#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000026#include <cfloat>
27#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000028
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029namespace sh
30{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000031
Nicolas Capense0ba27a2013-06-24 16:10:52 -040032TString OutputHLSL::TextureFunction::name() const
33{
34 TString name = "gl_texture";
35
Nicolas Capens6d232bb2013-07-08 15:56:38 -040036 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040037 {
38 name += "2D";
39 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040040 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040041 {
42 name += "3D";
43 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040044 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040045 {
46 name += "Cube";
47 }
48 else UNREACHABLE();
49
50 if (proj)
51 {
52 name += "Proj";
53 }
54
Nicolas Capensb1f45b72013-12-19 17:37:19 -050055 if (offset)
56 {
57 name += "Offset";
58 }
59
Nicolas Capens75fb4752013-07-10 15:14:47 -040060 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040061 {
Nicolas Capensfc014542014-02-18 14:47:13 -050062 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040063 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050064 case LOD: name += "Lod"; break;
65 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040066 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050067 case SIZE: name += "Size"; break;
68 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050069 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040070 default: UNREACHABLE();
71 }
72
73 return name + "(";
74}
75
76bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
77{
78 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040079 if (sampler > rhs.sampler) return false;
80
Nicolas Capense0ba27a2013-06-24 16:10:52 -040081 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040082 if (coords > rhs.coords) return false;
83
Nicolas Capense0ba27a2013-06-24 16:10:52 -040084 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040085 if (proj && !rhs.proj) return false;
86
87 if (!offset && rhs.offset) return true;
88 if (offset && !rhs.offset) return false;
89
Nicolas Capens75fb4752013-07-10 15:14:47 -040090 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040091 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040092
93 return false;
94}
95
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000096OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000097 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000098{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000099 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000100 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000101
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000102 mUsesFragColor = false;
103 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000104 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000105 mUsesFragCoord = false;
106 mUsesPointCoord = false;
107 mUsesFrontFacing = false;
108 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400109 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000110 mUsesXor = false;
111 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000112 mUsesMod2v = false;
113 mUsesMod2f = false;
114 mUsesMod3v = false;
115 mUsesMod3f = false;
116 mUsesMod4v = false;
117 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000118 mUsesFaceforward1 = false;
119 mUsesFaceforward2 = false;
120 mUsesFaceforward3 = false;
121 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000122 mUsesAtan2_1 = false;
123 mUsesAtan2_2 = false;
124 mUsesAtan2_3 = false;
125 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500126 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400127 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000128
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000129 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
130
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000131 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000132
133 mContainsLoopDiscontinuity = false;
134 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000135 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400136 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000137
138 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000139
Jamie Madill8daaba12014-06-13 10:04:33 -0400140 mStructureHLSL = new StructureHLSL;
Jamie Madillf91ce812014-06-13 10:04:34 -0400141 mUniformHLSL = new UniformHLSL(mStructureHLSL, mOutputType);
Jamie Madill8daaba12014-06-13 10:04:33 -0400142
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000143 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000144 {
Jamie Madill183bde52014-07-02 15:31:19 -0400145 if (mContext.shaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000146 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400147 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
148 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000149 }
150 else
151 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400152 // Reserve registers for dx_DepthRange and dx_ViewAdjust
153 mUniformHLSL->reserveUniformRegisters(2);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000154 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000155 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000156
Jamie Madillf91ce812014-06-13 10:04:34 -0400157 // Reserve registers for the default uniform block and driver constants
158 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159}
160
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000161OutputHLSL::~OutputHLSL()
162{
Jamie Madill8daaba12014-06-13 10:04:33 -0400163 SafeDelete(mUnfoldShortCircuit);
164 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400165 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000166}
167
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000168void OutputHLSL::output()
169{
Jamie Madill183bde52014-07-02 15:31:19 -0400170 mContainsLoopDiscontinuity = mContext.shaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400171 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
172 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000173
Jamie Madille53c98b2014-02-03 11:57:13 -0500174 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
175 // use a vertex attribute as a condition, and some related computation in the else block.
Jamie Madill183bde52014-07-02 15:31:19 -0400176 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500177 {
178 RewriteElseBlocks(mContext.treeRoot);
179 }
180
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000181 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 +0000182 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000183
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000184 mContext.infoSink().obj << mHeader.c_str();
185 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000186}
187
Jamie Madill570e04d2013-06-21 09:15:33 -0400188void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
189{
190 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
191 {
192 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
193
194 // This will mark the necessary block elements as referenced
195 flaggedNode->traverse(this);
196 TString structName(mBody.c_str());
197 mBody.erase();
198
199 mFlaggedStructOriginalNames[flaggedNode] = structName;
200
201 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
202 {
203 structName.erase(pos, 1);
204 }
205
206 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
207 }
208}
209
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000210TInfoSinkBase &OutputHLSL::getBodyStream()
211{
212 return mBody;
213}
214
Jamie Madillf2575982014-06-25 16:04:54 -0400215const std::vector<sh::Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000216{
Jamie Madillf91ce812014-06-13 10:04:34 -0400217 return mUniformHLSL->getUniforms();
daniel@transgaming.com043da132012-12-20 21:12:22 +0000218}
219
Jamie Madillf2575982014-06-25 16:04:54 -0400220const std::vector<sh::InterfaceBlock> &OutputHLSL::getInterfaceBlocks() const
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000221{
Jamie Madillf91ce812014-06-13 10:04:34 -0400222 return mUniformHLSL->getInterfaceBlocks();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000223}
224
Jamie Madillf2575982014-06-25 16:04:54 -0400225const std::vector<sh::Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400226{
227 return mActiveOutputVariables;
228}
229
Jamie Madillf2575982014-06-25 16:04:54 -0400230const std::vector<sh::Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400231{
232 return mActiveAttributes;
233}
234
Jamie Madillf2575982014-06-25 16:04:54 -0400235const std::vector<sh::Varying> &OutputHLSL::getVaryings() const
Jamie Madill47fdd132013-08-30 13:21:04 -0400236{
237 return mActiveVaryings;
238}
239
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000240int OutputHLSL::vectorSize(const TType &type) const
241{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000242 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000243 int arraySize = type.isArray() ? type.getArraySize() : 1;
244
245 return elementSize * arraySize;
246}
247
Jamie Madill98493dd2013-07-08 14:39:03 -0400248TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400249{
250 TString init;
251
252 TString preIndentString;
253 TString fullIndentString;
254
255 for (int spaces = 0; spaces < (indent * 4); spaces++)
256 {
257 preIndentString += ' ';
258 }
259
260 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
261 {
262 fullIndentString += ' ';
263 }
264
265 init += preIndentString + "{\n";
266
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 const TFieldList &fields = structure.fields();
268 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400269 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400270 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400271 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400272 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400273
Jamie Madill98493dd2013-07-08 14:39:03 -0400274 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400275 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400276 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400277 }
278 else
279 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400280 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400281 }
282 }
283
284 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
285
286 return init;
287}
288
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289void OutputHLSL::header()
290{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000291 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000293 TString varyings;
294 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400295 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000296
Jamie Madill829f59e2013-11-13 19:40:54 -0500297 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400298 {
299 TIntermTyped *structNode = flaggedStructIt->first;
300 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400301 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400302 const TString &originalName = mFlaggedStructOriginalNames[structNode];
303
Jamie Madill033dae62014-06-18 12:56:28 -0400304 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400305 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400306 flaggedStructs += "\n";
307 }
308
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000309 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
310 {
311 const TType &type = varying->second->getType();
312 const TString &name = varying->second->getSymbol();
313
314 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400315 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
316 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400317
Jamie Madill94599662013-08-30 13:21:10 -0400318 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000319 }
320
321 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
322 {
323 const TType &type = attribute->second->getType();
324 const TString &name = attribute->second->getSymbol();
325
Jamie Madill033dae62014-06-18 12:56:28 -0400326 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400327
Jamie Madillf2575982014-06-25 16:04:54 -0400328 sh::Attribute attributeVar(GLVariableType(type), GLVariablePrecision(type), name.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400329 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
330 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000331 }
332
Jamie Madill8daaba12014-06-13 10:04:33 -0400333 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400334
Jamie Madillf91ce812014-06-13 10:04:34 -0400335 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
336 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
337
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500338 if (mUsesDiscardRewriting)
339 {
340 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
341 }
342
Nicolas Capens655fe362014-04-11 13:12:34 -0400343 if (mUsesNestedBreak)
344 {
345 out << "#define ANGLE_USES_NESTED_BREAK" << "\n";
346 }
347
Jamie Madill183bde52014-07-02 15:31:19 -0400348 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000350 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000351 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000352
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000353 out << "// Varyings\n";
354 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400355 out << "\n";
356
357 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000358 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500359 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000360 {
Jamie Madill46131a32013-06-20 11:55:50 -0400361 const TString &variableName = outputVariableIt->first;
362 const TType &variableType = outputVariableIt->second->getType();
363 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
364
Jamie Madill033dae62014-06-18 12:56:28 -0400365 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400366 " = " + initializer(variableType) + ";\n";
367
Jamie Madillf2575982014-06-25 16:04:54 -0400368 sh::Attribute outputVar(GLVariableType(variableType), GLVariablePrecision(variableType), variableName.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400369 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400370 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000371 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000372 }
Jamie Madill46131a32013-06-20 11:55:50 -0400373 else
374 {
375 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
376
377 out << "static float4 gl_Color[" << numColorValues << "] =\n"
378 "{\n";
379 for (unsigned int i = 0; i < numColorValues; i++)
380 {
381 out << " float4(0, 0, 0, 0)";
382 if (i + 1 != numColorValues)
383 {
384 out << ",";
385 }
386 out << "\n";
387 }
388
389 out << "};\n";
390 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000391
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400392 if (mUsesFragDepth)
393 {
394 out << "static float gl_Depth = 0.0;\n";
395 }
396
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000397 if (mUsesFragCoord)
398 {
399 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
400 }
401
402 if (mUsesPointCoord)
403 {
404 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
405 }
406
407 if (mUsesFrontFacing)
408 {
409 out << "static bool gl_FrontFacing = false;\n";
410 }
411
412 out << "\n";
413
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000414 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000415 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000416 out << "struct gl_DepthRangeParameters\n"
417 "{\n"
418 " float near;\n"
419 " float far;\n"
420 " float diff;\n"
421 "};\n"
422 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000423 }
424
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000425 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000426 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000427 out << "cbuffer DriverConstants : register(b1)\n"
428 "{\n";
429
430 if (mUsesDepthRange)
431 {
432 out << " float3 dx_DepthRange : packoffset(c0);\n";
433 }
434
435 if (mUsesFragCoord)
436 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000437 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000438 }
439
440 if (mUsesFragCoord || mUsesFrontFacing)
441 {
442 out << " float3 dx_DepthFront : packoffset(c2);\n";
443 }
444
445 out << "};\n";
446 }
447 else
448 {
449 if (mUsesDepthRange)
450 {
451 out << "uniform float3 dx_DepthRange : register(c0);";
452 }
453
454 if (mUsesFragCoord)
455 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000456 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000457 }
458
459 if (mUsesFragCoord || mUsesFrontFacing)
460 {
461 out << "uniform float3 dx_DepthFront : register(c2);\n";
462 }
463 }
464
465 out << "\n";
466
467 if (mUsesDepthRange)
468 {
469 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
470 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000471 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000472
Jamie Madillf91ce812014-06-13 10:04:34 -0400473 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000474 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400475 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000476 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400477 out << flaggedStructs;
478 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000479 }
480
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000481 if (usingMRTExtension && mNumRenderTargets > 1)
482 {
483 out << "#define GL_USES_MRT\n";
484 }
485
486 if (mUsesFragColor)
487 {
488 out << "#define GL_USES_FRAG_COLOR\n";
489 }
490
491 if (mUsesFragData)
492 {
493 out << "#define GL_USES_FRAG_DATA\n";
494 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000496 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000497 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000498 out << "// Attributes\n";
499 out << attributes;
500 out << "\n"
501 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400502
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000503 if (mUsesPointSize)
504 {
505 out << "static float gl_PointSize = float(1);\n";
506 }
507
508 out << "\n"
509 "// Varyings\n";
510 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000511 out << "\n";
512
513 if (mUsesDepthRange)
514 {
515 out << "struct gl_DepthRangeParameters\n"
516 "{\n"
517 " float near;\n"
518 " float far;\n"
519 " float diff;\n"
520 "};\n"
521 "\n";
522 }
523
524 if (mOutputType == SH_HLSL11_OUTPUT)
525 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000526 if (mUsesDepthRange)
527 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000528 out << "cbuffer DriverConstants : register(b1)\n"
529 "{\n"
530 " float3 dx_DepthRange : packoffset(c0);\n"
531 "};\n"
532 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000533 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000534 }
535 else
536 {
537 if (mUsesDepthRange)
538 {
539 out << "uniform float3 dx_DepthRange : register(c0);\n";
540 }
541
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000542 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000543 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000544 }
545
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000546 if (mUsesDepthRange)
547 {
548 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
549 "\n";
550 }
551
Jamie Madillf91ce812014-06-13 10:04:34 -0400552 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000553 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400554 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000555 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400556 out << flaggedStructs;
557 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000558 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400559 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000560
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400561 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
562 {
563 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400564 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000565 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400566 switch(textureFunction->sampler)
567 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400568 case EbtSampler2D: out << "int2 "; break;
569 case EbtSampler3D: out << "int3 "; break;
570 case EbtSamplerCube: out << "int2 "; break;
571 case EbtSampler2DArray: out << "int3 "; break;
572 case EbtISampler2D: out << "int2 "; break;
573 case EbtISampler3D: out << "int3 "; break;
574 case EbtISamplerCube: out << "int2 "; break;
575 case EbtISampler2DArray: out << "int3 "; break;
576 case EbtUSampler2D: out << "int2 "; break;
577 case EbtUSampler3D: out << "int3 "; break;
578 case EbtUSamplerCube: out << "int2 "; break;
579 case EbtUSampler2DArray: out << "int3 "; break;
580 case EbtSampler2DShadow: out << "int2 "; break;
581 case EbtSamplerCubeShadow: out << "int2 "; break;
582 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400583 default: UNREACHABLE();
584 }
585 }
586 else // Sampling function
587 {
588 switch(textureFunction->sampler)
589 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400590 case EbtSampler2D: out << "float4 "; break;
591 case EbtSampler3D: out << "float4 "; break;
592 case EbtSamplerCube: out << "float4 "; break;
593 case EbtSampler2DArray: out << "float4 "; break;
594 case EbtISampler2D: out << "int4 "; break;
595 case EbtISampler3D: out << "int4 "; break;
596 case EbtISamplerCube: out << "int4 "; break;
597 case EbtISampler2DArray: out << "int4 "; break;
598 case EbtUSampler2D: out << "uint4 "; break;
599 case EbtUSampler3D: out << "uint4 "; break;
600 case EbtUSamplerCube: out << "uint4 "; break;
601 case EbtUSampler2DArray: out << "uint4 "; break;
602 case EbtSampler2DShadow: out << "float "; break;
603 case EbtSamplerCubeShadow: out << "float "; break;
604 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400605 default: UNREACHABLE();
606 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000607 }
608
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400609 // Function name
610 out << textureFunction->name();
611
612 // Argument list
613 int hlslCoords = 4;
614
615 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000616 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400617 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000618 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400619 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
620 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
621 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000622 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400623
Nicolas Capens75fb4752013-07-10 15:14:47 -0400624 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000625 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400626 case TextureFunction::IMPLICIT: break;
627 case TextureFunction::BIAS: hlslCoords = 4; break;
628 case TextureFunction::LOD: hlslCoords = 4; break;
629 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400630 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400631 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000632 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400633 }
634 else if (mOutputType == SH_HLSL11_OUTPUT)
635 {
636 switch(textureFunction->sampler)
637 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400638 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
639 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
640 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
641 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
642 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
643 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500644 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400645 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
646 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
647 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500648 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400649 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
650 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
651 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
652 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400653 default: UNREACHABLE();
654 }
655 }
656 else UNREACHABLE();
657
Nicolas Capensfc014542014-02-18 14:47:13 -0500658 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400659 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500660 switch(textureFunction->coords)
661 {
662 case 2: out << ", int2 t"; break;
663 case 3: out << ", int3 t"; break;
664 default: UNREACHABLE();
665 }
666 }
667 else // Floating-point coordinates (except textureSize)
668 {
669 switch(textureFunction->coords)
670 {
671 case 1: out << ", int lod"; break; // textureSize()
672 case 2: out << ", float2 t"; break;
673 case 3: out << ", float3 t"; break;
674 case 4: out << ", float4 t"; break;
675 default: UNREACHABLE();
676 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000677 }
678
Nicolas Capensd11d5492014-02-19 17:06:10 -0500679 if (textureFunction->method == TextureFunction::GRAD)
680 {
681 switch(textureFunction->sampler)
682 {
683 case EbtSampler2D:
684 case EbtISampler2D:
685 case EbtUSampler2D:
686 case EbtSampler2DArray:
687 case EbtISampler2DArray:
688 case EbtUSampler2DArray:
689 case EbtSampler2DShadow:
690 case EbtSampler2DArrayShadow:
691 out << ", float2 ddx, float2 ddy";
692 break;
693 case EbtSampler3D:
694 case EbtISampler3D:
695 case EbtUSampler3D:
696 case EbtSamplerCube:
697 case EbtISamplerCube:
698 case EbtUSamplerCube:
699 case EbtSamplerCubeShadow:
700 out << ", float3 ddx, float3 ddy";
701 break;
702 default: UNREACHABLE();
703 }
704 }
705
Nicolas Capens75fb4752013-07-10 15:14:47 -0400706 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000707 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400708 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400709 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400710 case TextureFunction::LOD: out << ", float lod"; break;
711 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400712 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400713 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500714 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500715 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400716 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000717 }
718
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500719 if (textureFunction->offset)
720 {
721 switch(textureFunction->sampler)
722 {
723 case EbtSampler2D: out << ", int2 offset"; break;
724 case EbtSampler3D: out << ", int3 offset"; break;
725 case EbtSampler2DArray: out << ", int2 offset"; break;
726 case EbtISampler2D: out << ", int2 offset"; break;
727 case EbtISampler3D: out << ", int3 offset"; break;
728 case EbtISampler2DArray: out << ", int2 offset"; break;
729 case EbtUSampler2D: out << ", int2 offset"; break;
730 case EbtUSampler3D: out << ", int3 offset"; break;
731 case EbtUSampler2DArray: out << ", int2 offset"; break;
732 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500733 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500734 default: UNREACHABLE();
735 }
736 }
737
Nicolas Capens84cfa122014-04-14 13:48:45 -0400738 if (textureFunction->method == TextureFunction::BIAS ||
739 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500740 {
741 out << ", float bias";
742 }
743
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400744 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400745 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400746
Nicolas Capens75fb4752013-07-10 15:14:47 -0400747 if (textureFunction->method == TextureFunction::SIZE)
748 {
749 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
750 {
751 if (IsSamplerArray(textureFunction->sampler))
752 {
753 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
754 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
755 }
756 else
757 {
758 out << " uint width; uint height; uint numberOfLevels;\n"
759 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
760 }
761 }
762 else if (IsSampler3D(textureFunction->sampler))
763 {
764 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
765 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
766 }
767 else UNREACHABLE();
768
769 switch(textureFunction->sampler)
770 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400771 case EbtSampler2D: out << " return int2(width, height);"; break;
772 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
773 case EbtSamplerCube: out << " return int2(width, height);"; break;
774 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
775 case EbtISampler2D: out << " return int2(width, height);"; break;
776 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
777 case EbtISamplerCube: out << " return int2(width, height);"; break;
778 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
779 case EbtUSampler2D: out << " return int2(width, height);"; break;
780 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
781 case EbtUSamplerCube: out << " return int2(width, height);"; break;
782 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
783 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
784 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
785 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400786 default: UNREACHABLE();
787 }
788 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400789 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400790 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500791 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
792 {
793 out << " float width; float height; float layers; float levels;\n";
794
795 out << " uint mip = 0;\n";
796
797 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
798
799 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
800 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
801 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
802 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
803
804 // FACE_POSITIVE_X = 000b
805 // FACE_NEGATIVE_X = 001b
806 // FACE_POSITIVE_Y = 010b
807 // FACE_NEGATIVE_Y = 011b
808 // FACE_POSITIVE_Z = 100b
809 // FACE_NEGATIVE_Z = 101b
810 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
811
812 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
813 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
814 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
815
816 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
817 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
818 }
819 else if (IsIntegerSampler(textureFunction->sampler) &&
820 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400821 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400822 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400823 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400824 if (IsSamplerArray(textureFunction->sampler))
825 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400826 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400827
Nicolas Capens9edebd62013-08-06 10:59:10 -0400828 if (textureFunction->method == TextureFunction::LOD0)
829 {
830 out << " uint mip = 0;\n";
831 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400832 else if (textureFunction->method == TextureFunction::LOD0BIAS)
833 {
834 out << " uint mip = bias;\n";
835 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400836 else
837 {
838 if (textureFunction->method == TextureFunction::IMPLICIT ||
839 textureFunction->method == TextureFunction::BIAS)
840 {
841 out << " x.GetDimensions(0, width, height, layers, levels);\n"
842 " float2 tSized = float2(t.x * width, t.y * height);\n"
843 " float dx = length(ddx(tSized));\n"
844 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500845 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400846
847 if (textureFunction->method == TextureFunction::BIAS)
848 {
849 out << " lod += bias;\n";
850 }
851 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500852 else if (textureFunction->method == TextureFunction::GRAD)
853 {
854 out << " x.GetDimensions(0, width, height, layers, levels);\n"
855 " float lod = log2(max(length(ddx), length(ddy)));\n";
856 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400857
858 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
859 }
860
861 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400862 }
863 else
864 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400865 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400866
Nicolas Capens9edebd62013-08-06 10:59:10 -0400867 if (textureFunction->method == TextureFunction::LOD0)
868 {
869 out << " uint mip = 0;\n";
870 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400871 else if (textureFunction->method == TextureFunction::LOD0BIAS)
872 {
873 out << " uint mip = bias;\n";
874 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400875 else
876 {
877 if (textureFunction->method == TextureFunction::IMPLICIT ||
878 textureFunction->method == TextureFunction::BIAS)
879 {
880 out << " x.GetDimensions(0, width, height, levels);\n"
881 " float2 tSized = float2(t.x * width, t.y * height);\n"
882 " float dx = length(ddx(tSized));\n"
883 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500884 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400885
886 if (textureFunction->method == TextureFunction::BIAS)
887 {
888 out << " lod += bias;\n";
889 }
890 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500891 else if (textureFunction->method == TextureFunction::LOD)
892 {
893 out << " x.GetDimensions(0, width, height, levels);\n";
894 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500895 else if (textureFunction->method == TextureFunction::GRAD)
896 {
897 out << " x.GetDimensions(0, width, height, levels);\n"
898 " float lod = log2(max(length(ddx), length(ddy)));\n";
899 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400900
901 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
902 }
903
904 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400905 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400906 }
907 else if (IsSampler3D(textureFunction->sampler))
908 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400909 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400910
Nicolas Capens9edebd62013-08-06 10:59:10 -0400911 if (textureFunction->method == TextureFunction::LOD0)
912 {
913 out << " uint mip = 0;\n";
914 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400915 else if (textureFunction->method == TextureFunction::LOD0BIAS)
916 {
917 out << " uint mip = bias;\n";
918 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400919 else
920 {
921 if (textureFunction->method == TextureFunction::IMPLICIT ||
922 textureFunction->method == TextureFunction::BIAS)
923 {
924 out << " x.GetDimensions(0, width, height, depth, levels);\n"
925 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
926 " float dx = length(ddx(tSized));\n"
927 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500928 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400929
930 if (textureFunction->method == TextureFunction::BIAS)
931 {
932 out << " lod += bias;\n";
933 }
934 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500935 else if (textureFunction->method == TextureFunction::GRAD)
936 {
937 out << " x.GetDimensions(0, width, height, depth, levels);\n"
938 " float lod = log2(max(length(ddx), length(ddy)));\n";
939 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400940
941 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
942 }
943
944 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400945 }
946 else UNREACHABLE();
947 }
948
949 out << " return ";
950
951 // HLSL intrinsic
952 if (mOutputType == SH_HLSL9_OUTPUT)
953 {
954 switch(textureFunction->sampler)
955 {
956 case EbtSampler2D: out << "tex2D"; break;
957 case EbtSamplerCube: out << "texCUBE"; break;
958 default: UNREACHABLE();
959 }
960
Nicolas Capens75fb4752013-07-10 15:14:47 -0400961 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400962 {
963 case TextureFunction::IMPLICIT: out << "(s, "; break;
964 case TextureFunction::BIAS: out << "bias(s, "; break;
965 case TextureFunction::LOD: out << "lod(s, "; break;
966 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400967 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400968 default: UNREACHABLE();
969 }
970 }
971 else if (mOutputType == SH_HLSL11_OUTPUT)
972 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500973 if (textureFunction->method == TextureFunction::GRAD)
974 {
975 if (IsIntegerSampler(textureFunction->sampler))
976 {
977 out << "x.Load(";
978 }
979 else if (IsShadowSampler(textureFunction->sampler))
980 {
981 out << "x.SampleCmpLevelZero(s, ";
982 }
983 else
984 {
985 out << "x.SampleGrad(s, ";
986 }
987 }
988 else if (IsIntegerSampler(textureFunction->sampler) ||
989 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400990 {
991 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400992 }
Nicolas Capenscb127d32013-07-15 17:26:18 -0400993 else if (IsShadowSampler(textureFunction->sampler))
994 {
995 out << "x.SampleCmp(s, ";
996 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400997 else
998 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400999 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001000 {
1001 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1002 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1003 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1004 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001005 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001006 default: UNREACHABLE();
1007 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001008 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001009 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001010 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001011
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001012 // Integer sampling requires integer addresses
1013 TString addressx = "";
1014 TString addressy = "";
1015 TString addressz = "";
1016 TString close = "";
1017
Nicolas Capensfc014542014-02-18 14:47:13 -05001018 if (IsIntegerSampler(textureFunction->sampler) ||
1019 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001020 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001021 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001022 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001023 case 2: out << "int3("; break;
1024 case 3: out << "int4("; break;
1025 default: UNREACHABLE();
1026 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001027
Nicolas Capensfc014542014-02-18 14:47:13 -05001028 // Convert from normalized floating-point to integer
1029 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001030 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001031 addressx = "int(floor(width * frac((";
1032 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001033
Nicolas Capensfc014542014-02-18 14:47:13 -05001034 if (IsSamplerArray(textureFunction->sampler))
1035 {
1036 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1037 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001038 else if (IsSamplerCube(textureFunction->sampler))
1039 {
1040 addressz = "((((";
1041 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001042 else
1043 {
1044 addressz = "int(floor(depth * frac((";
1045 }
1046
1047 close = "))))";
1048 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001049 }
1050 else
1051 {
1052 switch(hlslCoords)
1053 {
1054 case 2: out << "float2("; break;
1055 case 3: out << "float3("; break;
1056 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001057 default: UNREACHABLE();
1058 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001059 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001060
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001061 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001062
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001063 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001064 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001065 switch(textureFunction->coords)
1066 {
1067 case 3: proj = " / t.z"; break;
1068 case 4: proj = " / t.w"; break;
1069 default: UNREACHABLE();
1070 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001071 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001072
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001073 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001074
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001075 if (mOutputType == SH_HLSL9_OUTPUT)
1076 {
1077 if (hlslCoords >= 3)
1078 {
1079 if (textureFunction->coords < 3)
1080 {
1081 out << ", 0";
1082 }
1083 else
1084 {
1085 out << ", t.z" + proj;
1086 }
1087 }
1088
1089 if (hlslCoords == 4)
1090 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001091 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001092 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001093 case TextureFunction::BIAS: out << ", bias"; break;
1094 case TextureFunction::LOD: out << ", lod"; break;
1095 case TextureFunction::LOD0: out << ", 0"; break;
1096 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001097 default: UNREACHABLE();
1098 }
1099 }
1100
1101 out << "));\n";
1102 }
1103 else if (mOutputType == SH_HLSL11_OUTPUT)
1104 {
1105 if (hlslCoords >= 3)
1106 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001107 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1108 {
1109 out << ", face";
1110 }
1111 else
1112 {
1113 out << ", " + addressz + ("t.z" + proj) + close;
1114 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001115 }
1116
Nicolas Capensd11d5492014-02-19 17:06:10 -05001117 if (textureFunction->method == TextureFunction::GRAD)
1118 {
1119 if (IsIntegerSampler(textureFunction->sampler))
1120 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001121 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001122 }
1123 else if (IsShadowSampler(textureFunction->sampler))
1124 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001125 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001126 switch(textureFunction->coords)
1127 {
1128 case 3: out << "), t.z"; break;
1129 case 4: out << "), t.w"; break;
1130 default: UNREACHABLE();
1131 }
1132 }
1133 else
1134 {
1135 out << "), ddx, ddy";
1136 }
1137 }
1138 else if (IsIntegerSampler(textureFunction->sampler) ||
1139 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001140 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001141 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001142 }
1143 else if (IsShadowSampler(textureFunction->sampler))
1144 {
1145 // Compare value
1146 switch(textureFunction->coords)
1147 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001148 case 3: out << "), t.z"; break;
1149 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001150 default: UNREACHABLE();
1151 }
1152 }
1153 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001154 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001155 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001156 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001157 case TextureFunction::IMPLICIT: out << ")"; break;
1158 case TextureFunction::BIAS: out << "), bias"; break;
1159 case TextureFunction::LOD: out << "), lod"; break;
1160 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001161 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001162 default: UNREACHABLE();
1163 }
1164 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001165
1166 if (textureFunction->offset)
1167 {
1168 out << ", offset";
1169 }
1170
1171 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001172 }
1173 else UNREACHABLE();
1174 }
1175
1176 out << "\n"
1177 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001178 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001179 }
1180
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001181 if (mUsesFragCoord)
1182 {
1183 out << "#define GL_USES_FRAG_COORD\n";
1184 }
1185
1186 if (mUsesPointCoord)
1187 {
1188 out << "#define GL_USES_POINT_COORD\n";
1189 }
1190
1191 if (mUsesFrontFacing)
1192 {
1193 out << "#define GL_USES_FRONT_FACING\n";
1194 }
1195
1196 if (mUsesPointSize)
1197 {
1198 out << "#define GL_USES_POINT_SIZE\n";
1199 }
1200
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001201 if (mUsesFragDepth)
1202 {
1203 out << "#define GL_USES_FRAG_DEPTH\n";
1204 }
1205
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001206 if (mUsesDepthRange)
1207 {
1208 out << "#define GL_USES_DEPTH_RANGE\n";
1209 }
1210
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001211 if (mUsesXor)
1212 {
1213 out << "bool xor(bool p, bool q)\n"
1214 "{\n"
1215 " return (p || q) && !(p && q);\n"
1216 "}\n"
1217 "\n";
1218 }
1219
1220 if (mUsesMod1)
1221 {
1222 out << "float mod(float x, float y)\n"
1223 "{\n"
1224 " return x - y * floor(x / y);\n"
1225 "}\n"
1226 "\n";
1227 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001228
1229 if (mUsesMod2v)
1230 {
1231 out << "float2 mod(float2 x, float2 y)\n"
1232 "{\n"
1233 " return x - y * floor(x / y);\n"
1234 "}\n"
1235 "\n";
1236 }
1237
1238 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001239 {
1240 out << "float2 mod(float2 x, float y)\n"
1241 "{\n"
1242 " return x - y * floor(x / y);\n"
1243 "}\n"
1244 "\n";
1245 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001246
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001247 if (mUsesMod3v)
1248 {
1249 out << "float3 mod(float3 x, float3 y)\n"
1250 "{\n"
1251 " return x - y * floor(x / y);\n"
1252 "}\n"
1253 "\n";
1254 }
1255
1256 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001257 {
1258 out << "float3 mod(float3 x, float y)\n"
1259 "{\n"
1260 " return x - y * floor(x / y);\n"
1261 "}\n"
1262 "\n";
1263 }
1264
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001265 if (mUsesMod4v)
1266 {
1267 out << "float4 mod(float4 x, float4 y)\n"
1268 "{\n"
1269 " return x - y * floor(x / y);\n"
1270 "}\n"
1271 "\n";
1272 }
1273
1274 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001275 {
1276 out << "float4 mod(float4 x, float y)\n"
1277 "{\n"
1278 " return x - y * floor(x / y);\n"
1279 "}\n"
1280 "\n";
1281 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001282
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001283 if (mUsesFaceforward1)
1284 {
1285 out << "float faceforward(float N, float I, float Nref)\n"
1286 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001287 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001288 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001289 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001290 " }\n"
1291 " else\n"
1292 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001293 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001294 " }\n"
1295 "}\n"
1296 "\n";
1297 }
1298
1299 if (mUsesFaceforward2)
1300 {
1301 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1302 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001303 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001304 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001305 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001306 " }\n"
1307 " else\n"
1308 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001309 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001310 " }\n"
1311 "}\n"
1312 "\n";
1313 }
1314
1315 if (mUsesFaceforward3)
1316 {
1317 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1318 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001319 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001320 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001321 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001322 " }\n"
1323 " else\n"
1324 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001325 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001326 " }\n"
1327 "}\n"
1328 "\n";
1329 }
1330
1331 if (mUsesFaceforward4)
1332 {
1333 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1334 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001335 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001336 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001337 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001338 " }\n"
1339 " else\n"
1340 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001341 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001342 " }\n"
1343 "}\n"
1344 "\n";
1345 }
1346
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001347 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001348 {
1349 out << "float atanyx(float y, float x)\n"
1350 "{\n"
1351 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1352 " return atan2(y, x);\n"
1353 "}\n";
1354 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001355
1356 if (mUsesAtan2_2)
1357 {
1358 out << "float2 atanyx(float2 y, float2 x)\n"
1359 "{\n"
1360 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1361 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1362 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1363 "}\n";
1364 }
1365
1366 if (mUsesAtan2_3)
1367 {
1368 out << "float3 atanyx(float3 y, float3 x)\n"
1369 "{\n"
1370 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1371 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1372 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1373 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1374 "}\n";
1375 }
1376
1377 if (mUsesAtan2_4)
1378 {
1379 out << "float4 atanyx(float4 y, float4 x)\n"
1380 "{\n"
1381 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1382 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1383 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1384 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1385 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1386 "}\n";
1387 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001388}
1389
1390void OutputHLSL::visitSymbol(TIntermSymbol *node)
1391{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001392 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001393
Jamie Madill570e04d2013-06-21 09:15:33 -04001394 // Handle accessing std140 structs by value
1395 if (mFlaggedStructMappedNames.count(node) > 0)
1396 {
1397 out << mFlaggedStructMappedNames[node];
1398 return;
1399 }
1400
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001401 TString name = node->getSymbol();
1402
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001403 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001404 {
1405 mUsesDepthRange = true;
1406 out << name;
1407 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001408 else
1409 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001410 TQualifier qualifier = node->getQualifier();
1411
1412 if (qualifier == EvqUniform)
1413 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001414 const TType& nodeType = node->getType();
1415 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1416
1417 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001418 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001419 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001420 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001421 else
1422 {
1423 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001424 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001425
Jamie Madill033dae62014-06-18 12:56:28 -04001426 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001427 }
Jamie Madill19571812013-08-12 15:26:34 -07001428 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001429 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001430 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001431 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001432 }
Jamie Madill033dae62014-06-18 12:56:28 -04001433 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001434 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001435 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001436 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001437 }
Jamie Madill19571812013-08-12 15:26:34 -07001438 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001439 {
1440 mReferencedOutputVariables[name] = node;
1441 out << "out_" << name;
1442 }
1443 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001444 {
1445 out << "gl_Color[0]";
1446 mUsesFragColor = true;
1447 }
1448 else if (qualifier == EvqFragData)
1449 {
1450 out << "gl_Color";
1451 mUsesFragData = true;
1452 }
1453 else if (qualifier == EvqFragCoord)
1454 {
1455 mUsesFragCoord = true;
1456 out << name;
1457 }
1458 else if (qualifier == EvqPointCoord)
1459 {
1460 mUsesPointCoord = true;
1461 out << name;
1462 }
1463 else if (qualifier == EvqFrontFacing)
1464 {
1465 mUsesFrontFacing = true;
1466 out << name;
1467 }
1468 else if (qualifier == EvqPointSize)
1469 {
1470 mUsesPointSize = true;
1471 out << name;
1472 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001473 else if (name == "gl_FragDepthEXT")
1474 {
1475 mUsesFragDepth = true;
1476 out << "gl_Depth";
1477 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001478 else if (qualifier == EvqInternal)
1479 {
1480 out << name;
1481 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001482 else
1483 {
Jamie Madill033dae62014-06-18 12:56:28 -04001484 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001485 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001486 }
1487}
1488
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001489void OutputHLSL::visitRaw(TIntermRaw *node)
1490{
1491 mBody << node->getRawText();
1492}
1493
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001494bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1495{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001496 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001497
Jamie Madill570e04d2013-06-21 09:15:33 -04001498 // Handle accessing std140 structs by value
1499 if (mFlaggedStructMappedNames.count(node) > 0)
1500 {
1501 out << mFlaggedStructMappedNames[node];
1502 return false;
1503 }
1504
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001505 switch (node->getOp())
1506 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001507 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001508 case EOpInitialize:
1509 if (visit == PreVisit)
1510 {
1511 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1512 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1513 // new variable is created before the assignment is evaluated), so we need to convert
1514 // this to "float t = x, x = t;".
1515
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001516 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1517 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001518
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001519 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1520 expression->traverse(&searchSymbol);
1521 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001522
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001523 if (sameSymbol)
1524 {
1525 // Type already printed
1526 out << "t" + str(mUniqueIndex) + " = ";
1527 expression->traverse(this);
1528 out << ", ";
1529 symbolNode->traverse(this);
1530 out << " = t" + str(mUniqueIndex);
1531
1532 mUniqueIndex++;
1533 return false;
1534 }
1535 }
1536 else if (visit == InVisit)
1537 {
1538 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001539 }
1540 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001541 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1542 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1543 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1544 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1545 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1546 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001547 if (visit == PreVisit)
1548 {
1549 out << "(";
1550 }
1551 else if (visit == InVisit)
1552 {
1553 out << " = mul(";
1554 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001555 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001556 }
1557 else
1558 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001559 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001560 }
1561 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001562 case EOpMatrixTimesMatrixAssign:
1563 if (visit == PreVisit)
1564 {
1565 out << "(";
1566 }
1567 else if (visit == InVisit)
1568 {
1569 out << " = mul(";
1570 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001571 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001572 }
1573 else
1574 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001575 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001576 }
1577 break;
1578 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001579 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001580 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001581 const TType& leftType = node->getLeft()->getType();
1582 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001583 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001584 if (visit == PreVisit)
1585 {
1586 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1587 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001588 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001589 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001590 return false;
1591 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001592 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001593 else
1594 {
1595 outputTriplet(visit, "", "[", "]");
1596 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001597 }
1598 break;
1599 case EOpIndexIndirect:
1600 // We do not currently support indirect references to interface blocks
1601 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1602 outputTriplet(visit, "", "[", "]");
1603 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001604 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001605 if (visit == InVisit)
1606 {
1607 const TStructure* structure = node->getLeft()->getType().getStruct();
1608 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1609 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001610 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001611
1612 return false;
1613 }
1614 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001615 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001616 if (visit == InVisit)
1617 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001618 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1619 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1620 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001621 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001622
1623 return false;
1624 }
1625 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626 case EOpVectorSwizzle:
1627 if (visit == InVisit)
1628 {
1629 out << ".";
1630
1631 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1632
1633 if (swizzle)
1634 {
1635 TIntermSequence &sequence = swizzle->getSequence();
1636
1637 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1638 {
1639 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1640
1641 if (element)
1642 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001643 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001644
1645 switch (i)
1646 {
1647 case 0: out << "x"; break;
1648 case 1: out << "y"; break;
1649 case 2: out << "z"; break;
1650 case 3: out << "w"; break;
1651 default: UNREACHABLE();
1652 }
1653 }
1654 else UNREACHABLE();
1655 }
1656 }
1657 else UNREACHABLE();
1658
1659 return false; // Fully processed
1660 }
1661 break;
1662 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1663 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1664 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1665 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001666 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001667 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001668 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001669 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001670 if (node->getOp() == EOpEqual)
1671 {
1672 outputTriplet(visit, "(", " == ", ")");
1673 }
1674 else
1675 {
1676 outputTriplet(visit, "(", " != ", ")");
1677 }
1678 }
1679 else if (node->getLeft()->getBasicType() == EbtStruct)
1680 {
1681 if (node->getOp() == EOpEqual)
1682 {
1683 out << "(";
1684 }
1685 else
1686 {
1687 out << "!(";
1688 }
1689
Jamie Madill98493dd2013-07-08 14:39:03 -04001690 const TStructure &structure = *node->getLeft()->getType().getStruct();
1691 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001692
Jamie Madill98493dd2013-07-08 14:39:03 -04001693 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001694 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001695 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001696
1697 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001698 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001699 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001700 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001701
Jamie Madill98493dd2013-07-08 14:39:03 -04001702 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001703 {
1704 out << " && ";
1705 }
1706 }
1707
1708 out << ")";
1709
1710 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001711 }
1712 else
1713 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001714 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001715
1716 if (node->getOp() == EOpEqual)
1717 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001718 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001719 }
1720 else
1721 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001722 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001723 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001724 }
1725 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001726 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1727 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1728 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1729 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1730 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001731 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001732 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1733 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001734 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001735 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001736 if (node->getRight()->hasSideEffects())
1737 {
1738 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1739 return false;
1740 }
1741 else
1742 {
1743 outputTriplet(visit, "(", " || ", ")");
1744 return true;
1745 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001746 case EOpLogicalXor:
1747 mUsesXor = true;
1748 outputTriplet(visit, "xor(", ", ", ")");
1749 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001750 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001751 if (node->getRight()->hasSideEffects())
1752 {
1753 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1754 return false;
1755 }
1756 else
1757 {
1758 outputTriplet(visit, "(", " && ", ")");
1759 return true;
1760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761 default: UNREACHABLE();
1762 }
1763
1764 return true;
1765}
1766
1767bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1768{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769 switch (node->getOp())
1770 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001771 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1772 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1773 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1774 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1775 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1776 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1777 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001778 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1779 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1780 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1781 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1782 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1783 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1784 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1785 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1786 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1787 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1788 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1789 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1790 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1791 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1792 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1793 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1794 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1795 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1796 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1797 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1798 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001799 case EOpDFdx:
1800 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1801 {
1802 outputTriplet(visit, "(", "", ", 0.0)");
1803 }
1804 else
1805 {
1806 outputTriplet(visit, "ddx(", "", ")");
1807 }
1808 break;
1809 case EOpDFdy:
1810 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1811 {
1812 outputTriplet(visit, "(", "", ", 0.0)");
1813 }
1814 else
1815 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001816 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001817 }
1818 break;
1819 case EOpFwidth:
1820 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1821 {
1822 outputTriplet(visit, "(", "", ", 0.0)");
1823 }
1824 else
1825 {
1826 outputTriplet(visit, "fwidth(", "", ")");
1827 }
1828 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001829 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1830 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831 default: UNREACHABLE();
1832 }
1833
1834 return true;
1835}
1836
1837bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1838{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001839 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841 switch (node->getOp())
1842 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001843 case EOpSequence:
1844 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001845 if (mInsideFunction)
1846 {
Jamie Madill075edd82013-07-08 13:30:19 -04001847 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001848 out << "{\n";
1849 }
1850
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001851 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1852 {
Jamie Madill075edd82013-07-08 13:30:19 -04001853 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001854
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001855 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001856
1857 out << ";\n";
1858 }
1859
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001860 if (mInsideFunction)
1861 {
Jamie Madill075edd82013-07-08 13:30:19 -04001862 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001863 out << "}\n";
1864 }
1865
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001866 return false;
1867 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001868 case EOpDeclaration:
1869 if (visit == PreVisit)
1870 {
1871 TIntermSequence &sequence = node->getSequence();
1872 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001874 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001876 TStructure *structure = variable->getType().getStruct();
1877
1878 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001879 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001880 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001881 }
1882
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001883 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001884 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001885 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001886 {
1887 out << "static ";
1888 }
1889
Jamie Madill033dae62014-06-18 12:56:28 -04001890 out << TypeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001892 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001894 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001896 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001898 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001899 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001900 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001901 }
1902 else
1903 {
1904 (*sit)->traverse(this);
1905 }
1906
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001907 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001908 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001909 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001910 }
1911 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001913 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1914 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001915 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001916 }
1917 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 }
Jamie Madill033dae62014-06-18 12:56:28 -04001919 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001920 {
1921 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1922 {
1923 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1924
1925 if (symbol)
1926 {
1927 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1928 mReferencedVaryings[symbol->getSymbol()] = symbol;
1929 }
1930 else
1931 {
1932 (*sit)->traverse(this);
1933 }
1934 }
1935 }
1936
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937 return false;
1938 }
1939 else if (visit == InVisit)
1940 {
1941 out << ", ";
1942 }
1943 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001944 case EOpPrototype:
1945 if (visit == PreVisit)
1946 {
Jamie Madill033dae62014-06-18 12:56:28 -04001947 out << TypeString(node->getType()) << " " << Decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001948
1949 TIntermSequence &arguments = node->getSequence();
1950
1951 for (unsigned int i = 0; i < arguments.size(); i++)
1952 {
1953 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1954
1955 if (symbol)
1956 {
1957 out << argumentString(symbol);
1958
1959 if (i < arguments.size() - 1)
1960 {
1961 out << ", ";
1962 }
1963 }
1964 else UNREACHABLE();
1965 }
1966
1967 out << ");\n";
1968
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001969 // Also prototype the Lod0 variant if needed
1970 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1971 {
1972 mOutputLod0Function = true;
1973 node->traverse(this);
1974 mOutputLod0Function = false;
1975 }
1976
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001977 return false;
1978 }
1979 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001980 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981 case EOpFunction:
1982 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001983 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001984
Jamie Madill033dae62014-06-18 12:56:28 -04001985 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001986
1987 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001988 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001989 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001991 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992 {
Jamie Madill033dae62014-06-18 12:56:28 -04001993 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001994 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001995
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001996 TIntermSequence &sequence = node->getSequence();
1997 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1998
1999 for (unsigned int i = 0; i < arguments.size(); i++)
2000 {
2001 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2002
2003 if (symbol)
2004 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002005 TStructure *structure = symbol->getType().getStruct();
2006
2007 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002008 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002009 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002010 }
2011
2012 out << argumentString(symbol);
2013
2014 if (i < arguments.size() - 1)
2015 {
2016 out << ", ";
2017 }
2018 }
2019 else UNREACHABLE();
2020 }
2021
2022 out << ")\n"
2023 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002024
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002025 if (sequence.size() > 1)
2026 {
2027 mInsideFunction = true;
2028 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002029 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002030 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002031
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002032 out << "}\n";
2033
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002034 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2035 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002036 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002037 {
2038 mOutputLod0Function = true;
2039 node->traverse(this);
2040 mOutputLod0Function = false;
2041 }
2042 }
2043
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002044 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002045 }
2046 break;
2047 case EOpFunctionCall:
2048 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002049 TString name = TFunction::unmangleName(node->getName());
2050 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002051 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002052
2053 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002054 {
Jamie Madill033dae62014-06-18 12:56:28 -04002055 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002056 }
2057 else
2058 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002059 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2060
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002061 TextureFunction textureFunction;
2062 textureFunction.sampler = samplerType;
2063 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002064 textureFunction.method = TextureFunction::IMPLICIT;
2065 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002066 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002067
2068 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002069 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002070 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002071 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002072 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002073 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002074 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002075 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002076 }
Nicolas Capens46485082014-04-15 13:12:50 -04002077 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2078 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002079 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002080 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002081 }
Nicolas Capens46485082014-04-15 13:12:50 -04002082 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002083 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002084 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002085 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002086 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002087 else if (name == "textureSize")
2088 {
2089 textureFunction.method = TextureFunction::SIZE;
2090 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002091 else if (name == "textureOffset")
2092 {
2093 textureFunction.method = TextureFunction::IMPLICIT;
2094 textureFunction.offset = true;
2095 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002096 else if (name == "textureProjOffset")
2097 {
2098 textureFunction.method = TextureFunction::IMPLICIT;
2099 textureFunction.offset = true;
2100 textureFunction.proj = true;
2101 }
2102 else if (name == "textureLodOffset")
2103 {
2104 textureFunction.method = TextureFunction::LOD;
2105 textureFunction.offset = true;
2106 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002107 else if (name == "textureProjLodOffset")
2108 {
2109 textureFunction.method = TextureFunction::LOD;
2110 textureFunction.proj = true;
2111 textureFunction.offset = true;
2112 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002113 else if (name == "texelFetch")
2114 {
2115 textureFunction.method = TextureFunction::FETCH;
2116 }
2117 else if (name == "texelFetchOffset")
2118 {
2119 textureFunction.method = TextureFunction::FETCH;
2120 textureFunction.offset = true;
2121 }
Nicolas Capens46485082014-04-15 13:12:50 -04002122 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002123 {
2124 textureFunction.method = TextureFunction::GRAD;
2125 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002126 else if (name == "textureGradOffset")
2127 {
2128 textureFunction.method = TextureFunction::GRAD;
2129 textureFunction.offset = true;
2130 }
Nicolas Capens46485082014-04-15 13:12:50 -04002131 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002132 {
2133 textureFunction.method = TextureFunction::GRAD;
2134 textureFunction.proj = true;
2135 }
2136 else if (name == "textureProjGradOffset")
2137 {
2138 textureFunction.method = TextureFunction::GRAD;
2139 textureFunction.proj = true;
2140 textureFunction.offset = true;
2141 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002142 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002143
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002144 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002145 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002146 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2147
2148 if (textureFunction.offset)
2149 {
2150 mandatoryArgumentCount++;
2151 }
2152
2153 bool bias = (arguments.size() > mandatoryArgumentCount); // Bias argument is optional
2154
Jamie Madill183bde52014-07-02 15:31:19 -04002155 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002156 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002157 if (bias)
2158 {
2159 textureFunction.method = TextureFunction::LOD0BIAS;
2160 }
2161 else
2162 {
2163 textureFunction.method = TextureFunction::LOD0;
2164 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002165 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002166 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002167 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002168 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002169 }
2170 }
2171
2172 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002173
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002174 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002176
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002177 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2178 {
2179 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2180 {
2181 out << "texture_";
2182 (*arg)->traverse(this);
2183 out << ", sampler_";
2184 }
2185
2186 (*arg)->traverse(this);
2187
2188 if (arg < arguments.end() - 1)
2189 {
2190 out << ", ";
2191 }
2192 }
2193
2194 out << ")";
2195
2196 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002197 }
2198 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002199 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
2200 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", &node->getSequence()); break;
2201 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", &node->getSequence()); break;
2202 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", &node->getSequence()); break;
2203 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", &node->getSequence()); break;
2204 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", &node->getSequence()); break;
2205 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", &node->getSequence()); break;
2206 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", &node->getSequence()); break;
2207 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", &node->getSequence()); break;
2208 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", &node->getSequence()); break;
2209 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", &node->getSequence()); break;
2210 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", &node->getSequence()); break;
2211 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", &node->getSequence()); break;
2212 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", &node->getSequence()); break;
2213 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", &node->getSequence()); break;
2214 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", &node->getSequence()); break;
2215 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", &node->getSequence()); break;
2216 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", &node->getSequence()); break;
2217 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", &node->getSequence()); break;
2218 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", &node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002219 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002220 {
Jamie Madill033dae62014-06-18 12:56:28 -04002221 const TString &structName = StructNameString(*node->getType().getStruct());
Jamie Madill8daaba12014-06-13 10:04:33 -04002222 mStructureHLSL->addConstructor(node->getType(), structName, &node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002223 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2224 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002225 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002226 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2227 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2228 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2229 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2230 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2231 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002232 case EOpMod:
2233 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002234 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002235 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2236 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2237 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002238 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002239 case 11: mUsesMod1 = true; break;
2240 case 22: mUsesMod2v = true; break;
2241 case 21: mUsesMod2f = true; break;
2242 case 33: mUsesMod3v = true; break;
2243 case 31: mUsesMod3f = true; break;
2244 case 44: mUsesMod4v = true; break;
2245 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002246 default: UNREACHABLE();
2247 }
2248
2249 outputTriplet(visit, "mod(", ", ", ")");
2250 }
2251 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002252 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002254 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002255 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2256 {
2257 case 1: mUsesAtan2_1 = true; break;
2258 case 2: mUsesAtan2_2 = true; break;
2259 case 3: mUsesAtan2_3 = true; break;
2260 case 4: mUsesAtan2_4 = true; break;
2261 default: UNREACHABLE();
2262 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002263 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 break;
2265 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2266 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2267 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2268 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2269 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2270 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2271 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2272 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2273 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002274 case EOpFaceForward:
2275 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002276 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002277 {
2278 case 1: mUsesFaceforward1 = true; break;
2279 case 2: mUsesFaceforward2 = true; break;
2280 case 3: mUsesFaceforward3 = true; break;
2281 case 4: mUsesFaceforward4 = true; break;
2282 default: UNREACHABLE();
2283 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002284
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002285 outputTriplet(visit, "faceforward(", ", ", ")");
2286 }
2287 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2289 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2290 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291 default: UNREACHABLE();
2292 }
2293
2294 return true;
2295}
2296
2297bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2298{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002299 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002301 if (node->usesTernaryOperator())
2302 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002303 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002304 }
2305 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002307 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002308
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002309 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002310
2311 node->getCondition()->traverse(this);
2312
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002313 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002314
Jamie Madill075edd82013-07-08 13:30:19 -04002315 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002316 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002318 bool discard = false;
2319
daniel@transgaming.combb885322010-04-15 20:45:24 +00002320 if (node->getTrueBlock())
2321 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002322 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002323
2324 // Detect true discard
2325 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002326 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327
Jamie Madill075edd82013-07-08 13:30:19 -04002328 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002329 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002330
2331 if (node->getFalseBlock())
2332 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002333 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002334
Jamie Madill075edd82013-07-08 13:30:19 -04002335 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002336 out << "{\n";
2337
Jamie Madill075edd82013-07-08 13:30:19 -04002338 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002339 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002340
Jamie Madill075edd82013-07-08 13:30:19 -04002341 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002342 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002343
2344 // Detect false discard
2345 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2346 }
2347
2348 // ANGLE issue 486: Detect problematic conditional discard
2349 if (discard && FindSideEffectRewriting::search(node))
2350 {
2351 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002352 }
2353 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002354
2355 return false;
2356}
2357
2358void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2359{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002360 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361}
2362
2363bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2364{
Nicolas Capens655fe362014-04-11 13:12:34 -04002365 mNestedLoopDepth++;
2366
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002367 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2368
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002369 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002370 {
2371 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2372 }
2373
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002374 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002375 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002376 if (handleExcessiveLoop(node))
2377 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002378 mInsideDiscontinuousLoop = wasDiscontinuous;
2379 mNestedLoopDepth--;
2380
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002381 return false;
2382 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002383 }
2384
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002385 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386
alokp@chromium.org52813552010-11-16 18:36:09 +00002387 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002389 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002390
Jamie Madill075edd82013-07-08 13:30:19 -04002391 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002392 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393 }
2394 else
2395 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002396 out << "{for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002397
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398 if (node->getInit())
2399 {
2400 node->getInit()->traverse(this);
2401 }
2402
2403 out << "; ";
2404
alokp@chromium.org52813552010-11-16 18:36:09 +00002405 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002407 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 }
2409
2410 out << "; ";
2411
alokp@chromium.org52813552010-11-16 18:36:09 +00002412 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002414 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415 }
2416
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002417 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002418
Jamie Madill075edd82013-07-08 13:30:19 -04002419 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002420 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 }
2422
2423 if (node->getBody())
2424 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002425 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 }
2427
Jamie Madill075edd82013-07-08 13:30:19 -04002428 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002429 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430
alokp@chromium.org52813552010-11-16 18:36:09 +00002431 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 {
Jamie Madill075edd82013-07-08 13:30:19 -04002433 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 out << "while(\n";
2435
alokp@chromium.org52813552010-11-16 18:36:09 +00002436 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002437
daniel@transgaming.com73536982012-03-21 20:45:49 +00002438 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439 }
2440
daniel@transgaming.com73536982012-03-21 20:45:49 +00002441 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002443 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002444 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002445
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446 return false;
2447}
2448
2449bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2450{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002451 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452
2453 switch (node->getFlowOp())
2454 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002455 case EOpKill:
2456 outputTriplet(visit, "discard;\n", "", "");
2457 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002458 case EOpBreak:
2459 if (visit == PreVisit)
2460 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002461 if (mNestedLoopDepth > 1)
2462 {
2463 mUsesNestedBreak = true;
2464 }
2465
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002466 if (mExcessiveLoopIndex)
2467 {
2468 out << "{Break";
2469 mExcessiveLoopIndex->traverse(this);
2470 out << " = true; break;}\n";
2471 }
2472 else
2473 {
2474 out << "break;\n";
2475 }
2476 }
2477 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002478 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479 case EOpReturn:
2480 if (visit == PreVisit)
2481 {
2482 if (node->getExpression())
2483 {
2484 out << "return ";
2485 }
2486 else
2487 {
2488 out << "return;\n";
2489 }
2490 }
2491 else if (visit == PostVisit)
2492 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002493 if (node->getExpression())
2494 {
2495 out << ";\n";
2496 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 }
2498 break;
2499 default: UNREACHABLE();
2500 }
2501
2502 return true;
2503}
2504
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002505void OutputHLSL::traverseStatements(TIntermNode *node)
2506{
2507 if (isSingleStatement(node))
2508 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002509 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002510 }
2511
2512 node->traverse(this);
2513}
2514
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002515bool OutputHLSL::isSingleStatement(TIntermNode *node)
2516{
2517 TIntermAggregate *aggregate = node->getAsAggregate();
2518
2519 if (aggregate)
2520 {
2521 if (aggregate->getOp() == EOpSequence)
2522 {
2523 return false;
2524 }
2525 else
2526 {
2527 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2528 {
2529 if (!isSingleStatement(*sit))
2530 {
2531 return false;
2532 }
2533 }
2534
2535 return true;
2536 }
2537 }
2538
2539 return true;
2540}
2541
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002542// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2543// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002544bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2545{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002546 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002547 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002548
2549 // Parse loops of the form:
2550 // for(int index = initial; index [comparator] limit; index += increment)
2551 TIntermSymbol *index = NULL;
2552 TOperator comparator = EOpNull;
2553 int initial = 0;
2554 int limit = 0;
2555 int increment = 0;
2556
2557 // Parse index name and intial value
2558 if (node->getInit())
2559 {
2560 TIntermAggregate *init = node->getInit()->getAsAggregate();
2561
2562 if (init)
2563 {
2564 TIntermSequence &sequence = init->getSequence();
2565 TIntermTyped *variable = sequence[0]->getAsTyped();
2566
2567 if (variable && variable->getQualifier() == EvqTemporary)
2568 {
2569 TIntermBinary *assign = variable->getAsBinaryNode();
2570
2571 if (assign->getOp() == EOpInitialize)
2572 {
2573 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2574 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2575
2576 if (symbol && constant)
2577 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002578 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002579 {
2580 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002581 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002582 }
2583 }
2584 }
2585 }
2586 }
2587 }
2588
2589 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002590 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002591 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002592 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002593
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002594 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2595 {
2596 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2597
2598 if (constant)
2599 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002600 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002601 {
2602 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002603 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002604 }
2605 }
2606 }
2607 }
2608
2609 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002610 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002611 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002612 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2613 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002614
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002615 if (binaryTerminal)
2616 {
2617 TOperator op = binaryTerminal->getOp();
2618 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2619
2620 if (constant)
2621 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002622 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002623 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002624 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002625
2626 switch (op)
2627 {
2628 case EOpAddAssign: increment = value; break;
2629 case EOpSubAssign: increment = -value; break;
2630 default: UNIMPLEMENTED();
2631 }
2632 }
2633 }
2634 }
2635 else if (unaryTerminal)
2636 {
2637 TOperator op = unaryTerminal->getOp();
2638
2639 switch (op)
2640 {
2641 case EOpPostIncrement: increment = 1; break;
2642 case EOpPostDecrement: increment = -1; break;
2643 case EOpPreIncrement: increment = 1; break;
2644 case EOpPreDecrement: increment = -1; break;
2645 default: UNIMPLEMENTED();
2646 }
2647 }
2648 }
2649
2650 if (index != NULL && comparator != EOpNull && increment != 0)
2651 {
2652 if (comparator == EOpLessThanEqual)
2653 {
2654 comparator = EOpLessThan;
2655 limit += 1;
2656 }
2657
2658 if (comparator == EOpLessThan)
2659 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002660 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002661
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002662 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002663 {
2664 return false; // Not an excessive loop
2665 }
2666
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002667 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2668 mExcessiveLoopIndex = index;
2669
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002670 out << "{int ";
2671 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002672 out << ";\n"
2673 "bool Break";
2674 index->traverse(this);
2675 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002676
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002677 bool firstLoopFragment = true;
2678
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002679 while (iterations > 0)
2680 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002681 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002682
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002683 if (!firstLoopFragment)
2684 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002685 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002686 index->traverse(this);
2687 out << ") {\n";
2688 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002689
2690 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2691 {
2692 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2693 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002694
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002695 // for(int index = initial; index < clampedLimit; index += increment)
2696
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002697 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002698 index->traverse(this);
2699 out << " = ";
2700 out << initial;
2701
2702 out << "; ";
2703 index->traverse(this);
2704 out << " < ";
2705 out << clampedLimit;
2706
2707 out << "; ";
2708 index->traverse(this);
2709 out << " += ";
2710 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002711 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002712
Jamie Madill075edd82013-07-08 13:30:19 -04002713 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002714 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002715
2716 if (node->getBody())
2717 {
2718 node->getBody()->traverse(this);
2719 }
2720
Jamie Madill075edd82013-07-08 13:30:19 -04002721 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002722 out << ";}\n";
2723
2724 if (!firstLoopFragment)
2725 {
2726 out << "}\n";
2727 }
2728
2729 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002730
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002731 initial += MAX_LOOP_ITERATIONS * increment;
2732 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002733 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002734
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002735 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002736
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002737 mExcessiveLoopIndex = restoreIndex;
2738
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002739 return true;
2740 }
2741 else UNIMPLEMENTED();
2742 }
2743
2744 return false; // Not handled as an excessive loop
2745}
2746
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002747void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002749 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002750
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002751 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002752 {
2753 out << preString;
2754 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002755 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002756 {
2757 out << inString;
2758 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002759 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760 {
2761 out << postString;
2762 }
2763}
2764
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002765void OutputHLSL::outputLineDirective(int line)
2766{
2767 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2768 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002769 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002770 mBody << "#line " << line;
2771
2772 if (mContext.sourcePath)
2773 {
2774 mBody << " \"" << mContext.sourcePath << "\"";
2775 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002776
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002777 mBody << "\n";
2778 }
2779}
2780
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002781TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2782{
2783 TQualifier qualifier = symbol->getQualifier();
2784 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002785 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002786
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002787 if (name.empty()) // HLSL demands named arguments, also for prototypes
2788 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002789 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002790 }
2791 else
2792 {
Jamie Madill033dae62014-06-18 12:56:28 -04002793 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002794 }
2795
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002796 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2797 {
Jamie Madill033dae62014-06-18 12:56:28 -04002798 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002799 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002800 }
2801
Jamie Madill033dae62014-06-18 12:56:28 -04002802 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002803}
2804
2805TString OutputHLSL::initializer(const TType &type)
2806{
2807 TString string;
2808
Jamie Madill94bf7f22013-07-08 13:31:15 -04002809 size_t size = type.getObjectSize();
2810 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002811 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002812 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813
Jamie Madill94bf7f22013-07-08 13:31:15 -04002814 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 {
2816 string += ", ";
2817 }
2818 }
2819
daniel@transgaming.comead23042010-04-29 03:35:36 +00002820 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002821}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002822
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002823void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2824{
2825 TInfoSinkBase &out = mBody;
2826
2827 if (visit == PreVisit)
2828 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002829 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002830
2831 out << name + "(";
2832 }
2833 else if (visit == InVisit)
2834 {
2835 out << ", ";
2836 }
2837 else if (visit == PostVisit)
2838 {
2839 out << ")";
2840 }
2841}
2842
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002843const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2844{
2845 TInfoSinkBase &out = mBody;
2846
Jamie Madill98493dd2013-07-08 14:39:03 -04002847 const TStructure* structure = type.getStruct();
2848 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002849 {
Jamie Madill033dae62014-06-18 12:56:28 -04002850 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002851
Jamie Madill98493dd2013-07-08 14:39:03 -04002852 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002853
Jamie Madill98493dd2013-07-08 14:39:03 -04002854 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002855 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002856 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002857 constUnion = writeConstantUnion(*fieldType, constUnion);
2858
Jamie Madill98493dd2013-07-08 14:39:03 -04002859 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002860 {
2861 out << ", ";
2862 }
2863 }
2864
2865 out << ")";
2866 }
2867 else
2868 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002869 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002870 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002871
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002872 if (writeType)
2873 {
Jamie Madill033dae62014-06-18 12:56:28 -04002874 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002875 }
2876
Jamie Madill94bf7f22013-07-08 13:31:15 -04002877 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002878 {
2879 switch (constUnion->getType())
2880 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002881 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002882 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002883 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002884 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002885 default: UNREACHABLE();
2886 }
2887
2888 if (i != size - 1)
2889 {
2890 out << ", ";
2891 }
2892 }
2893
2894 if (writeType)
2895 {
2896 out << ")";
2897 }
2898 }
2899
2900 return constUnion;
2901}
2902
Jamie Madill77f74852014-07-08 15:02:34 -04002903class DeclareVaryingTraverser : public GetVariableTraverser<Varying>
Jamie Madill47fdd132013-08-30 13:21:04 -04002904{
Jamie Madill77f74852014-07-08 15:02:34 -04002905 public:
2906 DeclareVaryingTraverser(std::vector<Varying> *output,
2907 InterpolationType interpolation)
2908 : GetVariableTraverser(output),
2909 mInterpolation(interpolation)
2910 {}
Jamie Madill47fdd132013-08-30 13:21:04 -04002911
Jamie Madill77f74852014-07-08 15:02:34 -04002912 private:
2913 void visitVariable(Varying *varying)
Jamie Madill47fdd132013-08-30 13:21:04 -04002914 {
Jamie Madill77f74852014-07-08 15:02:34 -04002915 varying->interpolation = mInterpolation;
Jamie Madill47fdd132013-08-30 13:21:04 -04002916 }
Jamie Madill47fdd132013-08-30 13:21:04 -04002917
Jamie Madill77f74852014-07-08 15:02:34 -04002918 InterpolationType mInterpolation;
2919};
Jamie Madill28167c62013-08-30 13:21:10 -04002920
Jamie Madill77f74852014-07-08 15:02:34 -04002921void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier,
2922 const TString &name, std::vector<Varying> &fieldsOut)
2923{
2924 DeclareVaryingTraverser traverser(&fieldsOut, GetInterpolationType(baseTypeQualifier));
2925 traverser.traverse(type, name);
Jamie Madill47fdd132013-08-30 13:21:04 -04002926}
2927
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002928}