blob: 60201f63314a25e1f29c7fc9c1ca0d8973fe7daf [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
1489bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1490{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001491 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001492
Jamie Madill570e04d2013-06-21 09:15:33 -04001493 // Handle accessing std140 structs by value
1494 if (mFlaggedStructMappedNames.count(node) > 0)
1495 {
1496 out << mFlaggedStructMappedNames[node];
1497 return false;
1498 }
1499
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001500 switch (node->getOp())
1501 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001502 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001503 case EOpInitialize:
1504 if (visit == PreVisit)
1505 {
1506 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1507 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1508 // new variable is created before the assignment is evaluated), so we need to convert
1509 // this to "float t = x, x = t;".
1510
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001511 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1512 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001513
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001514 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1515 expression->traverse(&searchSymbol);
1516 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001517
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001518 if (sameSymbol)
1519 {
1520 // Type already printed
1521 out << "t" + str(mUniqueIndex) + " = ";
1522 expression->traverse(this);
1523 out << ", ";
1524 symbolNode->traverse(this);
1525 out << " = t" + str(mUniqueIndex);
1526
1527 mUniqueIndex++;
1528 return false;
1529 }
1530 }
1531 else if (visit == InVisit)
1532 {
1533 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001534 }
1535 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001536 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1537 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1538 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1539 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1540 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1541 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001542 if (visit == PreVisit)
1543 {
1544 out << "(";
1545 }
1546 else if (visit == InVisit)
1547 {
1548 out << " = mul(";
1549 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001550 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001551 }
1552 else
1553 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001554 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001555 }
1556 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001557 case EOpMatrixTimesMatrixAssign:
1558 if (visit == PreVisit)
1559 {
1560 out << "(";
1561 }
1562 else if (visit == InVisit)
1563 {
1564 out << " = mul(";
1565 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001566 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001567 }
1568 else
1569 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001570 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001571 }
1572 break;
1573 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001574 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001575 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001576 const TType& leftType = node->getLeft()->getType();
1577 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001578 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001579 if (visit == PreVisit)
1580 {
1581 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1582 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001583 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001584 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001585 return false;
1586 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001587 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001588 else
1589 {
1590 outputTriplet(visit, "", "[", "]");
1591 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001592 }
1593 break;
1594 case EOpIndexIndirect:
1595 // We do not currently support indirect references to interface blocks
1596 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1597 outputTriplet(visit, "", "[", "]");
1598 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001599 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001600 if (visit == InVisit)
1601 {
1602 const TStructure* structure = node->getLeft()->getType().getStruct();
1603 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1604 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001605 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001606
1607 return false;
1608 }
1609 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001610 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001611 if (visit == InVisit)
1612 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001613 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1614 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1615 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001616 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001617
1618 return false;
1619 }
1620 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001621 case EOpVectorSwizzle:
1622 if (visit == InVisit)
1623 {
1624 out << ".";
1625
1626 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1627
1628 if (swizzle)
1629 {
1630 TIntermSequence &sequence = swizzle->getSequence();
1631
1632 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1633 {
1634 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1635
1636 if (element)
1637 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001638 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639
1640 switch (i)
1641 {
1642 case 0: out << "x"; break;
1643 case 1: out << "y"; break;
1644 case 2: out << "z"; break;
1645 case 3: out << "w"; break;
1646 default: UNREACHABLE();
1647 }
1648 }
1649 else UNREACHABLE();
1650 }
1651 }
1652 else UNREACHABLE();
1653
1654 return false; // Fully processed
1655 }
1656 break;
1657 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1658 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1659 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1660 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001661 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001662 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001663 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001664 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001665 if (node->getOp() == EOpEqual)
1666 {
1667 outputTriplet(visit, "(", " == ", ")");
1668 }
1669 else
1670 {
1671 outputTriplet(visit, "(", " != ", ")");
1672 }
1673 }
1674 else if (node->getLeft()->getBasicType() == EbtStruct)
1675 {
1676 if (node->getOp() == EOpEqual)
1677 {
1678 out << "(";
1679 }
1680 else
1681 {
1682 out << "!(";
1683 }
1684
Jamie Madill98493dd2013-07-08 14:39:03 -04001685 const TStructure &structure = *node->getLeft()->getType().getStruct();
1686 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001687
Jamie Madill98493dd2013-07-08 14:39:03 -04001688 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001689 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001690 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001691
1692 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001693 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001694 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001695 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001696
Jamie Madill98493dd2013-07-08 14:39:03 -04001697 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001698 {
1699 out << " && ";
1700 }
1701 }
1702
1703 out << ")";
1704
1705 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001706 }
1707 else
1708 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001709 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001710
1711 if (node->getOp() == EOpEqual)
1712 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001713 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001714 }
1715 else
1716 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001717 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001718 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001719 }
1720 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1722 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1723 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1724 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1725 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001726 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001727 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1728 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001729 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001730 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001731 if (node->getRight()->hasSideEffects())
1732 {
1733 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1734 return false;
1735 }
1736 else
1737 {
1738 outputTriplet(visit, "(", " || ", ")");
1739 return true;
1740 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001741 case EOpLogicalXor:
1742 mUsesXor = true;
1743 outputTriplet(visit, "xor(", ", ", ")");
1744 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001745 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001746 if (node->getRight()->hasSideEffects())
1747 {
1748 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1749 return false;
1750 }
1751 else
1752 {
1753 outputTriplet(visit, "(", " && ", ")");
1754 return true;
1755 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 default: UNREACHABLE();
1757 }
1758
1759 return true;
1760}
1761
1762bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1763{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 switch (node->getOp())
1765 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001766 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1767 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1768 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1769 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1770 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1771 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1772 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001773 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1774 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1775 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1776 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1777 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1778 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1779 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1780 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1781 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1782 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1783 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1784 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1785 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1786 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1787 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1788 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1789 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1790 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1791 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1792 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1793 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001794 case EOpDFdx:
1795 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1796 {
1797 outputTriplet(visit, "(", "", ", 0.0)");
1798 }
1799 else
1800 {
1801 outputTriplet(visit, "ddx(", "", ")");
1802 }
1803 break;
1804 case EOpDFdy:
1805 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1806 {
1807 outputTriplet(visit, "(", "", ", 0.0)");
1808 }
1809 else
1810 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001811 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001812 }
1813 break;
1814 case EOpFwidth:
1815 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1816 {
1817 outputTriplet(visit, "(", "", ", 0.0)");
1818 }
1819 else
1820 {
1821 outputTriplet(visit, "fwidth(", "", ")");
1822 }
1823 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001824 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1825 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001826 default: UNREACHABLE();
1827 }
1828
1829 return true;
1830}
1831
1832bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1833{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001834 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001835
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836 switch (node->getOp())
1837 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001838 case EOpSequence:
1839 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001840 if (mInsideFunction)
1841 {
Jamie Madill075edd82013-07-08 13:30:19 -04001842 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001843 out << "{\n";
1844 }
1845
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001846 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1847 {
Jamie Madill075edd82013-07-08 13:30:19 -04001848 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001849
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001850 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001851
1852 out << ";\n";
1853 }
1854
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001855 if (mInsideFunction)
1856 {
Jamie Madill075edd82013-07-08 13:30:19 -04001857 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001858 out << "}\n";
1859 }
1860
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001861 return false;
1862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001863 case EOpDeclaration:
1864 if (visit == PreVisit)
1865 {
1866 TIntermSequence &sequence = node->getSequence();
1867 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001868
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001869 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001871 TStructure *structure = variable->getType().getStruct();
1872
1873 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001874 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001875 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001876 }
1877
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001878 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001880 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001881 {
1882 out << "static ";
1883 }
1884
Jamie Madill033dae62014-06-18 12:56:28 -04001885 out << TypeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001886
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001887 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001889 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001890
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001891 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001893 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001894 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001895 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001896 }
1897 else
1898 {
1899 (*sit)->traverse(this);
1900 }
1901
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001902 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001903 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001904 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905 }
1906 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001908 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1909 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001910 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001911 }
1912 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913 }
Jamie Madill033dae62014-06-18 12:56:28 -04001914 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001915 {
1916 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1917 {
1918 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1919
1920 if (symbol)
1921 {
1922 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1923 mReferencedVaryings[symbol->getSymbol()] = symbol;
1924 }
1925 else
1926 {
1927 (*sit)->traverse(this);
1928 }
1929 }
1930 }
1931
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001932 return false;
1933 }
1934 else if (visit == InVisit)
1935 {
1936 out << ", ";
1937 }
1938 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001939 case EOpPrototype:
1940 if (visit == PreVisit)
1941 {
Jamie Madill033dae62014-06-18 12:56:28 -04001942 out << TypeString(node->getType()) << " " << Decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001943
1944 TIntermSequence &arguments = node->getSequence();
1945
1946 for (unsigned int i = 0; i < arguments.size(); i++)
1947 {
1948 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1949
1950 if (symbol)
1951 {
1952 out << argumentString(symbol);
1953
1954 if (i < arguments.size() - 1)
1955 {
1956 out << ", ";
1957 }
1958 }
1959 else UNREACHABLE();
1960 }
1961
1962 out << ");\n";
1963
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001964 // Also prototype the Lod0 variant if needed
1965 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1966 {
1967 mOutputLod0Function = true;
1968 node->traverse(this);
1969 mOutputLod0Function = false;
1970 }
1971
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001972 return false;
1973 }
1974 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001975 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001976 case EOpFunction:
1977 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001978 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001979
Jamie Madill033dae62014-06-18 12:56:28 -04001980 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001981
1982 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001983 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001984 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001985 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001986 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987 {
Jamie Madill033dae62014-06-18 12:56:28 -04001988 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001989 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001990
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001991 TIntermSequence &sequence = node->getSequence();
1992 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1993
1994 for (unsigned int i = 0; i < arguments.size(); i++)
1995 {
1996 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1997
1998 if (symbol)
1999 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002000 TStructure *structure = symbol->getType().getStruct();
2001
2002 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002003 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002004 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002005 }
2006
2007 out << argumentString(symbol);
2008
2009 if (i < arguments.size() - 1)
2010 {
2011 out << ", ";
2012 }
2013 }
2014 else UNREACHABLE();
2015 }
2016
2017 out << ")\n"
2018 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002019
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002020 if (sequence.size() > 1)
2021 {
2022 mInsideFunction = true;
2023 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002024 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002026
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002027 out << "}\n";
2028
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002029 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2030 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002031 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002032 {
2033 mOutputLod0Function = true;
2034 node->traverse(this);
2035 mOutputLod0Function = false;
2036 }
2037 }
2038
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002039 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040 }
2041 break;
2042 case EOpFunctionCall:
2043 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002044 TString name = TFunction::unmangleName(node->getName());
2045 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002046 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002047
2048 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049 {
Jamie Madill033dae62014-06-18 12:56:28 -04002050 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051 }
2052 else
2053 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002054 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2055
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002056 TextureFunction textureFunction;
2057 textureFunction.sampler = samplerType;
2058 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002059 textureFunction.method = TextureFunction::IMPLICIT;
2060 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002061 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002062
2063 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002064 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002065 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002066 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002067 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002068 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002069 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002070 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002071 }
Nicolas Capens46485082014-04-15 13:12:50 -04002072 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2073 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002074 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002075 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002076 }
Nicolas Capens46485082014-04-15 13:12:50 -04002077 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002078 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002079 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002080 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002081 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002082 else if (name == "textureSize")
2083 {
2084 textureFunction.method = TextureFunction::SIZE;
2085 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002086 else if (name == "textureOffset")
2087 {
2088 textureFunction.method = TextureFunction::IMPLICIT;
2089 textureFunction.offset = true;
2090 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002091 else if (name == "textureProjOffset")
2092 {
2093 textureFunction.method = TextureFunction::IMPLICIT;
2094 textureFunction.offset = true;
2095 textureFunction.proj = true;
2096 }
2097 else if (name == "textureLodOffset")
2098 {
2099 textureFunction.method = TextureFunction::LOD;
2100 textureFunction.offset = true;
2101 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002102 else if (name == "textureProjLodOffset")
2103 {
2104 textureFunction.method = TextureFunction::LOD;
2105 textureFunction.proj = true;
2106 textureFunction.offset = true;
2107 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002108 else if (name == "texelFetch")
2109 {
2110 textureFunction.method = TextureFunction::FETCH;
2111 }
2112 else if (name == "texelFetchOffset")
2113 {
2114 textureFunction.method = TextureFunction::FETCH;
2115 textureFunction.offset = true;
2116 }
Nicolas Capens46485082014-04-15 13:12:50 -04002117 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002118 {
2119 textureFunction.method = TextureFunction::GRAD;
2120 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002121 else if (name == "textureGradOffset")
2122 {
2123 textureFunction.method = TextureFunction::GRAD;
2124 textureFunction.offset = true;
2125 }
Nicolas Capens46485082014-04-15 13:12:50 -04002126 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002127 {
2128 textureFunction.method = TextureFunction::GRAD;
2129 textureFunction.proj = true;
2130 }
2131 else if (name == "textureProjGradOffset")
2132 {
2133 textureFunction.method = TextureFunction::GRAD;
2134 textureFunction.proj = true;
2135 textureFunction.offset = true;
2136 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002137 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002138
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002139 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002140 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002141 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2142
2143 if (textureFunction.offset)
2144 {
2145 mandatoryArgumentCount++;
2146 }
2147
2148 bool bias = (arguments.size() > mandatoryArgumentCount); // Bias argument is optional
2149
Jamie Madill183bde52014-07-02 15:31:19 -04002150 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002151 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002152 if (bias)
2153 {
2154 textureFunction.method = TextureFunction::LOD0BIAS;
2155 }
2156 else
2157 {
2158 textureFunction.method = TextureFunction::LOD0;
2159 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002160 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002161 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002162 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002163 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002164 }
2165 }
2166
2167 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002168
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002169 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002171
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002172 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2173 {
2174 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2175 {
2176 out << "texture_";
2177 (*arg)->traverse(this);
2178 out << ", sampler_";
2179 }
2180
2181 (*arg)->traverse(this);
2182
2183 if (arg < arguments.end() - 1)
2184 {
2185 out << ", ";
2186 }
2187 }
2188
2189 out << ")";
2190
2191 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192 }
2193 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002194 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
2195 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", &node->getSequence()); break;
2196 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", &node->getSequence()); break;
2197 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", &node->getSequence()); break;
2198 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", &node->getSequence()); break;
2199 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", &node->getSequence()); break;
2200 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", &node->getSequence()); break;
2201 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", &node->getSequence()); break;
2202 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", &node->getSequence()); break;
2203 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", &node->getSequence()); break;
2204 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", &node->getSequence()); break;
2205 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", &node->getSequence()); break;
2206 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", &node->getSequence()); break;
2207 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", &node->getSequence()); break;
2208 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", &node->getSequence()); break;
2209 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", &node->getSequence()); break;
2210 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", &node->getSequence()); break;
2211 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", &node->getSequence()); break;
2212 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", &node->getSequence()); break;
2213 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", &node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002214 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002215 {
Jamie Madill033dae62014-06-18 12:56:28 -04002216 const TString &structName = StructNameString(*node->getType().getStruct());
Jamie Madill8daaba12014-06-13 10:04:33 -04002217 mStructureHLSL->addConstructor(node->getType(), structName, &node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002218 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2219 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002220 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002221 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2222 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2223 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2224 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2225 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2226 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002227 case EOpMod:
2228 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002229 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002230 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2231 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2232 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002233 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002234 case 11: mUsesMod1 = true; break;
2235 case 22: mUsesMod2v = true; break;
2236 case 21: mUsesMod2f = true; break;
2237 case 33: mUsesMod3v = true; break;
2238 case 31: mUsesMod3f = true; break;
2239 case 44: mUsesMod4v = true; break;
2240 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002241 default: UNREACHABLE();
2242 }
2243
2244 outputTriplet(visit, "mod(", ", ", ")");
2245 }
2246 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002247 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002249 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002250 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2251 {
2252 case 1: mUsesAtan2_1 = true; break;
2253 case 2: mUsesAtan2_2 = true; break;
2254 case 3: mUsesAtan2_3 = true; break;
2255 case 4: mUsesAtan2_4 = true; break;
2256 default: UNREACHABLE();
2257 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002258 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 break;
2260 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2261 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2262 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2263 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2264 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2265 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2266 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2267 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2268 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002269 case EOpFaceForward:
2270 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002271 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002272 {
2273 case 1: mUsesFaceforward1 = true; break;
2274 case 2: mUsesFaceforward2 = true; break;
2275 case 3: mUsesFaceforward3 = true; break;
2276 case 4: mUsesFaceforward4 = true; break;
2277 default: UNREACHABLE();
2278 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002279
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002280 outputTriplet(visit, "faceforward(", ", ", ")");
2281 }
2282 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2284 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2285 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286 default: UNREACHABLE();
2287 }
2288
2289 return true;
2290}
2291
2292bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2293{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002294 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002296 if (node->usesTernaryOperator())
2297 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002298 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002299 }
2300 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002302 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002303
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002304 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002305
2306 node->getCondition()->traverse(this);
2307
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002308 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002309
Jamie Madill075edd82013-07-08 13:30:19 -04002310 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002311 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002313 bool discard = false;
2314
daniel@transgaming.combb885322010-04-15 20:45:24 +00002315 if (node->getTrueBlock())
2316 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002317 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002318
2319 // Detect true discard
2320 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002321 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322
Jamie Madill075edd82013-07-08 13:30:19 -04002323 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002324 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002325
2326 if (node->getFalseBlock())
2327 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002328 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002329
Jamie Madill075edd82013-07-08 13:30:19 -04002330 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002331 out << "{\n";
2332
Jamie Madill075edd82013-07-08 13:30:19 -04002333 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002334 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002335
Jamie Madill075edd82013-07-08 13:30:19 -04002336 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002337 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002338
2339 // Detect false discard
2340 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2341 }
2342
2343 // ANGLE issue 486: Detect problematic conditional discard
2344 if (discard && FindSideEffectRewriting::search(node))
2345 {
2346 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002347 }
2348 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349
2350 return false;
2351}
2352
2353void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2354{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002355 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002356}
2357
2358bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2359{
Nicolas Capens655fe362014-04-11 13:12:34 -04002360 mNestedLoopDepth++;
2361
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002362 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2363
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002364 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002365 {
2366 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2367 }
2368
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002369 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002370 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002371 if (handleExcessiveLoop(node))
2372 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002373 mInsideDiscontinuousLoop = wasDiscontinuous;
2374 mNestedLoopDepth--;
2375
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002376 return false;
2377 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002378 }
2379
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002380 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381
alokp@chromium.org52813552010-11-16 18:36:09 +00002382 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002384 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002385
Jamie Madill075edd82013-07-08 13:30:19 -04002386 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002387 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388 }
2389 else
2390 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002391 out << "{for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002392
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393 if (node->getInit())
2394 {
2395 node->getInit()->traverse(this);
2396 }
2397
2398 out << "; ";
2399
alokp@chromium.org52813552010-11-16 18:36:09 +00002400 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002401 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002402 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 }
2404
2405 out << "; ";
2406
alokp@chromium.org52813552010-11-16 18:36:09 +00002407 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002409 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410 }
2411
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002412 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002413
Jamie Madill075edd82013-07-08 13:30:19 -04002414 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002415 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416 }
2417
2418 if (node->getBody())
2419 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002420 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 }
2422
Jamie Madill075edd82013-07-08 13:30:19 -04002423 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002424 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425
alokp@chromium.org52813552010-11-16 18:36:09 +00002426 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 {
Jamie Madill075edd82013-07-08 13:30:19 -04002428 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 out << "while(\n";
2430
alokp@chromium.org52813552010-11-16 18:36:09 +00002431 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432
daniel@transgaming.com73536982012-03-21 20:45:49 +00002433 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 }
2435
daniel@transgaming.com73536982012-03-21 20:45:49 +00002436 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002437
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002438 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002439 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002440
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 return false;
2442}
2443
2444bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2445{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002446 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447
2448 switch (node->getFlowOp())
2449 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002450 case EOpKill:
2451 outputTriplet(visit, "discard;\n", "", "");
2452 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002453 case EOpBreak:
2454 if (visit == PreVisit)
2455 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002456 if (mNestedLoopDepth > 1)
2457 {
2458 mUsesNestedBreak = true;
2459 }
2460
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002461 if (mExcessiveLoopIndex)
2462 {
2463 out << "{Break";
2464 mExcessiveLoopIndex->traverse(this);
2465 out << " = true; break;}\n";
2466 }
2467 else
2468 {
2469 out << "break;\n";
2470 }
2471 }
2472 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002473 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474 case EOpReturn:
2475 if (visit == PreVisit)
2476 {
2477 if (node->getExpression())
2478 {
2479 out << "return ";
2480 }
2481 else
2482 {
2483 out << "return;\n";
2484 }
2485 }
2486 else if (visit == PostVisit)
2487 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002488 if (node->getExpression())
2489 {
2490 out << ";\n";
2491 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492 }
2493 break;
2494 default: UNREACHABLE();
2495 }
2496
2497 return true;
2498}
2499
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002500void OutputHLSL::traverseStatements(TIntermNode *node)
2501{
2502 if (isSingleStatement(node))
2503 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002504 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002505 }
2506
2507 node->traverse(this);
2508}
2509
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002510bool OutputHLSL::isSingleStatement(TIntermNode *node)
2511{
2512 TIntermAggregate *aggregate = node->getAsAggregate();
2513
2514 if (aggregate)
2515 {
2516 if (aggregate->getOp() == EOpSequence)
2517 {
2518 return false;
2519 }
2520 else
2521 {
2522 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2523 {
2524 if (!isSingleStatement(*sit))
2525 {
2526 return false;
2527 }
2528 }
2529
2530 return true;
2531 }
2532 }
2533
2534 return true;
2535}
2536
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002537// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2538// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002539bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2540{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002541 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002542 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002543
2544 // Parse loops of the form:
2545 // for(int index = initial; index [comparator] limit; index += increment)
2546 TIntermSymbol *index = NULL;
2547 TOperator comparator = EOpNull;
2548 int initial = 0;
2549 int limit = 0;
2550 int increment = 0;
2551
2552 // Parse index name and intial value
2553 if (node->getInit())
2554 {
2555 TIntermAggregate *init = node->getInit()->getAsAggregate();
2556
2557 if (init)
2558 {
2559 TIntermSequence &sequence = init->getSequence();
2560 TIntermTyped *variable = sequence[0]->getAsTyped();
2561
2562 if (variable && variable->getQualifier() == EvqTemporary)
2563 {
2564 TIntermBinary *assign = variable->getAsBinaryNode();
2565
2566 if (assign->getOp() == EOpInitialize)
2567 {
2568 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2569 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2570
2571 if (symbol && constant)
2572 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002573 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002574 {
2575 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002576 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002577 }
2578 }
2579 }
2580 }
2581 }
2582 }
2583
2584 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002585 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002586 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002587 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002588
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002589 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2590 {
2591 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2592
2593 if (constant)
2594 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002595 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002596 {
2597 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002598 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002599 }
2600 }
2601 }
2602 }
2603
2604 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002605 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002606 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002607 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2608 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002609
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002610 if (binaryTerminal)
2611 {
2612 TOperator op = binaryTerminal->getOp();
2613 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2614
2615 if (constant)
2616 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002617 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002618 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002619 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002620
2621 switch (op)
2622 {
2623 case EOpAddAssign: increment = value; break;
2624 case EOpSubAssign: increment = -value; break;
2625 default: UNIMPLEMENTED();
2626 }
2627 }
2628 }
2629 }
2630 else if (unaryTerminal)
2631 {
2632 TOperator op = unaryTerminal->getOp();
2633
2634 switch (op)
2635 {
2636 case EOpPostIncrement: increment = 1; break;
2637 case EOpPostDecrement: increment = -1; break;
2638 case EOpPreIncrement: increment = 1; break;
2639 case EOpPreDecrement: increment = -1; break;
2640 default: UNIMPLEMENTED();
2641 }
2642 }
2643 }
2644
2645 if (index != NULL && comparator != EOpNull && increment != 0)
2646 {
2647 if (comparator == EOpLessThanEqual)
2648 {
2649 comparator = EOpLessThan;
2650 limit += 1;
2651 }
2652
2653 if (comparator == EOpLessThan)
2654 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002655 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002656
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002657 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002658 {
2659 return false; // Not an excessive loop
2660 }
2661
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002662 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2663 mExcessiveLoopIndex = index;
2664
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002665 out << "{int ";
2666 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002667 out << ";\n"
2668 "bool Break";
2669 index->traverse(this);
2670 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002671
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002672 bool firstLoopFragment = true;
2673
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002674 while (iterations > 0)
2675 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002676 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002677
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002678 if (!firstLoopFragment)
2679 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002680 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002681 index->traverse(this);
2682 out << ") {\n";
2683 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002684
2685 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2686 {
2687 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2688 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002689
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002690 // for(int index = initial; index < clampedLimit; index += increment)
2691
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002692 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002693 index->traverse(this);
2694 out << " = ";
2695 out << initial;
2696
2697 out << "; ";
2698 index->traverse(this);
2699 out << " < ";
2700 out << clampedLimit;
2701
2702 out << "; ";
2703 index->traverse(this);
2704 out << " += ";
2705 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002706 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002707
Jamie Madill075edd82013-07-08 13:30:19 -04002708 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002709 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002710
2711 if (node->getBody())
2712 {
2713 node->getBody()->traverse(this);
2714 }
2715
Jamie Madill075edd82013-07-08 13:30:19 -04002716 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002717 out << ";}\n";
2718
2719 if (!firstLoopFragment)
2720 {
2721 out << "}\n";
2722 }
2723
2724 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002725
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002726 initial += MAX_LOOP_ITERATIONS * increment;
2727 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002728 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002729
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002730 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002731
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002732 mExcessiveLoopIndex = restoreIndex;
2733
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002734 return true;
2735 }
2736 else UNIMPLEMENTED();
2737 }
2738
2739 return false; // Not handled as an excessive loop
2740}
2741
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002742void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002743{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002744 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002745
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002746 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002747 {
2748 out << preString;
2749 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002750 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002751 {
2752 out << inString;
2753 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002754 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002755 {
2756 out << postString;
2757 }
2758}
2759
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002760void OutputHLSL::outputLineDirective(int line)
2761{
2762 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2763 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002764 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002765 mBody << "#line " << line;
2766
2767 if (mContext.sourcePath)
2768 {
2769 mBody << " \"" << mContext.sourcePath << "\"";
2770 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002771
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002772 mBody << "\n";
2773 }
2774}
2775
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002776TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2777{
2778 TQualifier qualifier = symbol->getQualifier();
2779 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002780 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002781
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002782 if (name.empty()) // HLSL demands named arguments, also for prototypes
2783 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002784 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002785 }
2786 else
2787 {
Jamie Madill033dae62014-06-18 12:56:28 -04002788 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002789 }
2790
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002791 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2792 {
Jamie Madill033dae62014-06-18 12:56:28 -04002793 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002794 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002795 }
2796
Jamie Madill033dae62014-06-18 12:56:28 -04002797 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002798}
2799
2800TString OutputHLSL::initializer(const TType &type)
2801{
2802 TString string;
2803
Jamie Madill94bf7f22013-07-08 13:31:15 -04002804 size_t size = type.getObjectSize();
2805 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002806 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002807 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808
Jamie Madill94bf7f22013-07-08 13:31:15 -04002809 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810 {
2811 string += ", ";
2812 }
2813 }
2814
daniel@transgaming.comead23042010-04-29 03:35:36 +00002815 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002817
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002818void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2819{
2820 TInfoSinkBase &out = mBody;
2821
2822 if (visit == PreVisit)
2823 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002824 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002825
2826 out << name + "(";
2827 }
2828 else if (visit == InVisit)
2829 {
2830 out << ", ";
2831 }
2832 else if (visit == PostVisit)
2833 {
2834 out << ")";
2835 }
2836}
2837
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002838const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2839{
2840 TInfoSinkBase &out = mBody;
2841
Jamie Madill98493dd2013-07-08 14:39:03 -04002842 const TStructure* structure = type.getStruct();
2843 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002844 {
Jamie Madill033dae62014-06-18 12:56:28 -04002845 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002846
Jamie Madill98493dd2013-07-08 14:39:03 -04002847 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002848
Jamie Madill98493dd2013-07-08 14:39:03 -04002849 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002850 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002851 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002852 constUnion = writeConstantUnion(*fieldType, constUnion);
2853
Jamie Madill98493dd2013-07-08 14:39:03 -04002854 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002855 {
2856 out << ", ";
2857 }
2858 }
2859
2860 out << ")";
2861 }
2862 else
2863 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002864 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002865 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002866
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002867 if (writeType)
2868 {
Jamie Madill033dae62014-06-18 12:56:28 -04002869 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002870 }
2871
Jamie Madill94bf7f22013-07-08 13:31:15 -04002872 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002873 {
2874 switch (constUnion->getType())
2875 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002876 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002877 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002878 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002879 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002880 default: UNREACHABLE();
2881 }
2882
2883 if (i != size - 1)
2884 {
2885 out << ", ";
2886 }
2887 }
2888
2889 if (writeType)
2890 {
2891 out << ")";
2892 }
2893 }
2894
2895 return constUnion;
2896}
2897
Jamie Madillf2575982014-06-25 16:04:54 -04002898void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying> &fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04002899{
2900 const TStructure *structure = type.getStruct();
2901
Jamie Madillf2575982014-06-25 16:04:54 -04002902 InterpolationType interpolation = GetInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04002903 if (!structure)
2904 {
Jamie Madillf2575982014-06-25 16:04:54 -04002905 sh::Varying varying(GLVariableType(type), GLVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04002906 fieldsOut.push_back(varying);
2907 }
2908 else
2909 {
Jamie Madillf2575982014-06-25 16:04:54 -04002910 sh::Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04002911 const TFieldList &fields = structure->fields();
2912
Jamie Madill28167c62013-08-30 13:21:10 -04002913 structVarying.structName = structure->name().c_str();
2914
Jamie Madill47fdd132013-08-30 13:21:04 -04002915 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
2916 {
2917 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04002918 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04002919 }
2920
2921 fieldsOut.push_back(structVarying);
2922 }
2923}
2924
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002925}