blob: fe792213ac8fd36e8be6adbe1d7b93f4c43efb9d [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
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
14#include "common/utilities.h"
15#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
16#include "compiler/translator/DetectDiscontinuity.h"
17#include "compiler/translator/FlagStd140Structs.h"
18#include "compiler/translator/InfoSink.h"
19#include "compiler/translator/NodeSearch.h"
20#include "compiler/translator/RewriteElseBlocks.h"
21#include "compiler/translator/SearchSymbol.h"
22#include "compiler/translator/StructureHLSL.h"
23#include "compiler/translator/TranslatorHLSL.h"
24#include "compiler/translator/UnfoldShortCircuit.h"
25#include "compiler/translator/UniformHLSL.h"
26#include "compiler/translator/UtilsHLSL.h"
27#include "compiler/translator/blocklayout.h"
28#include "compiler/translator/compilerdebug.h"
29#include "compiler/translator/util.h"
30
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031namespace sh
32{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000033
Nicolas Capense0ba27a2013-06-24 16:10:52 -040034TString OutputHLSL::TextureFunction::name() const
35{
36 TString name = "gl_texture";
37
Nicolas Capens6d232bb2013-07-08 15:56:38 -040038 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040039 {
40 name += "2D";
41 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040042 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040043 {
44 name += "3D";
45 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040046 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040047 {
48 name += "Cube";
49 }
50 else UNREACHABLE();
51
52 if (proj)
53 {
54 name += "Proj";
55 }
56
Nicolas Capensb1f45b72013-12-19 17:37:19 -050057 if (offset)
58 {
59 name += "Offset";
60 }
61
Nicolas Capens75fb4752013-07-10 15:14:47 -040062 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040063 {
Nicolas Capensfc014542014-02-18 14:47:13 -050064 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040065 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050066 case LOD: name += "Lod"; break;
67 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040068 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050069 case SIZE: name += "Size"; break;
70 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050071 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040072 default: UNREACHABLE();
73 }
74
75 return name + "(";
76}
77
78bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
79{
80 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040081 if (sampler > rhs.sampler) return false;
82
Nicolas Capense0ba27a2013-06-24 16:10:52 -040083 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040084 if (coords > rhs.coords) return false;
85
Nicolas Capense0ba27a2013-06-24 16:10:52 -040086 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040087 if (proj && !rhs.proj) return false;
88
89 if (!offset && rhs.offset) return true;
90 if (offset && !rhs.offset) return false;
91
Nicolas Capens75fb4752013-07-10 15:14:47 -040092 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040093 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040094
95 return false;
96}
97
Jamie Madill54ad4f82014-09-03 09:40:46 -040098OutputHLSL::OutputHLSL(TParseContext &context, TranslatorHLSL *parentTranslator)
99 : TIntermTraverser(true, true, true),
100 mContext(context),
101 mOutputType(parentTranslator->getOutputType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000103 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000104 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000105
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000106 mUsesFragColor = false;
107 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000108 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000109 mUsesFragCoord = false;
110 mUsesPointCoord = false;
111 mUsesFrontFacing = false;
112 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400113 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000114 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500115 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400116 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000117
Jamie Madill54ad4f82014-09-03 09:40:46 -0400118 const ShBuiltInResources &resources = parentTranslator->getResources();
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000119 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
120
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000121 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000122
123 mContainsLoopDiscontinuity = false;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800124 mContainsAnyLoop = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000125 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000126 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400127 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000128
129 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000130
Jamie Madill8daaba12014-06-13 10:04:33 -0400131 mStructureHLSL = new StructureHLSL;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400132 mUniformHLSL = new UniformHLSL(mStructureHLSL, parentTranslator);
Jamie Madill8daaba12014-06-13 10:04:33 -0400133
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000134 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000135 {
Jamie Madill183bde52014-07-02 15:31:19 -0400136 if (mContext.shaderType == GL_FRAGMENT_SHADER)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000137 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400138 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
139 mUniformHLSL->reserveUniformRegisters(3);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000140 }
141 else
142 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400143 // Reserve registers for dx_DepthRange and dx_ViewAdjust
144 mUniformHLSL->reserveUniformRegisters(2);
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000145 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000146 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000147
Jamie Madillf91ce812014-06-13 10:04:34 -0400148 // Reserve registers for the default uniform block and driver constants
149 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150}
151
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000152OutputHLSL::~OutputHLSL()
153{
Jamie Madill8daaba12014-06-13 10:04:33 -0400154 SafeDelete(mUnfoldShortCircuit);
155 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400156 SafeDelete(mUniformHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000157}
158
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000159void OutputHLSL::output()
160{
Jamie Madill183bde52014-07-02 15:31:19 -0400161 mContainsLoopDiscontinuity = mContext.shaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Corentin Wallez80bacde2014-11-10 12:07:37 -0800162 mContainsAnyLoop = containsAnyLoop(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400163 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
164 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000165
Jamie Madille53c98b2014-02-03 11:57:13 -0500166 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
167 // use a vertex attribute as a condition, and some related computation in the else block.
Jamie Madill183bde52014-07-02 15:31:19 -0400168 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == GL_VERTEX_SHADER)
Jamie Madille53c98b2014-02-03 11:57:13 -0500169 {
170 RewriteElseBlocks(mContext.treeRoot);
171 }
172
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200173 BuiltInFunctionEmulatorHLSL builtInFunctionEmulator;
174 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(mContext.treeRoot);
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000175 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
Olli Etuahoe17e3192015-01-02 12:47:59 +0200176 header(&builtInFunctionEmulator);
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000177
Olli Etuahoe17e3192015-01-02 12:47:59 +0200178 TInfoSinkBase& sink = mContext.infoSink().obj;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200179 sink << mHeader.c_str();
180 sink << mBody.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200181
182 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000183}
184
Jamie Madill570e04d2013-06-21 09:15:33 -0400185void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
186{
187 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
188 {
189 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
190
191 // This will mark the necessary block elements as referenced
192 flaggedNode->traverse(this);
193 TString structName(mBody.c_str());
194 mBody.erase();
195
196 mFlaggedStructOriginalNames[flaggedNode] = structName;
197
198 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
199 {
200 structName.erase(pos, 1);
201 }
202
203 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
204 }
205}
206
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000207TInfoSinkBase &OutputHLSL::getBodyStream()
208{
209 return mBody;
210}
211
Jamie Madill4e1fd412014-07-10 17:50:10 -0400212const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
213{
214 return mUniformHLSL->getInterfaceBlockRegisterMap();
215}
216
Jamie Madill9fe25e92014-07-18 10:33:08 -0400217const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
218{
219 return mUniformHLSL->getUniformRegisterMap();
220}
221
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000222int OutputHLSL::vectorSize(const TType &type) const
223{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000224 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000225 int arraySize = type.isArray() ? type.getArraySize() : 1;
226
227 return elementSize * arraySize;
228}
229
Jamie Madill98493dd2013-07-08 14:39:03 -0400230TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400231{
232 TString init;
233
234 TString preIndentString;
235 TString fullIndentString;
236
237 for (int spaces = 0; spaces < (indent * 4); spaces++)
238 {
239 preIndentString += ' ';
240 }
241
242 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
243 {
244 fullIndentString += ' ';
245 }
246
247 init += preIndentString + "{\n";
248
Jamie Madill98493dd2013-07-08 14:39:03 -0400249 const TFieldList &fields = structure.fields();
250 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400251 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400252 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400253 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400254 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400255
Jamie Madill98493dd2013-07-08 14:39:03 -0400256 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400257 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400258 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400259 }
260 else
261 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400262 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400263 }
264 }
265
266 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
267
268 return init;
269}
270
Olli Etuahoe17e3192015-01-02 12:47:59 +0200271void OutputHLSL::header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000272{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000273 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000274
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000275 TString varyings;
276 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400277 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000278
Jamie Madill829f59e2013-11-13 19:40:54 -0500279 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400280 {
281 TIntermTyped *structNode = flaggedStructIt->first;
282 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400283 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400284 const TString &originalName = mFlaggedStructOriginalNames[structNode];
285
Jamie Madill033dae62014-06-18 12:56:28 -0400286 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400287 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400288 flaggedStructs += "\n";
289 }
290
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000291 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
292 {
293 const TType &type = varying->second->getType();
294 const TString &name = varying->second->getSymbol();
295
296 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400297 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
298 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000299 }
300
301 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
302 {
303 const TType &type = attribute->second->getType();
304 const TString &name = attribute->second->getSymbol();
305
Jamie Madill033dae62014-06-18 12:56:28 -0400306 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000307 }
308
Jamie Madill8daaba12014-06-13 10:04:33 -0400309 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400310
Jamie Madillf91ce812014-06-13 10:04:34 -0400311 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
312 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
313
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500314 if (mUsesDiscardRewriting)
315 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400316 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500317 }
318
Nicolas Capens655fe362014-04-11 13:12:34 -0400319 if (mUsesNestedBreak)
320 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400321 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400322 }
323
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400324 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
325 "#define LOOP [loop]\n"
326 "#define FLATTEN [flatten]\n"
327 "#else\n"
328 "#define LOOP\n"
329 "#define FLATTEN\n"
330 "#endif\n";
331
Jamie Madill183bde52014-07-02 15:31:19 -0400332 if (mContext.shaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000334 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000335 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000336
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000337 out << "// Varyings\n";
338 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400339 out << "\n";
340
341 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000342 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500343 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000344 {
Jamie Madill46131a32013-06-20 11:55:50 -0400345 const TString &variableName = outputVariableIt->first;
346 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400347
Jamie Madill033dae62014-06-18 12:56:28 -0400348 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400349 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000350 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000351 }
Jamie Madill46131a32013-06-20 11:55:50 -0400352 else
353 {
354 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
355
356 out << "static float4 gl_Color[" << numColorValues << "] =\n"
357 "{\n";
358 for (unsigned int i = 0; i < numColorValues; i++)
359 {
360 out << " float4(0, 0, 0, 0)";
361 if (i + 1 != numColorValues)
362 {
363 out << ",";
364 }
365 out << "\n";
366 }
367
368 out << "};\n";
369 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000370
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400371 if (mUsesFragDepth)
372 {
373 out << "static float gl_Depth = 0.0;\n";
374 }
375
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000376 if (mUsesFragCoord)
377 {
378 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
379 }
380
381 if (mUsesPointCoord)
382 {
383 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
384 }
385
386 if (mUsesFrontFacing)
387 {
388 out << "static bool gl_FrontFacing = false;\n";
389 }
390
391 out << "\n";
392
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000393 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000394 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000395 out << "struct gl_DepthRangeParameters\n"
396 "{\n"
397 " float near;\n"
398 " float far;\n"
399 " float diff;\n"
400 "};\n"
401 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000402 }
403
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000404 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000405 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000406 out << "cbuffer DriverConstants : register(b1)\n"
407 "{\n";
408
409 if (mUsesDepthRange)
410 {
411 out << " float3 dx_DepthRange : packoffset(c0);\n";
412 }
413
414 if (mUsesFragCoord)
415 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000416 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000417 }
418
419 if (mUsesFragCoord || mUsesFrontFacing)
420 {
421 out << " float3 dx_DepthFront : packoffset(c2);\n";
422 }
423
424 out << "};\n";
425 }
426 else
427 {
428 if (mUsesDepthRange)
429 {
430 out << "uniform float3 dx_DepthRange : register(c0);";
431 }
432
433 if (mUsesFragCoord)
434 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000435 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000436 }
437
438 if (mUsesFragCoord || mUsesFrontFacing)
439 {
440 out << "uniform float3 dx_DepthFront : register(c2);\n";
441 }
442 }
443
444 out << "\n";
445
446 if (mUsesDepthRange)
447 {
448 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
449 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000450 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000451
Jamie Madillf91ce812014-06-13 10:04:34 -0400452 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000453 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400454 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000455 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400456 out << flaggedStructs;
457 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000458 }
459
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000460 if (usingMRTExtension && mNumRenderTargets > 1)
461 {
462 out << "#define GL_USES_MRT\n";
463 }
464
465 if (mUsesFragColor)
466 {
467 out << "#define GL_USES_FRAG_COLOR\n";
468 }
469
470 if (mUsesFragData)
471 {
472 out << "#define GL_USES_FRAG_DATA\n";
473 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000475 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000476 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000477 out << "// Attributes\n";
478 out << attributes;
479 out << "\n"
480 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400481
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000482 if (mUsesPointSize)
483 {
484 out << "static float gl_PointSize = float(1);\n";
485 }
486
487 out << "\n"
488 "// Varyings\n";
489 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000490 out << "\n";
491
492 if (mUsesDepthRange)
493 {
494 out << "struct gl_DepthRangeParameters\n"
495 "{\n"
496 " float near;\n"
497 " float far;\n"
498 " float diff;\n"
499 "};\n"
500 "\n";
501 }
502
503 if (mOutputType == SH_HLSL11_OUTPUT)
504 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800505 out << "cbuffer DriverConstants : register(b1)\n"
506 "{\n";
507
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000508 if (mUsesDepthRange)
509 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800510 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000511 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800512
513 // dx_ViewAdjust will only be used in Feature Level 9 shaders.
514 // However, we declare it for all shaders (including Feature Level 10+).
515 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
516 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
517
518 out << "};\n"
519 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000520 }
521 else
522 {
523 if (mUsesDepthRange)
524 {
525 out << "uniform float3 dx_DepthRange : register(c0);\n";
526 }
527
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000528 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000529 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000530 }
531
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000532 if (mUsesDepthRange)
533 {
534 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
535 "\n";
536 }
537
Jamie Madillf91ce812014-06-13 10:04:34 -0400538 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000539 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400540 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000541 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400542 out << flaggedStructs;
543 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000544 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400545 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000546
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400547 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
548 {
549 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400550 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000551 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400552 switch(textureFunction->sampler)
553 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400554 case EbtSampler2D: out << "int2 "; break;
555 case EbtSampler3D: out << "int3 "; break;
556 case EbtSamplerCube: out << "int2 "; break;
557 case EbtSampler2DArray: out << "int3 "; break;
558 case EbtISampler2D: out << "int2 "; break;
559 case EbtISampler3D: out << "int3 "; break;
560 case EbtISamplerCube: out << "int2 "; break;
561 case EbtISampler2DArray: out << "int3 "; break;
562 case EbtUSampler2D: out << "int2 "; break;
563 case EbtUSampler3D: out << "int3 "; break;
564 case EbtUSamplerCube: out << "int2 "; break;
565 case EbtUSampler2DArray: out << "int3 "; break;
566 case EbtSampler2DShadow: out << "int2 "; break;
567 case EbtSamplerCubeShadow: out << "int2 "; break;
568 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400569 default: UNREACHABLE();
570 }
571 }
572 else // Sampling function
573 {
574 switch(textureFunction->sampler)
575 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400576 case EbtSampler2D: out << "float4 "; break;
577 case EbtSampler3D: out << "float4 "; break;
578 case EbtSamplerCube: out << "float4 "; break;
579 case EbtSampler2DArray: out << "float4 "; break;
580 case EbtISampler2D: out << "int4 "; break;
581 case EbtISampler3D: out << "int4 "; break;
582 case EbtISamplerCube: out << "int4 "; break;
583 case EbtISampler2DArray: out << "int4 "; break;
584 case EbtUSampler2D: out << "uint4 "; break;
585 case EbtUSampler3D: out << "uint4 "; break;
586 case EbtUSamplerCube: out << "uint4 "; break;
587 case EbtUSampler2DArray: out << "uint4 "; break;
588 case EbtSampler2DShadow: out << "float "; break;
589 case EbtSamplerCubeShadow: out << "float "; break;
590 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400591 default: UNREACHABLE();
592 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000593 }
594
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400595 // Function name
596 out << textureFunction->name();
597
598 // Argument list
599 int hlslCoords = 4;
600
601 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000602 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400603 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000604 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400605 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
606 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
607 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000608 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400609
Nicolas Capens75fb4752013-07-10 15:14:47 -0400610 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000611 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400612 case TextureFunction::IMPLICIT: break;
613 case TextureFunction::BIAS: hlslCoords = 4; break;
614 case TextureFunction::LOD: hlslCoords = 4; break;
615 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400616 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400617 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000618 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400619 }
620 else if (mOutputType == SH_HLSL11_OUTPUT)
621 {
622 switch(textureFunction->sampler)
623 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400624 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
625 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
626 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
627 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
628 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
629 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500630 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400631 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
632 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
633 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500634 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400635 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
636 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
637 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
638 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400639 default: UNREACHABLE();
640 }
641 }
642 else UNREACHABLE();
643
Nicolas Capensfc014542014-02-18 14:47:13 -0500644 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400645 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500646 switch(textureFunction->coords)
647 {
648 case 2: out << ", int2 t"; break;
649 case 3: out << ", int3 t"; break;
650 default: UNREACHABLE();
651 }
652 }
653 else // Floating-point coordinates (except textureSize)
654 {
655 switch(textureFunction->coords)
656 {
657 case 1: out << ", int lod"; break; // textureSize()
658 case 2: out << ", float2 t"; break;
659 case 3: out << ", float3 t"; break;
660 case 4: out << ", float4 t"; break;
661 default: UNREACHABLE();
662 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000663 }
664
Nicolas Capensd11d5492014-02-19 17:06:10 -0500665 if (textureFunction->method == TextureFunction::GRAD)
666 {
667 switch(textureFunction->sampler)
668 {
669 case EbtSampler2D:
670 case EbtISampler2D:
671 case EbtUSampler2D:
672 case EbtSampler2DArray:
673 case EbtISampler2DArray:
674 case EbtUSampler2DArray:
675 case EbtSampler2DShadow:
676 case EbtSampler2DArrayShadow:
677 out << ", float2 ddx, float2 ddy";
678 break;
679 case EbtSampler3D:
680 case EbtISampler3D:
681 case EbtUSampler3D:
682 case EbtSamplerCube:
683 case EbtISamplerCube:
684 case EbtUSamplerCube:
685 case EbtSamplerCubeShadow:
686 out << ", float3 ddx, float3 ddy";
687 break;
688 default: UNREACHABLE();
689 }
690 }
691
Nicolas Capens75fb4752013-07-10 15:14:47 -0400692 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000693 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400694 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400695 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400696 case TextureFunction::LOD: out << ", float lod"; break;
697 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400698 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400699 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500700 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500701 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400702 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000703 }
704
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500705 if (textureFunction->offset)
706 {
707 switch(textureFunction->sampler)
708 {
709 case EbtSampler2D: out << ", int2 offset"; break;
710 case EbtSampler3D: out << ", int3 offset"; break;
711 case EbtSampler2DArray: out << ", int2 offset"; break;
712 case EbtISampler2D: out << ", int2 offset"; break;
713 case EbtISampler3D: out << ", int3 offset"; break;
714 case EbtISampler2DArray: out << ", int2 offset"; break;
715 case EbtUSampler2D: out << ", int2 offset"; break;
716 case EbtUSampler3D: out << ", int3 offset"; break;
717 case EbtUSampler2DArray: out << ", int2 offset"; break;
718 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500719 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500720 default: UNREACHABLE();
721 }
722 }
723
Nicolas Capens84cfa122014-04-14 13:48:45 -0400724 if (textureFunction->method == TextureFunction::BIAS ||
725 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500726 {
727 out << ", float bias";
728 }
729
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400730 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400731 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400732
Nicolas Capens75fb4752013-07-10 15:14:47 -0400733 if (textureFunction->method == TextureFunction::SIZE)
734 {
735 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
736 {
737 if (IsSamplerArray(textureFunction->sampler))
738 {
739 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
740 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
741 }
742 else
743 {
744 out << " uint width; uint height; uint numberOfLevels;\n"
745 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
746 }
747 }
748 else if (IsSampler3D(textureFunction->sampler))
749 {
750 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
751 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
752 }
753 else UNREACHABLE();
754
755 switch(textureFunction->sampler)
756 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400757 case EbtSampler2D: out << " return int2(width, height);"; break;
758 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
759 case EbtSamplerCube: out << " return int2(width, height);"; break;
760 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
761 case EbtISampler2D: out << " return int2(width, height);"; break;
762 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
763 case EbtISamplerCube: out << " return int2(width, height);"; break;
764 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
765 case EbtUSampler2D: out << " return int2(width, height);"; break;
766 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
767 case EbtUSamplerCube: out << " return int2(width, height);"; break;
768 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
769 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
770 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
771 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400772 default: UNREACHABLE();
773 }
774 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400775 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400776 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500777 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
778 {
779 out << " float width; float height; float layers; float levels;\n";
780
781 out << " uint mip = 0;\n";
782
783 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
784
785 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
786 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
787 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
788 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
789
790 // FACE_POSITIVE_X = 000b
791 // FACE_NEGATIVE_X = 001b
792 // FACE_POSITIVE_Y = 010b
793 // FACE_NEGATIVE_Y = 011b
794 // FACE_POSITIVE_Z = 100b
795 // FACE_NEGATIVE_Z = 101b
796 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
797
798 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
799 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
800 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
801
802 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
803 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
804 }
805 else if (IsIntegerSampler(textureFunction->sampler) &&
806 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400807 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400808 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400809 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400810 if (IsSamplerArray(textureFunction->sampler))
811 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400812 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400813
Nicolas Capens9edebd62013-08-06 10:59:10 -0400814 if (textureFunction->method == TextureFunction::LOD0)
815 {
816 out << " uint mip = 0;\n";
817 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400818 else if (textureFunction->method == TextureFunction::LOD0BIAS)
819 {
820 out << " uint mip = bias;\n";
821 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400822 else
823 {
824 if (textureFunction->method == TextureFunction::IMPLICIT ||
825 textureFunction->method == TextureFunction::BIAS)
826 {
827 out << " x.GetDimensions(0, width, height, layers, levels);\n"
828 " float2 tSized = float2(t.x * width, t.y * height);\n"
829 " float dx = length(ddx(tSized));\n"
830 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500831 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400832
833 if (textureFunction->method == TextureFunction::BIAS)
834 {
835 out << " lod += bias;\n";
836 }
837 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500838 else if (textureFunction->method == TextureFunction::GRAD)
839 {
840 out << " x.GetDimensions(0, width, height, layers, levels);\n"
841 " float lod = log2(max(length(ddx), length(ddy)));\n";
842 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400843
844 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
845 }
846
847 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400848 }
849 else
850 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400851 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400852
Nicolas Capens9edebd62013-08-06 10:59:10 -0400853 if (textureFunction->method == TextureFunction::LOD0)
854 {
855 out << " uint mip = 0;\n";
856 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400857 else if (textureFunction->method == TextureFunction::LOD0BIAS)
858 {
859 out << " uint mip = bias;\n";
860 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400861 else
862 {
863 if (textureFunction->method == TextureFunction::IMPLICIT ||
864 textureFunction->method == TextureFunction::BIAS)
865 {
866 out << " x.GetDimensions(0, width, height, levels);\n"
867 " float2 tSized = float2(t.x * width, t.y * height);\n"
868 " float dx = length(ddx(tSized));\n"
869 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500870 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400871
872 if (textureFunction->method == TextureFunction::BIAS)
873 {
874 out << " lod += bias;\n";
875 }
876 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500877 else if (textureFunction->method == TextureFunction::LOD)
878 {
879 out << " x.GetDimensions(0, width, height, levels);\n";
880 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500881 else if (textureFunction->method == TextureFunction::GRAD)
882 {
883 out << " x.GetDimensions(0, width, height, levels);\n"
884 " float lod = log2(max(length(ddx), length(ddy)));\n";
885 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400886
887 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
888 }
889
890 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400891 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400892 }
893 else if (IsSampler3D(textureFunction->sampler))
894 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400895 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400896
Nicolas Capens9edebd62013-08-06 10:59:10 -0400897 if (textureFunction->method == TextureFunction::LOD0)
898 {
899 out << " uint mip = 0;\n";
900 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400901 else if (textureFunction->method == TextureFunction::LOD0BIAS)
902 {
903 out << " uint mip = bias;\n";
904 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400905 else
906 {
907 if (textureFunction->method == TextureFunction::IMPLICIT ||
908 textureFunction->method == TextureFunction::BIAS)
909 {
910 out << " x.GetDimensions(0, width, height, depth, levels);\n"
911 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
912 " float dx = length(ddx(tSized));\n"
913 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500914 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400915
916 if (textureFunction->method == TextureFunction::BIAS)
917 {
918 out << " lod += bias;\n";
919 }
920 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500921 else if (textureFunction->method == TextureFunction::GRAD)
922 {
923 out << " x.GetDimensions(0, width, height, depth, levels);\n"
924 " float lod = log2(max(length(ddx), length(ddy)));\n";
925 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400926
927 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
928 }
929
930 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400931 }
932 else UNREACHABLE();
933 }
934
935 out << " return ";
936
937 // HLSL intrinsic
938 if (mOutputType == SH_HLSL9_OUTPUT)
939 {
940 switch(textureFunction->sampler)
941 {
942 case EbtSampler2D: out << "tex2D"; break;
943 case EbtSamplerCube: out << "texCUBE"; break;
944 default: UNREACHABLE();
945 }
946
Nicolas Capens75fb4752013-07-10 15:14:47 -0400947 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400948 {
949 case TextureFunction::IMPLICIT: out << "(s, "; break;
950 case TextureFunction::BIAS: out << "bias(s, "; break;
951 case TextureFunction::LOD: out << "lod(s, "; break;
952 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400953 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400954 default: UNREACHABLE();
955 }
956 }
957 else if (mOutputType == SH_HLSL11_OUTPUT)
958 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500959 if (textureFunction->method == TextureFunction::GRAD)
960 {
961 if (IsIntegerSampler(textureFunction->sampler))
962 {
963 out << "x.Load(";
964 }
965 else if (IsShadowSampler(textureFunction->sampler))
966 {
967 out << "x.SampleCmpLevelZero(s, ";
968 }
969 else
970 {
971 out << "x.SampleGrad(s, ";
972 }
973 }
974 else if (IsIntegerSampler(textureFunction->sampler) ||
975 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400976 {
977 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400978 }
Nicolas Capenscb127d32013-07-15 17:26:18 -0400979 else if (IsShadowSampler(textureFunction->sampler))
980 {
981 out << "x.SampleCmp(s, ";
982 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400983 else
984 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400985 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400986 {
987 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
988 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
989 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
990 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400991 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400992 default: UNREACHABLE();
993 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400994 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400995 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400996 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400997
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400998 // Integer sampling requires integer addresses
999 TString addressx = "";
1000 TString addressy = "";
1001 TString addressz = "";
1002 TString close = "";
1003
Nicolas Capensfc014542014-02-18 14:47:13 -05001004 if (IsIntegerSampler(textureFunction->sampler) ||
1005 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001006 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001007 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001008 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001009 case 2: out << "int3("; break;
1010 case 3: out << "int4("; break;
1011 default: UNREACHABLE();
1012 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001013
Nicolas Capensfc014542014-02-18 14:47:13 -05001014 // Convert from normalized floating-point to integer
1015 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001016 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001017 addressx = "int(floor(width * frac((";
1018 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001019
Nicolas Capensfc014542014-02-18 14:47:13 -05001020 if (IsSamplerArray(textureFunction->sampler))
1021 {
1022 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1023 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001024 else if (IsSamplerCube(textureFunction->sampler))
1025 {
1026 addressz = "((((";
1027 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001028 else
1029 {
1030 addressz = "int(floor(depth * frac((";
1031 }
1032
1033 close = "))))";
1034 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001035 }
1036 else
1037 {
1038 switch(hlslCoords)
1039 {
1040 case 2: out << "float2("; break;
1041 case 3: out << "float3("; break;
1042 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001043 default: UNREACHABLE();
1044 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001045 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001046
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001047 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001048
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001049 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001050 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001051 switch(textureFunction->coords)
1052 {
1053 case 3: proj = " / t.z"; break;
1054 case 4: proj = " / t.w"; break;
1055 default: UNREACHABLE();
1056 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001057 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001058
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001059 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001060
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001061 if (mOutputType == SH_HLSL9_OUTPUT)
1062 {
1063 if (hlslCoords >= 3)
1064 {
1065 if (textureFunction->coords < 3)
1066 {
1067 out << ", 0";
1068 }
1069 else
1070 {
1071 out << ", t.z" + proj;
1072 }
1073 }
1074
1075 if (hlslCoords == 4)
1076 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001077 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001078 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001079 case TextureFunction::BIAS: out << ", bias"; break;
1080 case TextureFunction::LOD: out << ", lod"; break;
1081 case TextureFunction::LOD0: out << ", 0"; break;
1082 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001083 default: UNREACHABLE();
1084 }
1085 }
1086
1087 out << "));\n";
1088 }
1089 else if (mOutputType == SH_HLSL11_OUTPUT)
1090 {
1091 if (hlslCoords >= 3)
1092 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001093 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1094 {
1095 out << ", face";
1096 }
1097 else
1098 {
1099 out << ", " + addressz + ("t.z" + proj) + close;
1100 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001101 }
1102
Nicolas Capensd11d5492014-02-19 17:06:10 -05001103 if (textureFunction->method == TextureFunction::GRAD)
1104 {
1105 if (IsIntegerSampler(textureFunction->sampler))
1106 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001107 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001108 }
1109 else if (IsShadowSampler(textureFunction->sampler))
1110 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001111 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001112 switch(textureFunction->coords)
1113 {
1114 case 3: out << "), t.z"; break;
1115 case 4: out << "), t.w"; break;
1116 default: UNREACHABLE();
1117 }
1118 }
1119 else
1120 {
1121 out << "), ddx, ddy";
1122 }
1123 }
1124 else if (IsIntegerSampler(textureFunction->sampler) ||
1125 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001126 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001127 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001128 }
1129 else if (IsShadowSampler(textureFunction->sampler))
1130 {
1131 // Compare value
1132 switch(textureFunction->coords)
1133 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001134 case 3: out << "), t.z"; break;
1135 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001136 default: UNREACHABLE();
1137 }
1138 }
1139 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001140 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001141 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001142 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001143 case TextureFunction::IMPLICIT: out << ")"; break;
1144 case TextureFunction::BIAS: out << "), bias"; break;
1145 case TextureFunction::LOD: out << "), lod"; break;
1146 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001147 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001148 default: UNREACHABLE();
1149 }
1150 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001151
1152 if (textureFunction->offset)
1153 {
1154 out << ", offset";
1155 }
1156
1157 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001158 }
1159 else UNREACHABLE();
1160 }
1161
1162 out << "\n"
1163 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001164 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165 }
1166
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001167 if (mUsesFragCoord)
1168 {
1169 out << "#define GL_USES_FRAG_COORD\n";
1170 }
1171
1172 if (mUsesPointCoord)
1173 {
1174 out << "#define GL_USES_POINT_COORD\n";
1175 }
1176
1177 if (mUsesFrontFacing)
1178 {
1179 out << "#define GL_USES_FRONT_FACING\n";
1180 }
1181
1182 if (mUsesPointSize)
1183 {
1184 out << "#define GL_USES_POINT_SIZE\n";
1185 }
1186
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001187 if (mUsesFragDepth)
1188 {
1189 out << "#define GL_USES_FRAG_DEPTH\n";
1190 }
1191
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001192 if (mUsesDepthRange)
1193 {
1194 out << "#define GL_USES_DEPTH_RANGE\n";
1195 }
1196
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001197 if (mUsesXor)
1198 {
1199 out << "bool xor(bool p, bool q)\n"
1200 "{\n"
1201 " return (p || q) && !(p && q);\n"
1202 "}\n"
1203 "\n";
1204 }
1205
Olli Etuahoe17e3192015-01-02 12:47:59 +02001206 builtInFunctionEmulator->OutputEmulatedFunctionDefinition(out, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001207}
1208
1209void OutputHLSL::visitSymbol(TIntermSymbol *node)
1210{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001211 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001212
Jamie Madill570e04d2013-06-21 09:15:33 -04001213 // Handle accessing std140 structs by value
1214 if (mFlaggedStructMappedNames.count(node) > 0)
1215 {
1216 out << mFlaggedStructMappedNames[node];
1217 return;
1218 }
1219
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001220 TString name = node->getSymbol();
1221
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001222 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001223 {
1224 mUsesDepthRange = true;
1225 out << name;
1226 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001227 else
1228 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001229 TQualifier qualifier = node->getQualifier();
1230
1231 if (qualifier == EvqUniform)
1232 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001233 const TType& nodeType = node->getType();
1234 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1235
1236 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001237 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001238 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001239 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001240 else
1241 {
1242 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001243 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001244
Jamie Madill033dae62014-06-18 12:56:28 -04001245 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001246 }
Jamie Madill19571812013-08-12 15:26:34 -07001247 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001248 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001249 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001250 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001251 }
Jamie Madill033dae62014-06-18 12:56:28 -04001252 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001253 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001254 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001255 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001256 }
Jamie Madill19571812013-08-12 15:26:34 -07001257 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001258 {
1259 mReferencedOutputVariables[name] = node;
1260 out << "out_" << name;
1261 }
1262 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001263 {
1264 out << "gl_Color[0]";
1265 mUsesFragColor = true;
1266 }
1267 else if (qualifier == EvqFragData)
1268 {
1269 out << "gl_Color";
1270 mUsesFragData = true;
1271 }
1272 else if (qualifier == EvqFragCoord)
1273 {
1274 mUsesFragCoord = true;
1275 out << name;
1276 }
1277 else if (qualifier == EvqPointCoord)
1278 {
1279 mUsesPointCoord = true;
1280 out << name;
1281 }
1282 else if (qualifier == EvqFrontFacing)
1283 {
1284 mUsesFrontFacing = true;
1285 out << name;
1286 }
1287 else if (qualifier == EvqPointSize)
1288 {
1289 mUsesPointSize = true;
1290 out << name;
1291 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001292 else if (name == "gl_FragDepthEXT")
1293 {
1294 mUsesFragDepth = true;
1295 out << "gl_Depth";
1296 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001297 else if (qualifier == EvqInternal)
1298 {
1299 out << name;
1300 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001301 else
1302 {
Jamie Madill033dae62014-06-18 12:56:28 -04001303 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001304 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001305 }
1306}
1307
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001308void OutputHLSL::visitRaw(TIntermRaw *node)
1309{
1310 mBody << node->getRawText();
1311}
1312
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001313bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1314{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001315 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001316
Jamie Madill570e04d2013-06-21 09:15:33 -04001317 // Handle accessing std140 structs by value
1318 if (mFlaggedStructMappedNames.count(node) > 0)
1319 {
1320 out << mFlaggedStructMappedNames[node];
1321 return false;
1322 }
1323
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001324 switch (node->getOp())
1325 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001326 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001327 case EOpInitialize:
1328 if (visit == PreVisit)
1329 {
1330 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1331 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1332 // new variable is created before the assignment is evaluated), so we need to convert
1333 // this to "float t = x, x = t;".
1334
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001335 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1336 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001337
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001338 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1339 expression->traverse(&searchSymbol);
1340 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001341
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001342 if (sameSymbol)
1343 {
1344 // Type already printed
1345 out << "t" + str(mUniqueIndex) + " = ";
1346 expression->traverse(this);
1347 out << ", ";
1348 symbolNode->traverse(this);
1349 out << " = t" + str(mUniqueIndex);
1350
1351 mUniqueIndex++;
1352 return false;
1353 }
1354 }
1355 else if (visit == InVisit)
1356 {
1357 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001358 }
1359 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001360 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1361 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1362 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1363 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1364 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1365 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001366 if (visit == PreVisit)
1367 {
1368 out << "(";
1369 }
1370 else if (visit == InVisit)
1371 {
1372 out << " = mul(";
1373 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001374 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001375 }
1376 else
1377 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001378 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001379 }
1380 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001381 case EOpMatrixTimesMatrixAssign:
1382 if (visit == PreVisit)
1383 {
1384 out << "(";
1385 }
1386 else if (visit == InVisit)
1387 {
1388 out << " = mul(";
1389 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001390 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001391 }
1392 else
1393 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001394 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001395 }
1396 break;
1397 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001398 case EOpModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001399 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001400 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001401 const TType& leftType = node->getLeft()->getType();
1402 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001403 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001404 if (visit == PreVisit)
1405 {
1406 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1407 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001408 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001409 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001410 return false;
1411 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001412 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001413 else
1414 {
1415 outputTriplet(visit, "", "[", "]");
1416 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001417 }
1418 break;
1419 case EOpIndexIndirect:
1420 // We do not currently support indirect references to interface blocks
1421 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1422 outputTriplet(visit, "", "[", "]");
1423 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001424 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001425 if (visit == InVisit)
1426 {
1427 const TStructure* structure = node->getLeft()->getType().getStruct();
1428 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1429 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001430 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001431
1432 return false;
1433 }
1434 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001435 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001436 if (visit == InVisit)
1437 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001438 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1439 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1440 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001441 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001442
1443 return false;
1444 }
1445 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001446 case EOpVectorSwizzle:
1447 if (visit == InVisit)
1448 {
1449 out << ".";
1450
1451 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1452
1453 if (swizzle)
1454 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001455 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001456
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001457 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001458 {
1459 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1460
1461 if (element)
1462 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001463 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001464
1465 switch (i)
1466 {
1467 case 0: out << "x"; break;
1468 case 1: out << "y"; break;
1469 case 2: out << "z"; break;
1470 case 3: out << "w"; break;
1471 default: UNREACHABLE();
1472 }
1473 }
1474 else UNREACHABLE();
1475 }
1476 }
1477 else UNREACHABLE();
1478
1479 return false; // Fully processed
1480 }
1481 break;
1482 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1483 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1484 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1485 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001486 case EOpMod: outputTriplet(visit, "(", " % ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001487 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001488 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001489 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001490 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001491 if (node->getOp() == EOpEqual)
1492 {
1493 outputTriplet(visit, "(", " == ", ")");
1494 }
1495 else
1496 {
1497 outputTriplet(visit, "(", " != ", ")");
1498 }
1499 }
1500 else if (node->getLeft()->getBasicType() == EbtStruct)
1501 {
1502 if (node->getOp() == EOpEqual)
1503 {
1504 out << "(";
1505 }
1506 else
1507 {
1508 out << "!(";
1509 }
1510
Jamie Madill98493dd2013-07-08 14:39:03 -04001511 const TStructure &structure = *node->getLeft()->getType().getStruct();
1512 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001513
Jamie Madill98493dd2013-07-08 14:39:03 -04001514 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001515 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001516 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001517
1518 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001519 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001520 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001521 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001522
Jamie Madill98493dd2013-07-08 14:39:03 -04001523 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001524 {
1525 out << " && ";
1526 }
1527 }
1528
1529 out << ")";
1530
1531 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001532 }
1533 else
1534 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001535 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001536
1537 if (node->getOp() == EOpEqual)
1538 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001539 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001540 }
1541 else
1542 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001543 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001544 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001545 }
1546 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001547 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1548 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1549 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1550 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1551 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001552 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001553 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1554 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001555 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001556 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001557 if (node->getRight()->hasSideEffects())
1558 {
1559 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1560 return false;
1561 }
1562 else
1563 {
1564 outputTriplet(visit, "(", " || ", ")");
1565 return true;
1566 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001567 case EOpLogicalXor:
1568 mUsesXor = true;
1569 outputTriplet(visit, "xor(", ", ", ")");
1570 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001571 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001572 if (node->getRight()->hasSideEffects())
1573 {
1574 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1575 return false;
1576 }
1577 else
1578 {
1579 outputTriplet(visit, "(", " && ", ")");
1580 return true;
1581 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001582 default: UNREACHABLE();
1583 }
1584
1585 return true;
1586}
1587
1588bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1589{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001590 switch (node->getOp())
1591 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001592 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001593 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001594 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1595 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1596 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1597 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1598 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1599 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001600 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1601 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1602 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1603 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1604 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1605 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1606 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1607 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001608 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1609 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1610 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1611 case EOpAsinh:
1612 ASSERT(node->getUseEmulatedFunction());
1613 writeEmulatedFunctionTriplet(visit, "asinh(");
1614 break;
1615 case EOpAcosh:
1616 ASSERT(node->getUseEmulatedFunction());
1617 writeEmulatedFunctionTriplet(visit, "acosh(");
1618 break;
1619 case EOpAtanh:
1620 ASSERT(node->getUseEmulatedFunction());
1621 writeEmulatedFunctionTriplet(visit, "atanh(");
1622 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001623 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1624 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1625 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1626 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1627 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1628 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1629 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1630 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1631 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1632 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1633 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001634 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1635 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1636 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1637 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001638 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1639 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001640 case EOpDFdx:
1641 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1642 {
1643 outputTriplet(visit, "(", "", ", 0.0)");
1644 }
1645 else
1646 {
1647 outputTriplet(visit, "ddx(", "", ")");
1648 }
1649 break;
1650 case EOpDFdy:
1651 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1652 {
1653 outputTriplet(visit, "(", "", ", 0.0)");
1654 }
1655 else
1656 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001657 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001658 }
1659 break;
1660 case EOpFwidth:
1661 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1662 {
1663 outputTriplet(visit, "(", "", ", 0.0)");
1664 }
1665 else
1666 {
1667 outputTriplet(visit, "fwidth(", "", ")");
1668 }
1669 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001670 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1671 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 default: UNREACHABLE();
1673 }
1674
1675 return true;
1676}
1677
1678bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1679{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001680 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001681
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001682 switch (node->getOp())
1683 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001684 case EOpSequence:
1685 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001686 if (mInsideFunction)
1687 {
Jamie Madill075edd82013-07-08 13:30:19 -04001688 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001689 out << "{\n";
1690 }
1691
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001692 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001693 {
Jamie Madill075edd82013-07-08 13:30:19 -04001694 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001695
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001696 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001697
1698 out << ";\n";
1699 }
1700
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001701 if (mInsideFunction)
1702 {
Jamie Madill075edd82013-07-08 13:30:19 -04001703 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001704 out << "}\n";
1705 }
1706
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001707 return false;
1708 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709 case EOpDeclaration:
1710 if (visit == PreVisit)
1711 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001712 TIntermSequence *sequence = node->getSequence();
1713 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001714
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001715 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001717 TStructure *structure = variable->getType().getStruct();
1718
1719 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001720 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001721 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001722 }
1723
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001724 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001725 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001726 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 {
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001728 if (isSingleStatement(*sit))
1729 {
1730 mUnfoldShortCircuit->traverse(*sit);
1731 }
1732
Nicolas Capensd974db42014-10-07 10:50:19 -04001733 if (!mInsideFunction)
1734 {
1735 out << "static ";
1736 }
1737
1738 out << TypeString(variable->getType()) + " ";
1739
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001740 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001741
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001742 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001744 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001745 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001746 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001747 }
1748 else
1749 {
1750 (*sit)->traverse(this);
1751 }
1752
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001753 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001754 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001755 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 }
1757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001759 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1760 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001761 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001762 }
1763 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 }
Jamie Madill033dae62014-06-18 12:56:28 -04001765 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001766 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001767 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001768 {
1769 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1770
1771 if (symbol)
1772 {
1773 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1774 mReferencedVaryings[symbol->getSymbol()] = symbol;
1775 }
1776 else
1777 {
1778 (*sit)->traverse(this);
1779 }
1780 }
1781 }
1782
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783 return false;
1784 }
1785 else if (visit == InVisit)
1786 {
1787 out << ", ";
1788 }
1789 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001790 case EOpInvariantDeclaration:
1791 // Do not do any translation
1792 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001793 case EOpPrototype:
1794 if (visit == PreVisit)
1795 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001796 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001797
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001798 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001799
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001800 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001801 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001802 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001803
1804 if (symbol)
1805 {
1806 out << argumentString(symbol);
1807
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001808 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001809 {
1810 out << ", ";
1811 }
1812 }
1813 else UNREACHABLE();
1814 }
1815
1816 out << ");\n";
1817
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001818 // Also prototype the Lod0 variant if needed
1819 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1820 {
1821 mOutputLod0Function = true;
1822 node->traverse(this);
1823 mOutputLod0Function = false;
1824 }
1825
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001826 return false;
1827 }
1828 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001829 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 case EOpFunction:
1831 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001832 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833
Jamie Madill033dae62014-06-18 12:56:28 -04001834 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001835
1836 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001838 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001840 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841 {
Jamie Madill033dae62014-06-18 12:56:28 -04001842 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001843 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001844
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001845 TIntermSequence *sequence = node->getSequence();
1846 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001847
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001848 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001849 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001850 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001851
1852 if (symbol)
1853 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001854 TStructure *structure = symbol->getType().getStruct();
1855
1856 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001857 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001858 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001859 }
1860
1861 out << argumentString(symbol);
1862
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001863 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001864 {
1865 out << ", ";
1866 }
1867 }
1868 else UNREACHABLE();
1869 }
1870
1871 out << ")\n"
1872 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001873
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001874 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001875 {
1876 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001877 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001878 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001880
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001881 out << "}\n";
1882
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001883 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1884 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001885 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001886 {
1887 mOutputLod0Function = true;
1888 node->traverse(this);
1889 mOutputLod0Function = false;
1890 }
1891 }
1892
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001893 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894 }
1895 break;
1896 case EOpFunctionCall:
1897 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001898 TString name = TFunction::unmangleName(node->getName());
1899 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001900 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001901
1902 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903 {
Jamie Madill033dae62014-06-18 12:56:28 -04001904 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905 }
1906 else
1907 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001908 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001909
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001910 TextureFunction textureFunction;
1911 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001912 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04001913 textureFunction.method = TextureFunction::IMPLICIT;
1914 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001915 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001916
1917 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001918 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001919 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001920 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001921 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001922 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001923 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001924 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001925 }
Nicolas Capens46485082014-04-15 13:12:50 -04001926 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
1927 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001928 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001929 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001930 }
Nicolas Capens46485082014-04-15 13:12:50 -04001931 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001932 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001933 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001934 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001935 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04001936 else if (name == "textureSize")
1937 {
1938 textureFunction.method = TextureFunction::SIZE;
1939 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001940 else if (name == "textureOffset")
1941 {
1942 textureFunction.method = TextureFunction::IMPLICIT;
1943 textureFunction.offset = true;
1944 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05001945 else if (name == "textureProjOffset")
1946 {
1947 textureFunction.method = TextureFunction::IMPLICIT;
1948 textureFunction.offset = true;
1949 textureFunction.proj = true;
1950 }
1951 else if (name == "textureLodOffset")
1952 {
1953 textureFunction.method = TextureFunction::LOD;
1954 textureFunction.offset = true;
1955 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001956 else if (name == "textureProjLodOffset")
1957 {
1958 textureFunction.method = TextureFunction::LOD;
1959 textureFunction.proj = true;
1960 textureFunction.offset = true;
1961 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001962 else if (name == "texelFetch")
1963 {
1964 textureFunction.method = TextureFunction::FETCH;
1965 }
1966 else if (name == "texelFetchOffset")
1967 {
1968 textureFunction.method = TextureFunction::FETCH;
1969 textureFunction.offset = true;
1970 }
Nicolas Capens46485082014-04-15 13:12:50 -04001971 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05001972 {
1973 textureFunction.method = TextureFunction::GRAD;
1974 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05001975 else if (name == "textureGradOffset")
1976 {
1977 textureFunction.method = TextureFunction::GRAD;
1978 textureFunction.offset = true;
1979 }
Nicolas Capens46485082014-04-15 13:12:50 -04001980 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05001981 {
1982 textureFunction.method = TextureFunction::GRAD;
1983 textureFunction.proj = true;
1984 }
1985 else if (name == "textureProjGradOffset")
1986 {
1987 textureFunction.method = TextureFunction::GRAD;
1988 textureFunction.proj = true;
1989 textureFunction.offset = true;
1990 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001991 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001992
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001993 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001994 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001995 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
1996
1997 if (textureFunction.offset)
1998 {
1999 mandatoryArgumentCount++;
2000 }
2001
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002002 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002003
Jamie Madill183bde52014-07-02 15:31:19 -04002004 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002005 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002006 if (bias)
2007 {
2008 textureFunction.method = TextureFunction::LOD0BIAS;
2009 }
2010 else
2011 {
2012 textureFunction.method = TextureFunction::LOD0;
2013 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002014 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002015 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002016 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002017 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002018 }
2019 }
2020
2021 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002022
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002023 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002025
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002026 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002027 {
2028 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2029 {
2030 out << "texture_";
2031 (*arg)->traverse(this);
2032 out << ", sampler_";
2033 }
2034
2035 (*arg)->traverse(this);
2036
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002037 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002038 {
2039 out << ", ";
2040 }
2041 }
2042
2043 out << ")";
2044
2045 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046 }
2047 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002048 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002049 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2050 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2051 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2052 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2053 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2054 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2055 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2056 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2057 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2058 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2059 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2060 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2061 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2062 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2063 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2064 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2065 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2066 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2067 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002068 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002069 {
Jamie Madill033dae62014-06-18 12:56:28 -04002070 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002071 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002072 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2073 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002074 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002075 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2076 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2077 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2078 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2079 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2080 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002081 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002082 ASSERT(node->getUseEmulatedFunction());
2083 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002084 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002085 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002087 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002088 ASSERT(node->getUseEmulatedFunction());
2089 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 break;
2091 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2092 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2093 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2094 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2095 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2096 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2097 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2098 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2099 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002100 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002101 ASSERT(node->getUseEmulatedFunction());
2102 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002103 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2105 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2106 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107 default: UNREACHABLE();
2108 }
2109
2110 return true;
2111}
2112
2113bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2114{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002115 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002117 if (node->usesTernaryOperator())
2118 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002119 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002120 }
2121 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002123 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002124
Corentin Wallez80bacde2014-11-10 12:07:37 -08002125 // D3D errors when there is a gradient operation in a loop in an unflattened if
2126 // however flattening all the ifs in branch heavy shaders made D3D error too.
2127 // As a temporary workaround we flatten the ifs only if there is at least a loop
2128 // present somewhere in the shader.
2129 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2130 {
2131 out << "FLATTEN ";
2132 }
2133
2134 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002135
2136 node->getCondition()->traverse(this);
2137
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002138 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002139
Jamie Madill075edd82013-07-08 13:30:19 -04002140 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002141 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002143 bool discard = false;
2144
daniel@transgaming.combb885322010-04-15 20:45:24 +00002145 if (node->getTrueBlock())
2146 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002147 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002148
2149 // Detect true discard
2150 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002151 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152
Jamie Madill075edd82013-07-08 13:30:19 -04002153 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002154 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002155
2156 if (node->getFalseBlock())
2157 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002158 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002159
Jamie Madill075edd82013-07-08 13:30:19 -04002160 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002161 out << "{\n";
2162
Jamie Madill075edd82013-07-08 13:30:19 -04002163 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002164 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002165
Jamie Madill075edd82013-07-08 13:30:19 -04002166 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002167 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002168
2169 // Detect false discard
2170 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2171 }
2172
2173 // ANGLE issue 486: Detect problematic conditional discard
2174 if (discard && FindSideEffectRewriting::search(node))
2175 {
2176 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002177 }
2178 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179
2180 return false;
2181}
2182
2183void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2184{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002185 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186}
2187
2188bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2189{
Nicolas Capens655fe362014-04-11 13:12:34 -04002190 mNestedLoopDepth++;
2191
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002192 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2193
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002194 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002195 {
2196 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2197 }
2198
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002199 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002200 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002201 if (handleExcessiveLoop(node))
2202 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002203 mInsideDiscontinuousLoop = wasDiscontinuous;
2204 mNestedLoopDepth--;
2205
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002206 return false;
2207 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002208 }
2209
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002210 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211
alokp@chromium.org52813552010-11-16 18:36:09 +00002212 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002214 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002215
Jamie Madill075edd82013-07-08 13:30:19 -04002216 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002217 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002218 }
2219 else
2220 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002221 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002222
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002223 if (node->getInit())
2224 {
2225 node->getInit()->traverse(this);
2226 }
2227
2228 out << "; ";
2229
alokp@chromium.org52813552010-11-16 18:36:09 +00002230 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002231 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002232 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233 }
2234
2235 out << "; ";
2236
alokp@chromium.org52813552010-11-16 18:36:09 +00002237 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002239 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240 }
2241
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002242 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002243
Jamie Madill075edd82013-07-08 13:30:19 -04002244 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002245 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002246 }
2247
2248 if (node->getBody())
2249 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002250 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251 }
2252
Jamie Madill075edd82013-07-08 13:30:19 -04002253 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002254 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255
alokp@chromium.org52813552010-11-16 18:36:09 +00002256 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257 {
Jamie Madill075edd82013-07-08 13:30:19 -04002258 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 out << "while(\n";
2260
alokp@chromium.org52813552010-11-16 18:36:09 +00002261 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262
daniel@transgaming.com73536982012-03-21 20:45:49 +00002263 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 }
2265
daniel@transgaming.com73536982012-03-21 20:45:49 +00002266 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002268 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002269 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002270
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 return false;
2272}
2273
2274bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2275{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002276 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002277
2278 switch (node->getFlowOp())
2279 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002280 case EOpKill:
2281 outputTriplet(visit, "discard;\n", "", "");
2282 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002283 case EOpBreak:
2284 if (visit == PreVisit)
2285 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002286 if (mNestedLoopDepth > 1)
2287 {
2288 mUsesNestedBreak = true;
2289 }
2290
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002291 if (mExcessiveLoopIndex)
2292 {
2293 out << "{Break";
2294 mExcessiveLoopIndex->traverse(this);
2295 out << " = true; break;}\n";
2296 }
2297 else
2298 {
2299 out << "break;\n";
2300 }
2301 }
2302 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002303 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304 case EOpReturn:
2305 if (visit == PreVisit)
2306 {
2307 if (node->getExpression())
2308 {
2309 out << "return ";
2310 }
2311 else
2312 {
2313 out << "return;\n";
2314 }
2315 }
2316 else if (visit == PostVisit)
2317 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002318 if (node->getExpression())
2319 {
2320 out << ";\n";
2321 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322 }
2323 break;
2324 default: UNREACHABLE();
2325 }
2326
2327 return true;
2328}
2329
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002330void OutputHLSL::traverseStatements(TIntermNode *node)
2331{
2332 if (isSingleStatement(node))
2333 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002334 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002335 }
2336
2337 node->traverse(this);
2338}
2339
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002340bool OutputHLSL::isSingleStatement(TIntermNode *node)
2341{
2342 TIntermAggregate *aggregate = node->getAsAggregate();
2343
2344 if (aggregate)
2345 {
2346 if (aggregate->getOp() == EOpSequence)
2347 {
2348 return false;
2349 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002350 else if (aggregate->getOp() == EOpDeclaration)
2351 {
2352 // Declaring multiple comma-separated variables must be considered multiple statements
2353 // because each individual declaration has side effects which are visible in the next.
2354 return false;
2355 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002356 else
2357 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002358 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002359 {
2360 if (!isSingleStatement(*sit))
2361 {
2362 return false;
2363 }
2364 }
2365
2366 return true;
2367 }
2368 }
2369
2370 return true;
2371}
2372
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002373// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2374// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002375bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2376{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002377 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002378 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002379
2380 // Parse loops of the form:
2381 // for(int index = initial; index [comparator] limit; index += increment)
2382 TIntermSymbol *index = NULL;
2383 TOperator comparator = EOpNull;
2384 int initial = 0;
2385 int limit = 0;
2386 int increment = 0;
2387
2388 // Parse index name and intial value
2389 if (node->getInit())
2390 {
2391 TIntermAggregate *init = node->getInit()->getAsAggregate();
2392
2393 if (init)
2394 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002395 TIntermSequence *sequence = init->getSequence();
2396 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002397
2398 if (variable && variable->getQualifier() == EvqTemporary)
2399 {
2400 TIntermBinary *assign = variable->getAsBinaryNode();
2401
2402 if (assign->getOp() == EOpInitialize)
2403 {
2404 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2405 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2406
2407 if (symbol && constant)
2408 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002409 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002410 {
2411 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002412 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002413 }
2414 }
2415 }
2416 }
2417 }
2418 }
2419
2420 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002421 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002422 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002423 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002424
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002425 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2426 {
2427 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2428
2429 if (constant)
2430 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002431 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002432 {
2433 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002434 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002435 }
2436 }
2437 }
2438 }
2439
2440 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002441 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002442 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002443 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2444 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002445
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002446 if (binaryTerminal)
2447 {
2448 TOperator op = binaryTerminal->getOp();
2449 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2450
2451 if (constant)
2452 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002453 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002454 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002455 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002456
2457 switch (op)
2458 {
2459 case EOpAddAssign: increment = value; break;
2460 case EOpSubAssign: increment = -value; break;
2461 default: UNIMPLEMENTED();
2462 }
2463 }
2464 }
2465 }
2466 else if (unaryTerminal)
2467 {
2468 TOperator op = unaryTerminal->getOp();
2469
2470 switch (op)
2471 {
2472 case EOpPostIncrement: increment = 1; break;
2473 case EOpPostDecrement: increment = -1; break;
2474 case EOpPreIncrement: increment = 1; break;
2475 case EOpPreDecrement: increment = -1; break;
2476 default: UNIMPLEMENTED();
2477 }
2478 }
2479 }
2480
2481 if (index != NULL && comparator != EOpNull && increment != 0)
2482 {
2483 if (comparator == EOpLessThanEqual)
2484 {
2485 comparator = EOpLessThan;
2486 limit += 1;
2487 }
2488
2489 if (comparator == EOpLessThan)
2490 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002491 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002492
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002493 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002494 {
2495 return false; // Not an excessive loop
2496 }
2497
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002498 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2499 mExcessiveLoopIndex = index;
2500
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002501 out << "{int ";
2502 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002503 out << ";\n"
2504 "bool Break";
2505 index->traverse(this);
2506 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002507
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002508 bool firstLoopFragment = true;
2509
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002510 while (iterations > 0)
2511 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002512 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002513
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002514 if (!firstLoopFragment)
2515 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002516 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002517 index->traverse(this);
2518 out << ") {\n";
2519 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002520
2521 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2522 {
2523 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2524 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002525
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002526 // for(int index = initial; index < clampedLimit; index += increment)
2527
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002528 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002529 index->traverse(this);
2530 out << " = ";
2531 out << initial;
2532
2533 out << "; ";
2534 index->traverse(this);
2535 out << " < ";
2536 out << clampedLimit;
2537
2538 out << "; ";
2539 index->traverse(this);
2540 out << " += ";
2541 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002542 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002543
Jamie Madill075edd82013-07-08 13:30:19 -04002544 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002545 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002546
2547 if (node->getBody())
2548 {
2549 node->getBody()->traverse(this);
2550 }
2551
Jamie Madill075edd82013-07-08 13:30:19 -04002552 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002553 out << ";}\n";
2554
2555 if (!firstLoopFragment)
2556 {
2557 out << "}\n";
2558 }
2559
2560 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002561
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002562 initial += MAX_LOOP_ITERATIONS * increment;
2563 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002564 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002565
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002566 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002567
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002568 mExcessiveLoopIndex = restoreIndex;
2569
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002570 return true;
2571 }
2572 else UNIMPLEMENTED();
2573 }
2574
2575 return false; // Not handled as an excessive loop
2576}
2577
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002578void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002580 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002581
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002582 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002583 {
2584 out << preString;
2585 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002586 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002587 {
2588 out << inString;
2589 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002590 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002591 {
2592 out << postString;
2593 }
2594}
2595
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002596void OutputHLSL::outputLineDirective(int line)
2597{
2598 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2599 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002600 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002601 mBody << "#line " << line;
2602
2603 if (mContext.sourcePath)
2604 {
2605 mBody << " \"" << mContext.sourcePath << "\"";
2606 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002607
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002608 mBody << "\n";
2609 }
2610}
2611
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002612TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2613{
2614 TQualifier qualifier = symbol->getQualifier();
2615 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002616 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002617
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002618 if (name.empty()) // HLSL demands named arguments, also for prototypes
2619 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002620 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002621 }
2622 else
2623 {
Jamie Madill033dae62014-06-18 12:56:28 -04002624 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002625 }
2626
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002627 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2628 {
Jamie Madill033dae62014-06-18 12:56:28 -04002629 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002630 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002631 }
2632
Jamie Madill033dae62014-06-18 12:56:28 -04002633 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634}
2635
2636TString OutputHLSL::initializer(const TType &type)
2637{
2638 TString string;
2639
Jamie Madill94bf7f22013-07-08 13:31:15 -04002640 size_t size = type.getObjectSize();
2641 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002642 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002643 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002644
Jamie Madill94bf7f22013-07-08 13:31:15 -04002645 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646 {
2647 string += ", ";
2648 }
2649 }
2650
daniel@transgaming.comead23042010-04-29 03:35:36 +00002651 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002652}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002653
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002654void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2655{
2656 TInfoSinkBase &out = mBody;
2657
2658 if (visit == PreVisit)
2659 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002660 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002661
2662 out << name + "(";
2663 }
2664 else if (visit == InVisit)
2665 {
2666 out << ", ";
2667 }
2668 else if (visit == PostVisit)
2669 {
2670 out << ")";
2671 }
2672}
2673
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002674const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2675{
2676 TInfoSinkBase &out = mBody;
2677
Jamie Madill98493dd2013-07-08 14:39:03 -04002678 const TStructure* structure = type.getStruct();
2679 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002680 {
Jamie Madill033dae62014-06-18 12:56:28 -04002681 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002682
Jamie Madill98493dd2013-07-08 14:39:03 -04002683 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002684
Jamie Madill98493dd2013-07-08 14:39:03 -04002685 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002686 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002687 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002688 constUnion = writeConstantUnion(*fieldType, constUnion);
2689
Jamie Madill98493dd2013-07-08 14:39:03 -04002690 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002691 {
2692 out << ", ";
2693 }
2694 }
2695
2696 out << ")";
2697 }
2698 else
2699 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002700 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002701 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002702
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002703 if (writeType)
2704 {
Jamie Madill033dae62014-06-18 12:56:28 -04002705 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002706 }
2707
Jamie Madill94bf7f22013-07-08 13:31:15 -04002708 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002709 {
2710 switch (constUnion->getType())
2711 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002712 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002713 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002714 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002715 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002716 default: UNREACHABLE();
2717 }
2718
2719 if (i != size - 1)
2720 {
2721 out << ", ";
2722 }
2723 }
2724
2725 if (writeType)
2726 {
2727 out << ")";
2728 }
2729 }
2730
2731 return constUnion;
2732}
2733
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002734void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2735{
2736 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2737 outputTriplet(visit, preString.c_str(), ", ", ")");
2738}
2739
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740}