blob: cb2c9f7ba6abd7d38199f4ef00b233720e3a6de2 [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 {
Cooper Partine6664f02015-01-09 16:22:24 -0800143 // Reserve registers for dx_DepthRange, dx_ViewAdjust and dx_ViewCoords
144 mUniformHLSL->reserveUniformRegisters(3);
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
Cooper Partine6664f02015-01-09 16:22:24 -0800513 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800514 // 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";
Cooper Partine6664f02015-01-09 16:22:24 -0800517 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800518
519 out << "};\n"
520 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000521 }
522 else
523 {
524 if (mUsesDepthRange)
525 {
526 out << "uniform float3 dx_DepthRange : register(c0);\n";
527 }
528
Cooper Partine6664f02015-01-09 16:22:24 -0800529 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
530 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000531 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000532 }
533
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000534 if (mUsesDepthRange)
535 {
536 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
537 "\n";
538 }
539
Jamie Madillf91ce812014-06-13 10:04:34 -0400540 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000541 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400542 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000543 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400544 out << flaggedStructs;
545 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000546 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400547 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000548
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400549 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
550 {
551 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400552 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000553 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400554 switch(textureFunction->sampler)
555 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400556 case EbtSampler2D: out << "int2 "; break;
557 case EbtSampler3D: out << "int3 "; break;
558 case EbtSamplerCube: out << "int2 "; break;
559 case EbtSampler2DArray: out << "int3 "; break;
560 case EbtISampler2D: out << "int2 "; break;
561 case EbtISampler3D: out << "int3 "; break;
562 case EbtISamplerCube: out << "int2 "; break;
563 case EbtISampler2DArray: out << "int3 "; break;
564 case EbtUSampler2D: out << "int2 "; break;
565 case EbtUSampler3D: out << "int3 "; break;
566 case EbtUSamplerCube: out << "int2 "; break;
567 case EbtUSampler2DArray: out << "int3 "; break;
568 case EbtSampler2DShadow: out << "int2 "; break;
569 case EbtSamplerCubeShadow: out << "int2 "; break;
570 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400571 default: UNREACHABLE();
572 }
573 }
574 else // Sampling function
575 {
576 switch(textureFunction->sampler)
577 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400578 case EbtSampler2D: out << "float4 "; break;
579 case EbtSampler3D: out << "float4 "; break;
580 case EbtSamplerCube: out << "float4 "; break;
581 case EbtSampler2DArray: out << "float4 "; break;
582 case EbtISampler2D: out << "int4 "; break;
583 case EbtISampler3D: out << "int4 "; break;
584 case EbtISamplerCube: out << "int4 "; break;
585 case EbtISampler2DArray: out << "int4 "; break;
586 case EbtUSampler2D: out << "uint4 "; break;
587 case EbtUSampler3D: out << "uint4 "; break;
588 case EbtUSamplerCube: out << "uint4 "; break;
589 case EbtUSampler2DArray: out << "uint4 "; break;
590 case EbtSampler2DShadow: out << "float "; break;
591 case EbtSamplerCubeShadow: out << "float "; break;
592 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400593 default: UNREACHABLE();
594 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000595 }
596
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400597 // Function name
598 out << textureFunction->name();
599
600 // Argument list
601 int hlslCoords = 4;
602
603 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000604 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400605 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000606 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400607 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
608 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
609 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000610 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400611
Nicolas Capens75fb4752013-07-10 15:14:47 -0400612 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000613 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400614 case TextureFunction::IMPLICIT: break;
615 case TextureFunction::BIAS: hlslCoords = 4; break;
616 case TextureFunction::LOD: hlslCoords = 4; break;
617 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400618 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400619 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000620 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400621 }
622 else if (mOutputType == SH_HLSL11_OUTPUT)
623 {
624 switch(textureFunction->sampler)
625 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400626 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
627 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
628 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
629 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
630 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
631 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500632 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400633 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
634 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
635 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500636 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400637 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
638 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
639 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
640 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400641 default: UNREACHABLE();
642 }
643 }
644 else UNREACHABLE();
645
Nicolas Capensfc014542014-02-18 14:47:13 -0500646 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400647 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500648 switch(textureFunction->coords)
649 {
650 case 2: out << ", int2 t"; break;
651 case 3: out << ", int3 t"; break;
652 default: UNREACHABLE();
653 }
654 }
655 else // Floating-point coordinates (except textureSize)
656 {
657 switch(textureFunction->coords)
658 {
659 case 1: out << ", int lod"; break; // textureSize()
660 case 2: out << ", float2 t"; break;
661 case 3: out << ", float3 t"; break;
662 case 4: out << ", float4 t"; break;
663 default: UNREACHABLE();
664 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000665 }
666
Nicolas Capensd11d5492014-02-19 17:06:10 -0500667 if (textureFunction->method == TextureFunction::GRAD)
668 {
669 switch(textureFunction->sampler)
670 {
671 case EbtSampler2D:
672 case EbtISampler2D:
673 case EbtUSampler2D:
674 case EbtSampler2DArray:
675 case EbtISampler2DArray:
676 case EbtUSampler2DArray:
677 case EbtSampler2DShadow:
678 case EbtSampler2DArrayShadow:
679 out << ", float2 ddx, float2 ddy";
680 break;
681 case EbtSampler3D:
682 case EbtISampler3D:
683 case EbtUSampler3D:
684 case EbtSamplerCube:
685 case EbtISamplerCube:
686 case EbtUSamplerCube:
687 case EbtSamplerCubeShadow:
688 out << ", float3 ddx, float3 ddy";
689 break;
690 default: UNREACHABLE();
691 }
692 }
693
Nicolas Capens75fb4752013-07-10 15:14:47 -0400694 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000695 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400696 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400697 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400698 case TextureFunction::LOD: out << ", float lod"; break;
699 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400700 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400701 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500702 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500703 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400704 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000705 }
706
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500707 if (textureFunction->offset)
708 {
709 switch(textureFunction->sampler)
710 {
711 case EbtSampler2D: out << ", int2 offset"; break;
712 case EbtSampler3D: out << ", int3 offset"; break;
713 case EbtSampler2DArray: out << ", int2 offset"; break;
714 case EbtISampler2D: out << ", int2 offset"; break;
715 case EbtISampler3D: out << ", int3 offset"; break;
716 case EbtISampler2DArray: out << ", int2 offset"; break;
717 case EbtUSampler2D: out << ", int2 offset"; break;
718 case EbtUSampler3D: out << ", int3 offset"; break;
719 case EbtUSampler2DArray: out << ", int2 offset"; break;
720 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500721 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500722 default: UNREACHABLE();
723 }
724 }
725
Nicolas Capens84cfa122014-04-14 13:48:45 -0400726 if (textureFunction->method == TextureFunction::BIAS ||
727 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500728 {
729 out << ", float bias";
730 }
731
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400732 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400733 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400734
Nicolas Capens75fb4752013-07-10 15:14:47 -0400735 if (textureFunction->method == TextureFunction::SIZE)
736 {
737 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
738 {
739 if (IsSamplerArray(textureFunction->sampler))
740 {
741 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
742 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
743 }
744 else
745 {
746 out << " uint width; uint height; uint numberOfLevels;\n"
747 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
748 }
749 }
750 else if (IsSampler3D(textureFunction->sampler))
751 {
752 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
753 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
754 }
755 else UNREACHABLE();
756
757 switch(textureFunction->sampler)
758 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400759 case EbtSampler2D: out << " return int2(width, height);"; break;
760 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
761 case EbtSamplerCube: out << " return int2(width, height);"; break;
762 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
763 case EbtISampler2D: out << " return int2(width, height);"; break;
764 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
765 case EbtISamplerCube: out << " return int2(width, height);"; break;
766 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
767 case EbtUSampler2D: out << " return int2(width, height);"; break;
768 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
769 case EbtUSamplerCube: out << " return int2(width, height);"; break;
770 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
771 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
772 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
773 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400774 default: UNREACHABLE();
775 }
776 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400777 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400778 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500779 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
780 {
781 out << " float width; float height; float layers; float levels;\n";
782
783 out << " uint mip = 0;\n";
784
785 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
786
787 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
788 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
789 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
790 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
791
792 // FACE_POSITIVE_X = 000b
793 // FACE_NEGATIVE_X = 001b
794 // FACE_POSITIVE_Y = 010b
795 // FACE_NEGATIVE_Y = 011b
796 // FACE_POSITIVE_Z = 100b
797 // FACE_NEGATIVE_Z = 101b
798 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
799
800 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
801 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
802 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
803
804 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
805 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
806 }
807 else if (IsIntegerSampler(textureFunction->sampler) &&
808 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400809 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400810 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400811 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400812 if (IsSamplerArray(textureFunction->sampler))
813 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400814 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400815
Nicolas Capens9edebd62013-08-06 10:59:10 -0400816 if (textureFunction->method == TextureFunction::LOD0)
817 {
818 out << " uint mip = 0;\n";
819 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400820 else if (textureFunction->method == TextureFunction::LOD0BIAS)
821 {
822 out << " uint mip = bias;\n";
823 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400824 else
825 {
826 if (textureFunction->method == TextureFunction::IMPLICIT ||
827 textureFunction->method == TextureFunction::BIAS)
828 {
829 out << " x.GetDimensions(0, width, height, layers, levels);\n"
830 " float2 tSized = float2(t.x * width, t.y * height);\n"
831 " float dx = length(ddx(tSized));\n"
832 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500833 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400834
835 if (textureFunction->method == TextureFunction::BIAS)
836 {
837 out << " lod += bias;\n";
838 }
839 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500840 else if (textureFunction->method == TextureFunction::GRAD)
841 {
842 out << " x.GetDimensions(0, width, height, layers, levels);\n"
843 " float lod = log2(max(length(ddx), length(ddy)));\n";
844 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400845
846 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
847 }
848
849 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400850 }
851 else
852 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400853 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400854
Nicolas Capens9edebd62013-08-06 10:59:10 -0400855 if (textureFunction->method == TextureFunction::LOD0)
856 {
857 out << " uint mip = 0;\n";
858 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400859 else if (textureFunction->method == TextureFunction::LOD0BIAS)
860 {
861 out << " uint mip = bias;\n";
862 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400863 else
864 {
865 if (textureFunction->method == TextureFunction::IMPLICIT ||
866 textureFunction->method == TextureFunction::BIAS)
867 {
868 out << " x.GetDimensions(0, width, height, levels);\n"
869 " float2 tSized = float2(t.x * width, t.y * height);\n"
870 " float dx = length(ddx(tSized));\n"
871 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500872 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400873
874 if (textureFunction->method == TextureFunction::BIAS)
875 {
876 out << " lod += bias;\n";
877 }
878 }
Nicolas Capens2adc2562014-02-14 23:50:59 -0500879 else if (textureFunction->method == TextureFunction::LOD)
880 {
881 out << " x.GetDimensions(0, width, height, levels);\n";
882 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500883 else if (textureFunction->method == TextureFunction::GRAD)
884 {
885 out << " x.GetDimensions(0, width, height, levels);\n"
886 " float lod = log2(max(length(ddx), length(ddy)));\n";
887 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400888
889 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
890 }
891
892 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400893 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400894 }
895 else if (IsSampler3D(textureFunction->sampler))
896 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400897 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400898
Nicolas Capens9edebd62013-08-06 10:59:10 -0400899 if (textureFunction->method == TextureFunction::LOD0)
900 {
901 out << " uint mip = 0;\n";
902 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400903 else if (textureFunction->method == TextureFunction::LOD0BIAS)
904 {
905 out << " uint mip = bias;\n";
906 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400907 else
908 {
909 if (textureFunction->method == TextureFunction::IMPLICIT ||
910 textureFunction->method == TextureFunction::BIAS)
911 {
912 out << " x.GetDimensions(0, width, height, depth, levels);\n"
913 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
914 " float dx = length(ddx(tSized));\n"
915 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500916 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400917
918 if (textureFunction->method == TextureFunction::BIAS)
919 {
920 out << " lod += bias;\n";
921 }
922 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500923 else if (textureFunction->method == TextureFunction::GRAD)
924 {
925 out << " x.GetDimensions(0, width, height, depth, levels);\n"
926 " float lod = log2(max(length(ddx), length(ddy)));\n";
927 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400928
929 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
930 }
931
932 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400933 }
934 else UNREACHABLE();
935 }
936
937 out << " return ";
938
939 // HLSL intrinsic
940 if (mOutputType == SH_HLSL9_OUTPUT)
941 {
942 switch(textureFunction->sampler)
943 {
944 case EbtSampler2D: out << "tex2D"; break;
945 case EbtSamplerCube: out << "texCUBE"; break;
946 default: UNREACHABLE();
947 }
948
Nicolas Capens75fb4752013-07-10 15:14:47 -0400949 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400950 {
951 case TextureFunction::IMPLICIT: out << "(s, "; break;
952 case TextureFunction::BIAS: out << "bias(s, "; break;
953 case TextureFunction::LOD: out << "lod(s, "; break;
954 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400955 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400956 default: UNREACHABLE();
957 }
958 }
959 else if (mOutputType == SH_HLSL11_OUTPUT)
960 {
Nicolas Capensd11d5492014-02-19 17:06:10 -0500961 if (textureFunction->method == TextureFunction::GRAD)
962 {
963 if (IsIntegerSampler(textureFunction->sampler))
964 {
965 out << "x.Load(";
966 }
967 else if (IsShadowSampler(textureFunction->sampler))
968 {
969 out << "x.SampleCmpLevelZero(s, ";
970 }
971 else
972 {
973 out << "x.SampleGrad(s, ";
974 }
975 }
976 else if (IsIntegerSampler(textureFunction->sampler) ||
977 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400978 {
979 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400980 }
Nicolas Capenscb127d32013-07-15 17:26:18 -0400981 else if (IsShadowSampler(textureFunction->sampler))
982 {
983 out << "x.SampleCmp(s, ";
984 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400985 else
986 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400987 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400988 {
989 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
990 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
991 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
992 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400993 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400994 default: UNREACHABLE();
995 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400996 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400997 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400998 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400999
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001000 // Integer sampling requires integer addresses
1001 TString addressx = "";
1002 TString addressy = "";
1003 TString addressz = "";
1004 TString close = "";
1005
Nicolas Capensfc014542014-02-18 14:47:13 -05001006 if (IsIntegerSampler(textureFunction->sampler) ||
1007 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001008 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001009 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001010 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001011 case 2: out << "int3("; break;
1012 case 3: out << "int4("; break;
1013 default: UNREACHABLE();
1014 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001015
Nicolas Capensfc014542014-02-18 14:47:13 -05001016 // Convert from normalized floating-point to integer
1017 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001018 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001019 addressx = "int(floor(width * frac((";
1020 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001021
Nicolas Capensfc014542014-02-18 14:47:13 -05001022 if (IsSamplerArray(textureFunction->sampler))
1023 {
1024 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1025 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001026 else if (IsSamplerCube(textureFunction->sampler))
1027 {
1028 addressz = "((((";
1029 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001030 else
1031 {
1032 addressz = "int(floor(depth * frac((";
1033 }
1034
1035 close = "))))";
1036 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001037 }
1038 else
1039 {
1040 switch(hlslCoords)
1041 {
1042 case 2: out << "float2("; break;
1043 case 3: out << "float3("; break;
1044 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001045 default: UNREACHABLE();
1046 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001047 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001048
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001049 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001050
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001051 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001052 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001053 switch(textureFunction->coords)
1054 {
1055 case 3: proj = " / t.z"; break;
1056 case 4: proj = " / t.w"; break;
1057 default: UNREACHABLE();
1058 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001059 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001060
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001061 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001062
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001063 if (mOutputType == SH_HLSL9_OUTPUT)
1064 {
1065 if (hlslCoords >= 3)
1066 {
1067 if (textureFunction->coords < 3)
1068 {
1069 out << ", 0";
1070 }
1071 else
1072 {
1073 out << ", t.z" + proj;
1074 }
1075 }
1076
1077 if (hlslCoords == 4)
1078 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001079 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001080 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001081 case TextureFunction::BIAS: out << ", bias"; break;
1082 case TextureFunction::LOD: out << ", lod"; break;
1083 case TextureFunction::LOD0: out << ", 0"; break;
1084 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001085 default: UNREACHABLE();
1086 }
1087 }
1088
1089 out << "));\n";
1090 }
1091 else if (mOutputType == SH_HLSL11_OUTPUT)
1092 {
1093 if (hlslCoords >= 3)
1094 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001095 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1096 {
1097 out << ", face";
1098 }
1099 else
1100 {
1101 out << ", " + addressz + ("t.z" + proj) + close;
1102 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001103 }
1104
Nicolas Capensd11d5492014-02-19 17:06:10 -05001105 if (textureFunction->method == TextureFunction::GRAD)
1106 {
1107 if (IsIntegerSampler(textureFunction->sampler))
1108 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001109 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001110 }
1111 else if (IsShadowSampler(textureFunction->sampler))
1112 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001113 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001114 switch(textureFunction->coords)
1115 {
1116 case 3: out << "), t.z"; break;
1117 case 4: out << "), t.w"; break;
1118 default: UNREACHABLE();
1119 }
1120 }
1121 else
1122 {
1123 out << "), ddx, ddy";
1124 }
1125 }
1126 else if (IsIntegerSampler(textureFunction->sampler) ||
1127 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001128 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001129 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001130 }
1131 else if (IsShadowSampler(textureFunction->sampler))
1132 {
1133 // Compare value
1134 switch(textureFunction->coords)
1135 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001136 case 3: out << "), t.z"; break;
1137 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001138 default: UNREACHABLE();
1139 }
1140 }
1141 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001142 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001143 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001144 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001145 case TextureFunction::IMPLICIT: out << ")"; break;
1146 case TextureFunction::BIAS: out << "), bias"; break;
1147 case TextureFunction::LOD: out << "), lod"; break;
1148 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001149 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001150 default: UNREACHABLE();
1151 }
1152 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001153
1154 if (textureFunction->offset)
1155 {
1156 out << ", offset";
1157 }
1158
1159 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001160 }
1161 else UNREACHABLE();
1162 }
1163
1164 out << "\n"
1165 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001166 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001167 }
1168
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001169 if (mUsesFragCoord)
1170 {
1171 out << "#define GL_USES_FRAG_COORD\n";
1172 }
1173
1174 if (mUsesPointCoord)
1175 {
1176 out << "#define GL_USES_POINT_COORD\n";
1177 }
1178
1179 if (mUsesFrontFacing)
1180 {
1181 out << "#define GL_USES_FRONT_FACING\n";
1182 }
1183
1184 if (mUsesPointSize)
1185 {
1186 out << "#define GL_USES_POINT_SIZE\n";
1187 }
1188
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001189 if (mUsesFragDepth)
1190 {
1191 out << "#define GL_USES_FRAG_DEPTH\n";
1192 }
1193
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001194 if (mUsesDepthRange)
1195 {
1196 out << "#define GL_USES_DEPTH_RANGE\n";
1197 }
1198
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001199 if (mUsesXor)
1200 {
1201 out << "bool xor(bool p, bool q)\n"
1202 "{\n"
1203 " return (p || q) && !(p && q);\n"
1204 "}\n"
1205 "\n";
1206 }
1207
Olli Etuaho80a5a6c2015-01-12 15:35:27 +02001208 builtInFunctionEmulator->OutputEmulatedFunctionDefinition(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001209}
1210
1211void OutputHLSL::visitSymbol(TIntermSymbol *node)
1212{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001213 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001214
Jamie Madill570e04d2013-06-21 09:15:33 -04001215 // Handle accessing std140 structs by value
1216 if (mFlaggedStructMappedNames.count(node) > 0)
1217 {
1218 out << mFlaggedStructMappedNames[node];
1219 return;
1220 }
1221
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001222 TString name = node->getSymbol();
1223
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001224 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001225 {
1226 mUsesDepthRange = true;
1227 out << name;
1228 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229 else
1230 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001231 TQualifier qualifier = node->getQualifier();
1232
1233 if (qualifier == EvqUniform)
1234 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001235 const TType& nodeType = node->getType();
1236 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1237
1238 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001239 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001240 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001241 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001242 else
1243 {
1244 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001245 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001246
Jamie Madill033dae62014-06-18 12:56:28 -04001247 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001248 }
Jamie Madill19571812013-08-12 15:26:34 -07001249 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001250 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001251 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001252 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001253 }
Jamie Madill033dae62014-06-18 12:56:28 -04001254 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001255 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001256 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001257 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001258 }
Jamie Madill19571812013-08-12 15:26:34 -07001259 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001260 {
1261 mReferencedOutputVariables[name] = node;
1262 out << "out_" << name;
1263 }
1264 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001265 {
1266 out << "gl_Color[0]";
1267 mUsesFragColor = true;
1268 }
1269 else if (qualifier == EvqFragData)
1270 {
1271 out << "gl_Color";
1272 mUsesFragData = true;
1273 }
1274 else if (qualifier == EvqFragCoord)
1275 {
1276 mUsesFragCoord = true;
1277 out << name;
1278 }
1279 else if (qualifier == EvqPointCoord)
1280 {
1281 mUsesPointCoord = true;
1282 out << name;
1283 }
1284 else if (qualifier == EvqFrontFacing)
1285 {
1286 mUsesFrontFacing = true;
1287 out << name;
1288 }
1289 else if (qualifier == EvqPointSize)
1290 {
1291 mUsesPointSize = true;
1292 out << name;
1293 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001294 else if (name == "gl_FragDepthEXT")
1295 {
1296 mUsesFragDepth = true;
1297 out << "gl_Depth";
1298 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001299 else if (qualifier == EvqInternal)
1300 {
1301 out << name;
1302 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001303 else
1304 {
Jamie Madill033dae62014-06-18 12:56:28 -04001305 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001306 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001307 }
1308}
1309
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001310void OutputHLSL::visitRaw(TIntermRaw *node)
1311{
1312 mBody << node->getRawText();
1313}
1314
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001315bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1316{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001317 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001318
Jamie Madill570e04d2013-06-21 09:15:33 -04001319 // Handle accessing std140 structs by value
1320 if (mFlaggedStructMappedNames.count(node) > 0)
1321 {
1322 out << mFlaggedStructMappedNames[node];
1323 return false;
1324 }
1325
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001326 switch (node->getOp())
1327 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001328 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001329 case EOpInitialize:
1330 if (visit == PreVisit)
1331 {
1332 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1333 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1334 // new variable is created before the assignment is evaluated), so we need to convert
1335 // this to "float t = x, x = t;".
1336
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001337 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1338 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001339
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001340 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1341 expression->traverse(&searchSymbol);
1342 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001343
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001344 if (sameSymbol)
1345 {
1346 // Type already printed
1347 out << "t" + str(mUniqueIndex) + " = ";
1348 expression->traverse(this);
1349 out << ", ";
1350 symbolNode->traverse(this);
1351 out << " = t" + str(mUniqueIndex);
1352
1353 mUniqueIndex++;
1354 return false;
1355 }
1356 }
1357 else if (visit == InVisit)
1358 {
1359 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001360 }
1361 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001362 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1363 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1364 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1365 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1366 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1367 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001368 if (visit == PreVisit)
1369 {
1370 out << "(";
1371 }
1372 else if (visit == InVisit)
1373 {
1374 out << " = mul(";
1375 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001376 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001377 }
1378 else
1379 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001380 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001381 }
1382 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001383 case EOpMatrixTimesMatrixAssign:
1384 if (visit == PreVisit)
1385 {
1386 out << "(";
1387 }
1388 else if (visit == InVisit)
1389 {
1390 out << " = mul(";
1391 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001392 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001393 }
1394 else
1395 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001396 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001397 }
1398 break;
1399 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001400 case EOpModAssign: outputTriplet(visit, "(", " %= ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001401 case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break;
1402 case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break;
1403 case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break;
1404 case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break;
1405 case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001406 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001407 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001408 const TType& leftType = node->getLeft()->getType();
1409 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001410 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001411 if (visit == PreVisit)
1412 {
1413 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1414 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001415 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001416 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001417 return false;
1418 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001419 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001420 else
1421 {
1422 outputTriplet(visit, "", "[", "]");
1423 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001424 }
1425 break;
1426 case EOpIndexIndirect:
1427 // We do not currently support indirect references to interface blocks
1428 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1429 outputTriplet(visit, "", "[", "]");
1430 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001431 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001432 if (visit == InVisit)
1433 {
1434 const TStructure* structure = node->getLeft()->getType().getStruct();
1435 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1436 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001437 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001438
1439 return false;
1440 }
1441 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001442 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001443 if (visit == InVisit)
1444 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001445 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1446 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1447 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001448 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001449
1450 return false;
1451 }
1452 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001453 case EOpVectorSwizzle:
1454 if (visit == InVisit)
1455 {
1456 out << ".";
1457
1458 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1459
1460 if (swizzle)
1461 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001462 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001463
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001464 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001465 {
1466 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1467
1468 if (element)
1469 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001470 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001471
1472 switch (i)
1473 {
1474 case 0: out << "x"; break;
1475 case 1: out << "y"; break;
1476 case 2: out << "z"; break;
1477 case 3: out << "w"; break;
1478 default: UNREACHABLE();
1479 }
1480 }
1481 else UNREACHABLE();
1482 }
1483 }
1484 else UNREACHABLE();
1485
1486 return false; // Fully processed
1487 }
1488 break;
1489 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1490 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1491 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1492 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
Gregoire Payen de La Garanderiebe954a22014-12-23 00:05:28 +00001493 case EOpMod: outputTriplet(visit, "(", " % ", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001494 case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break;
1495 case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break;
1496 case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break;
1497 case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break;
1498 case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001499 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001500 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001501 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001502 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001503 if (node->getOp() == EOpEqual)
1504 {
1505 outputTriplet(visit, "(", " == ", ")");
1506 }
1507 else
1508 {
1509 outputTriplet(visit, "(", " != ", ")");
1510 }
1511 }
1512 else if (node->getLeft()->getBasicType() == EbtStruct)
1513 {
1514 if (node->getOp() == EOpEqual)
1515 {
1516 out << "(";
1517 }
1518 else
1519 {
1520 out << "!(";
1521 }
1522
Jamie Madill98493dd2013-07-08 14:39:03 -04001523 const TStructure &structure = *node->getLeft()->getType().getStruct();
1524 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001525
Jamie Madill98493dd2013-07-08 14:39:03 -04001526 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001527 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001528 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001529
1530 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001531 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001532 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001533 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001534
Jamie Madill98493dd2013-07-08 14:39:03 -04001535 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001536 {
1537 out << " && ";
1538 }
1539 }
1540
1541 out << ")";
1542
1543 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001544 }
1545 else
1546 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001547 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001548
1549 if (node->getOp() == EOpEqual)
1550 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001551 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001552 }
1553 else
1554 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001555 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001556 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001557 }
1558 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001559 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1560 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1561 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1562 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1563 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001564 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001565 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1566 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001567 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001568 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001569 if (node->getRight()->hasSideEffects())
1570 {
1571 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1572 return false;
1573 }
1574 else
1575 {
1576 outputTriplet(visit, "(", " || ", ")");
1577 return true;
1578 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001579 case EOpLogicalXor:
1580 mUsesXor = true;
1581 outputTriplet(visit, "xor(", ", ", ")");
1582 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001583 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001584 if (node->getRight()->hasSideEffects())
1585 {
1586 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1587 return false;
1588 }
1589 else
1590 {
1591 outputTriplet(visit, "(", " && ", ")");
1592 return true;
1593 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001594 default: UNREACHABLE();
1595 }
1596
1597 return true;
1598}
1599
1600bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1601{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602 switch (node->getOp())
1603 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04001604 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001605 case EOpPositive: outputTriplet(visit, "(+", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001606 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1607 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
Olli Etuaho31b5fc62015-01-16 12:13:36 +02001608 case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break;
Nicolas Capens16004fc2014-06-11 11:29:11 -04001609 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1610 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1611 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1612 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001613 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1614 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1615 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1616 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1617 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1618 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1619 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1620 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001621 case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break;
1622 case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break;
1623 case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break;
1624 case EOpAsinh:
1625 ASSERT(node->getUseEmulatedFunction());
1626 writeEmulatedFunctionTriplet(visit, "asinh(");
1627 break;
1628 case EOpAcosh:
1629 ASSERT(node->getUseEmulatedFunction());
1630 writeEmulatedFunctionTriplet(visit, "acosh(");
1631 break;
1632 case EOpAtanh:
1633 ASSERT(node->getUseEmulatedFunction());
1634 writeEmulatedFunctionTriplet(visit, "atanh(");
1635 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001636 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1637 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1638 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1639 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1640 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1641 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1642 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1643 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1644 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1645 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1646 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
Olli Etuahoe8d2c072015-01-08 16:33:54 +02001647 case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break;
1648 case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break;
1649 case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
1650 case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001651 case EOpPackSnorm2x16:
1652 ASSERT(node->getUseEmulatedFunction());
1653 writeEmulatedFunctionTriplet(visit, "packSnorm2x16(");
1654 break;
1655 case EOpPackUnorm2x16:
1656 ASSERT(node->getUseEmulatedFunction());
1657 writeEmulatedFunctionTriplet(visit, "packUnorm2x16(");
1658 break;
1659 case EOpPackHalf2x16:
1660 ASSERT(node->getUseEmulatedFunction());
1661 writeEmulatedFunctionTriplet(visit, "packHalf2x16(");
1662 break;
1663 case EOpUnpackSnorm2x16:
1664 ASSERT(node->getUseEmulatedFunction());
1665 writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16(");
1666 break;
1667 case EOpUnpackUnorm2x16:
1668 ASSERT(node->getUseEmulatedFunction());
1669 writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16(");
1670 break;
1671 case EOpUnpackHalf2x16:
1672 ASSERT(node->getUseEmulatedFunction());
1673 writeEmulatedFunctionTriplet(visit, "unpackHalf2x16(");
1674 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001675 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1676 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001677 case EOpDFdx:
1678 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1679 {
1680 outputTriplet(visit, "(", "", ", 0.0)");
1681 }
1682 else
1683 {
1684 outputTriplet(visit, "ddx(", "", ")");
1685 }
1686 break;
1687 case EOpDFdy:
1688 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1689 {
1690 outputTriplet(visit, "(", "", ", 0.0)");
1691 }
1692 else
1693 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001694 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001695 }
1696 break;
1697 case EOpFwidth:
1698 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1699 {
1700 outputTriplet(visit, "(", "", ", 0.0)");
1701 }
1702 else
1703 {
1704 outputTriplet(visit, "fwidth(", "", ")");
1705 }
1706 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001707 case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break;
1708 case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001709 case EOpInverse:
1710 ASSERT(node->getUseEmulatedFunction());
1711 writeEmulatedFunctionTriplet(visit, "inverse(");
1712 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001713
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001714 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1715 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716 default: UNREACHABLE();
1717 }
1718
1719 return true;
1720}
1721
1722bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1723{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001724 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001725
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001726 switch (node->getOp())
1727 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001728 case EOpSequence:
1729 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001730 if (mInsideFunction)
1731 {
Jamie Madill075edd82013-07-08 13:30:19 -04001732 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001733 out << "{\n";
1734 }
1735
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001736 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001737 {
Jamie Madill075edd82013-07-08 13:30:19 -04001738 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001739
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001740 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001741
1742 out << ";\n";
1743 }
1744
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001745 if (mInsideFunction)
1746 {
Jamie Madill075edd82013-07-08 13:30:19 -04001747 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001748 out << "}\n";
1749 }
1750
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001751 return false;
1752 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753 case EOpDeclaration:
1754 if (visit == PreVisit)
1755 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001756 TIntermSequence *sequence = node->getSequence();
1757 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001759 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001761 TStructure *structure = variable->getType().getStruct();
1762
1763 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00001764 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001765 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001766 }
1767
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001768 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001770 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771 {
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001772 if (isSingleStatement(*sit))
1773 {
1774 mUnfoldShortCircuit->traverse(*sit);
1775 }
1776
Nicolas Capensd974db42014-10-07 10:50:19 -04001777 if (!mInsideFunction)
1778 {
1779 out << "static ";
1780 }
1781
1782 out << TypeString(variable->getType()) + " ";
1783
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001784 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001786 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001787 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001788 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001789 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05001790 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001791 }
1792 else
1793 {
1794 (*sit)->traverse(this);
1795 }
1796
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001797 if (*sit != sequence->back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001798 {
Nicolas Capensd974db42014-10-07 10:50:19 -04001799 out << ";\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800 }
1801 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001802 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001803 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1804 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001805 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001806 }
1807 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808 }
Jamie Madill033dae62014-06-18 12:56:28 -04001809 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001810 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001811 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001812 {
1813 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1814
1815 if (symbol)
1816 {
1817 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1818 mReferencedVaryings[symbol->getSymbol()] = symbol;
1819 }
1820 else
1821 {
1822 (*sit)->traverse(this);
1823 }
1824 }
1825 }
1826
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827 return false;
1828 }
1829 else if (visit == InVisit)
1830 {
1831 out << ", ";
1832 }
1833 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001834 case EOpInvariantDeclaration:
1835 // Do not do any translation
1836 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001837 case EOpPrototype:
1838 if (visit == PreVisit)
1839 {
Olli Etuaho76acee82014-11-04 13:44:03 +02001840 out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001841
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001842 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001843
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001844 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001845 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001846 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001847
1848 if (symbol)
1849 {
1850 out << argumentString(symbol);
1851
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001852 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001853 {
1854 out << ", ";
1855 }
1856 }
1857 else UNREACHABLE();
1858 }
1859
1860 out << ");\n";
1861
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001862 // Also prototype the Lod0 variant if needed
1863 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1864 {
1865 mOutputLod0Function = true;
1866 node->traverse(this);
1867 mOutputLod0Function = false;
1868 }
1869
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001870 return false;
1871 }
1872 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001873 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874 case EOpFunction:
1875 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001876 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877
Jamie Madill033dae62014-06-18 12:56:28 -04001878 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001879
1880 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001882 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001884 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885 {
Jamie Madill033dae62014-06-18 12:56:28 -04001886 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001887 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001888
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001889 TIntermSequence *sequence = node->getSequence();
1890 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001891
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001892 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001893 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001894 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001895
1896 if (symbol)
1897 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001898 TStructure *structure = symbol->getType().getStruct();
1899
1900 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001901 {
Jamie Madill8daaba12014-06-13 10:04:33 -04001902 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001903 }
1904
1905 out << argumentString(symbol);
1906
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001907 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001908 {
1909 out << ", ";
1910 }
1911 }
1912 else UNREACHABLE();
1913 }
1914
1915 out << ")\n"
1916 "{\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001917
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001918 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001919 {
1920 mInsideFunction = true;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001921 (*sequence)[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001922 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001924
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001925 out << "}\n";
1926
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001927 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1928 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001929 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001930 {
1931 mOutputLod0Function = true;
1932 node->traverse(this);
1933 mOutputLod0Function = false;
1934 }
1935 }
1936
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001937 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001938 }
1939 break;
1940 case EOpFunctionCall:
1941 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001942 TString name = TFunction::unmangleName(node->getName());
1943 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001944 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001945
1946 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947 {
Jamie Madill033dae62014-06-18 12:56:28 -04001948 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949 }
1950 else
1951 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001952 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001953
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001954 TextureFunction textureFunction;
1955 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001956 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04001957 textureFunction.method = TextureFunction::IMPLICIT;
1958 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001959 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001960
1961 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001962 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001963 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001964 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001965 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001966 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001967 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001968 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001969 }
Nicolas Capens46485082014-04-15 13:12:50 -04001970 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
1971 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001972 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001973 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001974 }
Nicolas Capens46485082014-04-15 13:12:50 -04001975 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001976 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001977 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001978 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001979 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04001980 else if (name == "textureSize")
1981 {
1982 textureFunction.method = TextureFunction::SIZE;
1983 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001984 else if (name == "textureOffset")
1985 {
1986 textureFunction.method = TextureFunction::IMPLICIT;
1987 textureFunction.offset = true;
1988 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05001989 else if (name == "textureProjOffset")
1990 {
1991 textureFunction.method = TextureFunction::IMPLICIT;
1992 textureFunction.offset = true;
1993 textureFunction.proj = true;
1994 }
1995 else if (name == "textureLodOffset")
1996 {
1997 textureFunction.method = TextureFunction::LOD;
1998 textureFunction.offset = true;
1999 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002000 else if (name == "textureProjLodOffset")
2001 {
2002 textureFunction.method = TextureFunction::LOD;
2003 textureFunction.proj = true;
2004 textureFunction.offset = true;
2005 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002006 else if (name == "texelFetch")
2007 {
2008 textureFunction.method = TextureFunction::FETCH;
2009 }
2010 else if (name == "texelFetchOffset")
2011 {
2012 textureFunction.method = TextureFunction::FETCH;
2013 textureFunction.offset = true;
2014 }
Nicolas Capens46485082014-04-15 13:12:50 -04002015 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002016 {
2017 textureFunction.method = TextureFunction::GRAD;
2018 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002019 else if (name == "textureGradOffset")
2020 {
2021 textureFunction.method = TextureFunction::GRAD;
2022 textureFunction.offset = true;
2023 }
Nicolas Capens46485082014-04-15 13:12:50 -04002024 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002025 {
2026 textureFunction.method = TextureFunction::GRAD;
2027 textureFunction.proj = true;
2028 }
2029 else if (name == "textureProjGradOffset")
2030 {
2031 textureFunction.method = TextureFunction::GRAD;
2032 textureFunction.proj = true;
2033 textureFunction.offset = true;
2034 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002035 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002036
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002037 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002038 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002039 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2040
2041 if (textureFunction.offset)
2042 {
2043 mandatoryArgumentCount++;
2044 }
2045
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002046 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002047
Jamie Madill183bde52014-07-02 15:31:19 -04002048 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002049 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002050 if (bias)
2051 {
2052 textureFunction.method = TextureFunction::LOD0BIAS;
2053 }
2054 else
2055 {
2056 textureFunction.method = TextureFunction::LOD0;
2057 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002058 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002059 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002060 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002061 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002062 }
2063 }
2064
2065 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002066
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002067 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002068 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002069
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002070 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002071 {
2072 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2073 {
2074 out << "texture_";
2075 (*arg)->traverse(this);
2076 out << ", sampler_";
2077 }
2078
2079 (*arg)->traverse(this);
2080
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002081 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002082 {
2083 out << ", ";
2084 }
2085 }
2086
2087 out << ")";
2088
2089 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 }
2091 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002092 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002093 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2094 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2095 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2096 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2097 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2098 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2099 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2100 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2101 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2102 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2103 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2104 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2105 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2106 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2107 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2108 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2109 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2110 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2111 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002112 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002113 {
Jamie Madill033dae62014-06-18 12:56:28 -04002114 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002115 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002116 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2117 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002118 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002119 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2120 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2121 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2122 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2123 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2124 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002125 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002126 ASSERT(node->getUseEmulatedFunction());
2127 writeEmulatedFunctionTriplet(visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002128 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002129 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002131 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002132 ASSERT(node->getUseEmulatedFunction());
2133 writeEmulatedFunctionTriplet(visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134 break;
2135 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2136 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2137 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2138 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2139 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2140 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2141 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2142 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2143 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002144 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002145 ASSERT(node->getUseEmulatedFunction());
2146 writeEmulatedFunctionTriplet(visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002147 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2149 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002150 case EOpOuterProduct:
2151 ASSERT(node->getUseEmulatedFunction());
2152 writeEmulatedFunctionTriplet(visit, "outerProduct(");
2153 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155 default: UNREACHABLE();
2156 }
2157
2158 return true;
2159}
2160
2161bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2162{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002163 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002164
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002165 if (node->usesTernaryOperator())
2166 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002167 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002168 }
2169 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002171 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002172
Corentin Wallez80bacde2014-11-10 12:07:37 -08002173 // D3D errors when there is a gradient operation in a loop in an unflattened if
2174 // however flattening all the ifs in branch heavy shaders made D3D error too.
2175 // As a temporary workaround we flatten the ifs only if there is at least a loop
2176 // present somewhere in the shader.
2177 if (mContext.shaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
2178 {
2179 out << "FLATTEN ";
2180 }
2181
2182 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002183
2184 node->getCondition()->traverse(this);
2185
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002186 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002187
Jamie Madill075edd82013-07-08 13:30:19 -04002188 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002189 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002191 bool discard = false;
2192
daniel@transgaming.combb885322010-04-15 20:45:24 +00002193 if (node->getTrueBlock())
2194 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002195 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002196
2197 // Detect true discard
2198 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002199 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002200
Jamie Madill075edd82013-07-08 13:30:19 -04002201 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002202 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002203
2204 if (node->getFalseBlock())
2205 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002206 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002207
Jamie Madill075edd82013-07-08 13:30:19 -04002208 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002209 out << "{\n";
2210
Jamie Madill075edd82013-07-08 13:30:19 -04002211 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002212 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002213
Jamie Madill075edd82013-07-08 13:30:19 -04002214 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002215 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002216
2217 // Detect false discard
2218 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2219 }
2220
2221 // ANGLE issue 486: Detect problematic conditional discard
2222 if (discard && FindSideEffectRewriting::search(node))
2223 {
2224 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002225 }
2226 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002227
2228 return false;
2229}
2230
2231void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2232{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002233 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002234}
2235
2236bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2237{
Nicolas Capens655fe362014-04-11 13:12:34 -04002238 mNestedLoopDepth++;
2239
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002240 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2241
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002242 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002243 {
2244 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2245 }
2246
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002247 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002248 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002249 if (handleExcessiveLoop(node))
2250 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002251 mInsideDiscontinuousLoop = wasDiscontinuous;
2252 mNestedLoopDepth--;
2253
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002254 return false;
2255 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002256 }
2257
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002258 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259
alokp@chromium.org52813552010-11-16 18:36:09 +00002260 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002261 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002262 out << "{LOOP do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002263
Jamie Madill075edd82013-07-08 13:30:19 -04002264 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002265 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002266 }
2267 else
2268 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002269 out << "{LOOP for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002270
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 if (node->getInit())
2272 {
2273 node->getInit()->traverse(this);
2274 }
2275
2276 out << "; ";
2277
alokp@chromium.org52813552010-11-16 18:36:09 +00002278 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002280 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281 }
2282
2283 out << "; ";
2284
alokp@chromium.org52813552010-11-16 18:36:09 +00002285 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002287 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 }
2289
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002290 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002291
Jamie Madill075edd82013-07-08 13:30:19 -04002292 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002293 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 }
2295
2296 if (node->getBody())
2297 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002298 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 }
2300
Jamie Madill075edd82013-07-08 13:30:19 -04002301 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002302 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303
alokp@chromium.org52813552010-11-16 18:36:09 +00002304 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 {
Jamie Madill075edd82013-07-08 13:30:19 -04002306 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 out << "while(\n";
2308
alokp@chromium.org52813552010-11-16 18:36:09 +00002309 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310
daniel@transgaming.com73536982012-03-21 20:45:49 +00002311 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312 }
2313
daniel@transgaming.com73536982012-03-21 20:45:49 +00002314 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002316 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002317 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002318
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 return false;
2320}
2321
2322bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2323{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002324 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325
2326 switch (node->getFlowOp())
2327 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002328 case EOpKill:
2329 outputTriplet(visit, "discard;\n", "", "");
2330 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002331 case EOpBreak:
2332 if (visit == PreVisit)
2333 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002334 if (mNestedLoopDepth > 1)
2335 {
2336 mUsesNestedBreak = true;
2337 }
2338
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002339 if (mExcessiveLoopIndex)
2340 {
2341 out << "{Break";
2342 mExcessiveLoopIndex->traverse(this);
2343 out << " = true; break;}\n";
2344 }
2345 else
2346 {
2347 out << "break;\n";
2348 }
2349 }
2350 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002351 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 case EOpReturn:
2353 if (visit == PreVisit)
2354 {
2355 if (node->getExpression())
2356 {
2357 out << "return ";
2358 }
2359 else
2360 {
2361 out << "return;\n";
2362 }
2363 }
2364 else if (visit == PostVisit)
2365 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002366 if (node->getExpression())
2367 {
2368 out << ";\n";
2369 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 }
2371 break;
2372 default: UNREACHABLE();
2373 }
2374
2375 return true;
2376}
2377
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002378void OutputHLSL::traverseStatements(TIntermNode *node)
2379{
2380 if (isSingleStatement(node))
2381 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002382 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002383 }
2384
2385 node->traverse(this);
2386}
2387
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002388bool OutputHLSL::isSingleStatement(TIntermNode *node)
2389{
2390 TIntermAggregate *aggregate = node->getAsAggregate();
2391
2392 if (aggregate)
2393 {
2394 if (aggregate->getOp() == EOpSequence)
2395 {
2396 return false;
2397 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002398 else if (aggregate->getOp() == EOpDeclaration)
2399 {
2400 // Declaring multiple comma-separated variables must be considered multiple statements
2401 // because each individual declaration has side effects which are visible in the next.
2402 return false;
2403 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002404 else
2405 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002406 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002407 {
2408 if (!isSingleStatement(*sit))
2409 {
2410 return false;
2411 }
2412 }
2413
2414 return true;
2415 }
2416 }
2417
2418 return true;
2419}
2420
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002421// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2422// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002423bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2424{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002425 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002426 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002427
2428 // Parse loops of the form:
2429 // for(int index = initial; index [comparator] limit; index += increment)
2430 TIntermSymbol *index = NULL;
2431 TOperator comparator = EOpNull;
2432 int initial = 0;
2433 int limit = 0;
2434 int increment = 0;
2435
2436 // Parse index name and intial value
2437 if (node->getInit())
2438 {
2439 TIntermAggregate *init = node->getInit()->getAsAggregate();
2440
2441 if (init)
2442 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002443 TIntermSequence *sequence = init->getSequence();
2444 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002445
2446 if (variable && variable->getQualifier() == EvqTemporary)
2447 {
2448 TIntermBinary *assign = variable->getAsBinaryNode();
2449
2450 if (assign->getOp() == EOpInitialize)
2451 {
2452 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2453 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2454
2455 if (symbol && constant)
2456 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002457 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002458 {
2459 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002460 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002461 }
2462 }
2463 }
2464 }
2465 }
2466 }
2467
2468 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002469 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002470 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002471 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002472
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002473 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2474 {
2475 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2476
2477 if (constant)
2478 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002479 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002480 {
2481 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002482 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002483 }
2484 }
2485 }
2486 }
2487
2488 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002489 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002490 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002491 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2492 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002493
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002494 if (binaryTerminal)
2495 {
2496 TOperator op = binaryTerminal->getOp();
2497 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2498
2499 if (constant)
2500 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002501 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002502 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002503 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002504
2505 switch (op)
2506 {
2507 case EOpAddAssign: increment = value; break;
2508 case EOpSubAssign: increment = -value; break;
2509 default: UNIMPLEMENTED();
2510 }
2511 }
2512 }
2513 }
2514 else if (unaryTerminal)
2515 {
2516 TOperator op = unaryTerminal->getOp();
2517
2518 switch (op)
2519 {
2520 case EOpPostIncrement: increment = 1; break;
2521 case EOpPostDecrement: increment = -1; break;
2522 case EOpPreIncrement: increment = 1; break;
2523 case EOpPreDecrement: increment = -1; break;
2524 default: UNIMPLEMENTED();
2525 }
2526 }
2527 }
2528
2529 if (index != NULL && comparator != EOpNull && increment != 0)
2530 {
2531 if (comparator == EOpLessThanEqual)
2532 {
2533 comparator = EOpLessThan;
2534 limit += 1;
2535 }
2536
2537 if (comparator == EOpLessThan)
2538 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002539 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002540
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002541 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002542 {
2543 return false; // Not an excessive loop
2544 }
2545
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002546 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2547 mExcessiveLoopIndex = index;
2548
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002549 out << "{int ";
2550 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002551 out << ";\n"
2552 "bool Break";
2553 index->traverse(this);
2554 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002555
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002556 bool firstLoopFragment = true;
2557
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002558 while (iterations > 0)
2559 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002560 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002561
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002562 if (!firstLoopFragment)
2563 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002564 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002565 index->traverse(this);
2566 out << ") {\n";
2567 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002568
2569 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2570 {
2571 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2572 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002573
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002574 // for(int index = initial; index < clampedLimit; index += increment)
2575
Nicolas Capens5e0c80a2014-10-10 10:11:54 -04002576 out << "LOOP for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002577 index->traverse(this);
2578 out << " = ";
2579 out << initial;
2580
2581 out << "; ";
2582 index->traverse(this);
2583 out << " < ";
2584 out << clampedLimit;
2585
2586 out << "; ";
2587 index->traverse(this);
2588 out << " += ";
2589 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002590 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002591
Jamie Madill075edd82013-07-08 13:30:19 -04002592 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002593 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002594
2595 if (node->getBody())
2596 {
2597 node->getBody()->traverse(this);
2598 }
2599
Jamie Madill075edd82013-07-08 13:30:19 -04002600 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002601 out << ";}\n";
2602
2603 if (!firstLoopFragment)
2604 {
2605 out << "}\n";
2606 }
2607
2608 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002609
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002610 initial += MAX_LOOP_ITERATIONS * increment;
2611 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002612 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002613
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002614 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002615
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002616 mExcessiveLoopIndex = restoreIndex;
2617
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002618 return true;
2619 }
2620 else UNIMPLEMENTED();
2621 }
2622
2623 return false; // Not handled as an excessive loop
2624}
2625
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002626void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002627{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002628 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002630 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631 {
2632 out << preString;
2633 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002634 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002635 {
2636 out << inString;
2637 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002638 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002639 {
2640 out << postString;
2641 }
2642}
2643
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002644void OutputHLSL::outputLineDirective(int line)
2645{
2646 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2647 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002648 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002649 mBody << "#line " << line;
2650
2651 if (mContext.sourcePath)
2652 {
2653 mBody << " \"" << mContext.sourcePath << "\"";
2654 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002655
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002656 mBody << "\n";
2657 }
2658}
2659
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002660TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2661{
2662 TQualifier qualifier = symbol->getQualifier();
2663 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002664 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002665
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002666 if (name.empty()) // HLSL demands named arguments, also for prototypes
2667 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002668 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002669 }
2670 else
2671 {
Jamie Madill033dae62014-06-18 12:56:28 -04002672 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002673 }
2674
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002675 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2676 {
Jamie Madill033dae62014-06-18 12:56:28 -04002677 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
Jamie Madillf91ce812014-06-13 10:04:34 -04002678 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002679 }
2680
Jamie Madill033dae62014-06-18 12:56:28 -04002681 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002682}
2683
2684TString OutputHLSL::initializer(const TType &type)
2685{
2686 TString string;
2687
Jamie Madill94bf7f22013-07-08 13:31:15 -04002688 size_t size = type.getObjectSize();
2689 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002691 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692
Jamie Madill94bf7f22013-07-08 13:31:15 -04002693 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694 {
2695 string += ", ";
2696 }
2697 }
2698
daniel@transgaming.comead23042010-04-29 03:35:36 +00002699 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002701
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002702void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2703{
2704 TInfoSinkBase &out = mBody;
2705
2706 if (visit == PreVisit)
2707 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002708 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002709
2710 out << name + "(";
2711 }
2712 else if (visit == InVisit)
2713 {
2714 out << ", ";
2715 }
2716 else if (visit == PostVisit)
2717 {
2718 out << ")";
2719 }
2720}
2721
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002722const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2723{
2724 TInfoSinkBase &out = mBody;
2725
Jamie Madill98493dd2013-07-08 14:39:03 -04002726 const TStructure* structure = type.getStruct();
2727 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002728 {
Jamie Madill033dae62014-06-18 12:56:28 -04002729 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002730
Jamie Madill98493dd2013-07-08 14:39:03 -04002731 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002732
Jamie Madill98493dd2013-07-08 14:39:03 -04002733 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002734 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002735 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002736 constUnion = writeConstantUnion(*fieldType, constUnion);
2737
Jamie Madill98493dd2013-07-08 14:39:03 -04002738 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002739 {
2740 out << ", ";
2741 }
2742 }
2743
2744 out << ")";
2745 }
2746 else
2747 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002748 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002749 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002750
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002751 if (writeType)
2752 {
Jamie Madill033dae62014-06-18 12:56:28 -04002753 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002754 }
2755
Jamie Madill94bf7f22013-07-08 13:31:15 -04002756 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002757 {
2758 switch (constUnion->getType())
2759 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00002760 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002761 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04002762 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002763 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002764 default: UNREACHABLE();
2765 }
2766
2767 if (i != size - 1)
2768 {
2769 out << ", ";
2770 }
2771 }
2772
2773 if (writeType)
2774 {
2775 out << ")";
2776 }
2777 }
2778
2779 return constUnion;
2780}
2781
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002782void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr)
2783{
2784 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
2785 outputTriplet(visit, preString.c_str(), ", ", ")");
2786}
2787
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002788}