blob: 1bf1181af0ea2101a0a1bfded1658a4e11be039d [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
Jamie Madill9fe25e92014-07-18 10:33:08 -0400245const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
246{
247 return mUniformHLSL->getUniformRegisterMap();
248}
249
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000250int OutputHLSL::vectorSize(const TType &type) const
251{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000252 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000253 int arraySize = type.isArray() ? type.getArraySize() : 1;
254
255 return elementSize * arraySize;
256}
257
Jamie Madill98493dd2013-07-08 14:39:03 -0400258TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400259{
260 TString init;
261
262 TString preIndentString;
263 TString fullIndentString;
264
265 for (int spaces = 0; spaces < (indent * 4); spaces++)
266 {
267 preIndentString += ' ';
268 }
269
270 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
271 {
272 fullIndentString += ' ';
273 }
274
275 init += preIndentString + "{\n";
276
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 const TFieldList &fields = structure.fields();
278 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400279 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400280 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400281 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400282 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400283
Jamie Madill98493dd2013-07-08 14:39:03 -0400284 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400285 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400287 }
288 else
289 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400290 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400291 }
292 }
293
294 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
295
296 return init;
297}
298
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299void OutputHLSL::header()
300{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000301 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000303 TString varyings;
304 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400305 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000306
Jamie Madill829f59e2013-11-13 19:40:54 -0500307 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400308 {
309 TIntermTyped *structNode = flaggedStructIt->first;
310 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400311 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400312 const TString &originalName = mFlaggedStructOriginalNames[structNode];
313
Jamie Madill033dae62014-06-18 12:56:28 -0400314 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400315 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400316 flaggedStructs += "\n";
317 }
318
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000319 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
320 {
321 const TType &type = varying->second->getType();
322 const TString &name = varying->second->getSymbol();
323
324 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400325 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
326 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400327
Jamie Madill94599662013-08-30 13:21:10 -0400328 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000329 }
330
331 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
332 {
333 const TType &type = attribute->second->getType();
334 const TString &name = attribute->second->getSymbol();
335
Jamie Madill033dae62014-06-18 12:56:28 -0400336 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400337
Jamie Madillf2575982014-06-25 16:04:54 -0400338 sh::Attribute attributeVar(GLVariableType(type), GLVariablePrecision(type), name.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400339 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
340 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000341 }
342
Jamie Madill8daaba12014-06-13 10:04:33 -0400343 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400344
Jamie Madillf91ce812014-06-13 10:04:34 -0400345 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
346 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
347
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500348 if (mUsesDiscardRewriting)
349 {
350 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
351 }
352
Nicolas Capens655fe362014-04-11 13:12:34 -0400353 if (mUsesNestedBreak)
354 {
355 out << "#define ANGLE_USES_NESTED_BREAK" << "\n";
356 }
357
Jamie Madill183bde52014-07-02 15:31:19 -0400358 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000360 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000361 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000362
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000363 out << "// Varyings\n";
364 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400365 out << "\n";
366
367 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000368 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500369 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000370 {
Jamie Madill46131a32013-06-20 11:55:50 -0400371 const TString &variableName = outputVariableIt->first;
372 const TType &variableType = outputVariableIt->second->getType();
373 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
374
Jamie Madill033dae62014-06-18 12:56:28 -0400375 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400376 " = " + initializer(variableType) + ";\n";
377
Jamie Madillf2575982014-06-25 16:04:54 -0400378 sh::Attribute outputVar(GLVariableType(variableType), GLVariablePrecision(variableType), variableName.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400379 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400380 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000381 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000382 }
Jamie Madill46131a32013-06-20 11:55:50 -0400383 else
384 {
385 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
386
387 out << "static float4 gl_Color[" << numColorValues << "] =\n"
388 "{\n";
389 for (unsigned int i = 0; i < numColorValues; i++)
390 {
391 out << " float4(0, 0, 0, 0)";
392 if (i + 1 != numColorValues)
393 {
394 out << ",";
395 }
396 out << "\n";
397 }
398
399 out << "};\n";
400 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000401
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400402 if (mUsesFragDepth)
403 {
404 out << "static float gl_Depth = 0.0;\n";
405 }
406
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000407 if (mUsesFragCoord)
408 {
409 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
410 }
411
412 if (mUsesPointCoord)
413 {
414 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
415 }
416
417 if (mUsesFrontFacing)
418 {
419 out << "static bool gl_FrontFacing = false;\n";
420 }
421
422 out << "\n";
423
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000424 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000425 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000426 out << "struct gl_DepthRangeParameters\n"
427 "{\n"
428 " float near;\n"
429 " float far;\n"
430 " float diff;\n"
431 "};\n"
432 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000433 }
434
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000435 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000436 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000437 out << "cbuffer DriverConstants : register(b1)\n"
438 "{\n";
439
440 if (mUsesDepthRange)
441 {
442 out << " float3 dx_DepthRange : packoffset(c0);\n";
443 }
444
445 if (mUsesFragCoord)
446 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000447 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000448 }
449
450 if (mUsesFragCoord || mUsesFrontFacing)
451 {
452 out << " float3 dx_DepthFront : packoffset(c2);\n";
453 }
454
455 out << "};\n";
456 }
457 else
458 {
459 if (mUsesDepthRange)
460 {
461 out << "uniform float3 dx_DepthRange : register(c0);";
462 }
463
464 if (mUsesFragCoord)
465 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000466 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000467 }
468
469 if (mUsesFragCoord || mUsesFrontFacing)
470 {
471 out << "uniform float3 dx_DepthFront : register(c2);\n";
472 }
473 }
474
475 out << "\n";
476
477 if (mUsesDepthRange)
478 {
479 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
480 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000481 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000482
Jamie Madillf91ce812014-06-13 10:04:34 -0400483 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000484 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400485 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000486 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400487 out << flaggedStructs;
488 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000489 }
490
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000491 if (usingMRTExtension && mNumRenderTargets > 1)
492 {
493 out << "#define GL_USES_MRT\n";
494 }
495
496 if (mUsesFragColor)
497 {
498 out << "#define GL_USES_FRAG_COLOR\n";
499 }
500
501 if (mUsesFragData)
502 {
503 out << "#define GL_USES_FRAG_DATA\n";
504 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000505 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000506 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000508 out << "// Attributes\n";
509 out << attributes;
510 out << "\n"
511 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400512
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000513 if (mUsesPointSize)
514 {
515 out << "static float gl_PointSize = float(1);\n";
516 }
517
518 out << "\n"
519 "// Varyings\n";
520 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000521 out << "\n";
522
523 if (mUsesDepthRange)
524 {
525 out << "struct gl_DepthRangeParameters\n"
526 "{\n"
527 " float near;\n"
528 " float far;\n"
529 " float diff;\n"
530 "};\n"
531 "\n";
532 }
533
534 if (mOutputType == SH_HLSL11_OUTPUT)
535 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000536 if (mUsesDepthRange)
537 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000538 out << "cbuffer DriverConstants : register(b1)\n"
539 "{\n"
540 " float3 dx_DepthRange : packoffset(c0);\n"
541 "};\n"
542 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000543 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000544 }
545 else
546 {
547 if (mUsesDepthRange)
548 {
549 out << "uniform float3 dx_DepthRange : register(c0);\n";
550 }
551
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000552 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000553 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000554 }
555
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000556 if (mUsesDepthRange)
557 {
558 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
559 "\n";
560 }
561
Jamie Madillf91ce812014-06-13 10:04:34 -0400562 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000563 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400564 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000565 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400566 out << flaggedStructs;
567 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000568 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400569 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000570
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400571 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
572 {
573 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400574 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000575 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400576 switch(textureFunction->sampler)
577 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400578 case EbtSampler2D: out << "int2 "; break;
579 case EbtSampler3D: out << "int3 "; break;
580 case EbtSamplerCube: out << "int2 "; break;
581 case EbtSampler2DArray: out << "int3 "; break;
582 case EbtISampler2D: out << "int2 "; break;
583 case EbtISampler3D: out << "int3 "; break;
584 case EbtISamplerCube: out << "int2 "; break;
585 case EbtISampler2DArray: out << "int3 "; break;
586 case EbtUSampler2D: out << "int2 "; break;
587 case EbtUSampler3D: out << "int3 "; break;
588 case EbtUSamplerCube: out << "int2 "; break;
589 case EbtUSampler2DArray: out << "int3 "; break;
590 case EbtSampler2DShadow: out << "int2 "; break;
591 case EbtSamplerCubeShadow: out << "int2 "; break;
592 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400593 default: UNREACHABLE();
594 }
595 }
596 else // Sampling function
597 {
598 switch(textureFunction->sampler)
599 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400600 case EbtSampler2D: out << "float4 "; break;
601 case EbtSampler3D: out << "float4 "; break;
602 case EbtSamplerCube: out << "float4 "; break;
603 case EbtSampler2DArray: out << "float4 "; break;
604 case EbtISampler2D: out << "int4 "; break;
605 case EbtISampler3D: out << "int4 "; break;
606 case EbtISamplerCube: out << "int4 "; break;
607 case EbtISampler2DArray: out << "int4 "; break;
608 case EbtUSampler2D: out << "uint4 "; break;
609 case EbtUSampler3D: out << "uint4 "; break;
610 case EbtUSamplerCube: out << "uint4 "; break;
611 case EbtUSampler2DArray: out << "uint4 "; break;
612 case EbtSampler2DShadow: out << "float "; break;
613 case EbtSamplerCubeShadow: out << "float "; break;
614 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400615 default: UNREACHABLE();
616 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000617 }
618
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400619 // Function name
620 out << textureFunction->name();
621
622 // Argument list
623 int hlslCoords = 4;
624
625 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000626 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400627 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000628 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400629 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
630 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
631 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000632 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400633
Nicolas Capens75fb4752013-07-10 15:14:47 -0400634 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000635 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400636 case TextureFunction::IMPLICIT: break;
637 case TextureFunction::BIAS: hlslCoords = 4; break;
638 case TextureFunction::LOD: hlslCoords = 4; break;
639 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400640 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400641 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000642 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400643 }
644 else if (mOutputType == SH_HLSL11_OUTPUT)
645 {
646 switch(textureFunction->sampler)
647 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400648 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
649 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
650 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
651 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
652 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
653 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500654 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400655 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
656 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
657 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500658 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400659 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
660 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
661 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
662 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400663 default: UNREACHABLE();
664 }
665 }
666 else UNREACHABLE();
667
Nicolas Capensfc014542014-02-18 14:47:13 -0500668 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400669 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500670 switch(textureFunction->coords)
671 {
672 case 2: out << ", int2 t"; break;
673 case 3: out << ", int3 t"; break;
674 default: UNREACHABLE();
675 }
676 }
677 else // Floating-point coordinates (except textureSize)
678 {
679 switch(textureFunction->coords)
680 {
681 case 1: out << ", int lod"; break; // textureSize()
682 case 2: out << ", float2 t"; break;
683 case 3: out << ", float3 t"; break;
684 case 4: out << ", float4 t"; break;
685 default: UNREACHABLE();
686 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000687 }
688
Nicolas Capensd11d5492014-02-19 17:06:10 -0500689 if (textureFunction->method == TextureFunction::GRAD)
690 {
691 switch(textureFunction->sampler)
692 {
693 case EbtSampler2D:
694 case EbtISampler2D:
695 case EbtUSampler2D:
696 case EbtSampler2DArray:
697 case EbtISampler2DArray:
698 case EbtUSampler2DArray:
699 case EbtSampler2DShadow:
700 case EbtSampler2DArrayShadow:
701 out << ", float2 ddx, float2 ddy";
702 break;
703 case EbtSampler3D:
704 case EbtISampler3D:
705 case EbtUSampler3D:
706 case EbtSamplerCube:
707 case EbtISamplerCube:
708 case EbtUSamplerCube:
709 case EbtSamplerCubeShadow:
710 out << ", float3 ddx, float3 ddy";
711 break;
712 default: UNREACHABLE();
713 }
714 }
715
Nicolas Capens75fb4752013-07-10 15:14:47 -0400716 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000717 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400718 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400719 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400720 case TextureFunction::LOD: out << ", float lod"; break;
721 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400722 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400723 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500724 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500725 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400726 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000727 }
728
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500729 if (textureFunction->offset)
730 {
731 switch(textureFunction->sampler)
732 {
733 case EbtSampler2D: out << ", int2 offset"; break;
734 case EbtSampler3D: out << ", int3 offset"; break;
735 case EbtSampler2DArray: out << ", int2 offset"; break;
736 case EbtISampler2D: out << ", int2 offset"; break;
737 case EbtISampler3D: out << ", int3 offset"; break;
738 case EbtISampler2DArray: out << ", int2 offset"; break;
739 case EbtUSampler2D: out << ", int2 offset"; break;
740 case EbtUSampler3D: out << ", int3 offset"; break;
741 case EbtUSampler2DArray: out << ", int2 offset"; break;
742 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500743 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500744 default: UNREACHABLE();
745 }
746 }
747
Nicolas Capens84cfa122014-04-14 13:48:45 -0400748 if (textureFunction->method == TextureFunction::BIAS ||
749 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500750 {
751 out << ", float bias";
752 }
753
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400754 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400755 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400756
Nicolas Capens75fb4752013-07-10 15:14:47 -0400757 if (textureFunction->method == TextureFunction::SIZE)
758 {
759 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
760 {
761 if (IsSamplerArray(textureFunction->sampler))
762 {
763 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
764 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
765 }
766 else
767 {
768 out << " uint width; uint height; uint numberOfLevels;\n"
769 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
770 }
771 }
772 else if (IsSampler3D(textureFunction->sampler))
773 {
774 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
775 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
776 }
777 else UNREACHABLE();
778
779 switch(textureFunction->sampler)
780 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400781 case EbtSampler2D: out << " return int2(width, height);"; break;
782 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
783 case EbtSamplerCube: out << " return int2(width, height);"; break;
784 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
785 case EbtISampler2D: out << " return int2(width, height);"; break;
786 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
787 case EbtISamplerCube: out << " return int2(width, height);"; break;
788 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
789 case EbtUSampler2D: out << " return int2(width, height);"; break;
790 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
791 case EbtUSamplerCube: out << " return int2(width, height);"; break;
792 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
793 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
794 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
795 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400796 default: UNREACHABLE();
797 }
798 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400799 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400800 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500801 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
802 {
803 out << " float width; float height; float layers; float levels;\n";
804
805 out << " uint mip = 0;\n";
806
807 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
808
809 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
810 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
811 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
812 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
813
814 // FACE_POSITIVE_X = 000b
815 // FACE_NEGATIVE_X = 001b
816 // FACE_POSITIVE_Y = 010b
817 // FACE_NEGATIVE_Y = 011b
818 // FACE_POSITIVE_Z = 100b
819 // FACE_NEGATIVE_Z = 101b
820 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
821
822 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
823 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
824 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
825
826 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
827 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
828 }
829 else if (IsIntegerSampler(textureFunction->sampler) &&
830 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400831 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400832 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400833 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400834 if (IsSamplerArray(textureFunction->sampler))
835 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400836 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400837
Nicolas Capens9edebd62013-08-06 10:59:10 -0400838 if (textureFunction->method == TextureFunction::LOD0)
839 {
840 out << " uint mip = 0;\n";
841 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400842 else if (textureFunction->method == TextureFunction::LOD0BIAS)
843 {
844 out << " uint mip = bias;\n";
845 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400846 else
847 {
848 if (textureFunction->method == TextureFunction::IMPLICIT ||
849 textureFunction->method == TextureFunction::BIAS)
850 {
851 out << " x.GetDimensions(0, width, height, layers, levels);\n"
852 " float2 tSized = float2(t.x * width, t.y * height);\n"
853 " float dx = length(ddx(tSized));\n"
854 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500855 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400856
857 if (textureFunction->method == TextureFunction::BIAS)
858 {
859 out << " lod += bias;\n";
860 }
861 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500862 else if (textureFunction->method == TextureFunction::GRAD)
863 {
864 out << " x.GetDimensions(0, width, height, layers, levels);\n"
865 " float lod = log2(max(length(ddx), length(ddy)));\n";
866 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400867
868 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
869 }
870
871 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400872 }
873 else
874 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400875 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400876
Nicolas Capens9edebd62013-08-06 10:59:10 -0400877 if (textureFunction->method == TextureFunction::LOD0)
878 {
879 out << " uint mip = 0;\n";
880 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400881 else if (textureFunction->method == TextureFunction::LOD0BIAS)
882 {
883 out << " uint mip = bias;\n";
884 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400885 else
886 {
887 if (textureFunction->method == TextureFunction::IMPLICIT ||
888 textureFunction->method == TextureFunction::BIAS)
889 {
890 out << " x.GetDimensions(0, width, height, levels);\n"
891 " float2 tSized = float2(t.x * width, t.y * height);\n"
892 " float dx = length(ddx(tSized));\n"
893 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500894 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400895
896 if (textureFunction->method == TextureFunction::BIAS)
897 {
898 out << " lod += bias;\n";
899 }
900 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500901 else if (textureFunction->method == TextureFunction::LOD)
902 {
903 out << " x.GetDimensions(0, width, height, levels);\n";
904 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500905 else if (textureFunction->method == TextureFunction::GRAD)
906 {
907 out << " x.GetDimensions(0, width, height, levels);\n"
908 " float lod = log2(max(length(ddx), length(ddy)));\n";
909 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400910
911 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
912 }
913
914 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400915 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400916 }
917 else if (IsSampler3D(textureFunction->sampler))
918 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400919 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400920
Nicolas Capens9edebd62013-08-06 10:59:10 -0400921 if (textureFunction->method == TextureFunction::LOD0)
922 {
923 out << " uint mip = 0;\n";
924 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400925 else if (textureFunction->method == TextureFunction::LOD0BIAS)
926 {
927 out << " uint mip = bias;\n";
928 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400929 else
930 {
931 if (textureFunction->method == TextureFunction::IMPLICIT ||
932 textureFunction->method == TextureFunction::BIAS)
933 {
934 out << " x.GetDimensions(0, width, height, depth, levels);\n"
935 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
936 " float dx = length(ddx(tSized));\n"
937 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500938 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400939
940 if (textureFunction->method == TextureFunction::BIAS)
941 {
942 out << " lod += bias;\n";
943 }
944 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500945 else if (textureFunction->method == TextureFunction::GRAD)
946 {
947 out << " x.GetDimensions(0, width, height, depth, levels);\n"
948 " float lod = log2(max(length(ddx), length(ddy)));\n";
949 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400950
951 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
952 }
953
954 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400955 }
956 else UNREACHABLE();
957 }
958
959 out << " return ";
960
961 // HLSL intrinsic
962 if (mOutputType == SH_HLSL9_OUTPUT)
963 {
964 switch(textureFunction->sampler)
965 {
966 case EbtSampler2D: out << "tex2D"; break;
967 case EbtSamplerCube: out << "texCUBE"; break;
968 default: UNREACHABLE();
969 }
970
Nicolas Capens75fb4752013-07-10 15:14:47 -0400971 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400972 {
973 case TextureFunction::IMPLICIT: out << "(s, "; break;
974 case TextureFunction::BIAS: out << "bias(s, "; break;
975 case TextureFunction::LOD: out << "lod(s, "; break;
976 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400977 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400978 default: UNREACHABLE();
979 }
980 }
981 else if (mOutputType == SH_HLSL11_OUTPUT)
982 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500983 if (textureFunction->method == TextureFunction::GRAD)
984 {
985 if (IsIntegerSampler(textureFunction->sampler))
986 {
987 out << "x.Load(";
988 }
989 else if (IsShadowSampler(textureFunction->sampler))
990 {
991 out << "x.SampleCmpLevelZero(s, ";
992 }
993 else
994 {
995 out << "x.SampleGrad(s, ";
996 }
997 }
998 else if (IsIntegerSampler(textureFunction->sampler) ||
999 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001000 {
1001 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001002 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001003 else if (IsShadowSampler(textureFunction->sampler))
1004 {
1005 out << "x.SampleCmp(s, ";
1006 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001007 else
1008 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001009 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001010 {
1011 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1012 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1013 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1014 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001015 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001016 default: UNREACHABLE();
1017 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001018 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001019 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001020 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001021
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001022 // Integer sampling requires integer addresses
1023 TString addressx = "";
1024 TString addressy = "";
1025 TString addressz = "";
1026 TString close = "";
1027
Nicolas Capensfc014542014-02-18 14:47:13 -05001028 if (IsIntegerSampler(textureFunction->sampler) ||
1029 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001030 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001031 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001032 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001033 case 2: out << "int3("; break;
1034 case 3: out << "int4("; break;
1035 default: UNREACHABLE();
1036 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001037
Nicolas Capensfc014542014-02-18 14:47:13 -05001038 // Convert from normalized floating-point to integer
1039 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001040 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001041 addressx = "int(floor(width * frac((";
1042 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001043
Nicolas Capensfc014542014-02-18 14:47:13 -05001044 if (IsSamplerArray(textureFunction->sampler))
1045 {
1046 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1047 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001048 else if (IsSamplerCube(textureFunction->sampler))
1049 {
1050 addressz = "((((";
1051 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001052 else
1053 {
1054 addressz = "int(floor(depth * frac((";
1055 }
1056
1057 close = "))))";
1058 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001059 }
1060 else
1061 {
1062 switch(hlslCoords)
1063 {
1064 case 2: out << "float2("; break;
1065 case 3: out << "float3("; break;
1066 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001067 default: UNREACHABLE();
1068 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001069 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001070
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001071 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001072
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001073 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001074 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001075 switch(textureFunction->coords)
1076 {
1077 case 3: proj = " / t.z"; break;
1078 case 4: proj = " / t.w"; break;
1079 default: UNREACHABLE();
1080 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001081 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001082
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001083 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001084
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001085 if (mOutputType == SH_HLSL9_OUTPUT)
1086 {
1087 if (hlslCoords >= 3)
1088 {
1089 if (textureFunction->coords < 3)
1090 {
1091 out << ", 0";
1092 }
1093 else
1094 {
1095 out << ", t.z" + proj;
1096 }
1097 }
1098
1099 if (hlslCoords == 4)
1100 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001101 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001102 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001103 case TextureFunction::BIAS: out << ", bias"; break;
1104 case TextureFunction::LOD: out << ", lod"; break;
1105 case TextureFunction::LOD0: out << ", 0"; break;
1106 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001107 default: UNREACHABLE();
1108 }
1109 }
1110
1111 out << "));\n";
1112 }
1113 else if (mOutputType == SH_HLSL11_OUTPUT)
1114 {
1115 if (hlslCoords >= 3)
1116 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001117 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1118 {
1119 out << ", face";
1120 }
1121 else
1122 {
1123 out << ", " + addressz + ("t.z" + proj) + close;
1124 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001125 }
1126
Nicolas Capensd11d5492014-02-19 17:06:10 -05001127 if (textureFunction->method == TextureFunction::GRAD)
1128 {
1129 if (IsIntegerSampler(textureFunction->sampler))
1130 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001131 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001132 }
1133 else if (IsShadowSampler(textureFunction->sampler))
1134 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001135 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001136 switch(textureFunction->coords)
1137 {
1138 case 3: out << "), t.z"; break;
1139 case 4: out << "), t.w"; break;
1140 default: UNREACHABLE();
1141 }
1142 }
1143 else
1144 {
1145 out << "), ddx, ddy";
1146 }
1147 }
1148 else if (IsIntegerSampler(textureFunction->sampler) ||
1149 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001150 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001151 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001152 }
1153 else if (IsShadowSampler(textureFunction->sampler))
1154 {
1155 // Compare value
1156 switch(textureFunction->coords)
1157 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001158 case 3: out << "), t.z"; break;
1159 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001160 default: UNREACHABLE();
1161 }
1162 }
1163 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001164 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001165 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001166 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001167 case TextureFunction::IMPLICIT: out << ")"; break;
1168 case TextureFunction::BIAS: out << "), bias"; break;
1169 case TextureFunction::LOD: out << "), lod"; break;
1170 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001171 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001172 default: UNREACHABLE();
1173 }
1174 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001175
1176 if (textureFunction->offset)
1177 {
1178 out << ", offset";
1179 }
1180
1181 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001182 }
1183 else UNREACHABLE();
1184 }
1185
1186 out << "\n"
1187 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001188 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001189 }
1190
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001191 if (mUsesFragCoord)
1192 {
1193 out << "#define GL_USES_FRAG_COORD\n";
1194 }
1195
1196 if (mUsesPointCoord)
1197 {
1198 out << "#define GL_USES_POINT_COORD\n";
1199 }
1200
1201 if (mUsesFrontFacing)
1202 {
1203 out << "#define GL_USES_FRONT_FACING\n";
1204 }
1205
1206 if (mUsesPointSize)
1207 {
1208 out << "#define GL_USES_POINT_SIZE\n";
1209 }
1210
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001211 if (mUsesFragDepth)
1212 {
1213 out << "#define GL_USES_FRAG_DEPTH\n";
1214 }
1215
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001216 if (mUsesDepthRange)
1217 {
1218 out << "#define GL_USES_DEPTH_RANGE\n";
1219 }
1220
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001221 if (mUsesXor)
1222 {
1223 out << "bool xor(bool p, bool q)\n"
1224 "{\n"
1225 " return (p || q) && !(p && q);\n"
1226 "}\n"
1227 "\n";
1228 }
1229
1230 if (mUsesMod1)
1231 {
1232 out << "float mod(float x, float y)\n"
1233 "{\n"
1234 " return x - y * floor(x / y);\n"
1235 "}\n"
1236 "\n";
1237 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001238
1239 if (mUsesMod2v)
1240 {
1241 out << "float2 mod(float2 x, float2 y)\n"
1242 "{\n"
1243 " return x - y * floor(x / y);\n"
1244 "}\n"
1245 "\n";
1246 }
1247
1248 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001249 {
1250 out << "float2 mod(float2 x, float y)\n"
1251 "{\n"
1252 " return x - y * floor(x / y);\n"
1253 "}\n"
1254 "\n";
1255 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001256
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001257 if (mUsesMod3v)
1258 {
1259 out << "float3 mod(float3 x, float3 y)\n"
1260 "{\n"
1261 " return x - y * floor(x / y);\n"
1262 "}\n"
1263 "\n";
1264 }
1265
1266 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001267 {
1268 out << "float3 mod(float3 x, float y)\n"
1269 "{\n"
1270 " return x - y * floor(x / y);\n"
1271 "}\n"
1272 "\n";
1273 }
1274
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001275 if (mUsesMod4v)
1276 {
1277 out << "float4 mod(float4 x, float4 y)\n"
1278 "{\n"
1279 " return x - y * floor(x / y);\n"
1280 "}\n"
1281 "\n";
1282 }
1283
1284 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001285 {
1286 out << "float4 mod(float4 x, float y)\n"
1287 "{\n"
1288 " return x - y * floor(x / y);\n"
1289 "}\n"
1290 "\n";
1291 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001292
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001293 if (mUsesFaceforward1)
1294 {
1295 out << "float faceforward(float N, float I, float Nref)\n"
1296 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001297 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001298 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001299 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001300 " }\n"
1301 " else\n"
1302 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001303 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001304 " }\n"
1305 "}\n"
1306 "\n";
1307 }
1308
1309 if (mUsesFaceforward2)
1310 {
1311 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1312 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001313 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001314 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001315 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001316 " }\n"
1317 " else\n"
1318 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001319 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001320 " }\n"
1321 "}\n"
1322 "\n";
1323 }
1324
1325 if (mUsesFaceforward3)
1326 {
1327 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1328 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001329 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001330 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001331 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001332 " }\n"
1333 " else\n"
1334 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001335 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001336 " }\n"
1337 "}\n"
1338 "\n";
1339 }
1340
1341 if (mUsesFaceforward4)
1342 {
1343 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1344 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001345 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001346 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001347 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001348 " }\n"
1349 " else\n"
1350 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001351 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001352 " }\n"
1353 "}\n"
1354 "\n";
1355 }
1356
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001357 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001358 {
1359 out << "float atanyx(float y, float x)\n"
1360 "{\n"
1361 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1362 " return atan2(y, x);\n"
1363 "}\n";
1364 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001365
1366 if (mUsesAtan2_2)
1367 {
1368 out << "float2 atanyx(float2 y, float2 x)\n"
1369 "{\n"
1370 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1371 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1372 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1373 "}\n";
1374 }
1375
1376 if (mUsesAtan2_3)
1377 {
1378 out << "float3 atanyx(float3 y, float3 x)\n"
1379 "{\n"
1380 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1381 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1382 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1383 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1384 "}\n";
1385 }
1386
1387 if (mUsesAtan2_4)
1388 {
1389 out << "float4 atanyx(float4 y, float4 x)\n"
1390 "{\n"
1391 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1392 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1393 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1394 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1395 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1396 "}\n";
1397 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001398}
1399
1400void OutputHLSL::visitSymbol(TIntermSymbol *node)
1401{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001402 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001403
Jamie Madill570e04d2013-06-21 09:15:33 -04001404 // Handle accessing std140 structs by value
1405 if (mFlaggedStructMappedNames.count(node) > 0)
1406 {
1407 out << mFlaggedStructMappedNames[node];
1408 return;
1409 }
1410
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001411 TString name = node->getSymbol();
1412
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001413 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001414 {
1415 mUsesDepthRange = true;
1416 out << name;
1417 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001418 else
1419 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001420 TQualifier qualifier = node->getQualifier();
1421
1422 if (qualifier == EvqUniform)
1423 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001424 const TType& nodeType = node->getType();
1425 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1426
1427 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001428 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001429 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001430 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001431 else
1432 {
1433 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001434 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001435
Jamie Madill033dae62014-06-18 12:56:28 -04001436 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001437 }
Jamie Madill19571812013-08-12 15:26:34 -07001438 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001439 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001440 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001441 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001442 }
Jamie Madill033dae62014-06-18 12:56:28 -04001443 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001444 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001445 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001446 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001447 }
Jamie Madill19571812013-08-12 15:26:34 -07001448 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001449 {
1450 mReferencedOutputVariables[name] = node;
1451 out << "out_" << name;
1452 }
1453 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001454 {
1455 out << "gl_Color[0]";
1456 mUsesFragColor = true;
1457 }
1458 else if (qualifier == EvqFragData)
1459 {
1460 out << "gl_Color";
1461 mUsesFragData = true;
1462 }
1463 else if (qualifier == EvqFragCoord)
1464 {
1465 mUsesFragCoord = true;
1466 out << name;
1467 }
1468 else if (qualifier == EvqPointCoord)
1469 {
1470 mUsesPointCoord = true;
1471 out << name;
1472 }
1473 else if (qualifier == EvqFrontFacing)
1474 {
1475 mUsesFrontFacing = true;
1476 out << name;
1477 }
1478 else if (qualifier == EvqPointSize)
1479 {
1480 mUsesPointSize = true;
1481 out << name;
1482 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001483 else if (name == "gl_FragDepthEXT")
1484 {
1485 mUsesFragDepth = true;
1486 out << "gl_Depth";
1487 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001488 else if (qualifier == EvqInternal)
1489 {
1490 out << name;
1491 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001492 else
1493 {
Jamie Madill033dae62014-06-18 12:56:28 -04001494 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001495 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001496 }
1497}
1498
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001499void OutputHLSL::visitRaw(TIntermRaw *node)
1500{
1501 mBody << node->getRawText();
1502}
1503
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001504bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1505{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001506 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001507
Jamie Madill570e04d2013-06-21 09:15:33 -04001508 // Handle accessing std140 structs by value
1509 if (mFlaggedStructMappedNames.count(node) > 0)
1510 {
1511 out << mFlaggedStructMappedNames[node];
1512 return false;
1513 }
1514
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001515 switch (node->getOp())
1516 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001517 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001518 case EOpInitialize:
1519 if (visit == PreVisit)
1520 {
1521 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1522 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1523 // new variable is created before the assignment is evaluated), so we need to convert
1524 // this to "float t = x, x = t;".
1525
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001526 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1527 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001528
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001529 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1530 expression->traverse(&searchSymbol);
1531 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001532
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001533 if (sameSymbol)
1534 {
1535 // Type already printed
1536 out << "t" + str(mUniqueIndex) + " = ";
1537 expression->traverse(this);
1538 out << ", ";
1539 symbolNode->traverse(this);
1540 out << " = t" + str(mUniqueIndex);
1541
1542 mUniqueIndex++;
1543 return false;
1544 }
1545 }
1546 else if (visit == InVisit)
1547 {
1548 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001549 }
1550 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001551 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1552 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1553 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1554 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1555 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1556 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001557 if (visit == PreVisit)
1558 {
1559 out << "(";
1560 }
1561 else if (visit == InVisit)
1562 {
1563 out << " = mul(";
1564 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001565 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001566 }
1567 else
1568 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001569 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001570 }
1571 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001572 case EOpMatrixTimesMatrixAssign:
1573 if (visit == PreVisit)
1574 {
1575 out << "(";
1576 }
1577 else if (visit == InVisit)
1578 {
1579 out << " = mul(";
1580 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001581 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001582 }
1583 else
1584 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001585 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001586 }
1587 break;
1588 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001589 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001590 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001591 const TType& leftType = node->getLeft()->getType();
1592 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001593 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001594 if (visit == PreVisit)
1595 {
1596 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1597 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001598 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001599 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001600 return false;
1601 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001602 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001603 else
1604 {
1605 outputTriplet(visit, "", "[", "]");
1606 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001607 }
1608 break;
1609 case EOpIndexIndirect:
1610 // We do not currently support indirect references to interface blocks
1611 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1612 outputTriplet(visit, "", "[", "]");
1613 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001614 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001615 if (visit == InVisit)
1616 {
1617 const TStructure* structure = node->getLeft()->getType().getStruct();
1618 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1619 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001620 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001621
1622 return false;
1623 }
1624 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001625 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001626 if (visit == InVisit)
1627 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001628 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1629 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1630 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001631 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001632
1633 return false;
1634 }
1635 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001636 case EOpVectorSwizzle:
1637 if (visit == InVisit)
1638 {
1639 out << ".";
1640
1641 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1642
1643 if (swizzle)
1644 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001645 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001647 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001648 {
1649 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1650
1651 if (element)
1652 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001653 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001654
1655 switch (i)
1656 {
1657 case 0: out << "x"; break;
1658 case 1: out << "y"; break;
1659 case 2: out << "z"; break;
1660 case 3: out << "w"; break;
1661 default: UNREACHABLE();
1662 }
1663 }
1664 else UNREACHABLE();
1665 }
1666 }
1667 else UNREACHABLE();
1668
1669 return false; // Fully processed
1670 }
1671 break;
1672 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1673 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1674 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1675 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001676 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001677 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001678 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001679 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001680 if (node->getOp() == EOpEqual)
1681 {
1682 outputTriplet(visit, "(", " == ", ")");
1683 }
1684 else
1685 {
1686 outputTriplet(visit, "(", " != ", ")");
1687 }
1688 }
1689 else if (node->getLeft()->getBasicType() == EbtStruct)
1690 {
1691 if (node->getOp() == EOpEqual)
1692 {
1693 out << "(";
1694 }
1695 else
1696 {
1697 out << "!(";
1698 }
1699
Jamie Madill98493dd2013-07-08 14:39:03 -04001700 const TStructure &structure = *node->getLeft()->getType().getStruct();
1701 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001702
Jamie Madill98493dd2013-07-08 14:39:03 -04001703 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001704 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001705 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001706
1707 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001708 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001709 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001710 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001711
Jamie Madill98493dd2013-07-08 14:39:03 -04001712 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001713 {
1714 out << " && ";
1715 }
1716 }
1717
1718 out << ")";
1719
1720 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001721 }
1722 else
1723 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001724 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001725
1726 if (node->getOp() == EOpEqual)
1727 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001728 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001729 }
1730 else
1731 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001732 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001733 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001734 }
1735 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1737 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1738 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1739 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1740 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001741 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001742 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1743 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001744 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001745 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001746 if (node->getRight()->hasSideEffects())
1747 {
1748 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1749 return false;
1750 }
1751 else
1752 {
1753 outputTriplet(visit, "(", " || ", ")");
1754 return true;
1755 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001756 case EOpLogicalXor:
1757 mUsesXor = true;
1758 outputTriplet(visit, "xor(", ", ", ")");
1759 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001760 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001761 if (node->getRight()->hasSideEffects())
1762 {
1763 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1764 return false;
1765 }
1766 else
1767 {
1768 outputTriplet(visit, "(", " && ", ")");
1769 return true;
1770 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771 default: UNREACHABLE();
1772 }
1773
1774 return true;
1775}
1776
1777bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1778{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001779 switch (node->getOp())
1780 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001781 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1782 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1783 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1784 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1785 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1786 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1787 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001788 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1789 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1790 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1791 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1792 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1793 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1794 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1795 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1796 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1797 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1798 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1799 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1800 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1801 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1802 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1803 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1804 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1805 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1806 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1807 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1808 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001809 case EOpDFdx:
1810 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1811 {
1812 outputTriplet(visit, "(", "", ", 0.0)");
1813 }
1814 else
1815 {
1816 outputTriplet(visit, "ddx(", "", ")");
1817 }
1818 break;
1819 case EOpDFdy:
1820 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1821 {
1822 outputTriplet(visit, "(", "", ", 0.0)");
1823 }
1824 else
1825 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001826 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001827 }
1828 break;
1829 case EOpFwidth:
1830 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1831 {
1832 outputTriplet(visit, "(", "", ", 0.0)");
1833 }
1834 else
1835 {
1836 outputTriplet(visit, "fwidth(", "", ")");
1837 }
1838 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001839 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1840 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841 default: UNREACHABLE();
1842 }
1843
1844 return true;
1845}
1846
1847bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1848{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001849 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001851 switch (node->getOp())
1852 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001853 case EOpSequence:
1854 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001855 if (mInsideFunction)
1856 {
Jamie Madill075edd82013-07-08 13:30:19 -04001857 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001858 out << "{\n";
1859 }
1860
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001861 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001862 {
Jamie Madill075edd82013-07-08 13:30:19 -04001863 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001864
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001865 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001866
1867 out << ";\n";
1868 }
1869
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001870 if (mInsideFunction)
1871 {
Jamie Madill075edd82013-07-08 13:30:19 -04001872 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001873 out << "}\n";
1874 }
1875
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001876 return false;
1877 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878 case EOpDeclaration:
1879 if (visit == PreVisit)
1880 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001881 TIntermSequence *sequence = node->getSequence();
1882 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001884 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001886 TStructure *structure = variable->getType().getStruct();
1887
1888 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001889 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001890 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001891 }
1892
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001893 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001895 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001896 {
1897 out << "static ";
1898 }
1899
Jamie Madill033dae62014-06-18 12:56:28 -04001900 out << TypeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001901
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001902 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001904 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001906 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001908 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001909 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001910 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001911 }
1912 else
1913 {
1914 (*sit)->traverse(this);
1915 }
1916
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001917 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001918 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001919 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920 }
1921 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001923 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1924 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001925 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001926 }
1927 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001928 }
Jamie Madill033dae62014-06-18 12:56:28 -04001929 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001930 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001931 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001932 {
1933 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1934
1935 if (symbol)
1936 {
1937 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1938 mReferencedVaryings[symbol->getSymbol()] = symbol;
1939 }
1940 else
1941 {
1942 (*sit)->traverse(this);
1943 }
1944 }
1945 }
1946
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947 return false;
1948 }
1949 else if (visit == InVisit)
1950 {
1951 out << ", ";
1952 }
1953 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001954 case EOpPrototype:
1955 if (visit == PreVisit)
1956 {
Jamie Madill033dae62014-06-18 12:56:28 -04001957 out << TypeString(node->getType()) << " " << Decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001958
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001959 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001960
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001961 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001962 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001963 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001964
1965 if (symbol)
1966 {
1967 out << argumentString(symbol);
1968
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001969 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001970 {
1971 out << ", ";
1972 }
1973 }
1974 else UNREACHABLE();
1975 }
1976
1977 out << ");\n";
1978
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001979 // Also prototype the Lod0 variant if needed
1980 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1981 {
1982 mOutputLod0Function = true;
1983 node->traverse(this);
1984 mOutputLod0Function = false;
1985 }
1986
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001987 return false;
1988 }
1989 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001990 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991 case EOpFunction:
1992 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001993 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994
Jamie Madill033dae62014-06-18 12:56:28 -04001995 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001996
1997 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001999 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002001 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002002 {
Jamie Madill033dae62014-06-18 12:56:28 -04002003 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002004 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002005
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002006 TIntermSequence *sequence = node->getSequence();
2007 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002008
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002009 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002010 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002011 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002012
2013 if (symbol)
2014 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002015 TStructure *structure = symbol->getType().getStruct();
2016
2017 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002018 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002019 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002020 }
2021
2022 out << argumentString(symbol);
2023
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002024 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002025 {
2026 out << ", ";
2027 }
2028 }
2029 else UNREACHABLE();
2030 }
2031
2032 out << ")\n"
2033 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002034
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002035 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002036 {
2037 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002038 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002039 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002041
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002042 out << "}\n";
2043
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002044 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2045 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002046 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002047 {
2048 mOutputLod0Function = true;
2049 node->traverse(this);
2050 mOutputLod0Function = false;
2051 }
2052 }
2053
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002054 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055 }
2056 break;
2057 case EOpFunctionCall:
2058 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002059 TString name = TFunction::unmangleName(node->getName());
2060 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002061 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002062
2063 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064 {
Jamie Madill033dae62014-06-18 12:56:28 -04002065 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066 }
2067 else
2068 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002069 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002070
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002071 TextureFunction textureFunction;
2072 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002073 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002074 textureFunction.method = TextureFunction::IMPLICIT;
2075 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002076 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002077
2078 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002079 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002080 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002081 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002082 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002083 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002084 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002085 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002086 }
Nicolas Capens46485082014-04-15 13:12:50 -04002087 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2088 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002089 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002090 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002091 }
Nicolas Capens46485082014-04-15 13:12:50 -04002092 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002093 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002094 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002095 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002096 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002097 else if (name == "textureSize")
2098 {
2099 textureFunction.method = TextureFunction::SIZE;
2100 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002101 else if (name == "textureOffset")
2102 {
2103 textureFunction.method = TextureFunction::IMPLICIT;
2104 textureFunction.offset = true;
2105 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002106 else if (name == "textureProjOffset")
2107 {
2108 textureFunction.method = TextureFunction::IMPLICIT;
2109 textureFunction.offset = true;
2110 textureFunction.proj = true;
2111 }
2112 else if (name == "textureLodOffset")
2113 {
2114 textureFunction.method = TextureFunction::LOD;
2115 textureFunction.offset = true;
2116 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002117 else if (name == "textureProjLodOffset")
2118 {
2119 textureFunction.method = TextureFunction::LOD;
2120 textureFunction.proj = true;
2121 textureFunction.offset = true;
2122 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002123 else if (name == "texelFetch")
2124 {
2125 textureFunction.method = TextureFunction::FETCH;
2126 }
2127 else if (name == "texelFetchOffset")
2128 {
2129 textureFunction.method = TextureFunction::FETCH;
2130 textureFunction.offset = true;
2131 }
Nicolas Capens46485082014-04-15 13:12:50 -04002132 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002133 {
2134 textureFunction.method = TextureFunction::GRAD;
2135 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002136 else if (name == "textureGradOffset")
2137 {
2138 textureFunction.method = TextureFunction::GRAD;
2139 textureFunction.offset = true;
2140 }
Nicolas Capens46485082014-04-15 13:12:50 -04002141 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002142 {
2143 textureFunction.method = TextureFunction::GRAD;
2144 textureFunction.proj = true;
2145 }
2146 else if (name == "textureProjGradOffset")
2147 {
2148 textureFunction.method = TextureFunction::GRAD;
2149 textureFunction.proj = true;
2150 textureFunction.offset = true;
2151 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002152 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002153
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002154 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002155 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002156 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2157
2158 if (textureFunction.offset)
2159 {
2160 mandatoryArgumentCount++;
2161 }
2162
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002163 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002164
Jamie Madill183bde52014-07-02 15:31:19 -04002165 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002166 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002167 if (bias)
2168 {
2169 textureFunction.method = TextureFunction::LOD0BIAS;
2170 }
2171 else
2172 {
2173 textureFunction.method = TextureFunction::LOD0;
2174 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002175 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002176 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002177 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002178 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002179 }
2180 }
2181
2182 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002183
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002184 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002186
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002187 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002188 {
2189 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2190 {
2191 out << "texture_";
2192 (*arg)->traverse(this);
2193 out << ", sampler_";
2194 }
2195
2196 (*arg)->traverse(this);
2197
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002198 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002199 {
2200 out << ", ";
2201 }
2202 }
2203
2204 out << ")";
2205
2206 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002207 }
2208 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002209 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002210 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2211 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2212 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2213 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2214 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2215 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2216 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2217 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2218 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2219 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2220 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2221 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2222 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2223 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2224 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2225 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2226 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2227 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2228 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002229 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002230 {
Jamie Madill033dae62014-06-18 12:56:28 -04002231 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002232 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002233 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2234 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002235 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002236 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2237 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2238 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2239 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2240 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2241 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002242 case EOpMod:
2243 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002244 // We need to look at the number of components in both arguments
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002245 const int modValue = (*node->getSequence())[0]->getAsTyped()->getNominalSize() * 10 +
2246 (*node->getSequence())[1]->getAsTyped()->getNominalSize();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002247 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002248 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002249 case 11: mUsesMod1 = true; break;
2250 case 22: mUsesMod2v = true; break;
2251 case 21: mUsesMod2f = true; break;
2252 case 33: mUsesMod3v = true; break;
2253 case 31: mUsesMod3f = true; break;
2254 case 44: mUsesMod4v = true; break;
2255 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002256 default: UNREACHABLE();
2257 }
2258
2259 outputTriplet(visit, "mod(", ", ", ")");
2260 }
2261 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002262 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002264 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2265 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize())
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002266 {
2267 case 1: mUsesAtan2_1 = true; break;
2268 case 2: mUsesAtan2_2 = true; break;
2269 case 3: mUsesAtan2_3 = true; break;
2270 case 4: mUsesAtan2_4 = true; break;
2271 default: UNREACHABLE();
2272 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002273 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 break;
2275 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2276 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2277 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2278 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2279 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2280 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2281 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2282 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2283 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002284 case EOpFaceForward:
2285 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002286 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002287 {
2288 case 1: mUsesFaceforward1 = true; break;
2289 case 2: mUsesFaceforward2 = true; break;
2290 case 3: mUsesFaceforward3 = true; break;
2291 case 4: mUsesFaceforward4 = true; break;
2292 default: UNREACHABLE();
2293 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002294
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002295 outputTriplet(visit, "faceforward(", ", ", ")");
2296 }
2297 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2299 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2300 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301 default: UNREACHABLE();
2302 }
2303
2304 return true;
2305}
2306
2307bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2308{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002309 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002311 if (node->usesTernaryOperator())
2312 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002313 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002314 }
2315 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002317 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002318
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002319 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002320
2321 node->getCondition()->traverse(this);
2322
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002323 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002324
Jamie Madill075edd82013-07-08 13:30:19 -04002325 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002326 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002328 bool discard = false;
2329
daniel@transgaming.combb885322010-04-15 20:45:24 +00002330 if (node->getTrueBlock())
2331 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002332 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002333
2334 // Detect true discard
2335 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002336 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337
Jamie Madill075edd82013-07-08 13:30:19 -04002338 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002339 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002340
2341 if (node->getFalseBlock())
2342 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002343 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002344
Jamie Madill075edd82013-07-08 13:30:19 -04002345 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002346 out << "{\n";
2347
Jamie Madill075edd82013-07-08 13:30:19 -04002348 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002349 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002350
Jamie Madill075edd82013-07-08 13:30:19 -04002351 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002352 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002353
2354 // Detect false discard
2355 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2356 }
2357
2358 // ANGLE issue 486: Detect problematic conditional discard
2359 if (discard && FindSideEffectRewriting::search(node))
2360 {
2361 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002362 }
2363 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002364
2365 return false;
2366}
2367
2368void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2369{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002370 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002371}
2372
2373bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2374{
Nicolas Capens655fe362014-04-11 13:12:34 -04002375 mNestedLoopDepth++;
2376
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002377 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2378
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002379 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002380 {
2381 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2382 }
2383
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002384 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002385 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002386 if (handleExcessiveLoop(node))
2387 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002388 mInsideDiscontinuousLoop = wasDiscontinuous;
2389 mNestedLoopDepth--;
2390
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002391 return false;
2392 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002393 }
2394
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002395 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396
alokp@chromium.org52813552010-11-16 18:36:09 +00002397 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002399 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002400
Jamie Madill075edd82013-07-08 13:30:19 -04002401 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002402 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 }
2404 else
2405 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002406 out << "{for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002407
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 if (node->getInit())
2409 {
2410 node->getInit()->traverse(this);
2411 }
2412
2413 out << "; ";
2414
alokp@chromium.org52813552010-11-16 18:36:09 +00002415 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002417 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418 }
2419
2420 out << "; ";
2421
alokp@chromium.org52813552010-11-16 18:36:09 +00002422 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002424 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425 }
2426
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002427 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002428
Jamie Madill075edd82013-07-08 13:30:19 -04002429 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002430 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431 }
2432
2433 if (node->getBody())
2434 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002435 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436 }
2437
Jamie Madill075edd82013-07-08 13:30:19 -04002438 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002439 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002440
alokp@chromium.org52813552010-11-16 18:36:09 +00002441 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442 {
Jamie Madill075edd82013-07-08 13:30:19 -04002443 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 out << "while(\n";
2445
alokp@chromium.org52813552010-11-16 18:36:09 +00002446 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447
daniel@transgaming.com73536982012-03-21 20:45:49 +00002448 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 }
2450
daniel@transgaming.com73536982012-03-21 20:45:49 +00002451 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002453 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002454 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002455
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 return false;
2457}
2458
2459bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2460{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002461 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462
2463 switch (node->getFlowOp())
2464 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002465 case EOpKill:
2466 outputTriplet(visit, "discard;\n", "", "");
2467 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002468 case EOpBreak:
2469 if (visit == PreVisit)
2470 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002471 if (mNestedLoopDepth > 1)
2472 {
2473 mUsesNestedBreak = true;
2474 }
2475
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002476 if (mExcessiveLoopIndex)
2477 {
2478 out << "{Break";
2479 mExcessiveLoopIndex->traverse(this);
2480 out << " = true; break;}\n";
2481 }
2482 else
2483 {
2484 out << "break;\n";
2485 }
2486 }
2487 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002488 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002489 case EOpReturn:
2490 if (visit == PreVisit)
2491 {
2492 if (node->getExpression())
2493 {
2494 out << "return ";
2495 }
2496 else
2497 {
2498 out << "return;\n";
2499 }
2500 }
2501 else if (visit == PostVisit)
2502 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002503 if (node->getExpression())
2504 {
2505 out << ";\n";
2506 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507 }
2508 break;
2509 default: UNREACHABLE();
2510 }
2511
2512 return true;
2513}
2514
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002515void OutputHLSL::traverseStatements(TIntermNode *node)
2516{
2517 if (isSingleStatement(node))
2518 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002519 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002520 }
2521
2522 node->traverse(this);
2523}
2524
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002525bool OutputHLSL::isSingleStatement(TIntermNode *node)
2526{
2527 TIntermAggregate *aggregate = node->getAsAggregate();
2528
2529 if (aggregate)
2530 {
2531 if (aggregate->getOp() == EOpSequence)
2532 {
2533 return false;
2534 }
2535 else
2536 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002537 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002538 {
2539 if (!isSingleStatement(*sit))
2540 {
2541 return false;
2542 }
2543 }
2544
2545 return true;
2546 }
2547 }
2548
2549 return true;
2550}
2551
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002552// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2553// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002554bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2555{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002556 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002557 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002558
2559 // Parse loops of the form:
2560 // for(int index = initial; index [comparator] limit; index += increment)
2561 TIntermSymbol *index = NULL;
2562 TOperator comparator = EOpNull;
2563 int initial = 0;
2564 int limit = 0;
2565 int increment = 0;
2566
2567 // Parse index name and intial value
2568 if (node->getInit())
2569 {
2570 TIntermAggregate *init = node->getInit()->getAsAggregate();
2571
2572 if (init)
2573 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002574 TIntermSequence *sequence = init->getSequence();
2575 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002576
2577 if (variable && variable->getQualifier() == EvqTemporary)
2578 {
2579 TIntermBinary *assign = variable->getAsBinaryNode();
2580
2581 if (assign->getOp() == EOpInitialize)
2582 {
2583 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2584 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2585
2586 if (symbol && constant)
2587 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002588 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002589 {
2590 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002591 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002592 }
2593 }
2594 }
2595 }
2596 }
2597 }
2598
2599 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002600 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002601 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002602 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002603
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002604 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2605 {
2606 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2607
2608 if (constant)
2609 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002610 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002611 {
2612 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002613 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002614 }
2615 }
2616 }
2617 }
2618
2619 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002620 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002621 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002622 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2623 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002624
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002625 if (binaryTerminal)
2626 {
2627 TOperator op = binaryTerminal->getOp();
2628 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2629
2630 if (constant)
2631 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002632 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002633 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002634 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002635
2636 switch (op)
2637 {
2638 case EOpAddAssign: increment = value; break;
2639 case EOpSubAssign: increment = -value; break;
2640 default: UNIMPLEMENTED();
2641 }
2642 }
2643 }
2644 }
2645 else if (unaryTerminal)
2646 {
2647 TOperator op = unaryTerminal->getOp();
2648
2649 switch (op)
2650 {
2651 case EOpPostIncrement: increment = 1; break;
2652 case EOpPostDecrement: increment = -1; break;
2653 case EOpPreIncrement: increment = 1; break;
2654 case EOpPreDecrement: increment = -1; break;
2655 default: UNIMPLEMENTED();
2656 }
2657 }
2658 }
2659
2660 if (index != NULL && comparator != EOpNull && increment != 0)
2661 {
2662 if (comparator == EOpLessThanEqual)
2663 {
2664 comparator = EOpLessThan;
2665 limit += 1;
2666 }
2667
2668 if (comparator == EOpLessThan)
2669 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002670 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002671
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002672 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002673 {
2674 return false; // Not an excessive loop
2675 }
2676
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002677 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2678 mExcessiveLoopIndex = index;
2679
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002680 out << "{int ";
2681 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002682 out << ";\n"
2683 "bool Break";
2684 index->traverse(this);
2685 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002686
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002687 bool firstLoopFragment = true;
2688
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002689 while (iterations > 0)
2690 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002691 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002692
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002693 if (!firstLoopFragment)
2694 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002695 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002696 index->traverse(this);
2697 out << ") {\n";
2698 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002699
2700 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2701 {
2702 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2703 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002704
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002705 // for(int index = initial; index < clampedLimit; index += increment)
2706
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002707 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002708 index->traverse(this);
2709 out << " = ";
2710 out << initial;
2711
2712 out << "; ";
2713 index->traverse(this);
2714 out << " < ";
2715 out << clampedLimit;
2716
2717 out << "; ";
2718 index->traverse(this);
2719 out << " += ";
2720 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002721 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002722
Jamie Madill075edd82013-07-08 13:30:19 -04002723 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002724 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002725
2726 if (node->getBody())
2727 {
2728 node->getBody()->traverse(this);
2729 }
2730
Jamie Madill075edd82013-07-08 13:30:19 -04002731 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002732 out << ";}\n";
2733
2734 if (!firstLoopFragment)
2735 {
2736 out << "}\n";
2737 }
2738
2739 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002740
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002741 initial += MAX_LOOP_ITERATIONS * increment;
2742 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002743 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002744
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002745 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002746
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002747 mExcessiveLoopIndex = restoreIndex;
2748
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002749 return true;
2750 }
2751 else UNIMPLEMENTED();
2752 }
2753
2754 return false; // Not handled as an excessive loop
2755}
2756
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002757void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002759 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002761 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002762 {
2763 out << preString;
2764 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002765 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766 {
2767 out << inString;
2768 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002769 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002770 {
2771 out << postString;
2772 }
2773}
2774
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002775void OutputHLSL::outputLineDirective(int line)
2776{
2777 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2778 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002779 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002780 mBody << "#line " << line;
2781
2782 if (mContext.sourcePath)
2783 {
2784 mBody << " \"" << mContext.sourcePath << "\"";
2785 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002786
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002787 mBody << "\n";
2788 }
2789}
2790
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002791TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2792{
2793 TQualifier qualifier = symbol->getQualifier();
2794 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002795 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002796
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002797 if (name.empty()) // HLSL demands named arguments, also for prototypes
2798 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002799 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002800 }
2801 else
2802 {
Jamie Madill033dae62014-06-18 12:56:28 -04002803 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002804 }
2805
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002806 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2807 {
Jamie Madill033dae62014-06-18 12:56:28 -04002808 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002809 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002810 }
2811
Jamie Madill033dae62014-06-18 12:56:28 -04002812 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813}
2814
2815TString OutputHLSL::initializer(const TType &type)
2816{
2817 TString string;
2818
Jamie Madill94bf7f22013-07-08 13:31:15 -04002819 size_t size = type.getObjectSize();
2820 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002821 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002822 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002823
Jamie Madill94bf7f22013-07-08 13:31:15 -04002824 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002825 {
2826 string += ", ";
2827 }
2828 }
2829
daniel@transgaming.comead23042010-04-29 03:35:36 +00002830 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002831}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002832
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002833void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2834{
2835 TInfoSinkBase &out = mBody;
2836
2837 if (visit == PreVisit)
2838 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002839 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002840
2841 out << name + "(";
2842 }
2843 else if (visit == InVisit)
2844 {
2845 out << ", ";
2846 }
2847 else if (visit == PostVisit)
2848 {
2849 out << ")";
2850 }
2851}
2852
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002853const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2854{
2855 TInfoSinkBase &out = mBody;
2856
Jamie Madill98493dd2013-07-08 14:39:03 -04002857 const TStructure* structure = type.getStruct();
2858 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002859 {
Jamie Madill033dae62014-06-18 12:56:28 -04002860 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002861
Jamie Madill98493dd2013-07-08 14:39:03 -04002862 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002863
Jamie Madill98493dd2013-07-08 14:39:03 -04002864 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002865 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002866 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002867 constUnion = writeConstantUnion(*fieldType, constUnion);
2868
Jamie Madill98493dd2013-07-08 14:39:03 -04002869 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002870 {
2871 out << ", ";
2872 }
2873 }
2874
2875 out << ")";
2876 }
2877 else
2878 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002879 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002880 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002881
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002882 if (writeType)
2883 {
Jamie Madill033dae62014-06-18 12:56:28 -04002884 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002885 }
2886
Jamie Madill94bf7f22013-07-08 13:31:15 -04002887 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002888 {
2889 switch (constUnion->getType())
2890 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002891 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002892 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002893 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002894 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002895 default: UNREACHABLE();
2896 }
2897
2898 if (i != size - 1)
2899 {
2900 out << ", ";
2901 }
2902 }
2903
2904 if (writeType)
2905 {
2906 out << ")";
2907 }
2908 }
2909
2910 return constUnion;
2911}
2912
Jamie Madill77f74852014-07-08 15:02:34 -04002913class DeclareVaryingTraverser : public GetVariableTraverser<Varying>
Jamie Madill47fdd132013-08-30 13:21:04 -04002914{
Jamie Madill77f74852014-07-08 15:02:34 -04002915 public:
2916 DeclareVaryingTraverser(std::vector<Varying> *output,
2917 InterpolationType interpolation)
2918 : GetVariableTraverser(output),
2919 mInterpolation(interpolation)
2920 {}
Jamie Madill47fdd132013-08-30 13:21:04 -04002921
Jamie Madill77f74852014-07-08 15:02:34 -04002922 private:
2923 void visitVariable(Varying *varying)
Jamie Madill47fdd132013-08-30 13:21:04 -04002924 {
Jamie Madill77f74852014-07-08 15:02:34 -04002925 varying->interpolation = mInterpolation;
Jamie Madill47fdd132013-08-30 13:21:04 -04002926 }
Jamie Madill47fdd132013-08-30 13:21:04 -04002927
Jamie Madill77f74852014-07-08 15:02:34 -04002928 InterpolationType mInterpolation;
2929};
Jamie Madill28167c62013-08-30 13:21:10 -04002930
Jamie Madill77f74852014-07-08 15:02:34 -04002931void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier,
2932 const TString &name, std::vector<Varying> &fieldsOut)
2933{
2934 DeclareVaryingTraverser traverser(&fieldsOut, GetInterpolationType(baseTypeQualifier));
2935 traverser.traverse(type, name);
Jamie Madill47fdd132013-08-30 13:21:04 -04002936}
2937
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002938}