blob: 56ceb205af67dbcc993924eef394689f8cd772ad [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;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001670 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1671 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
1672
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001673 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1674 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675 default: UNREACHABLE();
1676 }
1677
1678 return true;
1679}
1680
1681bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1682{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001683 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001684
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685 switch (node->getOp())
1686 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001687 case EOpSequence:
1688 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001689 if (mInsideFunction)
1690 {
Jamie Madill075edd82013-07-08 13:30:19 -04001691 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001692 out << "{\n";
1693 }
1694
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001695 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001696 {
Jamie Madill075edd82013-07-08 13:30:19 -04001697 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001698
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001699 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001700
1701 out << ";\n";
1702 }
1703
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001704 if (mInsideFunction)
1705 {
Jamie Madill075edd82013-07-08 13:30:19 -04001706 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001707 out << "}\n";
1708 }
1709
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001710 return false;
1711 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712 case EOpDeclaration:
1713 if (visit == PreVisit)
1714 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001715 TIntermSequence *sequence = node->getSequence();
1716 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001718 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001720 TStructure *structure = variable->getType().getStruct();
1721
1722 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001723 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001724 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001725 }
1726
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001727 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001729 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001730 {
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001731 if (isSingleStatement(*sit))
1732 {
1733 mUnfoldShortCircuit->traverse(*sit);
1734 }
1735
Nicolas Capensd974db42014-10-07 10:50:19 -04001736 if (!mInsideFunction)
1737 {
1738 out << "static ";
1739 }
1740
1741 out << TypeString(variable->getType()) + " ";
1742
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001743 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001745 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001747 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001748 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001749 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001750 }
1751 else
1752 {
1753 (*sit)->traverse(this);
1754 }
1755
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001756 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001757 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001758 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 }
1760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001762 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1763 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001764 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001765 }
1766 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767 }
Jamie Madill033dae62014-06-18 12:56:28 -04001768 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001769 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001770 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001771 {
1772 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1773
1774 if (symbol)
1775 {
1776 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1777 mReferencedVaryings[symbol->getSymbol()] = symbol;
1778 }
1779 else
1780 {
1781 (*sit)->traverse(this);
1782 }
1783 }
1784 }
1785
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786 return false;
1787 }
1788 else if (visit == InVisit)
1789 {
1790 out << ", ";
1791 }
1792 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001793 case EOpInvariantDeclaration:
1794 // Do not do any translation
1795 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001796 case EOpPrototype:
1797 if (visit == PreVisit)
1798 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001799 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001800
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001801 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001802
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001803 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001804 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001805 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001806
1807 if (symbol)
1808 {
1809 out << argumentString(symbol);
1810
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001811 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001812 {
1813 out << ", ";
1814 }
1815 }
1816 else UNREACHABLE();
1817 }
1818
1819 out << ");\n";
1820
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001821 // Also prototype the Lod0 variant if needed
1822 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1823 {
1824 mOutputLod0Function = true;
1825 node->traverse(this);
1826 mOutputLod0Function = false;
1827 }
1828
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001829 return false;
1830 }
1831 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001832 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833 case EOpFunction:
1834 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001835 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836
Jamie Madill033dae62014-06-18 12:56:28 -04001837 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001838
1839 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001841 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001843 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001844 {
Jamie Madill033dae62014-06-18 12:56:28 -04001845 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001846 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001847
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001848 TIntermSequence *sequence = node->getSequence();
1849 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001850
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001851 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001852 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001853 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001854
1855 if (symbol)
1856 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001857 TStructure *structure = symbol->getType().getStruct();
1858
1859 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001860 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001861 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001862 }
1863
1864 out << argumentString(symbol);
1865
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001866 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001867 {
1868 out << ", ";
1869 }
1870 }
1871 else UNREACHABLE();
1872 }
1873
1874 out << ")\n"
1875 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001876
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001877 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001878 {
1879 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001880 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001881 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001882 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001883
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001884 out << "}\n";
1885
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001886 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1887 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001888 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001889 {
1890 mOutputLod0Function = true;
1891 node->traverse(this);
1892 mOutputLod0Function = false;
1893 }
1894 }
1895
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001896 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897 }
1898 break;
1899 case EOpFunctionCall:
1900 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001901 TString name = TFunction::unmangleName(node->getName());
1902 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001903 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001904
1905 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001906 {
Jamie Madill033dae62014-06-18 12:56:28 -04001907 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908 }
1909 else
1910 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001911 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001912
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001913 TextureFunction textureFunction;
1914 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001915 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04001916 textureFunction.method = TextureFunction::IMPLICIT;
1917 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001918 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001919
1920 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001921 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001922 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001923 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001924 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001925 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001926 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001927 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001928 }
Nicolas Capens46485082014-04-15 13:12:50 -04001929 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
1930 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001931 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001932 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001933 }
Nicolas Capens46485082014-04-15 13:12:50 -04001934 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001935 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001936 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001937 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001938 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04001939 else if (name == "textureSize")
1940 {
1941 textureFunction.method = TextureFunction::SIZE;
1942 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001943 else if (name == "textureOffset")
1944 {
1945 textureFunction.method = TextureFunction::IMPLICIT;
1946 textureFunction.offset = true;
1947 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05001948 else if (name == "textureProjOffset")
1949 {
1950 textureFunction.method = TextureFunction::IMPLICIT;
1951 textureFunction.offset = true;
1952 textureFunction.proj = true;
1953 }
1954 else if (name == "textureLodOffset")
1955 {
1956 textureFunction.method = TextureFunction::LOD;
1957 textureFunction.offset = true;
1958 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001959 else if (name == "textureProjLodOffset")
1960 {
1961 textureFunction.method = TextureFunction::LOD;
1962 textureFunction.proj = true;
1963 textureFunction.offset = true;
1964 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001965 else if (name == "texelFetch")
1966 {
1967 textureFunction.method = TextureFunction::FETCH;
1968 }
1969 else if (name == "texelFetchOffset")
1970 {
1971 textureFunction.method = TextureFunction::FETCH;
1972 textureFunction.offset = true;
1973 }
Nicolas Capens46485082014-04-15 13:12:50 -04001974 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05001975 {
1976 textureFunction.method = TextureFunction::GRAD;
1977 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05001978 else if (name == "textureGradOffset")
1979 {
1980 textureFunction.method = TextureFunction::GRAD;
1981 textureFunction.offset = true;
1982 }
Nicolas Capens46485082014-04-15 13:12:50 -04001983 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05001984 {
1985 textureFunction.method = TextureFunction::GRAD;
1986 textureFunction.proj = true;
1987 }
1988 else if (name == "textureProjGradOffset")
1989 {
1990 textureFunction.method = TextureFunction::GRAD;
1991 textureFunction.proj = true;
1992 textureFunction.offset = true;
1993 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001994 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001995
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001996 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001997 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001998 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
1999
2000 if (textureFunction.offset)
2001 {
2002 mandatoryArgumentCount++;
2003 }
2004
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002005 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002006
Jamie Madill183bde52014-07-02 15:31:19 -04002007 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002008 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002009 if (bias)
2010 {
2011 textureFunction.method = TextureFunction::LOD0BIAS;
2012 }
2013 else
2014 {
2015 textureFunction.method = TextureFunction::LOD0;
2016 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002017 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002018 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002019 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002020 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002021 }
2022 }
2023
2024 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002025
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002026 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002028
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002029 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002030 {
2031 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2032 {
2033 out << "texture_";
2034 (*arg)->traverse(this);
2035 out << ", sampler_";
2036 }
2037
2038 (*arg)->traverse(this);
2039
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002040 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002041 {
2042 out << ", ";
2043 }
2044 }
2045
2046 out << ")";
2047
2048 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049 }
2050 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002051 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002052 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2053 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2054 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2055 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2056 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2057 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2058 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2059 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2060 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2061 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2062 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2063 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2064 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2065 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2066 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2067 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2068 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2069 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2070 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002071 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002072 {
Jamie Madill033dae62014-06-18 12:56:28 -04002073 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002074 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002075 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2076 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002077 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002078 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2079 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2080 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2081 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2082 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2083 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002084 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002085 ASSERT(node->getUseEmulatedFunction());
2086 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002087 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002088 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002090 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002091 ASSERT(node->getUseEmulatedFunction());
2092 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093 break;
2094 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2095 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2096 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2097 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2098 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2099 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2100 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2101 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2102 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002103 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002104 ASSERT(node->getUseEmulatedFunction());
2105 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002106 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2108 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002109 case EOpOuterProduct:
2110 ASSERT(node->getUseEmulatedFunction());
2111 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2112 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114 default: UNREACHABLE();
2115 }
2116
2117 return true;
2118}
2119
2120bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2121{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002122 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002124 if (node->usesTernaryOperator())
2125 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002126 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002127 }
2128 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002129 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002130 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002131
Corentin Wallez80bacde2014-11-10 12:07:37 -08002132 // D3D errors when there is a gradient operation in a loop in an unflattened if
2133 // however flattening all the ifs in branch heavy shaders made D3D error too.
2134 // As a temporary workaround we flatten the ifs only if there is at least a loop
2135 // present somewhere in the shader.
2136 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2137 {
2138 out << "FLATTEN ";
2139 }
2140
2141 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002142
2143 node->getCondition()->traverse(this);
2144
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002145 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002146
Jamie Madill075edd82013-07-08 13:30:19 -04002147 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002148 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002150 bool discard = false;
2151
daniel@transgaming.combb885322010-04-15 20:45:24 +00002152 if (node->getTrueBlock())
2153 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002154 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002155
2156 // Detect true discard
2157 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002158 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002159
Jamie Madill075edd82013-07-08 13:30:19 -04002160 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002161 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002162
2163 if (node->getFalseBlock())
2164 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002165 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002166
Jamie Madill075edd82013-07-08 13:30:19 -04002167 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002168 out << "{\n";
2169
Jamie Madill075edd82013-07-08 13:30:19 -04002170 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002171 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002172
Jamie Madill075edd82013-07-08 13:30:19 -04002173 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002174 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002175
2176 // Detect false discard
2177 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2178 }
2179
2180 // ANGLE issue 486: Detect problematic conditional discard
2181 if (discard && FindSideEffectRewriting::search(node))
2182 {
2183 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002184 }
2185 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186
2187 return false;
2188}
2189
2190void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2191{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002192 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002193}
2194
2195bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2196{
Nicolas Capens655fe362014-04-11 13:12:34 -04002197 mNestedLoopDepth++;
2198
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002199 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2200
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002201 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002202 {
2203 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2204 }
2205
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002206 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002207 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002208 if (handleExcessiveLoop(node))
2209 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002210 mInsideDiscontinuousLoop = wasDiscontinuous;
2211 mNestedLoopDepth--;
2212
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002213 return false;
2214 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002215 }
2216
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002217 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002218
alokp@chromium.org52813552010-11-16 18:36:09 +00002219 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002220 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002221 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002222
Jamie Madill075edd82013-07-08 13:30:19 -04002223 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002224 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002225 }
2226 else
2227 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002228 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002229
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002230 if (node->getInit())
2231 {
2232 node->getInit()->traverse(this);
2233 }
2234
2235 out << "; ";
2236
alokp@chromium.org52813552010-11-16 18:36:09 +00002237 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002239 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240 }
2241
2242 out << "; ";
2243
alokp@chromium.org52813552010-11-16 18:36:09 +00002244 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002246 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002247 }
2248
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002249 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002250
Jamie Madill075edd82013-07-08 13:30:19 -04002251 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002252 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253 }
2254
2255 if (node->getBody())
2256 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002257 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002258 }
2259
Jamie Madill075edd82013-07-08 13:30:19 -04002260 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002261 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262
alokp@chromium.org52813552010-11-16 18:36:09 +00002263 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 {
Jamie Madill075edd82013-07-08 13:30:19 -04002265 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002266 out << "while(\n";
2267
alokp@chromium.org52813552010-11-16 18:36:09 +00002268 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269
daniel@transgaming.com73536982012-03-21 20:45:49 +00002270 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 }
2272
daniel@transgaming.com73536982012-03-21 20:45:49 +00002273 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002275 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002276 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002277
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278 return false;
2279}
2280
2281bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2282{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002283 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002284
2285 switch (node->getFlowOp())
2286 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002287 case EOpKill:
2288 outputTriplet(visit, "discard;\n", "", "");
2289 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002290 case EOpBreak:
2291 if (visit == PreVisit)
2292 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002293 if (mNestedLoopDepth > 1)
2294 {
2295 mUsesNestedBreak = true;
2296 }
2297
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002298 if (mExcessiveLoopIndex)
2299 {
2300 out << "{Break";
2301 mExcessiveLoopIndex->traverse(this);
2302 out << " = true; break;}\n";
2303 }
2304 else
2305 {
2306 out << "break;\n";
2307 }
2308 }
2309 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002310 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 case EOpReturn:
2312 if (visit == PreVisit)
2313 {
2314 if (node->getExpression())
2315 {
2316 out << "return ";
2317 }
2318 else
2319 {
2320 out << "return;\n";
2321 }
2322 }
2323 else if (visit == PostVisit)
2324 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002325 if (node->getExpression())
2326 {
2327 out << ";\n";
2328 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329 }
2330 break;
2331 default: UNREACHABLE();
2332 }
2333
2334 return true;
2335}
2336
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002337void OutputHLSL::traverseStatements(TIntermNode *node)
2338{
2339 if (isSingleStatement(node))
2340 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002341 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002342 }
2343
2344 node->traverse(this);
2345}
2346
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002347bool OutputHLSL::isSingleStatement(TIntermNode *node)
2348{
2349 TIntermAggregate *aggregate = node->getAsAggregate();
2350
2351 if (aggregate)
2352 {
2353 if (aggregate->getOp() == EOpSequence)
2354 {
2355 return false;
2356 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002357 else if (aggregate->getOp() == EOpDeclaration)
2358 {
2359 // Declaring multiple comma-separated variables must be considered multiple statements
2360 // because each individual declaration has side effects which are visible in the next.
2361 return false;
2362 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002363 else
2364 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002365 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002366 {
2367 if (!isSingleStatement(*sit))
2368 {
2369 return false;
2370 }
2371 }
2372
2373 return true;
2374 }
2375 }
2376
2377 return true;
2378}
2379
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002380// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2381// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002382bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2383{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002384 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002385 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002386
2387 // Parse loops of the form:
2388 // for(int index = initial; index [comparator] limit; index += increment)
2389 TIntermSymbol *index = NULL;
2390 TOperator comparator = EOpNull;
2391 int initial = 0;
2392 int limit = 0;
2393 int increment = 0;
2394
2395 // Parse index name and intial value
2396 if (node->getInit())
2397 {
2398 TIntermAggregate *init = node->getInit()->getAsAggregate();
2399
2400 if (init)
2401 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002402 TIntermSequence *sequence = init->getSequence();
2403 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002404
2405 if (variable && variable->getQualifier() == EvqTemporary)
2406 {
2407 TIntermBinary *assign = variable->getAsBinaryNode();
2408
2409 if (assign->getOp() == EOpInitialize)
2410 {
2411 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2412 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2413
2414 if (symbol && constant)
2415 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002416 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002417 {
2418 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002419 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002420 }
2421 }
2422 }
2423 }
2424 }
2425 }
2426
2427 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002428 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002429 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002430 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002431
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002432 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2433 {
2434 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2435
2436 if (constant)
2437 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002438 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002439 {
2440 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002441 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002442 }
2443 }
2444 }
2445 }
2446
2447 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002448 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002449 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002450 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2451 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002452
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002453 if (binaryTerminal)
2454 {
2455 TOperator op = binaryTerminal->getOp();
2456 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2457
2458 if (constant)
2459 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002460 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002461 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002462 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002463
2464 switch (op)
2465 {
2466 case EOpAddAssign: increment = value; break;
2467 case EOpSubAssign: increment = -value; break;
2468 default: UNIMPLEMENTED();
2469 }
2470 }
2471 }
2472 }
2473 else if (unaryTerminal)
2474 {
2475 TOperator op = unaryTerminal->getOp();
2476
2477 switch (op)
2478 {
2479 case EOpPostIncrement: increment = 1; break;
2480 case EOpPostDecrement: increment = -1; break;
2481 case EOpPreIncrement: increment = 1; break;
2482 case EOpPreDecrement: increment = -1; break;
2483 default: UNIMPLEMENTED();
2484 }
2485 }
2486 }
2487
2488 if (index != NULL && comparator != EOpNull && increment != 0)
2489 {
2490 if (comparator == EOpLessThanEqual)
2491 {
2492 comparator = EOpLessThan;
2493 limit += 1;
2494 }
2495
2496 if (comparator == EOpLessThan)
2497 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002498 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002499
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002500 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002501 {
2502 return false; // Not an excessive loop
2503 }
2504
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002505 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2506 mExcessiveLoopIndex = index;
2507
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002508 out << "{int ";
2509 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002510 out << ";\n"
2511 "bool Break";
2512 index->traverse(this);
2513 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002514
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002515 bool firstLoopFragment = true;
2516
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002517 while (iterations > 0)
2518 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002519 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002520
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002521 if (!firstLoopFragment)
2522 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002523 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002524 index->traverse(this);
2525 out << ") {\n";
2526 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002527
2528 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2529 {
2530 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2531 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002532
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002533 // for(int index = initial; index < clampedLimit; index += increment)
2534
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002535 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002536 index->traverse(this);
2537 out << " = ";
2538 out << initial;
2539
2540 out << "; ";
2541 index->traverse(this);
2542 out << " < ";
2543 out << clampedLimit;
2544
2545 out << "; ";
2546 index->traverse(this);
2547 out << " += ";
2548 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002549 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002550
Jamie Madill075edd82013-07-08 13:30:19 -04002551 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002552 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002553
2554 if (node->getBody())
2555 {
2556 node->getBody()->traverse(this);
2557 }
2558
Jamie Madill075edd82013-07-08 13:30:19 -04002559 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002560 out << ";}\n";
2561
2562 if (!firstLoopFragment)
2563 {
2564 out << "}\n";
2565 }
2566
2567 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002568
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002569 initial += MAX_LOOP_ITERATIONS * increment;
2570 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002571 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002572
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002573 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002574
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002575 mExcessiveLoopIndex = restoreIndex;
2576
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002577 return true;
2578 }
2579 else UNIMPLEMENTED();
2580 }
2581
2582 return false; // Not handled as an excessive loop
2583}
2584
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002585void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002586{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002587 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002588
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002589 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002590 {
2591 out << preString;
2592 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002593 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002594 {
2595 out << inString;
2596 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002597 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002598 {
2599 out << postString;
2600 }
2601}
2602
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002603void OutputHLSL::outputLineDirective(int line)
2604{
2605 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2606 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002607 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002608 mBody << "#line " << line;
2609
2610 if (mContext.sourcePath)
2611 {
2612 mBody << " \"" << mContext.sourcePath << "\"";
2613 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002614
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002615 mBody << "\n";
2616 }
2617}
2618
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002619TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2620{
2621 TQualifier qualifier = symbol->getQualifier();
2622 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002623 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002624
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002625 if (name.empty()) // HLSL demands named arguments, also for prototypes
2626 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002627 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002628 }
2629 else
2630 {
Jamie Madill033dae62014-06-18 12:56:28 -04002631 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002632 }
2633
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002634 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2635 {
Jamie Madill033dae62014-06-18 12:56:28 -04002636 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002637 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002638 }
2639
Jamie Madill033dae62014-06-18 12:56:28 -04002640 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641}
2642
2643TString OutputHLSL::initializer(const TType &type)
2644{
2645 TString string;
2646
Jamie Madill94bf7f22013-07-08 13:31:15 -04002647 size_t size = type.getObjectSize();
2648 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002649 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002650 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651
Jamie Madill94bf7f22013-07-08 13:31:15 -04002652 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002653 {
2654 string += ", ";
2655 }
2656 }
2657
daniel@transgaming.comead23042010-04-29 03:35:36 +00002658 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002659}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002660
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002661void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2662{
2663 TInfoSinkBase &out = mBody;
2664
2665 if (visit == PreVisit)
2666 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002667 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002668
2669 out << name + "(";
2670 }
2671 else if (visit == InVisit)
2672 {
2673 out << ", ";
2674 }
2675 else if (visit == PostVisit)
2676 {
2677 out << ")";
2678 }
2679}
2680
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002681const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2682{
2683 TInfoSinkBase &out = mBody;
2684
Jamie Madill98493dd2013-07-08 14:39:03 -04002685 const TStructure* structure = type.getStruct();
2686 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002687 {
Jamie Madill033dae62014-06-18 12:56:28 -04002688 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002689
Jamie Madill98493dd2013-07-08 14:39:03 -04002690 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002691
Jamie Madill98493dd2013-07-08 14:39:03 -04002692 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002693 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002694 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002695 constUnion = writeConstantUnion(*fieldType, constUnion);
2696
Jamie Madill98493dd2013-07-08 14:39:03 -04002697 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002698 {
2699 out << ", ";
2700 }
2701 }
2702
2703 out << ")";
2704 }
2705 else
2706 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002707 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002708 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002709
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002710 if (writeType)
2711 {
Jamie Madill033dae62014-06-18 12:56:28 -04002712 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002713 }
2714
Jamie Madill94bf7f22013-07-08 13:31:15 -04002715 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002716 {
2717 switch (constUnion->getType())
2718 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002719 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002720 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002721 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002722 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002723 default: UNREACHABLE();
2724 }
2725
2726 if (i != size - 1)
2727 {
2728 out << ", ";
2729 }
2730 }
2731
2732 if (writeType)
2733 {
2734 out << ")";
2735 }
2736 }
2737
2738 return constUnion;
2739}
2740
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002741void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2742{
2743 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2744 outputTriplet(visit, preString.c_str(), ", ", ")");
2745}
2746
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002747}