blob: c5e05041eb0ec543cfbce1051bef8918ebc6f365 [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
Jamie Madill4e1fd412014-07-10 17:50:10 -0400240const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
241{
242 return mUniformHLSL->getInterfaceBlockRegisterMap();
243}
244
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000245int OutputHLSL::vectorSize(const TType &type) const
246{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000247 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000248 int arraySize = type.isArray() ? type.getArraySize() : 1;
249
250 return elementSize * arraySize;
251}
252
Jamie Madill98493dd2013-07-08 14:39:03 -0400253TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400254{
255 TString init;
256
257 TString preIndentString;
258 TString fullIndentString;
259
260 for (int spaces = 0; spaces < (indent * 4); spaces++)
261 {
262 preIndentString += ' ';
263 }
264
265 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
266 {
267 fullIndentString += ' ';
268 }
269
270 init += preIndentString + "{\n";
271
Jamie Madill98493dd2013-07-08 14:39:03 -0400272 const TFieldList &fields = structure.fields();
273 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400274 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400276 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400278
Jamie Madill98493dd2013-07-08 14:39:03 -0400279 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400280 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400281 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400282 }
283 else
284 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400285 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 }
287 }
288
289 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
290
291 return init;
292}
293
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294void OutputHLSL::header()
295{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000296 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000298 TString varyings;
299 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400300 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000301
Jamie Madill829f59e2013-11-13 19:40:54 -0500302 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400303 {
304 TIntermTyped *structNode = flaggedStructIt->first;
305 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400306 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400307 const TString &originalName = mFlaggedStructOriginalNames[structNode];
308
Jamie Madill033dae62014-06-18 12:56:28 -0400309 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400310 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400311 flaggedStructs += "\n";
312 }
313
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000314 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
315 {
316 const TType &type = varying->second->getType();
317 const TString &name = varying->second->getSymbol();
318
319 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400320 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
321 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400322
Jamie Madill94599662013-08-30 13:21:10 -0400323 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000324 }
325
326 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
327 {
328 const TType &type = attribute->second->getType();
329 const TString &name = attribute->second->getSymbol();
330
Jamie Madill033dae62014-06-18 12:56:28 -0400331 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400332
Jamie Madillf2575982014-06-25 16:04:54 -0400333 sh::Attribute attributeVar(GLVariableType(type), GLVariablePrecision(type), name.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400334 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
335 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000336 }
337
Jamie Madill8daaba12014-06-13 10:04:33 -0400338 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400339
Jamie Madillf91ce812014-06-13 10:04:34 -0400340 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
341 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
342
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500343 if (mUsesDiscardRewriting)
344 {
345 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
346 }
347
Nicolas Capens655fe362014-04-11 13:12:34 -0400348 if (mUsesNestedBreak)
349 {
350 out << "#define ANGLE_USES_NESTED_BREAK" << "\n";
351 }
352
Jamie Madill183bde52014-07-02 15:31:19 -0400353 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000355 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000356 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000357
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000358 out << "// Varyings\n";
359 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400360 out << "\n";
361
362 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000363 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500364 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000365 {
Jamie Madill46131a32013-06-20 11:55:50 -0400366 const TString &variableName = outputVariableIt->first;
367 const TType &variableType = outputVariableIt->second->getType();
368 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
369
Jamie Madill033dae62014-06-18 12:56:28 -0400370 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400371 " = " + initializer(variableType) + ";\n";
372
Jamie Madillf2575982014-06-25 16:04:54 -0400373 sh::Attribute outputVar(GLVariableType(variableType), GLVariablePrecision(variableType), variableName.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400374 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400375 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000376 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000377 }
Jamie Madill46131a32013-06-20 11:55:50 -0400378 else
379 {
380 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
381
382 out << "static float4 gl_Color[" << numColorValues << "] =\n"
383 "{\n";
384 for (unsigned int i = 0; i < numColorValues; i++)
385 {
386 out << " float4(0, 0, 0, 0)";
387 if (i + 1 != numColorValues)
388 {
389 out << ",";
390 }
391 out << "\n";
392 }
393
394 out << "};\n";
395 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000396
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400397 if (mUsesFragDepth)
398 {
399 out << "static float gl_Depth = 0.0;\n";
400 }
401
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000402 if (mUsesFragCoord)
403 {
404 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
405 }
406
407 if (mUsesPointCoord)
408 {
409 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
410 }
411
412 if (mUsesFrontFacing)
413 {
414 out << "static bool gl_FrontFacing = false;\n";
415 }
416
417 out << "\n";
418
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000419 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000420 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000421 out << "struct gl_DepthRangeParameters\n"
422 "{\n"
423 " float near;\n"
424 " float far;\n"
425 " float diff;\n"
426 "};\n"
427 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000428 }
429
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000430 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000431 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000432 out << "cbuffer DriverConstants : register(b1)\n"
433 "{\n";
434
435 if (mUsesDepthRange)
436 {
437 out << " float3 dx_DepthRange : packoffset(c0);\n";
438 }
439
440 if (mUsesFragCoord)
441 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000442 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000443 }
444
445 if (mUsesFragCoord || mUsesFrontFacing)
446 {
447 out << " float3 dx_DepthFront : packoffset(c2);\n";
448 }
449
450 out << "};\n";
451 }
452 else
453 {
454 if (mUsesDepthRange)
455 {
456 out << "uniform float3 dx_DepthRange : register(c0);";
457 }
458
459 if (mUsesFragCoord)
460 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000461 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000462 }
463
464 if (mUsesFragCoord || mUsesFrontFacing)
465 {
466 out << "uniform float3 dx_DepthFront : register(c2);\n";
467 }
468 }
469
470 out << "\n";
471
472 if (mUsesDepthRange)
473 {
474 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
475 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000476 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000477
Jamie Madillf91ce812014-06-13 10:04:34 -0400478 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000479 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400480 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000481 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400482 out << flaggedStructs;
483 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000484 }
485
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000486 if (usingMRTExtension && mNumRenderTargets > 1)
487 {
488 out << "#define GL_USES_MRT\n";
489 }
490
491 if (mUsesFragColor)
492 {
493 out << "#define GL_USES_FRAG_COLOR\n";
494 }
495
496 if (mUsesFragData)
497 {
498 out << "#define GL_USES_FRAG_DATA\n";
499 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000501 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000503 out << "// Attributes\n";
504 out << attributes;
505 out << "\n"
506 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400507
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000508 if (mUsesPointSize)
509 {
510 out << "static float gl_PointSize = float(1);\n";
511 }
512
513 out << "\n"
514 "// Varyings\n";
515 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000516 out << "\n";
517
518 if (mUsesDepthRange)
519 {
520 out << "struct gl_DepthRangeParameters\n"
521 "{\n"
522 " float near;\n"
523 " float far;\n"
524 " float diff;\n"
525 "};\n"
526 "\n";
527 }
528
529 if (mOutputType == SH_HLSL11_OUTPUT)
530 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000531 if (mUsesDepthRange)
532 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000533 out << "cbuffer DriverConstants : register(b1)\n"
534 "{\n"
535 " float3 dx_DepthRange : packoffset(c0);\n"
536 "};\n"
537 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000538 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000539 }
540 else
541 {
542 if (mUsesDepthRange)
543 {
544 out << "uniform float3 dx_DepthRange : register(c0);\n";
545 }
546
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000547 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000548 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000549 }
550
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000551 if (mUsesDepthRange)
552 {
553 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
554 "\n";
555 }
556
Jamie Madillf91ce812014-06-13 10:04:34 -0400557 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000558 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400559 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000560 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400561 out << flaggedStructs;
562 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000563 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400564 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000565
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400566 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
567 {
568 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400569 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000570 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400571 switch(textureFunction->sampler)
572 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400573 case EbtSampler2D: out << "int2 "; break;
574 case EbtSampler3D: out << "int3 "; break;
575 case EbtSamplerCube: out << "int2 "; break;
576 case EbtSampler2DArray: out << "int3 "; break;
577 case EbtISampler2D: out << "int2 "; break;
578 case EbtISampler3D: out << "int3 "; break;
579 case EbtISamplerCube: out << "int2 "; break;
580 case EbtISampler2DArray: out << "int3 "; break;
581 case EbtUSampler2D: out << "int2 "; break;
582 case EbtUSampler3D: out << "int3 "; break;
583 case EbtUSamplerCube: out << "int2 "; break;
584 case EbtUSampler2DArray: out << "int3 "; break;
585 case EbtSampler2DShadow: out << "int2 "; break;
586 case EbtSamplerCubeShadow: out << "int2 "; break;
587 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400588 default: UNREACHABLE();
589 }
590 }
591 else // Sampling function
592 {
593 switch(textureFunction->sampler)
594 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400595 case EbtSampler2D: out << "float4 "; break;
596 case EbtSampler3D: out << "float4 "; break;
597 case EbtSamplerCube: out << "float4 "; break;
598 case EbtSampler2DArray: out << "float4 "; break;
599 case EbtISampler2D: out << "int4 "; break;
600 case EbtISampler3D: out << "int4 "; break;
601 case EbtISamplerCube: out << "int4 "; break;
602 case EbtISampler2DArray: out << "int4 "; break;
603 case EbtUSampler2D: out << "uint4 "; break;
604 case EbtUSampler3D: out << "uint4 "; break;
605 case EbtUSamplerCube: out << "uint4 "; break;
606 case EbtUSampler2DArray: out << "uint4 "; break;
607 case EbtSampler2DShadow: out << "float "; break;
608 case EbtSamplerCubeShadow: out << "float "; break;
609 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400610 default: UNREACHABLE();
611 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000612 }
613
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400614 // Function name
615 out << textureFunction->name();
616
617 // Argument list
618 int hlslCoords = 4;
619
620 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000621 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400622 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000623 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400624 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
625 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
626 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000627 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400628
Nicolas Capens75fb4752013-07-10 15:14:47 -0400629 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000630 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400631 case TextureFunction::IMPLICIT: break;
632 case TextureFunction::BIAS: hlslCoords = 4; break;
633 case TextureFunction::LOD: hlslCoords = 4; break;
634 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400635 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400636 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000637 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400638 }
639 else if (mOutputType == SH_HLSL11_OUTPUT)
640 {
641 switch(textureFunction->sampler)
642 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400643 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
644 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
645 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
646 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
647 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
648 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500649 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400650 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
651 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
652 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500653 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400654 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
655 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
656 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
657 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400658 default: UNREACHABLE();
659 }
660 }
661 else UNREACHABLE();
662
Nicolas Capensfc014542014-02-18 14:47:13 -0500663 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400664 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500665 switch(textureFunction->coords)
666 {
667 case 2: out << ", int2 t"; break;
668 case 3: out << ", int3 t"; break;
669 default: UNREACHABLE();
670 }
671 }
672 else // Floating-point coordinates (except textureSize)
673 {
674 switch(textureFunction->coords)
675 {
676 case 1: out << ", int lod"; break; // textureSize()
677 case 2: out << ", float2 t"; break;
678 case 3: out << ", float3 t"; break;
679 case 4: out << ", float4 t"; break;
680 default: UNREACHABLE();
681 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000682 }
683
Nicolas Capensd11d5492014-02-19 17:06:10 -0500684 if (textureFunction->method == TextureFunction::GRAD)
685 {
686 switch(textureFunction->sampler)
687 {
688 case EbtSampler2D:
689 case EbtISampler2D:
690 case EbtUSampler2D:
691 case EbtSampler2DArray:
692 case EbtISampler2DArray:
693 case EbtUSampler2DArray:
694 case EbtSampler2DShadow:
695 case EbtSampler2DArrayShadow:
696 out << ", float2 ddx, float2 ddy";
697 break;
698 case EbtSampler3D:
699 case EbtISampler3D:
700 case EbtUSampler3D:
701 case EbtSamplerCube:
702 case EbtISamplerCube:
703 case EbtUSamplerCube:
704 case EbtSamplerCubeShadow:
705 out << ", float3 ddx, float3 ddy";
706 break;
707 default: UNREACHABLE();
708 }
709 }
710
Nicolas Capens75fb4752013-07-10 15:14:47 -0400711 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000712 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400713 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400714 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400715 case TextureFunction::LOD: out << ", float lod"; break;
716 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400717 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400718 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500719 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500720 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400721 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000722 }
723
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500724 if (textureFunction->offset)
725 {
726 switch(textureFunction->sampler)
727 {
728 case EbtSampler2D: out << ", int2 offset"; break;
729 case EbtSampler3D: out << ", int3 offset"; break;
730 case EbtSampler2DArray: out << ", int2 offset"; break;
731 case EbtISampler2D: out << ", int2 offset"; break;
732 case EbtISampler3D: out << ", int3 offset"; break;
733 case EbtISampler2DArray: out << ", int2 offset"; break;
734 case EbtUSampler2D: out << ", int2 offset"; break;
735 case EbtUSampler3D: out << ", int3 offset"; break;
736 case EbtUSampler2DArray: out << ", int2 offset"; break;
737 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500738 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500739 default: UNREACHABLE();
740 }
741 }
742
Nicolas Capens84cfa122014-04-14 13:48:45 -0400743 if (textureFunction->method == TextureFunction::BIAS ||
744 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500745 {
746 out << ", float bias";
747 }
748
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400749 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400750 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400751
Nicolas Capens75fb4752013-07-10 15:14:47 -0400752 if (textureFunction->method == TextureFunction::SIZE)
753 {
754 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
755 {
756 if (IsSamplerArray(textureFunction->sampler))
757 {
758 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
759 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
760 }
761 else
762 {
763 out << " uint width; uint height; uint numberOfLevels;\n"
764 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
765 }
766 }
767 else if (IsSampler3D(textureFunction->sampler))
768 {
769 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
770 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
771 }
772 else UNREACHABLE();
773
774 switch(textureFunction->sampler)
775 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400776 case EbtSampler2D: out << " return int2(width, height);"; break;
777 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
778 case EbtSamplerCube: out << " return int2(width, height);"; break;
779 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
780 case EbtISampler2D: out << " return int2(width, height);"; break;
781 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
782 case EbtISamplerCube: out << " return int2(width, height);"; break;
783 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
784 case EbtUSampler2D: out << " return int2(width, height);"; break;
785 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
786 case EbtUSamplerCube: out << " return int2(width, height);"; break;
787 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
788 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
789 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
790 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400791 default: UNREACHABLE();
792 }
793 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400794 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400795 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500796 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
797 {
798 out << " float width; float height; float layers; float levels;\n";
799
800 out << " uint mip = 0;\n";
801
802 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
803
804 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
805 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
806 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
807 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
808
809 // FACE_POSITIVE_X = 000b
810 // FACE_NEGATIVE_X = 001b
811 // FACE_POSITIVE_Y = 010b
812 // FACE_NEGATIVE_Y = 011b
813 // FACE_POSITIVE_Z = 100b
814 // FACE_NEGATIVE_Z = 101b
815 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
816
817 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
818 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
819 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
820
821 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
822 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
823 }
824 else if (IsIntegerSampler(textureFunction->sampler) &&
825 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400826 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400827 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400828 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400829 if (IsSamplerArray(textureFunction->sampler))
830 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400831 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400832
Nicolas Capens9edebd62013-08-06 10:59:10 -0400833 if (textureFunction->method == TextureFunction::LOD0)
834 {
835 out << " uint mip = 0;\n";
836 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400837 else if (textureFunction->method == TextureFunction::LOD0BIAS)
838 {
839 out << " uint mip = bias;\n";
840 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400841 else
842 {
843 if (textureFunction->method == TextureFunction::IMPLICIT ||
844 textureFunction->method == TextureFunction::BIAS)
845 {
846 out << " x.GetDimensions(0, width, height, layers, levels);\n"
847 " float2 tSized = float2(t.x * width, t.y * height);\n"
848 " float dx = length(ddx(tSized));\n"
849 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500850 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400851
852 if (textureFunction->method == TextureFunction::BIAS)
853 {
854 out << " lod += bias;\n";
855 }
856 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500857 else if (textureFunction->method == TextureFunction::GRAD)
858 {
859 out << " x.GetDimensions(0, width, height, layers, levels);\n"
860 " float lod = log2(max(length(ddx), length(ddy)));\n";
861 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400862
863 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
864 }
865
866 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400867 }
868 else
869 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400870 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400871
Nicolas Capens9edebd62013-08-06 10:59:10 -0400872 if (textureFunction->method == TextureFunction::LOD0)
873 {
874 out << " uint mip = 0;\n";
875 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400876 else if (textureFunction->method == TextureFunction::LOD0BIAS)
877 {
878 out << " uint mip = bias;\n";
879 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400880 else
881 {
882 if (textureFunction->method == TextureFunction::IMPLICIT ||
883 textureFunction->method == TextureFunction::BIAS)
884 {
885 out << " x.GetDimensions(0, width, height, levels);\n"
886 " float2 tSized = float2(t.x * width, t.y * height);\n"
887 " float dx = length(ddx(tSized));\n"
888 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500889 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400890
891 if (textureFunction->method == TextureFunction::BIAS)
892 {
893 out << " lod += bias;\n";
894 }
895 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500896 else if (textureFunction->method == TextureFunction::LOD)
897 {
898 out << " x.GetDimensions(0, width, height, levels);\n";
899 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500900 else if (textureFunction->method == TextureFunction::GRAD)
901 {
902 out << " x.GetDimensions(0, width, height, levels);\n"
903 " float lod = log2(max(length(ddx), length(ddy)));\n";
904 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400905
906 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
907 }
908
909 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400910 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400911 }
912 else if (IsSampler3D(textureFunction->sampler))
913 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400914 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400915
Nicolas Capens9edebd62013-08-06 10:59:10 -0400916 if (textureFunction->method == TextureFunction::LOD0)
917 {
918 out << " uint mip = 0;\n";
919 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400920 else if (textureFunction->method == TextureFunction::LOD0BIAS)
921 {
922 out << " uint mip = bias;\n";
923 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400924 else
925 {
926 if (textureFunction->method == TextureFunction::IMPLICIT ||
927 textureFunction->method == TextureFunction::BIAS)
928 {
929 out << " x.GetDimensions(0, width, height, depth, levels);\n"
930 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
931 " float dx = length(ddx(tSized));\n"
932 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500933 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400934
935 if (textureFunction->method == TextureFunction::BIAS)
936 {
937 out << " lod += bias;\n";
938 }
939 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500940 else if (textureFunction->method == TextureFunction::GRAD)
941 {
942 out << " x.GetDimensions(0, width, height, depth, levels);\n"
943 " float lod = log2(max(length(ddx), length(ddy)));\n";
944 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400945
946 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
947 }
948
949 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400950 }
951 else UNREACHABLE();
952 }
953
954 out << " return ";
955
956 // HLSL intrinsic
957 if (mOutputType == SH_HLSL9_OUTPUT)
958 {
959 switch(textureFunction->sampler)
960 {
961 case EbtSampler2D: out << "tex2D"; break;
962 case EbtSamplerCube: out << "texCUBE"; break;
963 default: UNREACHABLE();
964 }
965
Nicolas Capens75fb4752013-07-10 15:14:47 -0400966 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400967 {
968 case TextureFunction::IMPLICIT: out << "(s, "; break;
969 case TextureFunction::BIAS: out << "bias(s, "; break;
970 case TextureFunction::LOD: out << "lod(s, "; break;
971 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400972 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400973 default: UNREACHABLE();
974 }
975 }
976 else if (mOutputType == SH_HLSL11_OUTPUT)
977 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500978 if (textureFunction->method == TextureFunction::GRAD)
979 {
980 if (IsIntegerSampler(textureFunction->sampler))
981 {
982 out << "x.Load(";
983 }
984 else if (IsShadowSampler(textureFunction->sampler))
985 {
986 out << "x.SampleCmpLevelZero(s, ";
987 }
988 else
989 {
990 out << "x.SampleGrad(s, ";
991 }
992 }
993 else if (IsIntegerSampler(textureFunction->sampler) ||
994 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400995 {
996 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400997 }
Nicolas Capenscb127d32013-07-15 17:26:18 -0400998 else if (IsShadowSampler(textureFunction->sampler))
999 {
1000 out << "x.SampleCmp(s, ";
1001 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001002 else
1003 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001004 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001005 {
1006 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1007 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1008 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1009 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001010 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001011 default: UNREACHABLE();
1012 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001013 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001014 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001015 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001016
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001017 // Integer sampling requires integer addresses
1018 TString addressx = "";
1019 TString addressy = "";
1020 TString addressz = "";
1021 TString close = "";
1022
Nicolas Capensfc014542014-02-18 14:47:13 -05001023 if (IsIntegerSampler(textureFunction->sampler) ||
1024 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001025 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001026 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001027 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001028 case 2: out << "int3("; break;
1029 case 3: out << "int4("; break;
1030 default: UNREACHABLE();
1031 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001032
Nicolas Capensfc014542014-02-18 14:47:13 -05001033 // Convert from normalized floating-point to integer
1034 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001035 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001036 addressx = "int(floor(width * frac((";
1037 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001038
Nicolas Capensfc014542014-02-18 14:47:13 -05001039 if (IsSamplerArray(textureFunction->sampler))
1040 {
1041 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1042 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001043 else if (IsSamplerCube(textureFunction->sampler))
1044 {
1045 addressz = "((((";
1046 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001047 else
1048 {
1049 addressz = "int(floor(depth * frac((";
1050 }
1051
1052 close = "))))";
1053 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001054 }
1055 else
1056 {
1057 switch(hlslCoords)
1058 {
1059 case 2: out << "float2("; break;
1060 case 3: out << "float3("; break;
1061 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001062 default: UNREACHABLE();
1063 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001064 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001065
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001066 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001067
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001068 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001069 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001070 switch(textureFunction->coords)
1071 {
1072 case 3: proj = " / t.z"; break;
1073 case 4: proj = " / t.w"; break;
1074 default: UNREACHABLE();
1075 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001076 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001077
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001078 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001079
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001080 if (mOutputType == SH_HLSL9_OUTPUT)
1081 {
1082 if (hlslCoords >= 3)
1083 {
1084 if (textureFunction->coords < 3)
1085 {
1086 out << ", 0";
1087 }
1088 else
1089 {
1090 out << ", t.z" + proj;
1091 }
1092 }
1093
1094 if (hlslCoords == 4)
1095 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001096 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001097 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001098 case TextureFunction::BIAS: out << ", bias"; break;
1099 case TextureFunction::LOD: out << ", lod"; break;
1100 case TextureFunction::LOD0: out << ", 0"; break;
1101 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001102 default: UNREACHABLE();
1103 }
1104 }
1105
1106 out << "));\n";
1107 }
1108 else if (mOutputType == SH_HLSL11_OUTPUT)
1109 {
1110 if (hlslCoords >= 3)
1111 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001112 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1113 {
1114 out << ", face";
1115 }
1116 else
1117 {
1118 out << ", " + addressz + ("t.z" + proj) + close;
1119 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001120 }
1121
Nicolas Capensd11d5492014-02-19 17:06:10 -05001122 if (textureFunction->method == TextureFunction::GRAD)
1123 {
1124 if (IsIntegerSampler(textureFunction->sampler))
1125 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001126 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001127 }
1128 else if (IsShadowSampler(textureFunction->sampler))
1129 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001130 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001131 switch(textureFunction->coords)
1132 {
1133 case 3: out << "), t.z"; break;
1134 case 4: out << "), t.w"; break;
1135 default: UNREACHABLE();
1136 }
1137 }
1138 else
1139 {
1140 out << "), ddx, ddy";
1141 }
1142 }
1143 else if (IsIntegerSampler(textureFunction->sampler) ||
1144 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001145 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001146 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001147 }
1148 else if (IsShadowSampler(textureFunction->sampler))
1149 {
1150 // Compare value
1151 switch(textureFunction->coords)
1152 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001153 case 3: out << "), t.z"; break;
1154 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001155 default: UNREACHABLE();
1156 }
1157 }
1158 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001159 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001160 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001161 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001162 case TextureFunction::IMPLICIT: out << ")"; break;
1163 case TextureFunction::BIAS: out << "), bias"; break;
1164 case TextureFunction::LOD: out << "), lod"; break;
1165 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001166 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001167 default: UNREACHABLE();
1168 }
1169 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001170
1171 if (textureFunction->offset)
1172 {
1173 out << ", offset";
1174 }
1175
1176 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001177 }
1178 else UNREACHABLE();
1179 }
1180
1181 out << "\n"
1182 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001183 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184 }
1185
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001186 if (mUsesFragCoord)
1187 {
1188 out << "#define GL_USES_FRAG_COORD\n";
1189 }
1190
1191 if (mUsesPointCoord)
1192 {
1193 out << "#define GL_USES_POINT_COORD\n";
1194 }
1195
1196 if (mUsesFrontFacing)
1197 {
1198 out << "#define GL_USES_FRONT_FACING\n";
1199 }
1200
1201 if (mUsesPointSize)
1202 {
1203 out << "#define GL_USES_POINT_SIZE\n";
1204 }
1205
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001206 if (mUsesFragDepth)
1207 {
1208 out << "#define GL_USES_FRAG_DEPTH\n";
1209 }
1210
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001211 if (mUsesDepthRange)
1212 {
1213 out << "#define GL_USES_DEPTH_RANGE\n";
1214 }
1215
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001216 if (mUsesXor)
1217 {
1218 out << "bool xor(bool p, bool q)\n"
1219 "{\n"
1220 " return (p || q) && !(p && q);\n"
1221 "}\n"
1222 "\n";
1223 }
1224
1225 if (mUsesMod1)
1226 {
1227 out << "float mod(float x, float y)\n"
1228 "{\n"
1229 " return x - y * floor(x / y);\n"
1230 "}\n"
1231 "\n";
1232 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001233
1234 if (mUsesMod2v)
1235 {
1236 out << "float2 mod(float2 x, float2 y)\n"
1237 "{\n"
1238 " return x - y * floor(x / y);\n"
1239 "}\n"
1240 "\n";
1241 }
1242
1243 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001244 {
1245 out << "float2 mod(float2 x, float y)\n"
1246 "{\n"
1247 " return x - y * floor(x / y);\n"
1248 "}\n"
1249 "\n";
1250 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001251
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001252 if (mUsesMod3v)
1253 {
1254 out << "float3 mod(float3 x, float3 y)\n"
1255 "{\n"
1256 " return x - y * floor(x / y);\n"
1257 "}\n"
1258 "\n";
1259 }
1260
1261 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001262 {
1263 out << "float3 mod(float3 x, float y)\n"
1264 "{\n"
1265 " return x - y * floor(x / y);\n"
1266 "}\n"
1267 "\n";
1268 }
1269
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001270 if (mUsesMod4v)
1271 {
1272 out << "float4 mod(float4 x, float4 y)\n"
1273 "{\n"
1274 " return x - y * floor(x / y);\n"
1275 "}\n"
1276 "\n";
1277 }
1278
1279 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001280 {
1281 out << "float4 mod(float4 x, float y)\n"
1282 "{\n"
1283 " return x - y * floor(x / y);\n"
1284 "}\n"
1285 "\n";
1286 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001287
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001288 if (mUsesFaceforward1)
1289 {
1290 out << "float faceforward(float N, float I, float Nref)\n"
1291 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001292 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001293 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001294 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001295 " }\n"
1296 " else\n"
1297 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001298 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001299 " }\n"
1300 "}\n"
1301 "\n";
1302 }
1303
1304 if (mUsesFaceforward2)
1305 {
1306 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1307 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001308 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001309 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001310 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001311 " }\n"
1312 " else\n"
1313 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001314 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001315 " }\n"
1316 "}\n"
1317 "\n";
1318 }
1319
1320 if (mUsesFaceforward3)
1321 {
1322 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1323 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001324 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001325 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001326 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001327 " }\n"
1328 " else\n"
1329 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001330 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001331 " }\n"
1332 "}\n"
1333 "\n";
1334 }
1335
1336 if (mUsesFaceforward4)
1337 {
1338 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1339 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001340 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001341 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001342 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001343 " }\n"
1344 " else\n"
1345 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001346 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001347 " }\n"
1348 "}\n"
1349 "\n";
1350 }
1351
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001352 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001353 {
1354 out << "float atanyx(float y, float x)\n"
1355 "{\n"
1356 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1357 " return atan2(y, x);\n"
1358 "}\n";
1359 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001360
1361 if (mUsesAtan2_2)
1362 {
1363 out << "float2 atanyx(float2 y, float2 x)\n"
1364 "{\n"
1365 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1366 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1367 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1368 "}\n";
1369 }
1370
1371 if (mUsesAtan2_3)
1372 {
1373 out << "float3 atanyx(float3 y, float3 x)\n"
1374 "{\n"
1375 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1376 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1377 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1378 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1379 "}\n";
1380 }
1381
1382 if (mUsesAtan2_4)
1383 {
1384 out << "float4 atanyx(float4 y, float4 x)\n"
1385 "{\n"
1386 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1387 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1388 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1389 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1390 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1391 "}\n";
1392 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001393}
1394
1395void OutputHLSL::visitSymbol(TIntermSymbol *node)
1396{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001397 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001398
Jamie Madill570e04d2013-06-21 09:15:33 -04001399 // Handle accessing std140 structs by value
1400 if (mFlaggedStructMappedNames.count(node) > 0)
1401 {
1402 out << mFlaggedStructMappedNames[node];
1403 return;
1404 }
1405
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001406 TString name = node->getSymbol();
1407
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001408 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001409 {
1410 mUsesDepthRange = true;
1411 out << name;
1412 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001413 else
1414 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001415 TQualifier qualifier = node->getQualifier();
1416
1417 if (qualifier == EvqUniform)
1418 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001419 const TType& nodeType = node->getType();
1420 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1421
1422 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001423 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001424 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001425 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001426 else
1427 {
1428 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001429 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001430
Jamie Madill033dae62014-06-18 12:56:28 -04001431 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001432 }
Jamie Madill19571812013-08-12 15:26:34 -07001433 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001434 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001435 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001436 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001437 }
Jamie Madill033dae62014-06-18 12:56:28 -04001438 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001439 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001440 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001441 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001442 }
Jamie Madill19571812013-08-12 15:26:34 -07001443 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001444 {
1445 mReferencedOutputVariables[name] = node;
1446 out << "out_" << name;
1447 }
1448 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001449 {
1450 out << "gl_Color[0]";
1451 mUsesFragColor = true;
1452 }
1453 else if (qualifier == EvqFragData)
1454 {
1455 out << "gl_Color";
1456 mUsesFragData = true;
1457 }
1458 else if (qualifier == EvqFragCoord)
1459 {
1460 mUsesFragCoord = true;
1461 out << name;
1462 }
1463 else if (qualifier == EvqPointCoord)
1464 {
1465 mUsesPointCoord = true;
1466 out << name;
1467 }
1468 else if (qualifier == EvqFrontFacing)
1469 {
1470 mUsesFrontFacing = true;
1471 out << name;
1472 }
1473 else if (qualifier == EvqPointSize)
1474 {
1475 mUsesPointSize = true;
1476 out << name;
1477 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001478 else if (name == "gl_FragDepthEXT")
1479 {
1480 mUsesFragDepth = true;
1481 out << "gl_Depth";
1482 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001483 else if (qualifier == EvqInternal)
1484 {
1485 out << name;
1486 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001487 else
1488 {
Jamie Madill033dae62014-06-18 12:56:28 -04001489 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001490 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001491 }
1492}
1493
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001494void OutputHLSL::visitRaw(TIntermRaw *node)
1495{
1496 mBody << node->getRawText();
1497}
1498
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001499bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1500{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001501 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001502
Jamie Madill570e04d2013-06-21 09:15:33 -04001503 // Handle accessing std140 structs by value
1504 if (mFlaggedStructMappedNames.count(node) > 0)
1505 {
1506 out << mFlaggedStructMappedNames[node];
1507 return false;
1508 }
1509
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001510 switch (node->getOp())
1511 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001512 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001513 case EOpInitialize:
1514 if (visit == PreVisit)
1515 {
1516 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1517 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1518 // new variable is created before the assignment is evaluated), so we need to convert
1519 // this to "float t = x, x = t;".
1520
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001521 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1522 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001523
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001524 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1525 expression->traverse(&searchSymbol);
1526 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001527
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001528 if (sameSymbol)
1529 {
1530 // Type already printed
1531 out << "t" + str(mUniqueIndex) + " = ";
1532 expression->traverse(this);
1533 out << ", ";
1534 symbolNode->traverse(this);
1535 out << " = t" + str(mUniqueIndex);
1536
1537 mUniqueIndex++;
1538 return false;
1539 }
1540 }
1541 else if (visit == InVisit)
1542 {
1543 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001544 }
1545 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001546 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1547 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1548 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1549 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1550 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1551 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001552 if (visit == PreVisit)
1553 {
1554 out << "(";
1555 }
1556 else if (visit == InVisit)
1557 {
1558 out << " = mul(";
1559 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001560 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001561 }
1562 else
1563 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001564 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001565 }
1566 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001567 case EOpMatrixTimesMatrixAssign:
1568 if (visit == PreVisit)
1569 {
1570 out << "(";
1571 }
1572 else if (visit == InVisit)
1573 {
1574 out << " = mul(";
1575 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001576 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001577 }
1578 else
1579 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001580 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001581 }
1582 break;
1583 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001584 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001585 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001586 const TType& leftType = node->getLeft()->getType();
1587 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001588 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001589 if (visit == PreVisit)
1590 {
1591 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1592 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001593 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001594 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001595 return false;
1596 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001597 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001598 else
1599 {
1600 outputTriplet(visit, "", "[", "]");
1601 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001602 }
1603 break;
1604 case EOpIndexIndirect:
1605 // We do not currently support indirect references to interface blocks
1606 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1607 outputTriplet(visit, "", "[", "]");
1608 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001609 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001610 if (visit == InVisit)
1611 {
1612 const TStructure* structure = node->getLeft()->getType().getStruct();
1613 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1614 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001615 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001616
1617 return false;
1618 }
1619 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001620 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001621 if (visit == InVisit)
1622 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001623 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1624 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1625 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001626 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001627
1628 return false;
1629 }
1630 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001631 case EOpVectorSwizzle:
1632 if (visit == InVisit)
1633 {
1634 out << ".";
1635
1636 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1637
1638 if (swizzle)
1639 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001640 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001641
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001642 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001643 {
1644 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1645
1646 if (element)
1647 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001648 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649
1650 switch (i)
1651 {
1652 case 0: out << "x"; break;
1653 case 1: out << "y"; break;
1654 case 2: out << "z"; break;
1655 case 3: out << "w"; break;
1656 default: UNREACHABLE();
1657 }
1658 }
1659 else UNREACHABLE();
1660 }
1661 }
1662 else UNREACHABLE();
1663
1664 return false; // Fully processed
1665 }
1666 break;
1667 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1668 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1669 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1670 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001671 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001672 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001673 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001674 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001675 if (node->getOp() == EOpEqual)
1676 {
1677 outputTriplet(visit, "(", " == ", ")");
1678 }
1679 else
1680 {
1681 outputTriplet(visit, "(", " != ", ")");
1682 }
1683 }
1684 else if (node->getLeft()->getBasicType() == EbtStruct)
1685 {
1686 if (node->getOp() == EOpEqual)
1687 {
1688 out << "(";
1689 }
1690 else
1691 {
1692 out << "!(";
1693 }
1694
Jamie Madill98493dd2013-07-08 14:39:03 -04001695 const TStructure &structure = *node->getLeft()->getType().getStruct();
1696 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001697
Jamie Madill98493dd2013-07-08 14:39:03 -04001698 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001699 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001700 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001701
1702 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001703 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001704 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001705 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001706
Jamie Madill98493dd2013-07-08 14:39:03 -04001707 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001708 {
1709 out << " && ";
1710 }
1711 }
1712
1713 out << ")";
1714
1715 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001716 }
1717 else
1718 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001719 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001720
1721 if (node->getOp() == EOpEqual)
1722 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001723 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001724 }
1725 else
1726 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001727 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001728 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001729 }
1730 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1732 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1733 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1734 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1735 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001736 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001737 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1738 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001739 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001740 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001741 if (node->getRight()->hasSideEffects())
1742 {
1743 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1744 return false;
1745 }
1746 else
1747 {
1748 outputTriplet(visit, "(", " || ", ")");
1749 return true;
1750 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001751 case EOpLogicalXor:
1752 mUsesXor = true;
1753 outputTriplet(visit, "xor(", ", ", ")");
1754 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001755 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001756 if (node->getRight()->hasSideEffects())
1757 {
1758 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1759 return false;
1760 }
1761 else
1762 {
1763 outputTriplet(visit, "(", " && ", ")");
1764 return true;
1765 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 default: UNREACHABLE();
1767 }
1768
1769 return true;
1770}
1771
1772bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1773{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774 switch (node->getOp())
1775 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001776 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1777 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1778 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1779 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1780 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1781 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1782 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001783 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1784 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1785 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1786 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1787 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1788 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1789 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1790 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1791 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1792 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1793 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1794 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1795 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1796 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1797 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1798 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1799 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1800 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1801 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1802 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1803 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001804 case EOpDFdx:
1805 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1806 {
1807 outputTriplet(visit, "(", "", ", 0.0)");
1808 }
1809 else
1810 {
1811 outputTriplet(visit, "ddx(", "", ")");
1812 }
1813 break;
1814 case EOpDFdy:
1815 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1816 {
1817 outputTriplet(visit, "(", "", ", 0.0)");
1818 }
1819 else
1820 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001821 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001822 }
1823 break;
1824 case EOpFwidth:
1825 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1826 {
1827 outputTriplet(visit, "(", "", ", 0.0)");
1828 }
1829 else
1830 {
1831 outputTriplet(visit, "fwidth(", "", ")");
1832 }
1833 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001834 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1835 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836 default: UNREACHABLE();
1837 }
1838
1839 return true;
1840}
1841
1842bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1843{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001844 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001845
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846 switch (node->getOp())
1847 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001848 case EOpSequence:
1849 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001850 if (mInsideFunction)
1851 {
Jamie Madill075edd82013-07-08 13:30:19 -04001852 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001853 out << "{\n";
1854 }
1855
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001856 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001857 {
Jamie Madill075edd82013-07-08 13:30:19 -04001858 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001859
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001860 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001861
1862 out << ";\n";
1863 }
1864
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001865 if (mInsideFunction)
1866 {
Jamie Madill075edd82013-07-08 13:30:19 -04001867 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001868 out << "}\n";
1869 }
1870
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001871 return false;
1872 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873 case EOpDeclaration:
1874 if (visit == PreVisit)
1875 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001876 TIntermSequence *sequence = node->getSequence();
1877 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001879 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001881 TStructure *structure = variable->getType().getStruct();
1882
1883 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001884 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001885 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001886 }
1887
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001888 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001890 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001891 {
1892 out << "static ";
1893 }
1894
Jamie Madill033dae62014-06-18 12:56:28 -04001895 out << TypeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001897 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001899 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001901 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001902 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001903 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001904 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001905 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001906 }
1907 else
1908 {
1909 (*sit)->traverse(this);
1910 }
1911
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001912 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001913 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001914 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915 }
1916 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001918 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1919 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001920 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001921 }
1922 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923 }
Jamie Madill033dae62014-06-18 12:56:28 -04001924 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001925 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001926 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001927 {
1928 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1929
1930 if (symbol)
1931 {
1932 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1933 mReferencedVaryings[symbol->getSymbol()] = symbol;
1934 }
1935 else
1936 {
1937 (*sit)->traverse(this);
1938 }
1939 }
1940 }
1941
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942 return false;
1943 }
1944 else if (visit == InVisit)
1945 {
1946 out << ", ";
1947 }
1948 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001949 case EOpPrototype:
1950 if (visit == PreVisit)
1951 {
Jamie Madill033dae62014-06-18 12:56:28 -04001952 out << TypeString(node->getType()) << " " << Decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001953
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001954 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001955
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001956 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001957 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001958 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001959
1960 if (symbol)
1961 {
1962 out << argumentString(symbol);
1963
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001964 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001965 {
1966 out << ", ";
1967 }
1968 }
1969 else UNREACHABLE();
1970 }
1971
1972 out << ");\n";
1973
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001974 // Also prototype the Lod0 variant if needed
1975 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1976 {
1977 mOutputLod0Function = true;
1978 node->traverse(this);
1979 mOutputLod0Function = false;
1980 }
1981
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001982 return false;
1983 }
1984 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001985 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001986 case EOpFunction:
1987 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001988 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989
Jamie Madill033dae62014-06-18 12:56:28 -04001990 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001991
1992 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001994 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001995 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001996 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997 {
Jamie Madill033dae62014-06-18 12:56:28 -04001998 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001999 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002000
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002001 TIntermSequence *sequence = node->getSequence();
2002 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002003
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002004 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002005 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002006 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002007
2008 if (symbol)
2009 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002010 TStructure *structure = symbol->getType().getStruct();
2011
2012 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002013 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002014 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002015 }
2016
2017 out << argumentString(symbol);
2018
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002019 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002020 {
2021 out << ", ";
2022 }
2023 }
2024 else UNREACHABLE();
2025 }
2026
2027 out << ")\n"
2028 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002029
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002030 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002031 {
2032 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002033 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002034 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002036
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002037 out << "}\n";
2038
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002039 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2040 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002041 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002042 {
2043 mOutputLod0Function = true;
2044 node->traverse(this);
2045 mOutputLod0Function = false;
2046 }
2047 }
2048
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002049 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050 }
2051 break;
2052 case EOpFunctionCall:
2053 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002054 TString name = TFunction::unmangleName(node->getName());
2055 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002056 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002057
2058 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059 {
Jamie Madill033dae62014-06-18 12:56:28 -04002060 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002061 }
2062 else
2063 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002064 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002065
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002066 TextureFunction textureFunction;
2067 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002068 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002069 textureFunction.method = TextureFunction::IMPLICIT;
2070 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002071 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002072
2073 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002074 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002075 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002076 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002077 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002078 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002079 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002080 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002081 }
Nicolas Capens46485082014-04-15 13:12:50 -04002082 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2083 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002084 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002085 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002086 }
Nicolas Capens46485082014-04-15 13:12:50 -04002087 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002088 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002089 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002090 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002091 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002092 else if (name == "textureSize")
2093 {
2094 textureFunction.method = TextureFunction::SIZE;
2095 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002096 else if (name == "textureOffset")
2097 {
2098 textureFunction.method = TextureFunction::IMPLICIT;
2099 textureFunction.offset = true;
2100 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002101 else if (name == "textureProjOffset")
2102 {
2103 textureFunction.method = TextureFunction::IMPLICIT;
2104 textureFunction.offset = true;
2105 textureFunction.proj = true;
2106 }
2107 else if (name == "textureLodOffset")
2108 {
2109 textureFunction.method = TextureFunction::LOD;
2110 textureFunction.offset = true;
2111 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002112 else if (name == "textureProjLodOffset")
2113 {
2114 textureFunction.method = TextureFunction::LOD;
2115 textureFunction.proj = true;
2116 textureFunction.offset = true;
2117 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002118 else if (name == "texelFetch")
2119 {
2120 textureFunction.method = TextureFunction::FETCH;
2121 }
2122 else if (name == "texelFetchOffset")
2123 {
2124 textureFunction.method = TextureFunction::FETCH;
2125 textureFunction.offset = true;
2126 }
Nicolas Capens46485082014-04-15 13:12:50 -04002127 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002128 {
2129 textureFunction.method = TextureFunction::GRAD;
2130 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002131 else if (name == "textureGradOffset")
2132 {
2133 textureFunction.method = TextureFunction::GRAD;
2134 textureFunction.offset = true;
2135 }
Nicolas Capens46485082014-04-15 13:12:50 -04002136 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002137 {
2138 textureFunction.method = TextureFunction::GRAD;
2139 textureFunction.proj = true;
2140 }
2141 else if (name == "textureProjGradOffset")
2142 {
2143 textureFunction.method = TextureFunction::GRAD;
2144 textureFunction.proj = true;
2145 textureFunction.offset = true;
2146 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002147 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002148
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002149 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002150 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002151 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2152
2153 if (textureFunction.offset)
2154 {
2155 mandatoryArgumentCount++;
2156 }
2157
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002158 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002159
Jamie Madill183bde52014-07-02 15:31:19 -04002160 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002161 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002162 if (bias)
2163 {
2164 textureFunction.method = TextureFunction::LOD0BIAS;
2165 }
2166 else
2167 {
2168 textureFunction.method = TextureFunction::LOD0;
2169 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002170 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002171 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002172 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002173 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002174 }
2175 }
2176
2177 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002178
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002179 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002181
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002182 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002183 {
2184 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2185 {
2186 out << "texture_";
2187 (*arg)->traverse(this);
2188 out << ", sampler_";
2189 }
2190
2191 (*arg)->traverse(this);
2192
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002193 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002194 {
2195 out << ", ";
2196 }
2197 }
2198
2199 out << ")";
2200
2201 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002202 }
2203 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002204 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002205 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2206 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2207 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2208 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2209 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2210 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2211 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2212 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2213 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2214 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2215 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2216 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2217 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2218 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2219 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2220 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2221 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2222 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2223 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002224 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002225 {
Jamie Madill033dae62014-06-18 12:56:28 -04002226 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002227 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002228 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2229 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002230 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002231 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2232 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2233 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2234 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2235 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2236 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002237 case EOpMod:
2238 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002239 // We need to look at the number of components in both arguments
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002240 const int modValue = (*node->getSequence())[0]->getAsTyped()->getNominalSize() * 10 +
2241 (*node->getSequence())[1]->getAsTyped()->getNominalSize();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002242 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002243 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002244 case 11: mUsesMod1 = true; break;
2245 case 22: mUsesMod2v = true; break;
2246 case 21: mUsesMod2f = true; break;
2247 case 33: mUsesMod3v = true; break;
2248 case 31: mUsesMod3f = true; break;
2249 case 44: mUsesMod4v = true; break;
2250 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002251 default: UNREACHABLE();
2252 }
2253
2254 outputTriplet(visit, "mod(", ", ", ")");
2255 }
2256 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002257 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002258 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002259 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2260 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize())
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002261 {
2262 case 1: mUsesAtan2_1 = true; break;
2263 case 2: mUsesAtan2_2 = true; break;
2264 case 3: mUsesAtan2_3 = true; break;
2265 case 4: mUsesAtan2_4 = true; break;
2266 default: UNREACHABLE();
2267 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002268 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 break;
2270 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2271 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2272 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2273 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2274 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2275 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2276 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2277 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2278 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002279 case EOpFaceForward:
2280 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002281 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002282 {
2283 case 1: mUsesFaceforward1 = true; break;
2284 case 2: mUsesFaceforward2 = true; break;
2285 case 3: mUsesFaceforward3 = true; break;
2286 case 4: mUsesFaceforward4 = true; break;
2287 default: UNREACHABLE();
2288 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002289
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002290 outputTriplet(visit, "faceforward(", ", ", ")");
2291 }
2292 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2294 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2295 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 default: UNREACHABLE();
2297 }
2298
2299 return true;
2300}
2301
2302bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2303{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002304 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002306 if (node->usesTernaryOperator())
2307 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002308 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002309 }
2310 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002312 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002313
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002314 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002315
2316 node->getCondition()->traverse(this);
2317
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002318 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002319
Jamie Madill075edd82013-07-08 13:30:19 -04002320 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002321 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002323 bool discard = false;
2324
daniel@transgaming.combb885322010-04-15 20:45:24 +00002325 if (node->getTrueBlock())
2326 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002327 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002328
2329 // Detect true discard
2330 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002331 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332
Jamie Madill075edd82013-07-08 13:30:19 -04002333 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002334 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002335
2336 if (node->getFalseBlock())
2337 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002338 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002339
Jamie Madill075edd82013-07-08 13:30:19 -04002340 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002341 out << "{\n";
2342
Jamie Madill075edd82013-07-08 13:30:19 -04002343 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002344 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002345
Jamie Madill075edd82013-07-08 13:30:19 -04002346 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002347 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002348
2349 // Detect false discard
2350 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2351 }
2352
2353 // ANGLE issue 486: Detect problematic conditional discard
2354 if (discard && FindSideEffectRewriting::search(node))
2355 {
2356 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002357 }
2358 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359
2360 return false;
2361}
2362
2363void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2364{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002365 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366}
2367
2368bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2369{
Nicolas Capens655fe362014-04-11 13:12:34 -04002370 mNestedLoopDepth++;
2371
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002372 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2373
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002374 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002375 {
2376 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2377 }
2378
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002379 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002380 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002381 if (handleExcessiveLoop(node))
2382 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002383 mInsideDiscontinuousLoop = wasDiscontinuous;
2384 mNestedLoopDepth--;
2385
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002386 return false;
2387 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002388 }
2389
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002390 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391
alokp@chromium.org52813552010-11-16 18:36:09 +00002392 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002394 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002395
Jamie Madill075edd82013-07-08 13:30:19 -04002396 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002397 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398 }
2399 else
2400 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002401 out << "{for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002402
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 if (node->getInit())
2404 {
2405 node->getInit()->traverse(this);
2406 }
2407
2408 out << "; ";
2409
alokp@chromium.org52813552010-11-16 18:36:09 +00002410 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002412 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 }
2414
2415 out << "; ";
2416
alokp@chromium.org52813552010-11-16 18:36:09 +00002417 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002419 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420 }
2421
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002422 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002423
Jamie Madill075edd82013-07-08 13:30:19 -04002424 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002425 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 }
2427
2428 if (node->getBody())
2429 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002430 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431 }
2432
Jamie Madill075edd82013-07-08 13:30:19 -04002433 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002434 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435
alokp@chromium.org52813552010-11-16 18:36:09 +00002436 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002437 {
Jamie Madill075edd82013-07-08 13:30:19 -04002438 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439 out << "while(\n";
2440
alokp@chromium.org52813552010-11-16 18:36:09 +00002441 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442
daniel@transgaming.com73536982012-03-21 20:45:49 +00002443 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 }
2445
daniel@transgaming.com73536982012-03-21 20:45:49 +00002446 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002448 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002449 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002450
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002451 return false;
2452}
2453
2454bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2455{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002456 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002457
2458 switch (node->getFlowOp())
2459 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002460 case EOpKill:
2461 outputTriplet(visit, "discard;\n", "", "");
2462 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002463 case EOpBreak:
2464 if (visit == PreVisit)
2465 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002466 if (mNestedLoopDepth > 1)
2467 {
2468 mUsesNestedBreak = true;
2469 }
2470
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002471 if (mExcessiveLoopIndex)
2472 {
2473 out << "{Break";
2474 mExcessiveLoopIndex->traverse(this);
2475 out << " = true; break;}\n";
2476 }
2477 else
2478 {
2479 out << "break;\n";
2480 }
2481 }
2482 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002483 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002484 case EOpReturn:
2485 if (visit == PreVisit)
2486 {
2487 if (node->getExpression())
2488 {
2489 out << "return ";
2490 }
2491 else
2492 {
2493 out << "return;\n";
2494 }
2495 }
2496 else if (visit == PostVisit)
2497 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002498 if (node->getExpression())
2499 {
2500 out << ";\n";
2501 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002502 }
2503 break;
2504 default: UNREACHABLE();
2505 }
2506
2507 return true;
2508}
2509
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002510void OutputHLSL::traverseStatements(TIntermNode *node)
2511{
2512 if (isSingleStatement(node))
2513 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002514 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002515 }
2516
2517 node->traverse(this);
2518}
2519
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002520bool OutputHLSL::isSingleStatement(TIntermNode *node)
2521{
2522 TIntermAggregate *aggregate = node->getAsAggregate();
2523
2524 if (aggregate)
2525 {
2526 if (aggregate->getOp() == EOpSequence)
2527 {
2528 return false;
2529 }
2530 else
2531 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002532 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002533 {
2534 if (!isSingleStatement(*sit))
2535 {
2536 return false;
2537 }
2538 }
2539
2540 return true;
2541 }
2542 }
2543
2544 return true;
2545}
2546
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002547// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2548// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002549bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2550{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002551 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002552 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002553
2554 // Parse loops of the form:
2555 // for(int index = initial; index [comparator] limit; index += increment)
2556 TIntermSymbol *index = NULL;
2557 TOperator comparator = EOpNull;
2558 int initial = 0;
2559 int limit = 0;
2560 int increment = 0;
2561
2562 // Parse index name and intial value
2563 if (node->getInit())
2564 {
2565 TIntermAggregate *init = node->getInit()->getAsAggregate();
2566
2567 if (init)
2568 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002569 TIntermSequence *sequence = init->getSequence();
2570 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002571
2572 if (variable && variable->getQualifier() == EvqTemporary)
2573 {
2574 TIntermBinary *assign = variable->getAsBinaryNode();
2575
2576 if (assign->getOp() == EOpInitialize)
2577 {
2578 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2579 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2580
2581 if (symbol && constant)
2582 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002583 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002584 {
2585 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002586 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002587 }
2588 }
2589 }
2590 }
2591 }
2592 }
2593
2594 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002595 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002596 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002597 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002598
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002599 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2600 {
2601 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2602
2603 if (constant)
2604 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002605 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002606 {
2607 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002608 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002609 }
2610 }
2611 }
2612 }
2613
2614 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002615 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002616 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002617 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2618 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002619
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002620 if (binaryTerminal)
2621 {
2622 TOperator op = binaryTerminal->getOp();
2623 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2624
2625 if (constant)
2626 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002627 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002628 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002629 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002630
2631 switch (op)
2632 {
2633 case EOpAddAssign: increment = value; break;
2634 case EOpSubAssign: increment = -value; break;
2635 default: UNIMPLEMENTED();
2636 }
2637 }
2638 }
2639 }
2640 else if (unaryTerminal)
2641 {
2642 TOperator op = unaryTerminal->getOp();
2643
2644 switch (op)
2645 {
2646 case EOpPostIncrement: increment = 1; break;
2647 case EOpPostDecrement: increment = -1; break;
2648 case EOpPreIncrement: increment = 1; break;
2649 case EOpPreDecrement: increment = -1; break;
2650 default: UNIMPLEMENTED();
2651 }
2652 }
2653 }
2654
2655 if (index != NULL && comparator != EOpNull && increment != 0)
2656 {
2657 if (comparator == EOpLessThanEqual)
2658 {
2659 comparator = EOpLessThan;
2660 limit += 1;
2661 }
2662
2663 if (comparator == EOpLessThan)
2664 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002665 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002666
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002667 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002668 {
2669 return false; // Not an excessive loop
2670 }
2671
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002672 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2673 mExcessiveLoopIndex = index;
2674
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002675 out << "{int ";
2676 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002677 out << ";\n"
2678 "bool Break";
2679 index->traverse(this);
2680 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002681
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002682 bool firstLoopFragment = true;
2683
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002684 while (iterations > 0)
2685 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002686 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002687
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002688 if (!firstLoopFragment)
2689 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002690 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002691 index->traverse(this);
2692 out << ") {\n";
2693 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002694
2695 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2696 {
2697 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2698 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002699
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002700 // for(int index = initial; index < clampedLimit; index += increment)
2701
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002702 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002703 index->traverse(this);
2704 out << " = ";
2705 out << initial;
2706
2707 out << "; ";
2708 index->traverse(this);
2709 out << " < ";
2710 out << clampedLimit;
2711
2712 out << "; ";
2713 index->traverse(this);
2714 out << " += ";
2715 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002716 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002717
Jamie Madill075edd82013-07-08 13:30:19 -04002718 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002719 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002720
2721 if (node->getBody())
2722 {
2723 node->getBody()->traverse(this);
2724 }
2725
Jamie Madill075edd82013-07-08 13:30:19 -04002726 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002727 out << ";}\n";
2728
2729 if (!firstLoopFragment)
2730 {
2731 out << "}\n";
2732 }
2733
2734 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002735
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002736 initial += MAX_LOOP_ITERATIONS * increment;
2737 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002738 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002739
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002740 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002741
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002742 mExcessiveLoopIndex = restoreIndex;
2743
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002744 return true;
2745 }
2746 else UNIMPLEMENTED();
2747 }
2748
2749 return false; // Not handled as an excessive loop
2750}
2751
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002752void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002753{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002754 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002755
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002756 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002757 {
2758 out << preString;
2759 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002760 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002761 {
2762 out << inString;
2763 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002764 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002765 {
2766 out << postString;
2767 }
2768}
2769
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002770void OutputHLSL::outputLineDirective(int line)
2771{
2772 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2773 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002774 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002775 mBody << "#line " << line;
2776
2777 if (mContext.sourcePath)
2778 {
2779 mBody << " \"" << mContext.sourcePath << "\"";
2780 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002781
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002782 mBody << "\n";
2783 }
2784}
2785
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002786TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2787{
2788 TQualifier qualifier = symbol->getQualifier();
2789 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002790 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002791
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002792 if (name.empty()) // HLSL demands named arguments, also for prototypes
2793 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002794 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002795 }
2796 else
2797 {
Jamie Madill033dae62014-06-18 12:56:28 -04002798 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002799 }
2800
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002801 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2802 {
Jamie Madill033dae62014-06-18 12:56:28 -04002803 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002804 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002805 }
2806
Jamie Madill033dae62014-06-18 12:56:28 -04002807 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808}
2809
2810TString OutputHLSL::initializer(const TType &type)
2811{
2812 TString string;
2813
Jamie Madill94bf7f22013-07-08 13:31:15 -04002814 size_t size = type.getObjectSize();
2815 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002817 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818
Jamie Madill94bf7f22013-07-08 13:31:15 -04002819 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002820 {
2821 string += ", ";
2822 }
2823 }
2824
daniel@transgaming.comead23042010-04-29 03:35:36 +00002825 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002826}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002827
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002828void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2829{
2830 TInfoSinkBase &out = mBody;
2831
2832 if (visit == PreVisit)
2833 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002834 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002835
2836 out << name + "(";
2837 }
2838 else if (visit == InVisit)
2839 {
2840 out << ", ";
2841 }
2842 else if (visit == PostVisit)
2843 {
2844 out << ")";
2845 }
2846}
2847
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002848const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2849{
2850 TInfoSinkBase &out = mBody;
2851
Jamie Madill98493dd2013-07-08 14:39:03 -04002852 const TStructure* structure = type.getStruct();
2853 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002854 {
Jamie Madill033dae62014-06-18 12:56:28 -04002855 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002856
Jamie Madill98493dd2013-07-08 14:39:03 -04002857 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002858
Jamie Madill98493dd2013-07-08 14:39:03 -04002859 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002860 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002861 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002862 constUnion = writeConstantUnion(*fieldType, constUnion);
2863
Jamie Madill98493dd2013-07-08 14:39:03 -04002864 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002865 {
2866 out << ", ";
2867 }
2868 }
2869
2870 out << ")";
2871 }
2872 else
2873 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002874 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002875 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002876
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002877 if (writeType)
2878 {
Jamie Madill033dae62014-06-18 12:56:28 -04002879 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002880 }
2881
Jamie Madill94bf7f22013-07-08 13:31:15 -04002882 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002883 {
2884 switch (constUnion->getType())
2885 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002886 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002887 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002888 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002889 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002890 default: UNREACHABLE();
2891 }
2892
2893 if (i != size - 1)
2894 {
2895 out << ", ";
2896 }
2897 }
2898
2899 if (writeType)
2900 {
2901 out << ")";
2902 }
2903 }
2904
2905 return constUnion;
2906}
2907
Jamie Madill77f74852014-07-08 15:02:34 -04002908class DeclareVaryingTraverser : public GetVariableTraverser<Varying>
Jamie Madill47fdd132013-08-30 13:21:04 -04002909{
Jamie Madill77f74852014-07-08 15:02:34 -04002910 public:
2911 DeclareVaryingTraverser(std::vector<Varying> *output,
2912 InterpolationType interpolation)
2913 : GetVariableTraverser(output),
2914 mInterpolation(interpolation)
2915 {}
Jamie Madill47fdd132013-08-30 13:21:04 -04002916
Jamie Madill77f74852014-07-08 15:02:34 -04002917 private:
2918 void visitVariable(Varying *varying)
Jamie Madill47fdd132013-08-30 13:21:04 -04002919 {
Jamie Madill77f74852014-07-08 15:02:34 -04002920 varying->interpolation = mInterpolation;
Jamie Madill47fdd132013-08-30 13:21:04 -04002921 }
Jamie Madill47fdd132013-08-30 13:21:04 -04002922
Jamie Madill77f74852014-07-08 15:02:34 -04002923 InterpolationType mInterpolation;
2924};
Jamie Madill28167c62013-08-30 13:21:10 -04002925
Jamie Madill77f74852014-07-08 15:02:34 -04002926void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier,
2927 const TString &name, std::vector<Varying> &fieldsOut)
2928{
2929 DeclareVaryingTraverser traverser(&fieldsOut, GetInterpolationType(baseTypeQualifier));
2930 traverser.traverse(type, name);
Jamie Madill47fdd132013-08-30 13:21:04 -04002931}
2932
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002933}