blob: de1d4af555a7174747823042c45462357f3ef789 [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 Etuaho80a5a6c2015-01-12 15:35:27 +02001206 builtInFunctionEmulator->OutputEmulatedFunctionDefinition(out);
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;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001399 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1400 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1401 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1402 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1403 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001404 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001405 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001406 const TType& leftType = node->getLeft()->getType();
1407 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001408 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001409 if (visit == PreVisit)
1410 {
1411 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1412 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001413 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001414 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001415 return false;
1416 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001417 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001418 else
1419 {
1420 outputTriplet(visit, "", "[", "]");
1421 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001422 }
1423 break;
1424 case EOpIndexIndirect:
1425 // We do not currently support indirect references to interface blocks
1426 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1427 outputTriplet(visit, "", "[", "]");
1428 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001429 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001430 if (visit == InVisit)
1431 {
1432 const TStructure* structure = node->getLeft()->getType().getStruct();
1433 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1434 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001435 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001436
1437 return false;
1438 }
1439 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001440 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001441 if (visit == InVisit)
1442 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001443 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1444 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1445 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001446 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001447
1448 return false;
1449 }
1450 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001451 case EOpVectorSwizzle:
1452 if (visit == InVisit)
1453 {
1454 out << ".";
1455
1456 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1457
1458 if (swizzle)
1459 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001460 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001461
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001462 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001463 {
1464 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1465
1466 if (element)
1467 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001468 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001469
1470 switch (i)
1471 {
1472 case 0: out << "x"; break;
1473 case 1: out << "y"; break;
1474 case 2: out << "z"; break;
1475 case 3: out << "w"; break;
1476 default: UNREACHABLE();
1477 }
1478 }
1479 else UNREACHABLE();
1480 }
1481 }
1482 else UNREACHABLE();
1483
1484 return false; // Fully processed
1485 }
1486 break;
1487 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1488 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1489 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1490 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001491 case EOpMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001492 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1493 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1494 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1495 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1496 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001497 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001498 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001499 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001500 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001501 if (node->getOp() == EOpEqual)
1502 {
1503 outputTriplet(visit, "(", " == ", ")");
1504 }
1505 else
1506 {
1507 outputTriplet(visit, "(", " != ", ")");
1508 }
1509 }
1510 else if (node->getLeft()->getBasicType() == EbtStruct)
1511 {
1512 if (node->getOp() == EOpEqual)
1513 {
1514 out << "(";
1515 }
1516 else
1517 {
1518 out << "!(";
1519 }
1520
Jamie Madill98493dd2013-07-08 14:39:03 -04001521 const TStructure &structure = *node->getLeft()->getType().getStruct();
1522 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001523
Jamie Madill98493dd2013-07-08 14:39:03 -04001524 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001525 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001526 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001527
1528 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001529 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001530 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001531 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001532
Jamie Madill98493dd2013-07-08 14:39:03 -04001533 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001534 {
1535 out << " && ";
1536 }
1537 }
1538
1539 out << ")";
1540
1541 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001542 }
1543 else
1544 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001545 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001546
1547 if (node->getOp() == EOpEqual)
1548 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001549 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001550 }
1551 else
1552 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001553 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001554 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001555 }
1556 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001557 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1558 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1559 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1560 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1561 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001562 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001563 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1564 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001565 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001566 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001567 if (node->getRight()->hasSideEffects())
1568 {
1569 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1570 return false;
1571 }
1572 else
1573 {
1574 outputTriplet(visit, "(", " || ", ")");
1575 return true;
1576 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001577 case EOpLogicalXor:
1578 mUsesXor = true;
1579 outputTriplet(visit, "xor(", ", ", ")");
1580 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001581 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001582 if (node->getRight()->hasSideEffects())
1583 {
1584 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1585 return false;
1586 }
1587 else
1588 {
1589 outputTriplet(visit, "(", " && ", ")");
1590 return true;
1591 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592 default: UNREACHABLE();
1593 }
1594
1595 return true;
1596}
1597
1598bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1599{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001600 switch (node->getOp())
1601 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001602 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001603 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001604 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1605 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001606 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001607 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1608 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1609 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1610 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001611 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1612 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1613 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1614 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1615 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1616 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1617 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1618 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001619 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1620 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1621 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1622 case EOpAsinh:
1623 ASSERT(node->getUseEmulatedFunction());
1624 writeEmulatedFunctionTriplet(visit, "asinh(");
1625 break;
1626 case EOpAcosh:
1627 ASSERT(node->getUseEmulatedFunction());
1628 writeEmulatedFunctionTriplet(visit, "acosh(");
1629 break;
1630 case EOpAtanh:
1631 ASSERT(node->getUseEmulatedFunction());
1632 writeEmulatedFunctionTriplet(visit, "atanh(");
1633 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001634 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1635 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1636 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1637 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1638 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1639 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1640 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1641 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1642 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1643 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1644 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001645 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1646 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1647 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1648 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001649 case EOpPackSnorm2x16:
1650 ASSERT(node->getUseEmulatedFunction());
1651 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1652 break;
1653 case EOpPackUnorm2x16:
1654 ASSERT(node->getUseEmulatedFunction());
1655 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1656 break;
1657 case EOpPackHalf2x16:
1658 ASSERT(node->getUseEmulatedFunction());
1659 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1660 break;
1661 case EOpUnpackSnorm2x16:
1662 ASSERT(node->getUseEmulatedFunction());
1663 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1664 break;
1665 case EOpUnpackUnorm2x16:
1666 ASSERT(node->getUseEmulatedFunction());
1667 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1668 break;
1669 case EOpUnpackHalf2x16:
1670 ASSERT(node->getUseEmulatedFunction());
1671 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1672 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001673 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1674 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001675 case EOpDFdx:
1676 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1677 {
1678 outputTriplet(visit, "(", "", ", 0.0)");
1679 }
1680 else
1681 {
1682 outputTriplet(visit, "ddx(", "", ")");
1683 }
1684 break;
1685 case EOpDFdy:
1686 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1687 {
1688 outputTriplet(visit, "(", "", ", 0.0)");
1689 }
1690 else
1691 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001692 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001693 }
1694 break;
1695 case EOpFwidth:
1696 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1697 {
1698 outputTriplet(visit, "(", "", ", 0.0)");
1699 }
1700 else
1701 {
1702 outputTriplet(visit, "fwidth(", "", ")");
1703 }
1704 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001705 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1706 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001707 case EOpInverse:
1708 ASSERT(node->getUseEmulatedFunction());
1709 writeEmulatedFunctionTriplet(visit, "inverse(");
1710 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001711
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001712 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1713 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001714 default: UNREACHABLE();
1715 }
1716
1717 return true;
1718}
1719
1720bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1721{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001722 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724 switch (node->getOp())
1725 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001726 case EOpSequence:
1727 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001728 if (mInsideFunction)
1729 {
Jamie Madill075edd82013-07-08 13:30:19 -04001730 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001731 out << "{\n";
1732 }
1733
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001734 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001735 {
Jamie Madill075edd82013-07-08 13:30:19 -04001736 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001737
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001738 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001739
1740 out << ";\n";
1741 }
1742
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001743 if (mInsideFunction)
1744 {
Jamie Madill075edd82013-07-08 13:30:19 -04001745 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001746 out << "}\n";
1747 }
1748
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001749 return false;
1750 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 case EOpDeclaration:
1752 if (visit == PreVisit)
1753 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001754 TIntermSequence *sequence = node->getSequence();
1755 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001757 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001759 TStructure *structure = variable->getType().getStruct();
1760
1761 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001762 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001763 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001764 }
1765
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001766 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001768 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769 {
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001770 if (isSingleStatement(*sit))
1771 {
1772 mUnfoldShortCircuit->traverse(*sit);
1773 }
1774
Nicolas Capensd974db42014-10-07 10:50:19 -04001775 if (!mInsideFunction)
1776 {
1777 out << "static ";
1778 }
1779
1780 out << TypeString(variable->getType()) + " ";
1781
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001782 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001784 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001786 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001787 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001788 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001789 }
1790 else
1791 {
1792 (*sit)->traverse(this);
1793 }
1794
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001795 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001796 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001797 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798 }
1799 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001801 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1802 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001803 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001804 }
1805 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806 }
Jamie Madill033dae62014-06-18 12:56:28 -04001807 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001808 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001809 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001810 {
1811 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1812
1813 if (symbol)
1814 {
1815 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1816 mReferencedVaryings[symbol->getSymbol()] = symbol;
1817 }
1818 else
1819 {
1820 (*sit)->traverse(this);
1821 }
1822 }
1823 }
1824
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825 return false;
1826 }
1827 else if (visit == InVisit)
1828 {
1829 out << ", ";
1830 }
1831 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001832 case EOpInvariantDeclaration:
1833 // Do not do any translation
1834 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001835 case EOpPrototype:
1836 if (visit == PreVisit)
1837 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001838 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001839
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001840 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001841
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001842 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001843 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001844 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001845
1846 if (symbol)
1847 {
1848 out << argumentString(symbol);
1849
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001850 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001851 {
1852 out << ", ";
1853 }
1854 }
1855 else UNREACHABLE();
1856 }
1857
1858 out << ");\n";
1859
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001860 // Also prototype the Lod0 variant if needed
1861 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1862 {
1863 mOutputLod0Function = true;
1864 node->traverse(this);
1865 mOutputLod0Function = false;
1866 }
1867
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001868 return false;
1869 }
1870 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001871 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001872 case EOpFunction:
1873 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001874 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875
Jamie Madill033dae62014-06-18 12:56:28 -04001876 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001877
1878 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001880 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001882 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883 {
Jamie Madill033dae62014-06-18 12:56:28 -04001884 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001885 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001886
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001887 TIntermSequence *sequence = node->getSequence();
1888 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001889
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001890 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001891 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001892 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001893
1894 if (symbol)
1895 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001896 TStructure *structure = symbol->getType().getStruct();
1897
1898 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001899 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001900 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001901 }
1902
1903 out << argumentString(symbol);
1904
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001905 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001906 {
1907 out << ", ";
1908 }
1909 }
1910 else UNREACHABLE();
1911 }
1912
1913 out << ")\n"
1914 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001915
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001916 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001917 {
1918 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001919 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001920 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001922
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001923 out << "}\n";
1924
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001925 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1926 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001927 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001928 {
1929 mOutputLod0Function = true;
1930 node->traverse(this);
1931 mOutputLod0Function = false;
1932 }
1933 }
1934
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001935 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001936 }
1937 break;
1938 case EOpFunctionCall:
1939 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001940 TString name = TFunction::unmangleName(node->getName());
1941 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001942 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001943
1944 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945 {
Jamie Madill033dae62014-06-18 12:56:28 -04001946 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947 }
1948 else
1949 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001950 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001951
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001952 TextureFunction textureFunction;
1953 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001954 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04001955 textureFunction.method = TextureFunction::IMPLICIT;
1956 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001957 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001958
1959 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001960 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001961 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001962 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001963 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001964 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001965 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001966 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001967 }
Nicolas Capens46485082014-04-15 13:12:50 -04001968 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
1969 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001970 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001971 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001972 }
Nicolas Capens46485082014-04-15 13:12:50 -04001973 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001974 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001975 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001976 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001977 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04001978 else if (name == "textureSize")
1979 {
1980 textureFunction.method = TextureFunction::SIZE;
1981 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001982 else if (name == "textureOffset")
1983 {
1984 textureFunction.method = TextureFunction::IMPLICIT;
1985 textureFunction.offset = true;
1986 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05001987 else if (name == "textureProjOffset")
1988 {
1989 textureFunction.method = TextureFunction::IMPLICIT;
1990 textureFunction.offset = true;
1991 textureFunction.proj = true;
1992 }
1993 else if (name == "textureLodOffset")
1994 {
1995 textureFunction.method = TextureFunction::LOD;
1996 textureFunction.offset = true;
1997 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001998 else if (name == "textureProjLodOffset")
1999 {
2000 textureFunction.method = TextureFunction::LOD;
2001 textureFunction.proj = true;
2002 textureFunction.offset = true;
2003 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002004 else if (name == "texelFetch")
2005 {
2006 textureFunction.method = TextureFunction::FETCH;
2007 }
2008 else if (name == "texelFetchOffset")
2009 {
2010 textureFunction.method = TextureFunction::FETCH;
2011 textureFunction.offset = true;
2012 }
Nicolas Capens46485082014-04-15 13:12:50 -04002013 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002014 {
2015 textureFunction.method = TextureFunction::GRAD;
2016 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002017 else if (name == "textureGradOffset")
2018 {
2019 textureFunction.method = TextureFunction::GRAD;
2020 textureFunction.offset = true;
2021 }
Nicolas Capens46485082014-04-15 13:12:50 -04002022 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002023 {
2024 textureFunction.method = TextureFunction::GRAD;
2025 textureFunction.proj = true;
2026 }
2027 else if (name == "textureProjGradOffset")
2028 {
2029 textureFunction.method = TextureFunction::GRAD;
2030 textureFunction.proj = true;
2031 textureFunction.offset = true;
2032 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002033 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002034
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002035 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002036 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002037 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2038
2039 if (textureFunction.offset)
2040 {
2041 mandatoryArgumentCount++;
2042 }
2043
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002044 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002045
Jamie Madill183bde52014-07-02 15:31:19 -04002046 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002047 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002048 if (bias)
2049 {
2050 textureFunction.method = TextureFunction::LOD0BIAS;
2051 }
2052 else
2053 {
2054 textureFunction.method = TextureFunction::LOD0;
2055 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002056 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002057 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002058 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002059 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002060 }
2061 }
2062
2063 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002064
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002065 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002067
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002068 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002069 {
2070 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2071 {
2072 out << "texture_";
2073 (*arg)->traverse(this);
2074 out << ", sampler_";
2075 }
2076
2077 (*arg)->traverse(this);
2078
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002079 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002080 {
2081 out << ", ";
2082 }
2083 }
2084
2085 out << ")";
2086
2087 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 }
2089 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002090 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002091 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2092 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2093 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2094 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2095 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2096 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2097 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2098 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2099 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2100 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2101 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2102 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2103 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2104 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2105 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2106 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2107 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2108 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2109 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002110 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002111 {
Jamie Madill033dae62014-06-18 12:56:28 -04002112 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002113 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002114 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2115 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002116 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002117 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2118 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2119 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2120 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2121 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2122 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002123 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002124 ASSERT(node->getUseEmulatedFunction());
2125 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002126 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002127 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002129 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002130 ASSERT(node->getUseEmulatedFunction());
2131 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132 break;
2133 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2134 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2135 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2136 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2137 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2138 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2139 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2140 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2141 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002142 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002143 ASSERT(node->getUseEmulatedFunction());
2144 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002145 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2147 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002148 case EOpOuterProduct:
2149 ASSERT(node->getUseEmulatedFunction());
2150 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2151 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002153 default: UNREACHABLE();
2154 }
2155
2156 return true;
2157}
2158
2159bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2160{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002161 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002163 if (node->usesTernaryOperator())
2164 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002165 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002166 }
2167 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002169 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002170
Corentin Wallez80bacde2014-11-10 12:07:37 -08002171 // D3D errors when there is a gradient operation in a loop in an unflattened if
2172 // however flattening all the ifs in branch heavy shaders made D3D error too.
2173 // As a temporary workaround we flatten the ifs only if there is at least a loop
2174 // present somewhere in the shader.
2175 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2176 {
2177 out << "FLATTEN ";
2178 }
2179
2180 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002181
2182 node->getCondition()->traverse(this);
2183
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002184 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002185
Jamie Madill075edd82013-07-08 13:30:19 -04002186 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002187 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002189 bool discard = false;
2190
daniel@transgaming.combb885322010-04-15 20:45:24 +00002191 if (node->getTrueBlock())
2192 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002193 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002194
2195 // Detect true discard
2196 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002197 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002198
Jamie Madill075edd82013-07-08 13:30:19 -04002199 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002200 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002201
2202 if (node->getFalseBlock())
2203 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002204 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002205
Jamie Madill075edd82013-07-08 13:30:19 -04002206 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002207 out << "{\n";
2208
Jamie Madill075edd82013-07-08 13:30:19 -04002209 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002210 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002211
Jamie Madill075edd82013-07-08 13:30:19 -04002212 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002213 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002214
2215 // Detect false discard
2216 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2217 }
2218
2219 // ANGLE issue 486: Detect problematic conditional discard
2220 if (discard && FindSideEffectRewriting::search(node))
2221 {
2222 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002223 }
2224 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002225
2226 return false;
2227}
2228
2229void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2230{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002231 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002232}
2233
2234bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2235{
Nicolas Capens655fe362014-04-11 13:12:34 -04002236 mNestedLoopDepth++;
2237
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002238 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2239
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002240 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002241 {
2242 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2243 }
2244
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002245 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002246 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002247 if (handleExcessiveLoop(node))
2248 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002249 mInsideDiscontinuousLoop = wasDiscontinuous;
2250 mNestedLoopDepth--;
2251
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002252 return false;
2253 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002254 }
2255
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002256 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257
alokp@chromium.org52813552010-11-16 18:36:09 +00002258 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002260 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002261
Jamie Madill075edd82013-07-08 13:30:19 -04002262 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002263 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 }
2265 else
2266 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002267 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002268
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 if (node->getInit())
2270 {
2271 node->getInit()->traverse(this);
2272 }
2273
2274 out << "; ";
2275
alokp@chromium.org52813552010-11-16 18:36:09 +00002276 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002277 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002278 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 }
2280
2281 out << "; ";
2282
alokp@chromium.org52813552010-11-16 18:36:09 +00002283 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002284 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002285 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286 }
2287
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002288 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002289
Jamie Madill075edd82013-07-08 13:30:19 -04002290 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002291 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 }
2293
2294 if (node->getBody())
2295 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002296 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297 }
2298
Jamie Madill075edd82013-07-08 13:30:19 -04002299 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002300 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
alokp@chromium.org52813552010-11-16 18:36:09 +00002302 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 {
Jamie Madill075edd82013-07-08 13:30:19 -04002304 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 out << "while(\n";
2306
alokp@chromium.org52813552010-11-16 18:36:09 +00002307 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002308
daniel@transgaming.com73536982012-03-21 20:45:49 +00002309 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 }
2311
daniel@transgaming.com73536982012-03-21 20:45:49 +00002312 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002314 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002315 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002316
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317 return false;
2318}
2319
2320bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2321{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002322 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323
2324 switch (node->getFlowOp())
2325 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002326 case EOpKill:
2327 outputTriplet(visit, "discard;\n", "", "");
2328 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002329 case EOpBreak:
2330 if (visit == PreVisit)
2331 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002332 if (mNestedLoopDepth > 1)
2333 {
2334 mUsesNestedBreak = true;
2335 }
2336
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002337 if (mExcessiveLoopIndex)
2338 {
2339 out << "{Break";
2340 mExcessiveLoopIndex->traverse(this);
2341 out << " = true; break;}\n";
2342 }
2343 else
2344 {
2345 out << "break;\n";
2346 }
2347 }
2348 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002349 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350 case EOpReturn:
2351 if (visit == PreVisit)
2352 {
2353 if (node->getExpression())
2354 {
2355 out << "return ";
2356 }
2357 else
2358 {
2359 out << "return;\n";
2360 }
2361 }
2362 else if (visit == PostVisit)
2363 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002364 if (node->getExpression())
2365 {
2366 out << ";\n";
2367 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368 }
2369 break;
2370 default: UNREACHABLE();
2371 }
2372
2373 return true;
2374}
2375
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002376void OutputHLSL::traverseStatements(TIntermNode *node)
2377{
2378 if (isSingleStatement(node))
2379 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002380 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002381 }
2382
2383 node->traverse(this);
2384}
2385
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002386bool OutputHLSL::isSingleStatement(TIntermNode *node)
2387{
2388 TIntermAggregate *aggregate = node->getAsAggregate();
2389
2390 if (aggregate)
2391 {
2392 if (aggregate->getOp() == EOpSequence)
2393 {
2394 return false;
2395 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002396 else if (aggregate->getOp() == EOpDeclaration)
2397 {
2398 // Declaring multiple comma-separated variables must be considered multiple statements
2399 // because each individual declaration has side effects which are visible in the next.
2400 return false;
2401 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002402 else
2403 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002404 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002405 {
2406 if (!isSingleStatement(*sit))
2407 {
2408 return false;
2409 }
2410 }
2411
2412 return true;
2413 }
2414 }
2415
2416 return true;
2417}
2418
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002419// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2420// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002421bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2422{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002423 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002424 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002425
2426 // Parse loops of the form:
2427 // for(int index = initial; index [comparator] limit; index += increment)
2428 TIntermSymbol *index = NULL;
2429 TOperator comparator = EOpNull;
2430 int initial = 0;
2431 int limit = 0;
2432 int increment = 0;
2433
2434 // Parse index name and intial value
2435 if (node->getInit())
2436 {
2437 TIntermAggregate *init = node->getInit()->getAsAggregate();
2438
2439 if (init)
2440 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002441 TIntermSequence *sequence = init->getSequence();
2442 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002443
2444 if (variable && variable->getQualifier() == EvqTemporary)
2445 {
2446 TIntermBinary *assign = variable->getAsBinaryNode();
2447
2448 if (assign->getOp() == EOpInitialize)
2449 {
2450 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2451 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2452
2453 if (symbol && constant)
2454 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002455 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002456 {
2457 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002458 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002459 }
2460 }
2461 }
2462 }
2463 }
2464 }
2465
2466 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002467 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002468 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002469 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002470
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002471 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2472 {
2473 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2474
2475 if (constant)
2476 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002477 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002478 {
2479 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002480 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002481 }
2482 }
2483 }
2484 }
2485
2486 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002487 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002488 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002489 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2490 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002491
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002492 if (binaryTerminal)
2493 {
2494 TOperator op = binaryTerminal->getOp();
2495 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2496
2497 if (constant)
2498 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002499 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002500 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002501 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002502
2503 switch (op)
2504 {
2505 case EOpAddAssign: increment = value; break;
2506 case EOpSubAssign: increment = -value; break;
2507 default: UNIMPLEMENTED();
2508 }
2509 }
2510 }
2511 }
2512 else if (unaryTerminal)
2513 {
2514 TOperator op = unaryTerminal->getOp();
2515
2516 switch (op)
2517 {
2518 case EOpPostIncrement: increment = 1; break;
2519 case EOpPostDecrement: increment = -1; break;
2520 case EOpPreIncrement: increment = 1; break;
2521 case EOpPreDecrement: increment = -1; break;
2522 default: UNIMPLEMENTED();
2523 }
2524 }
2525 }
2526
2527 if (index != NULL && comparator != EOpNull && increment != 0)
2528 {
2529 if (comparator == EOpLessThanEqual)
2530 {
2531 comparator = EOpLessThan;
2532 limit += 1;
2533 }
2534
2535 if (comparator == EOpLessThan)
2536 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002537 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002538
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002539 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002540 {
2541 return false; // Not an excessive loop
2542 }
2543
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002544 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2545 mExcessiveLoopIndex = index;
2546
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002547 out << "{int ";
2548 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002549 out << ";\n"
2550 "bool Break";
2551 index->traverse(this);
2552 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002553
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002554 bool firstLoopFragment = true;
2555
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002556 while (iterations > 0)
2557 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002558 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002559
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002560 if (!firstLoopFragment)
2561 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002562 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002563 index->traverse(this);
2564 out << ") {\n";
2565 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002566
2567 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2568 {
2569 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2570 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002571
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002572 // for(int index = initial; index < clampedLimit; index += increment)
2573
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002574 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002575 index->traverse(this);
2576 out << " = ";
2577 out << initial;
2578
2579 out << "; ";
2580 index->traverse(this);
2581 out << " < ";
2582 out << clampedLimit;
2583
2584 out << "; ";
2585 index->traverse(this);
2586 out << " += ";
2587 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002588 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002589
Jamie Madill075edd82013-07-08 13:30:19 -04002590 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002591 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002592
2593 if (node->getBody())
2594 {
2595 node->getBody()->traverse(this);
2596 }
2597
Jamie Madill075edd82013-07-08 13:30:19 -04002598 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002599 out << ";}\n";
2600
2601 if (!firstLoopFragment)
2602 {
2603 out << "}\n";
2604 }
2605
2606 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002607
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002608 initial += MAX_LOOP_ITERATIONS * increment;
2609 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002610 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002611
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002612 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002613
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002614 mExcessiveLoopIndex = restoreIndex;
2615
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002616 return true;
2617 }
2618 else UNIMPLEMENTED();
2619 }
2620
2621 return false; // Not handled as an excessive loop
2622}
2623
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002624void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002625{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002626 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002627
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002628 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 {
2630 out << preString;
2631 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002632 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002633 {
2634 out << inString;
2635 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002636 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002637 {
2638 out << postString;
2639 }
2640}
2641
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002642void OutputHLSL::outputLineDirective(int line)
2643{
2644 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2645 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002646 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002647 mBody << "#line " << line;
2648
2649 if (mContext.sourcePath)
2650 {
2651 mBody << " \"" << mContext.sourcePath << "\"";
2652 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002653
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002654 mBody << "\n";
2655 }
2656}
2657
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002658TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2659{
2660 TQualifier qualifier = symbol->getQualifier();
2661 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002662 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002663
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002664 if (name.empty()) // HLSL demands named arguments, also for prototypes
2665 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002666 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002667 }
2668 else
2669 {
Jamie Madill033dae62014-06-18 12:56:28 -04002670 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002671 }
2672
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002673 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2674 {
Jamie Madill033dae62014-06-18 12:56:28 -04002675 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002676 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002677 }
2678
Jamie Madill033dae62014-06-18 12:56:28 -04002679 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002680}
2681
2682TString OutputHLSL::initializer(const TType &type)
2683{
2684 TString string;
2685
Jamie Madill94bf7f22013-07-08 13:31:15 -04002686 size_t size = type.getObjectSize();
2687 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002689 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690
Jamie Madill94bf7f22013-07-08 13:31:15 -04002691 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692 {
2693 string += ", ";
2694 }
2695 }
2696
daniel@transgaming.comead23042010-04-29 03:35:36 +00002697 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002698}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002699
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002700void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2701{
2702 TInfoSinkBase &out = mBody;
2703
2704 if (visit == PreVisit)
2705 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002706 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002707
2708 out << name + "(";
2709 }
2710 else if (visit == InVisit)
2711 {
2712 out << ", ";
2713 }
2714 else if (visit == PostVisit)
2715 {
2716 out << ")";
2717 }
2718}
2719
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002720const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2721{
2722 TInfoSinkBase &out = mBody;
2723
Jamie Madill98493dd2013-07-08 14:39:03 -04002724 const TStructure* structure = type.getStruct();
2725 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002726 {
Jamie Madill033dae62014-06-18 12:56:28 -04002727 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002728
Jamie Madill98493dd2013-07-08 14:39:03 -04002729 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002730
Jamie Madill98493dd2013-07-08 14:39:03 -04002731 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002732 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002733 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002734 constUnion = writeConstantUnion(*fieldType, constUnion);
2735
Jamie Madill98493dd2013-07-08 14:39:03 -04002736 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002737 {
2738 out << ", ";
2739 }
2740 }
2741
2742 out << ")";
2743 }
2744 else
2745 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002746 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002747 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002748
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002749 if (writeType)
2750 {
Jamie Madill033dae62014-06-18 12:56:28 -04002751 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002752 }
2753
Jamie Madill94bf7f22013-07-08 13:31:15 -04002754 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002755 {
2756 switch (constUnion->getType())
2757 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002758 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002759 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002760 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002761 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002762 default: UNREACHABLE();
2763 }
2764
2765 if (i != size - 1)
2766 {
2767 out << ", ";
2768 }
2769 }
2770
2771 if (writeType)
2772 {
2773 out << ")";
2774 }
2775 }
2776
2777 return constUnion;
2778}
2779
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002780void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2781{
2782 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2783 outputTriplet(visit, preString.c_str(), ", ", ")");
2784}
2785
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786}