blob: 70093907726e5c116a2358ea5d5ec16cc1f692bf [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000010#include "common/utilities.h"
Geoff Lang17732822013-08-29 13:46:49 -040011#include "compiler/translator/compilerdebug.h"
12#include "compiler/translator/InfoSink.h"
13#include "compiler/translator/DetectDiscontinuity.h"
14#include "compiler/translator/SearchSymbol.h"
15#include "compiler/translator/UnfoldShortCircuit.h"
16#include "compiler/translator/HLSLLayoutEncoder.h"
17#include "compiler/translator/FlagStd140Structs.h"
Jamie Madill3c9eeb92013-11-04 11:09:26 -050018#include "compiler/translator/NodeSearch.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000020#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000021#include <cfloat>
22#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000023
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024namespace sh
25{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000026// Integer to TString conversion
Geoff Lang036aa492013-10-09 16:23:30 -040027template <typename T>
28TString str(T i)
daniel@transgaming.com005c7392010-04-15 20:45:27 +000029{
Geoff Lang036aa492013-10-09 16:23:30 -040030 ASSERT(std::numeric_limits<T>::is_integer);
31 char buffer[(CHAR_BIT * sizeof(T) / 3) + 3];
32 const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
33 snprintf(buffer, sizeof(buffer), formatStr, i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000034 return buffer;
35}
36
Nicolas Capense0ba27a2013-06-24 16:10:52 -040037TString OutputHLSL::TextureFunction::name() const
38{
39 TString name = "gl_texture";
40
Nicolas Capens6d232bb2013-07-08 15:56:38 -040041 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040042 {
43 name += "2D";
44 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040045 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040046 {
47 name += "3D";
48 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040049 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040050 {
51 name += "Cube";
52 }
53 else UNREACHABLE();
54
55 if (proj)
56 {
57 name += "Proj";
58 }
59
Nicolas Capens75fb4752013-07-10 15:14:47 -040060 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040061 {
62 case IMPLICIT: break;
63 case BIAS: break;
64 case LOD: name += "Lod"; break;
65 case LOD0: name += "Lod0"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -040066 case SIZE: name += "Size"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040067 default: UNREACHABLE();
68 }
69
70 return name + "(";
71}
72
Jamie Madillc2141fb2013-08-30 13:21:08 -040073const char *RegisterPrefix(const TType &type)
74{
75 if (IsSampler(type.getBasicType()))
76 {
77 return "s";
78 }
79 else
80 {
81 return "c";
82 }
83}
84
Nicolas Capense0ba27a2013-06-24 16:10:52 -040085bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
86{
87 if (sampler < rhs.sampler) return true;
88 if (coords < rhs.coords) return true;
89 if (!proj && rhs.proj) return true;
Nicolas Capens75fb4752013-07-10 15:14:47 -040090 if (method < rhs.method) return true;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040091
92 return false;
93}
94
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000095OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000096 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000098 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000099 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000100
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000101 mUsesFragColor = false;
102 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000103 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000104 mUsesFragCoord = false;
105 mUsesPointCoord = false;
106 mUsesFrontFacing = false;
107 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400108 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000109 mUsesXor = false;
110 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000111 mUsesMod2v = false;
112 mUsesMod2f = false;
113 mUsesMod3v = false;
114 mUsesMod3f = false;
115 mUsesMod4v = false;
116 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000117 mUsesFaceforward1 = false;
118 mUsesFaceforward2 = false;
119 mUsesFaceforward3 = false;
120 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000121 mUsesAtan2_1 = false;
122 mUsesAtan2_2 = false;
123 mUsesAtan2_3 = false;
124 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500125 mUsesDiscardRewriting = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000126
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000127 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
128
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000129 mScopeDepth = 0;
130
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000131 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000132
133 mContainsLoopDiscontinuity = false;
134 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000135 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000136
137 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000138
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000139 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000140 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000141 if (mContext.shaderType == SH_FRAGMENT_SHADER)
142 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000143 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000144 }
145 else
146 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000147 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000148 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000149 }
150 else
151 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000152 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000153 }
154
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000155 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000156 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400157 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158}
159
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000160OutputHLSL::~OutputHLSL()
161{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000162 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000163}
164
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000165void OutputHLSL::output()
166{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000167 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400168 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
169 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000170
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000171 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000172 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000173
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000174 mContext.infoSink().obj << mHeader.c_str();
175 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000176}
177
Jamie Madill570e04d2013-06-21 09:15:33 -0400178void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
179{
180 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
181 {
182 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
183
184 // This will mark the necessary block elements as referenced
185 flaggedNode->traverse(this);
186 TString structName(mBody.c_str());
187 mBody.erase();
188
189 mFlaggedStructOriginalNames[flaggedNode] = structName;
190
191 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
192 {
193 structName.erase(pos, 1);
194 }
195
196 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
197 }
198}
199
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000200TInfoSinkBase &OutputHLSL::getBodyStream()
201{
202 return mBody;
203}
204
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400205const std::vector<Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000206{
207 return mActiveUniforms;
208}
209
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000210const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
211{
212 return mActiveInterfaceBlocks;
213}
214
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400215const std::vector<Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400216{
217 return mActiveOutputVariables;
218}
219
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400220const std::vector<Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400221{
222 return mActiveAttributes;
223}
224
Jamie Madill47fdd132013-08-30 13:21:04 -0400225const std::vector<Varying> &OutputHLSL::getVaryings() const
226{
227 return mActiveVaryings;
228}
229
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000230int OutputHLSL::vectorSize(const TType &type) const
231{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000232 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000233 int arraySize = type.isArray() ? type.getArraySize() : 1;
234
235 return elementSize * arraySize;
236}
237
Jamie Madill98493dd2013-07-08 14:39:03 -0400238TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000239{
Jamie Madill98493dd2013-07-08 14:39:03 -0400240 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000241 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400242 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000243 }
244 else
245 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400246 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000247 }
248}
249
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000250TString OutputHLSL::decoratePrivate(const TString &privateText)
251{
252 return "dx_" + privateText;
253}
254
Jamie Madill98493dd2013-07-08 14:39:03 -0400255TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000256{
Jamie Madill98493dd2013-07-08 14:39:03 -0400257 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000258}
259
Jamie Madill98493dd2013-07-08 14:39:03 -0400260TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000261{
Jamie Madill98493dd2013-07-08 14:39:03 -0400262 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000263 {
264 return "";
265 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400266 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000267 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400268 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000269 }
270 else
271 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400272 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000273 }
274}
275
Jamie Madill98493dd2013-07-08 14:39:03 -0400276TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000277{
Jamie Madill98493dd2013-07-08 14:39:03 -0400278 const TType &fieldType = *field.type();
279 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400280 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000281
Jamie Madill98493dd2013-07-08 14:39:03 -0400282 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000283 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400284 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400285 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000287 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400288 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000289 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400290 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400291 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000292 }
293 else
294 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400295 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000296 }
297}
298
Jamie Madill98493dd2013-07-08 14:39:03 -0400299TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
300{
301 TString hlsl;
302
303 int elementIndex = 0;
304
305 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
306 {
307 const TField &field = *interfaceBlock.fields()[typeIndex];
308 const TType &fieldType = *field.type();
309
310 if (blockStorage == EbsStd140)
311 {
312 // 2 and 3 component vector types in some cases need pre-padding
313 hlsl += std140PrePaddingString(fieldType, &elementIndex);
314 }
315
316 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
317 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
318
319 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
320 if (blockStorage == EbsStd140)
321 {
322 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
323 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
324 }
325 }
326
327 return hlsl;
328}
329
330TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
331{
332 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
333
334 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
335 "{\n" +
336 interfaceBlockFieldString(interfaceBlock, blockStorage) +
337 "};\n\n";
338}
339
340TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
341{
342 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
343 const TString &blockName = interfaceBlock.name() + arrayIndexString;
344 TString hlsl;
345
346 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
347 "{\n";
348
349 if (interfaceBlock.hasInstanceName())
350 {
351 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
352 }
353 else
354 {
355 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
356 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
357 }
358
359 hlsl += "};\n\n";
360
361 return hlsl;
362}
363
Jamie Madill574d9dd2013-06-20 11:55:56 -0400364TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
365{
366 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
367 {
368 // no padding needed, HLSL will align the field to a new register
369 *elementIndex = 0;
370 return "";
371 }
372
373 const GLenum glType = glVariableType(type);
374 const int numComponents = gl::UniformComponentCount(glType);
375
376 if (numComponents >= 4)
377 {
378 // no padding needed, HLSL will align the field to a new register
379 *elementIndex = 0;
380 return "";
381 }
382
383 if (*elementIndex + numComponents > 4)
384 {
385 // no padding needed, HLSL will align the field to a new register
386 *elementIndex = numComponents;
387 return "";
388 }
389
390 TString padding;
391
392 const int alignment = numComponents == 3 ? 4 : numComponents;
393 const int paddingOffset = (*elementIndex % alignment);
394
395 if (paddingOffset != 0)
396 {
397 // padding is neccessary
398 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
399 {
400 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
401 }
402
403 *elementIndex += (alignment - paddingOffset);
404 }
405
406 *elementIndex += numComponents;
407 *elementIndex %= 4;
408
409 return padding;
410}
411
Jamie Madille4075c92013-06-21 09:15:32 -0400412TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400413{
Jamie Madillc835df62013-06-21 09:15:32 -0400414 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400415 {
416 return "";
417 }
418
Jamie Madill574d9dd2013-06-20 11:55:56 -0400419 int numComponents = 0;
420
421 if (type.isMatrix())
422 {
Jamie Madille4075c92013-06-21 09:15:32 -0400423 // This method can also be called from structureString, which does not use layout qualifiers.
424 // Thus, use the method parameter for determining the matrix packing.
425 //
426 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
427 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
428 //
429 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400430 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400431 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
432 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400433 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400434 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400435 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400436 numComponents = mStd140StructElementIndexes[structName];
437
438 if (numComponents == 0)
439 {
440 return "";
441 }
Jamie Madillc835df62013-06-21 09:15:32 -0400442 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400443 else
444 {
Jamie Madillc835df62013-06-21 09:15:32 -0400445 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400446 numComponents = gl::UniformComponentCount(glType);
447 }
448
449 TString padding;
450 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
451 {
452 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
453 }
454 return padding;
455}
456
Jamie Madill440dc742013-06-20 11:55:55 -0400457// Use the same layout for packed and shared
458void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
459{
460 interfaceBlock->layout = newLayout;
461 interfaceBlock->blockInfo.clear();
462
463 switch (newLayout)
464 {
465 case BLOCKLAYOUT_SHARED:
466 case BLOCKLAYOUT_PACKED:
467 {
468 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400469 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400470 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
471 }
472 break;
473
474 case BLOCKLAYOUT_STANDARD:
475 {
476 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400477 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400478 interfaceBlock->dataSize = stdEncoder.getBlockSize();
479 }
480 break;
481
482 default:
483 UNREACHABLE();
484 break;
485 }
486}
487
Jamie Madill574d9dd2013-06-20 11:55:56 -0400488BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
489{
490 switch (blockStorage)
491 {
492 case EbsPacked: return BLOCKLAYOUT_PACKED;
493 case EbsShared: return BLOCKLAYOUT_SHARED;
494 case EbsStd140: return BLOCKLAYOUT_STANDARD;
495 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
496 }
497}
498
Jamie Madill98493dd2013-07-08 14:39:03 -0400499TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400500{
501 TString init;
502
503 TString preIndentString;
504 TString fullIndentString;
505
506 for (int spaces = 0; spaces < (indent * 4); spaces++)
507 {
508 preIndentString += ' ';
509 }
510
511 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
512 {
513 fullIndentString += ' ';
514 }
515
516 init += preIndentString + "{\n";
517
Jamie Madill98493dd2013-07-08 14:39:03 -0400518 const TFieldList &fields = structure.fields();
519 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400520 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400521 const TField &field = *fields[fieldIndex];
522 const TString &fieldName = rhsStructName + "." + decorate(field.name());
523 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400524
Jamie Madill98493dd2013-07-08 14:39:03 -0400525 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400526 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400527 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400528 }
529 else
530 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400531 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400532 }
533 }
534
535 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
536
537 return init;
538}
539
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000540void OutputHLSL::header()
541{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000542 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000543
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000544 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000545 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000546 TString varyings;
547 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400548 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000549
Jamie Madillc2141fb2013-08-30 13:21:08 -0400550 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000551 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400552 const TIntermSymbol &uniform = *uniformIt->second;
553 const TType &type = uniform.getType();
554 const TString &name = uniform.getSymbol();
555
556 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000557
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000558 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
559 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400560 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400561 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000562
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000563 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400564 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000565 }
566 else
567 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400568 const TStructure *structure = type.getStruct();
569 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
570
571 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
572
573 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000574 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000575 }
576
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000577 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
578 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000579 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400580 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
581 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000582
Jamie Madill98493dd2013-07-08 14:39:03 -0400583 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
584 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
585 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000586 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400587 const TField &field = *fieldList[typeIndex];
588 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400589 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000590 }
591
Jamie Madill98493dd2013-07-08 14:39:03 -0400592 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000593
Jamie Madill98493dd2013-07-08 14:39:03 -0400594 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
595 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700596
597 if (interfaceBlock.matrixPacking() == EmpRowMajor)
598 {
599 activeBlock.isRowMajorLayout = true;
600 }
601
Jamie Madill98493dd2013-07-08 14:39:03 -0400602 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000603
Jamie Madill98493dd2013-07-08 14:39:03 -0400604 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000605 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400606 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000607 }
608
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000609 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000610 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000611 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
612 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400613 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000614 }
615 }
616 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000617 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400618 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000619 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000620 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000621
Jamie Madill829f59e2013-11-13 19:40:54 -0500622 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400623 {
624 TIntermTyped *structNode = flaggedStructIt->first;
625 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400626 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400627 const TString &originalName = mFlaggedStructOriginalNames[structNode];
628
Jamie Madill98493dd2013-07-08 14:39:03 -0400629 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
630 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400631 flaggedStructs += "\n";
632 }
633
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000634 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
635 {
636 const TType &type = varying->second->getType();
637 const TString &name = varying->second->getSymbol();
638
639 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000640 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
641 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400642
Jamie Madill94599662013-08-30 13:21:10 -0400643 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000644 }
645
646 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
647 {
648 const TType &type = attribute->second->getType();
649 const TString &name = attribute->second->getSymbol();
650
651 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400652
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400653 Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
654 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
655 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000656 }
657
Jamie Madill529077d2013-06-20 11:55:54 -0400658 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
659 {
660 out << *structDeclaration;
661 }
662
663 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
664 {
665 out << *constructor;
666 }
667
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500668 if (mUsesDiscardRewriting)
669 {
670 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
671 }
672
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400673 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000675 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000676 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000677
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000678 out << "// Varyings\n";
679 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400680 out << "\n";
681
682 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000683 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500684 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000685 {
Jamie Madill46131a32013-06-20 11:55:50 -0400686 const TString &variableName = outputVariableIt->first;
687 const TType &variableType = outputVariableIt->second->getType();
688 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
689
690 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
691 " = " + initializer(variableType) + ";\n";
692
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400693 Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
694 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400695 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000696 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000697 }
Jamie Madill46131a32013-06-20 11:55:50 -0400698 else
699 {
700 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
701
702 out << "static float4 gl_Color[" << numColorValues << "] =\n"
703 "{\n";
704 for (unsigned int i = 0; i < numColorValues; i++)
705 {
706 out << " float4(0, 0, 0, 0)";
707 if (i + 1 != numColorValues)
708 {
709 out << ",";
710 }
711 out << "\n";
712 }
713
714 out << "};\n";
715 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000716
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400717 if (mUsesFragDepth)
718 {
719 out << "static float gl_Depth = 0.0;\n";
720 }
721
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000722 if (mUsesFragCoord)
723 {
724 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
725 }
726
727 if (mUsesPointCoord)
728 {
729 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
730 }
731
732 if (mUsesFrontFacing)
733 {
734 out << "static bool gl_FrontFacing = false;\n";
735 }
736
737 out << "\n";
738
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000739 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000740 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000741 out << "struct gl_DepthRangeParameters\n"
742 "{\n"
743 " float near;\n"
744 " float far;\n"
745 " float diff;\n"
746 "};\n"
747 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000748 }
749
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000750 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000751 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000752 out << "cbuffer DriverConstants : register(b1)\n"
753 "{\n";
754
755 if (mUsesDepthRange)
756 {
757 out << " float3 dx_DepthRange : packoffset(c0);\n";
758 }
759
760 if (mUsesFragCoord)
761 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000762 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000763 }
764
765 if (mUsesFragCoord || mUsesFrontFacing)
766 {
767 out << " float3 dx_DepthFront : packoffset(c2);\n";
768 }
769
770 out << "};\n";
771 }
772 else
773 {
774 if (mUsesDepthRange)
775 {
776 out << "uniform float3 dx_DepthRange : register(c0);";
777 }
778
779 if (mUsesFragCoord)
780 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000781 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000782 }
783
784 if (mUsesFragCoord || mUsesFrontFacing)
785 {
786 out << "uniform float3 dx_DepthFront : register(c2);\n";
787 }
788 }
789
790 out << "\n";
791
792 if (mUsesDepthRange)
793 {
794 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
795 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000796 }
797
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000799 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000800
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000801 if (!interfaceBlocks.empty())
802 {
803 out << interfaceBlocks;
804 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400805
806 if (!flaggedStructs.empty())
807 {
808 out << "// Std140 Structures accessed by value\n";
809 out << "\n";
810 out << flaggedStructs;
811 out << "\n";
812 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000813 }
814
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000815 if (usingMRTExtension && mNumRenderTargets > 1)
816 {
817 out << "#define GL_USES_MRT\n";
818 }
819
820 if (mUsesFragColor)
821 {
822 out << "#define GL_USES_FRAG_COLOR\n";
823 }
824
825 if (mUsesFragData)
826 {
827 out << "#define GL_USES_FRAG_DATA\n";
828 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000830 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000832 out << "// Attributes\n";
833 out << attributes;
834 out << "\n"
835 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
836
837 if (mUsesPointSize)
838 {
839 out << "static float gl_PointSize = float(1);\n";
840 }
841
842 out << "\n"
843 "// Varyings\n";
844 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000845 out << "\n";
846
847 if (mUsesDepthRange)
848 {
849 out << "struct gl_DepthRangeParameters\n"
850 "{\n"
851 " float near;\n"
852 " float far;\n"
853 " float diff;\n"
854 "};\n"
855 "\n";
856 }
857
858 if (mOutputType == SH_HLSL11_OUTPUT)
859 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000860 if (mUsesDepthRange)
861 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000862 out << "cbuffer DriverConstants : register(b1)\n"
863 "{\n"
864 " float3 dx_DepthRange : packoffset(c0);\n"
865 "};\n"
866 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000867 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000868 }
869 else
870 {
871 if (mUsesDepthRange)
872 {
873 out << "uniform float3 dx_DepthRange : register(c0);\n";
874 }
875
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000876 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000877 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000878 }
879
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000880 if (mUsesDepthRange)
881 {
882 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
883 "\n";
884 }
885
886 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000888
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000889 if (!interfaceBlocks.empty())
890 {
891 out << interfaceBlocks;
892 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400893
894 if (!flaggedStructs.empty())
895 {
896 out << "// Std140 Structures accessed by value\n";
897 out << "\n";
898 out << flaggedStructs;
899 out << "\n";
900 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000901 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400902 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000903
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400904 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
905 {
906 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400907 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000908 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400909 switch(textureFunction->sampler)
910 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400911 case EbtSampler2D: out << "int2 "; break;
912 case EbtSampler3D: out << "int3 "; break;
913 case EbtSamplerCube: out << "int2 "; break;
914 case EbtSampler2DArray: out << "int3 "; break;
915 case EbtISampler2D: out << "int2 "; break;
916 case EbtISampler3D: out << "int3 "; break;
917 case EbtISamplerCube: out << "int2 "; break;
918 case EbtISampler2DArray: out << "int3 "; break;
919 case EbtUSampler2D: out << "int2 "; break;
920 case EbtUSampler3D: out << "int3 "; break;
921 case EbtUSamplerCube: out << "int2 "; break;
922 case EbtUSampler2DArray: out << "int3 "; break;
923 case EbtSampler2DShadow: out << "int2 "; break;
924 case EbtSamplerCubeShadow: out << "int2 "; break;
925 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400926 default: UNREACHABLE();
927 }
928 }
929 else // Sampling function
930 {
931 switch(textureFunction->sampler)
932 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400933 case EbtSampler2D: out << "float4 "; break;
934 case EbtSampler3D: out << "float4 "; break;
935 case EbtSamplerCube: out << "float4 "; break;
936 case EbtSampler2DArray: out << "float4 "; break;
937 case EbtISampler2D: out << "int4 "; break;
938 case EbtISampler3D: out << "int4 "; break;
939 case EbtISamplerCube: out << "int4 "; break;
940 case EbtISampler2DArray: out << "int4 "; break;
941 case EbtUSampler2D: out << "uint4 "; break;
942 case EbtUSampler3D: out << "uint4 "; break;
943 case EbtUSamplerCube: out << "uint4 "; break;
944 case EbtUSampler2DArray: out << "uint4 "; break;
945 case EbtSampler2DShadow: out << "float "; break;
946 case EbtSamplerCubeShadow: out << "float "; break;
947 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400948 default: UNREACHABLE();
949 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000950 }
951
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400952 // Function name
953 out << textureFunction->name();
954
955 // Argument list
956 int hlslCoords = 4;
957
958 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000959 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400960 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000961 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400962 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
963 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
964 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000965 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400966
Nicolas Capens75fb4752013-07-10 15:14:47 -0400967 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000968 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400969 case TextureFunction::IMPLICIT: break;
970 case TextureFunction::BIAS: hlslCoords = 4; break;
971 case TextureFunction::LOD: hlslCoords = 4; break;
972 case TextureFunction::LOD0: hlslCoords = 4; break;
973 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000974 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400975 }
976 else if (mOutputType == SH_HLSL11_OUTPUT)
977 {
978 switch(textureFunction->sampler)
979 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400980 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
981 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
982 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
983 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
984 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
985 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
986 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
987 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
988 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
989 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
990 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
991 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
992 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
993 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
994 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400995 default: UNREACHABLE();
996 }
997 }
998 else UNREACHABLE();
999
1000 switch(textureFunction->coords)
1001 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001002 case 1: out << ", int lod"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001003 case 2: out << ", float2 t"; break;
1004 case 3: out << ", float3 t"; break;
1005 case 4: out << ", float4 t"; break;
1006 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001007 }
1008
Nicolas Capens75fb4752013-07-10 15:14:47 -04001009 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001010 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001011 case TextureFunction::IMPLICIT: break;
1012 case TextureFunction::BIAS: out << ", float bias"; break;
1013 case TextureFunction::LOD: out << ", float lod"; break;
1014 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001015 case TextureFunction::SIZE: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001016 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001017 }
1018
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001019 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001020 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001021
Nicolas Capens75fb4752013-07-10 15:14:47 -04001022 if (textureFunction->method == TextureFunction::SIZE)
1023 {
1024 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1025 {
1026 if (IsSamplerArray(textureFunction->sampler))
1027 {
1028 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1029 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1030 }
1031 else
1032 {
1033 out << " uint width; uint height; uint numberOfLevels;\n"
1034 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1035 }
1036 }
1037 else if (IsSampler3D(textureFunction->sampler))
1038 {
1039 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1040 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1041 }
1042 else UNREACHABLE();
1043
1044 switch(textureFunction->sampler)
1045 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001046 case EbtSampler2D: out << " return int2(width, height);"; break;
1047 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1048 case EbtSamplerCube: out << " return int2(width, height);"; break;
1049 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1050 case EbtISampler2D: out << " return int2(width, height);"; break;
1051 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1052 case EbtISamplerCube: out << " return int2(width, height);"; break;
1053 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1054 case EbtUSampler2D: out << " return int2(width, height);"; break;
1055 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1056 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1057 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1058 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1059 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1060 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001061 default: UNREACHABLE();
1062 }
1063 }
1064 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001065 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001066 // Currently unsupported because TextureCube does not support Load
1067 // This will require emulation using a Texture2DArray with 6 faces
1068 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001069 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001070 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001071 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001072 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001073 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001074 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001075 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001076 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001077 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001078 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001079 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001080 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001081 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001082 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001083 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001084 if (IsSamplerArray(textureFunction->sampler))
1085 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001086 out << " float width; float height; float layers; float levels;\n";
1087
1088 if (textureFunction->method == TextureFunction::LOD0)
1089 {
1090 out << " uint mip = 0;\n";
1091 }
1092 else
1093 {
1094 if (textureFunction->method == TextureFunction::IMPLICIT ||
1095 textureFunction->method == TextureFunction::BIAS)
1096 {
1097 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1098 " float2 tSized = float2(t.x * width, t.y * height);\n"
1099 " float dx = length(ddx(tSized));\n"
1100 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001101 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001102
1103 if (textureFunction->method == TextureFunction::BIAS)
1104 {
1105 out << " lod += bias;\n";
1106 }
1107 }
1108
1109 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1110 }
1111
1112 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001113 }
1114 else
1115 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001116 out << " float width; float height; float levels;\n";
1117
1118 if (textureFunction->method == TextureFunction::LOD0)
1119 {
1120 out << " uint mip = 0;\n";
1121 }
1122 else
1123 {
1124 if (textureFunction->method == TextureFunction::IMPLICIT ||
1125 textureFunction->method == TextureFunction::BIAS)
1126 {
1127 out << " x.GetDimensions(0, width, height, levels);\n"
1128 " float2 tSized = float2(t.x * width, t.y * height);\n"
1129 " float dx = length(ddx(tSized));\n"
1130 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001131 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001132
1133 if (textureFunction->method == TextureFunction::BIAS)
1134 {
1135 out << " lod += bias;\n";
1136 }
1137 }
1138
1139 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1140 }
1141
1142 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001143 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001144 }
1145 else if (IsSampler3D(textureFunction->sampler))
1146 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001147 out << " float width; float height; float depth; float levels;\n";
1148
1149 if (textureFunction->method == TextureFunction::LOD0)
1150 {
1151 out << " uint mip = 0;\n";
1152 }
1153 else
1154 {
1155 if (textureFunction->method == TextureFunction::IMPLICIT ||
1156 textureFunction->method == TextureFunction::BIAS)
1157 {
1158 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1159 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1160 " float dx = length(ddx(tSized));\n"
1161 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001162 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001163
1164 if (textureFunction->method == TextureFunction::BIAS)
1165 {
1166 out << " lod += bias;\n";
1167 }
1168 }
1169
1170 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1171 }
1172
1173 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001174 }
1175 else UNREACHABLE();
1176 }
1177
1178 out << " return ";
1179
1180 // HLSL intrinsic
1181 if (mOutputType == SH_HLSL9_OUTPUT)
1182 {
1183 switch(textureFunction->sampler)
1184 {
1185 case EbtSampler2D: out << "tex2D"; break;
1186 case EbtSamplerCube: out << "texCUBE"; break;
1187 default: UNREACHABLE();
1188 }
1189
Nicolas Capens75fb4752013-07-10 15:14:47 -04001190 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001191 {
1192 case TextureFunction::IMPLICIT: out << "(s, "; break;
1193 case TextureFunction::BIAS: out << "bias(s, "; break;
1194 case TextureFunction::LOD: out << "lod(s, "; break;
1195 case TextureFunction::LOD0: out << "lod(s, "; break;
1196 default: UNREACHABLE();
1197 }
1198 }
1199 else if (mOutputType == SH_HLSL11_OUTPUT)
1200 {
1201 if (IsIntegerSampler(textureFunction->sampler))
1202 {
1203 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001204 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001205 else if (IsShadowSampler(textureFunction->sampler))
1206 {
1207 out << "x.SampleCmp(s, ";
1208 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001209 else
1210 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001211 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001212 {
1213 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1214 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1215 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1216 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1217 default: UNREACHABLE();
1218 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001219 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001220 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001221 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001222
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001223 // Integer sampling requires integer addresses
1224 TString addressx = "";
1225 TString addressy = "";
1226 TString addressz = "";
1227 TString close = "";
1228
1229 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001230 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001231 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001232 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001233 case 2: out << "int3("; break;
1234 case 3: out << "int4("; break;
1235 default: UNREACHABLE();
1236 }
1237
Nicolas Capensc98406a2013-07-10 14:52:44 -04001238 addressx = "int(floor(width * frac((";
1239 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001240
1241 if (IsSamplerArray(textureFunction->sampler))
1242 {
1243 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1244 }
1245 else
1246 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001247 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001248 }
1249
1250 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001251 }
1252 else
1253 {
1254 switch(hlslCoords)
1255 {
1256 case 2: out << "float2("; break;
1257 case 3: out << "float3("; break;
1258 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001259 default: UNREACHABLE();
1260 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001261 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001262
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001263 TString proj = ""; // Only used for projected textures
1264
1265 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001266 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001267 switch(textureFunction->coords)
1268 {
1269 case 3: proj = " / t.z"; break;
1270 case 4: proj = " / t.w"; break;
1271 default: UNREACHABLE();
1272 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001273 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001274
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001275 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001276
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001277 if (mOutputType == SH_HLSL9_OUTPUT)
1278 {
1279 if (hlslCoords >= 3)
1280 {
1281 if (textureFunction->coords < 3)
1282 {
1283 out << ", 0";
1284 }
1285 else
1286 {
1287 out << ", t.z" + proj;
1288 }
1289 }
1290
1291 if (hlslCoords == 4)
1292 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001293 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001294 {
1295 case TextureFunction::BIAS: out << ", bias"; break;
1296 case TextureFunction::LOD: out << ", lod"; break;
1297 case TextureFunction::LOD0: out << ", 0"; break;
1298 default: UNREACHABLE();
1299 }
1300 }
1301
1302 out << "));\n";
1303 }
1304 else if (mOutputType == SH_HLSL11_OUTPUT)
1305 {
1306 if (hlslCoords >= 3)
1307 {
1308 out << ", " + addressz + ("t.z" + proj) + close;
1309 }
1310
Nicolas Capenscb127d32013-07-15 17:26:18 -04001311 if (IsIntegerSampler(textureFunction->sampler))
1312 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001313 out << ", mip));";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001314 }
1315 else if (IsShadowSampler(textureFunction->sampler))
1316 {
1317 // Compare value
1318 switch(textureFunction->coords)
1319 {
1320 case 3: out << "), t.z);"; break;
1321 case 4: out << "), t.w);"; break;
1322 default: UNREACHABLE();
1323 }
1324 }
1325 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001326 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001327 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001328 {
1329 case TextureFunction::IMPLICIT: out << "));"; break;
1330 case TextureFunction::BIAS: out << "), bias);"; break;
1331 case TextureFunction::LOD: out << "), lod);"; break;
1332 case TextureFunction::LOD0: out << "), 0);"; break;
1333 default: UNREACHABLE();
1334 }
1335 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001336 }
1337 else UNREACHABLE();
1338 }
1339
1340 out << "\n"
1341 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001342 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001343 }
1344
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001345 if (mUsesFragCoord)
1346 {
1347 out << "#define GL_USES_FRAG_COORD\n";
1348 }
1349
1350 if (mUsesPointCoord)
1351 {
1352 out << "#define GL_USES_POINT_COORD\n";
1353 }
1354
1355 if (mUsesFrontFacing)
1356 {
1357 out << "#define GL_USES_FRONT_FACING\n";
1358 }
1359
1360 if (mUsesPointSize)
1361 {
1362 out << "#define GL_USES_POINT_SIZE\n";
1363 }
1364
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001365 if (mUsesFragDepth)
1366 {
1367 out << "#define GL_USES_FRAG_DEPTH\n";
1368 }
1369
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001370 if (mUsesDepthRange)
1371 {
1372 out << "#define GL_USES_DEPTH_RANGE\n";
1373 }
1374
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001375 if (mUsesXor)
1376 {
1377 out << "bool xor(bool p, bool q)\n"
1378 "{\n"
1379 " return (p || q) && !(p && q);\n"
1380 "}\n"
1381 "\n";
1382 }
1383
1384 if (mUsesMod1)
1385 {
1386 out << "float mod(float x, float y)\n"
1387 "{\n"
1388 " return x - y * floor(x / y);\n"
1389 "}\n"
1390 "\n";
1391 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001392
1393 if (mUsesMod2v)
1394 {
1395 out << "float2 mod(float2 x, float2 y)\n"
1396 "{\n"
1397 " return x - y * floor(x / y);\n"
1398 "}\n"
1399 "\n";
1400 }
1401
1402 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001403 {
1404 out << "float2 mod(float2 x, float y)\n"
1405 "{\n"
1406 " return x - y * floor(x / y);\n"
1407 "}\n"
1408 "\n";
1409 }
1410
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001411 if (mUsesMod3v)
1412 {
1413 out << "float3 mod(float3 x, float3 y)\n"
1414 "{\n"
1415 " return x - y * floor(x / y);\n"
1416 "}\n"
1417 "\n";
1418 }
1419
1420 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001421 {
1422 out << "float3 mod(float3 x, float y)\n"
1423 "{\n"
1424 " return x - y * floor(x / y);\n"
1425 "}\n"
1426 "\n";
1427 }
1428
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001429 if (mUsesMod4v)
1430 {
1431 out << "float4 mod(float4 x, float4 y)\n"
1432 "{\n"
1433 " return x - y * floor(x / y);\n"
1434 "}\n"
1435 "\n";
1436 }
1437
1438 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001439 {
1440 out << "float4 mod(float4 x, float y)\n"
1441 "{\n"
1442 " return x - y * floor(x / y);\n"
1443 "}\n"
1444 "\n";
1445 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001446
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001447 if (mUsesFaceforward1)
1448 {
1449 out << "float faceforward(float N, float I, float Nref)\n"
1450 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001451 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001452 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001453 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001454 " }\n"
1455 " else\n"
1456 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001457 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001458 " }\n"
1459 "}\n"
1460 "\n";
1461 }
1462
1463 if (mUsesFaceforward2)
1464 {
1465 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1466 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001467 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001468 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001469 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001470 " }\n"
1471 " else\n"
1472 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001473 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001474 " }\n"
1475 "}\n"
1476 "\n";
1477 }
1478
1479 if (mUsesFaceforward3)
1480 {
1481 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1482 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001483 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001484 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001485 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001486 " }\n"
1487 " else\n"
1488 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001489 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001490 " }\n"
1491 "}\n"
1492 "\n";
1493 }
1494
1495 if (mUsesFaceforward4)
1496 {
1497 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1498 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001499 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001500 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001501 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001502 " }\n"
1503 " else\n"
1504 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001505 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001506 " }\n"
1507 "}\n"
1508 "\n";
1509 }
1510
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001511 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001512 {
1513 out << "float atanyx(float y, float x)\n"
1514 "{\n"
1515 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1516 " return atan2(y, x);\n"
1517 "}\n";
1518 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001519
1520 if (mUsesAtan2_2)
1521 {
1522 out << "float2 atanyx(float2 y, float2 x)\n"
1523 "{\n"
1524 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1525 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1526 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1527 "}\n";
1528 }
1529
1530 if (mUsesAtan2_3)
1531 {
1532 out << "float3 atanyx(float3 y, float3 x)\n"
1533 "{\n"
1534 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1535 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1536 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1537 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1538 "}\n";
1539 }
1540
1541 if (mUsesAtan2_4)
1542 {
1543 out << "float4 atanyx(float4 y, float4 x)\n"
1544 "{\n"
1545 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1546 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1547 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1548 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1549 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1550 "}\n";
1551 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001552}
1553
1554void OutputHLSL::visitSymbol(TIntermSymbol *node)
1555{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001556 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001557
Jamie Madill570e04d2013-06-21 09:15:33 -04001558 // Handle accessing std140 structs by value
1559 if (mFlaggedStructMappedNames.count(node) > 0)
1560 {
1561 out << mFlaggedStructMappedNames[node];
1562 return;
1563 }
1564
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001565 TString name = node->getSymbol();
1566
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001567 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001568 {
1569 mUsesDepthRange = true;
1570 out << name;
1571 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001572 else
1573 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001574 TQualifier qualifier = node->getQualifier();
1575
1576 if (qualifier == EvqUniform)
1577 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001578 const TType& nodeType = node->getType();
1579 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1580
1581 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001582 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001583 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001584 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001585 else
1586 {
1587 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001588 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001589
1590 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001591 }
Jamie Madill19571812013-08-12 15:26:34 -07001592 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001593 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001594 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001595 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001596 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001597 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001598 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001599 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001600 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001601 }
Jamie Madill19571812013-08-12 15:26:34 -07001602 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001603 {
1604 mReferencedOutputVariables[name] = node;
1605 out << "out_" << name;
1606 }
1607 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001608 {
1609 out << "gl_Color[0]";
1610 mUsesFragColor = true;
1611 }
1612 else if (qualifier == EvqFragData)
1613 {
1614 out << "gl_Color";
1615 mUsesFragData = true;
1616 }
1617 else if (qualifier == EvqFragCoord)
1618 {
1619 mUsesFragCoord = true;
1620 out << name;
1621 }
1622 else if (qualifier == EvqPointCoord)
1623 {
1624 mUsesPointCoord = true;
1625 out << name;
1626 }
1627 else if (qualifier == EvqFrontFacing)
1628 {
1629 mUsesFrontFacing = true;
1630 out << name;
1631 }
1632 else if (qualifier == EvqPointSize)
1633 {
1634 mUsesPointSize = true;
1635 out << name;
1636 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001637 else if (name == "gl_FragDepthEXT")
1638 {
1639 mUsesFragDepth = true;
1640 out << "gl_Depth";
1641 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001642 else
1643 {
1644 out << decorate(name);
1645 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646 }
1647}
1648
1649bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1650{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001651 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001652
Jamie Madill570e04d2013-06-21 09:15:33 -04001653 // Handle accessing std140 structs by value
1654 if (mFlaggedStructMappedNames.count(node) > 0)
1655 {
1656 out << mFlaggedStructMappedNames[node];
1657 return false;
1658 }
1659
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001660 switch (node->getOp())
1661 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001662 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001663 case EOpInitialize:
1664 if (visit == PreVisit)
1665 {
1666 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1667 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1668 // new variable is created before the assignment is evaluated), so we need to convert
1669 // this to "float t = x, x = t;".
1670
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001671 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1672 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001673
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001674 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1675 expression->traverse(&searchSymbol);
1676 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001677
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001678 if (sameSymbol)
1679 {
1680 // Type already printed
1681 out << "t" + str(mUniqueIndex) + " = ";
1682 expression->traverse(this);
1683 out << ", ";
1684 symbolNode->traverse(this);
1685 out << " = t" + str(mUniqueIndex);
1686
1687 mUniqueIndex++;
1688 return false;
1689 }
1690 }
1691 else if (visit == InVisit)
1692 {
1693 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001694 }
1695 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001696 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1697 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1698 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1699 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1700 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1701 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001702 if (visit == PreVisit)
1703 {
1704 out << "(";
1705 }
1706 else if (visit == InVisit)
1707 {
1708 out << " = mul(";
1709 node->getLeft()->traverse(this);
1710 out << ", transpose(";
1711 }
1712 else
1713 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001714 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001715 }
1716 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001717 case EOpMatrixTimesMatrixAssign:
1718 if (visit == PreVisit)
1719 {
1720 out << "(";
1721 }
1722 else if (visit == InVisit)
1723 {
1724 out << " = mul(";
1725 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001726 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001727 }
1728 else
1729 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001730 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001731 }
1732 break;
1733 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001734 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001735 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001736 const TType& leftType = node->getLeft()->getType();
1737 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001738 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001739 if (visit == PreVisit)
1740 {
1741 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1742 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1743
1744 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1745 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1746
1747 return false;
1748 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001749 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001750 else
1751 {
1752 outputTriplet(visit, "", "[", "]");
1753 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001754 }
1755 break;
1756 case EOpIndexIndirect:
1757 // We do not currently support indirect references to interface blocks
1758 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1759 outputTriplet(visit, "", "[", "]");
1760 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001761 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001762 if (visit == InVisit)
1763 {
1764 const TStructure* structure = node->getLeft()->getType().getStruct();
1765 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1766 const TField* field = structure->fields()[index->getIConst(0)];
1767 out << "." + decorateField(field->name(), *structure);
1768
1769 return false;
1770 }
1771 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001772 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001773 if (visit == InVisit)
1774 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001775 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1776 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1777 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1778 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001779
1780 return false;
1781 }
1782 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783 case EOpVectorSwizzle:
1784 if (visit == InVisit)
1785 {
1786 out << ".";
1787
1788 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1789
1790 if (swizzle)
1791 {
1792 TIntermSequence &sequence = swizzle->getSequence();
1793
1794 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1795 {
1796 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1797
1798 if (element)
1799 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001800 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001801
1802 switch (i)
1803 {
1804 case 0: out << "x"; break;
1805 case 1: out << "y"; break;
1806 case 2: out << "z"; break;
1807 case 3: out << "w"; break;
1808 default: UNREACHABLE();
1809 }
1810 }
1811 else UNREACHABLE();
1812 }
1813 }
1814 else UNREACHABLE();
1815
1816 return false; // Fully processed
1817 }
1818 break;
1819 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1820 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1821 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1822 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001823 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001824 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001825 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001826 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001827 if (node->getOp() == EOpEqual)
1828 {
1829 outputTriplet(visit, "(", " == ", ")");
1830 }
1831 else
1832 {
1833 outputTriplet(visit, "(", " != ", ")");
1834 }
1835 }
1836 else if (node->getLeft()->getBasicType() == EbtStruct)
1837 {
1838 if (node->getOp() == EOpEqual)
1839 {
1840 out << "(";
1841 }
1842 else
1843 {
1844 out << "!(";
1845 }
1846
Jamie Madill98493dd2013-07-08 14:39:03 -04001847 const TStructure &structure = *node->getLeft()->getType().getStruct();
1848 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001849
Jamie Madill98493dd2013-07-08 14:39:03 -04001850 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001851 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001852 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001853
1854 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001855 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001856 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001857 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001858
Jamie Madill98493dd2013-07-08 14:39:03 -04001859 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001860 {
1861 out << " && ";
1862 }
1863 }
1864
1865 out << ")";
1866
1867 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001868 }
1869 else
1870 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001871 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001872
1873 if (node->getOp() == EOpEqual)
1874 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001875 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001876 }
1877 else
1878 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001879 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001880 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001881 }
1882 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1884 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1885 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1886 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1887 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001888 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001889 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1890 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001891 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001892 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001893 if (node->getRight()->hasSideEffects())
1894 {
1895 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1896 return false;
1897 }
1898 else
1899 {
1900 outputTriplet(visit, "(", " || ", ")");
1901 return true;
1902 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001903 case EOpLogicalXor:
1904 mUsesXor = true;
1905 outputTriplet(visit, "xor(", ", ", ")");
1906 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001907 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001908 if (node->getRight()->hasSideEffects())
1909 {
1910 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1911 return false;
1912 }
1913 else
1914 {
1915 outputTriplet(visit, "(", " && ", ")");
1916 return true;
1917 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 default: UNREACHABLE();
1919 }
1920
1921 return true;
1922}
1923
1924bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1925{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 switch (node->getOp())
1927 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001928 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1929 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1930 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1931 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1932 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1933 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1934 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001936 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937 case EOpConvFloatToBool:
1938 switch (node->getOperand()->getType().getNominalSize())
1939 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001940 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1941 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1942 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1943 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944 default: UNREACHABLE();
1945 }
1946 break;
1947 case EOpConvBoolToFloat:
1948 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001949 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001950 switch (node->getOperand()->getType().getNominalSize())
1951 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001952 case 1: outputTriplet(visit, "float(", "", ")"); break;
1953 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1954 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1955 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001956 default: UNREACHABLE();
1957 }
1958 break;
1959 case EOpConvFloatToInt:
1960 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001961 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962 switch (node->getOperand()->getType().getNominalSize())
1963 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001964 case 1: outputTriplet(visit, "int(", "", ")"); break;
1965 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1966 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1967 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968 default: UNREACHABLE();
1969 }
1970 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001971 case EOpConvFloatToUInt:
1972 case EOpConvBoolToUInt:
1973 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04001974 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001975 {
1976 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001977 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1978 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1979 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001980 default: UNREACHABLE();
1981 }
1982 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001983 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1984 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1985 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1986 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1987 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1988 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1989 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1990 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1991 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1992 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1993 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1994 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1995 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1996 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1997 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1998 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1999 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2000 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2001 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2002 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2003 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002004 case EOpDFdx:
2005 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2006 {
2007 outputTriplet(visit, "(", "", ", 0.0)");
2008 }
2009 else
2010 {
2011 outputTriplet(visit, "ddx(", "", ")");
2012 }
2013 break;
2014 case EOpDFdy:
2015 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2016 {
2017 outputTriplet(visit, "(", "", ", 0.0)");
2018 }
2019 else
2020 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002021 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002022 }
2023 break;
2024 case EOpFwidth:
2025 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2026 {
2027 outputTriplet(visit, "(", "", ", 0.0)");
2028 }
2029 else
2030 {
2031 outputTriplet(visit, "fwidth(", "", ")");
2032 }
2033 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002034 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2035 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002036 default: UNREACHABLE();
2037 }
2038
2039 return true;
2040}
2041
2042bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2043{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002044 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002045
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046 switch (node->getOp())
2047 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002048 case EOpSequence:
2049 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002050 if (mInsideFunction)
2051 {
Jamie Madill075edd82013-07-08 13:30:19 -04002052 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002053 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002054
2055 mScopeDepth++;
2056
2057 if (mScopeBracket.size() < mScopeDepth)
2058 {
2059 mScopeBracket.push_back(0); // New scope level
2060 }
2061 else
2062 {
2063 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2064 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002065 }
2066
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002067 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2068 {
Jamie Madill075edd82013-07-08 13:30:19 -04002069 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002070
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002071 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002072
2073 out << ";\n";
2074 }
2075
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002076 if (mInsideFunction)
2077 {
Jamie Madill075edd82013-07-08 13:30:19 -04002078 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002079 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002080
2081 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002082 }
2083
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002084 return false;
2085 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 case EOpDeclaration:
2087 if (visit == PreVisit)
2088 {
2089 TIntermSequence &sequence = node->getSequence();
2090 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002091
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002092 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002094 if (variable->getType().getStruct())
2095 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002096 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002097 }
2098
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002099 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002101 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002102 {
2103 out << "static ";
2104 }
2105
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002106 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002108 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002110 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002112 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002114 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002115 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002116 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002117 }
2118 else
2119 {
2120 (*sit)->traverse(this);
2121 }
2122
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002123 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002124 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002125 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126 }
2127 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002129 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2130 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002131 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002132 }
2133 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002135 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002136 {
2137 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2138 {
2139 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2140
2141 if (symbol)
2142 {
2143 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2144 mReferencedVaryings[symbol->getSymbol()] = symbol;
2145 }
2146 else
2147 {
2148 (*sit)->traverse(this);
2149 }
2150 }
2151 }
2152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002153 return false;
2154 }
2155 else if (visit == InVisit)
2156 {
2157 out << ", ";
2158 }
2159 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002160 case EOpPrototype:
2161 if (visit == PreVisit)
2162 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002163 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002164
2165 TIntermSequence &arguments = node->getSequence();
2166
2167 for (unsigned int i = 0; i < arguments.size(); i++)
2168 {
2169 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2170
2171 if (symbol)
2172 {
2173 out << argumentString(symbol);
2174
2175 if (i < arguments.size() - 1)
2176 {
2177 out << ", ";
2178 }
2179 }
2180 else UNREACHABLE();
2181 }
2182
2183 out << ");\n";
2184
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002185 // Also prototype the Lod0 variant if needed
2186 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2187 {
2188 mOutputLod0Function = true;
2189 node->traverse(this);
2190 mOutputLod0Function = false;
2191 }
2192
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002193 return false;
2194 }
2195 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002196 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002197 case EOpFunction:
2198 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002199 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002200
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002201 out << typeString(node->getType()) << " ";
2202
2203 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002204 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002205 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002207 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002208 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002209 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002210 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002211
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002212 TIntermSequence &sequence = node->getSequence();
2213 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2214
2215 for (unsigned int i = 0; i < arguments.size(); i++)
2216 {
2217 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2218
2219 if (symbol)
2220 {
2221 if (symbol->getType().getStruct())
2222 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002223 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002224 }
2225
2226 out << argumentString(symbol);
2227
2228 if (i < arguments.size() - 1)
2229 {
2230 out << ", ";
2231 }
2232 }
2233 else UNREACHABLE();
2234 }
2235
2236 out << ")\n"
2237 "{\n";
2238
2239 if (sequence.size() > 1)
2240 {
2241 mInsideFunction = true;
2242 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002243 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002245
2246 out << "}\n";
2247
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002248 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2249 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002250 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002251 {
2252 mOutputLod0Function = true;
2253 node->traverse(this);
2254 mOutputLod0Function = false;
2255 }
2256 }
2257
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002258 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 }
2260 break;
2261 case EOpFunctionCall:
2262 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002263 TString name = TFunction::unmangleName(node->getName());
2264 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002265 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002266
2267 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002268 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002269 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002270 }
2271 else
2272 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002273 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2274
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002275 TextureFunction textureFunction;
2276 textureFunction.sampler = samplerType;
2277 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002278 textureFunction.method = TextureFunction::IMPLICIT;
2279 textureFunction.proj = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002280
2281 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002282 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002283 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002284 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002285 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002286 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002287 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002288 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002289 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002290 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002291 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002292 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002293 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002294 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002295 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002296 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002297 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002298 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002299 else if (name == "textureSize")
2300 {
2301 textureFunction.method = TextureFunction::SIZE;
2302 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002303 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002304
Nicolas Capens75fb4752013-07-10 15:14:47 -04002305 if (textureFunction.method != TextureFunction::LOD &&
2306 textureFunction.method != TextureFunction::SIZE)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002307 {
2308 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2309 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002310 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002311 }
2312 else if (arguments.size() == 3)
2313 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002314 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002315 }
2316 }
2317
2318 mUsesTexture.insert(textureFunction);
2319
2320 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002322
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002323 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2324 {
2325 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2326 {
2327 out << "texture_";
2328 (*arg)->traverse(this);
2329 out << ", sampler_";
2330 }
2331
2332 (*arg)->traverse(this);
2333
2334 if (arg < arguments.end() - 1)
2335 {
2336 out << ", ";
2337 }
2338 }
2339
2340 out << ")";
2341
2342 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002343 }
2344 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002345 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002346 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002347 addConstructor(node->getType(), "vec1", &node->getSequence());
2348 outputTriplet(visit, "vec1(", "", ")");
2349 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002350 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002351 addConstructor(node->getType(), "vec2", &node->getSequence());
2352 outputTriplet(visit, "vec2(", ", ", ")");
2353 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002354 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002355 addConstructor(node->getType(), "vec3", &node->getSequence());
2356 outputTriplet(visit, "vec3(", ", ", ")");
2357 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002358 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002359 addConstructor(node->getType(), "vec4", &node->getSequence());
2360 outputTriplet(visit, "vec4(", ", ", ")");
2361 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002362 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002363 addConstructor(node->getType(), "bvec1", &node->getSequence());
2364 outputTriplet(visit, "bvec1(", "", ")");
2365 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002366 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002367 addConstructor(node->getType(), "bvec2", &node->getSequence());
2368 outputTriplet(visit, "bvec2(", ", ", ")");
2369 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002370 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002371 addConstructor(node->getType(), "bvec3", &node->getSequence());
2372 outputTriplet(visit, "bvec3(", ", ", ")");
2373 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002374 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002375 addConstructor(node->getType(), "bvec4", &node->getSequence());
2376 outputTriplet(visit, "bvec4(", ", ", ")");
2377 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002378 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002379 addConstructor(node->getType(), "ivec1", &node->getSequence());
2380 outputTriplet(visit, "ivec1(", "", ")");
2381 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002382 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002383 addConstructor(node->getType(), "ivec2", &node->getSequence());
2384 outputTriplet(visit, "ivec2(", ", ", ")");
2385 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002386 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002387 addConstructor(node->getType(), "ivec3", &node->getSequence());
2388 outputTriplet(visit, "ivec3(", ", ", ")");
2389 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002390 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002391 addConstructor(node->getType(), "ivec4", &node->getSequence());
2392 outputTriplet(visit, "ivec4(", ", ", ")");
2393 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002394 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002395 addConstructor(node->getType(), "uvec1", &node->getSequence());
2396 outputTriplet(visit, "uvec1(", "", ")");
2397 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002398 case EOpConstructUVec2:
2399 addConstructor(node->getType(), "uvec2", &node->getSequence());
2400 outputTriplet(visit, "uvec2(", ", ", ")");
2401 break;
2402 case EOpConstructUVec3:
2403 addConstructor(node->getType(), "uvec3", &node->getSequence());
2404 outputTriplet(visit, "uvec3(", ", ", ")");
2405 break;
2406 case EOpConstructUVec4:
2407 addConstructor(node->getType(), "uvec4", &node->getSequence());
2408 outputTriplet(visit, "uvec4(", ", ", ")");
2409 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002410 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002411 addConstructor(node->getType(), "mat2", &node->getSequence());
2412 outputTriplet(visit, "mat2(", ", ", ")");
2413 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002414 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002415 addConstructor(node->getType(), "mat3", &node->getSequence());
2416 outputTriplet(visit, "mat3(", ", ", ")");
2417 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002418 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002419 addConstructor(node->getType(), "mat4", &node->getSequence());
2420 outputTriplet(visit, "mat4(", ", ", ")");
2421 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002422 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002423 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2424 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002425 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002426 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2427 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2428 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2429 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2430 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2431 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002432 case EOpMod:
2433 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002434 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002435 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2436 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2437 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002438 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002439 case 11: mUsesMod1 = true; break;
2440 case 22: mUsesMod2v = true; break;
2441 case 21: mUsesMod2f = true; break;
2442 case 33: mUsesMod3v = true; break;
2443 case 31: mUsesMod3f = true; break;
2444 case 44: mUsesMod4v = true; break;
2445 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002446 default: UNREACHABLE();
2447 }
2448
2449 outputTriplet(visit, "mod(", ", ", ")");
2450 }
2451 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002452 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002453 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002454 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002455 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2456 {
2457 case 1: mUsesAtan2_1 = true; break;
2458 case 2: mUsesAtan2_2 = true; break;
2459 case 3: mUsesAtan2_3 = true; break;
2460 case 4: mUsesAtan2_4 = true; break;
2461 default: UNREACHABLE();
2462 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002463 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002464 break;
2465 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2466 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2467 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2468 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2469 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2470 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2471 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2472 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2473 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002474 case EOpFaceForward:
2475 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002476 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002477 {
2478 case 1: mUsesFaceforward1 = true; break;
2479 case 2: mUsesFaceforward2 = true; break;
2480 case 3: mUsesFaceforward3 = true; break;
2481 case 4: mUsesFaceforward4 = true; break;
2482 default: UNREACHABLE();
2483 }
2484
2485 outputTriplet(visit, "faceforward(", ", ", ")");
2486 }
2487 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2489 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2490 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491 default: UNREACHABLE();
2492 }
2493
2494 return true;
2495}
2496
2497bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2498{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002499 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002500
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002501 if (node->usesTernaryOperator())
2502 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002503 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002504 }
2505 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002506 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002507 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002508
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002509 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002510
2511 node->getCondition()->traverse(this);
2512
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002513 out << ")\n";
2514
Jamie Madill075edd82013-07-08 13:30:19 -04002515 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002516 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002517
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002518 bool discard = false;
2519
daniel@transgaming.combb885322010-04-15 20:45:24 +00002520 if (node->getTrueBlock())
2521 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002522 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002523
2524 // Detect true discard
2525 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002526 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002527
Jamie Madill075edd82013-07-08 13:30:19 -04002528 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002529 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002530
2531 if (node->getFalseBlock())
2532 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002533 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002534
Jamie Madill075edd82013-07-08 13:30:19 -04002535 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002536 out << "{\n";
2537
Jamie Madill075edd82013-07-08 13:30:19 -04002538 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002539 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002540
Jamie Madill075edd82013-07-08 13:30:19 -04002541 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002542 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002543
2544 // Detect false discard
2545 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2546 }
2547
2548 // ANGLE issue 486: Detect problematic conditional discard
2549 if (discard && FindSideEffectRewriting::search(node))
2550 {
2551 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002552 }
2553 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002554
2555 return false;
2556}
2557
2558void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2559{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002560 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561}
2562
2563bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2564{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002565 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2566
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002567 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002568 {
2569 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2570 }
2571
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002572 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002573 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002574 if (handleExcessiveLoop(node))
2575 {
2576 return false;
2577 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002578 }
2579
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002580 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002581
alokp@chromium.org52813552010-11-16 18:36:09 +00002582 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002583 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002584 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002585
Jamie Madill075edd82013-07-08 13:30:19 -04002586 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002587 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002588 }
2589 else
2590 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002591 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002592
2593 if (node->getInit())
2594 {
2595 node->getInit()->traverse(this);
2596 }
2597
2598 out << "; ";
2599
alokp@chromium.org52813552010-11-16 18:36:09 +00002600 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002601 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002602 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002603 }
2604
2605 out << "; ";
2606
alokp@chromium.org52813552010-11-16 18:36:09 +00002607 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002608 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002609 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002610 }
2611
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002612 out << ")\n";
2613
Jamie Madill075edd82013-07-08 13:30:19 -04002614 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002615 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 }
2617
2618 if (node->getBody())
2619 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002620 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621 }
2622
Jamie Madill075edd82013-07-08 13:30:19 -04002623 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002624 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002625
alokp@chromium.org52813552010-11-16 18:36:09 +00002626 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002627 {
Jamie Madill075edd82013-07-08 13:30:19 -04002628 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 out << "while(\n";
2630
alokp@chromium.org52813552010-11-16 18:36:09 +00002631 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002632
daniel@transgaming.com73536982012-03-21 20:45:49 +00002633 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634 }
2635
daniel@transgaming.com73536982012-03-21 20:45:49 +00002636 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002637
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002638 mInsideDiscontinuousLoop = wasDiscontinuous;
2639
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002640 return false;
2641}
2642
2643bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2644{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002645 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646
2647 switch (node->getFlowOp())
2648 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002649 case EOpKill:
2650 outputTriplet(visit, "discard;\n", "", "");
2651 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002652 case EOpBreak:
2653 if (visit == PreVisit)
2654 {
2655 if (mExcessiveLoopIndex)
2656 {
2657 out << "{Break";
2658 mExcessiveLoopIndex->traverse(this);
2659 out << " = true; break;}\n";
2660 }
2661 else
2662 {
2663 out << "break;\n";
2664 }
2665 }
2666 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002667 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002668 case EOpReturn:
2669 if (visit == PreVisit)
2670 {
2671 if (node->getExpression())
2672 {
2673 out << "return ";
2674 }
2675 else
2676 {
2677 out << "return;\n";
2678 }
2679 }
2680 else if (visit == PostVisit)
2681 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002682 if (node->getExpression())
2683 {
2684 out << ";\n";
2685 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686 }
2687 break;
2688 default: UNREACHABLE();
2689 }
2690
2691 return true;
2692}
2693
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002694void OutputHLSL::traverseStatements(TIntermNode *node)
2695{
2696 if (isSingleStatement(node))
2697 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002698 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002699 }
2700
2701 node->traverse(this);
2702}
2703
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002704bool OutputHLSL::isSingleStatement(TIntermNode *node)
2705{
2706 TIntermAggregate *aggregate = node->getAsAggregate();
2707
2708 if (aggregate)
2709 {
2710 if (aggregate->getOp() == EOpSequence)
2711 {
2712 return false;
2713 }
2714 else
2715 {
2716 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2717 {
2718 if (!isSingleStatement(*sit))
2719 {
2720 return false;
2721 }
2722 }
2723
2724 return true;
2725 }
2726 }
2727
2728 return true;
2729}
2730
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002731// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2732// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002733bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2734{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002735 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002736 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002737
2738 // Parse loops of the form:
2739 // for(int index = initial; index [comparator] limit; index += increment)
2740 TIntermSymbol *index = NULL;
2741 TOperator comparator = EOpNull;
2742 int initial = 0;
2743 int limit = 0;
2744 int increment = 0;
2745
2746 // Parse index name and intial value
2747 if (node->getInit())
2748 {
2749 TIntermAggregate *init = node->getInit()->getAsAggregate();
2750
2751 if (init)
2752 {
2753 TIntermSequence &sequence = init->getSequence();
2754 TIntermTyped *variable = sequence[0]->getAsTyped();
2755
2756 if (variable && variable->getQualifier() == EvqTemporary)
2757 {
2758 TIntermBinary *assign = variable->getAsBinaryNode();
2759
2760 if (assign->getOp() == EOpInitialize)
2761 {
2762 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2763 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2764
2765 if (symbol && constant)
2766 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002767 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002768 {
2769 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002770 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002771 }
2772 }
2773 }
2774 }
2775 }
2776 }
2777
2778 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002779 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002780 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002781 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002782
2783 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2784 {
2785 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2786
2787 if (constant)
2788 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002789 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002790 {
2791 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002792 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002793 }
2794 }
2795 }
2796 }
2797
2798 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002799 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002800 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002801 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2802 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002803
2804 if (binaryTerminal)
2805 {
2806 TOperator op = binaryTerminal->getOp();
2807 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2808
2809 if (constant)
2810 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002811 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002812 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002813 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002814
2815 switch (op)
2816 {
2817 case EOpAddAssign: increment = value; break;
2818 case EOpSubAssign: increment = -value; break;
2819 default: UNIMPLEMENTED();
2820 }
2821 }
2822 }
2823 }
2824 else if (unaryTerminal)
2825 {
2826 TOperator op = unaryTerminal->getOp();
2827
2828 switch (op)
2829 {
2830 case EOpPostIncrement: increment = 1; break;
2831 case EOpPostDecrement: increment = -1; break;
2832 case EOpPreIncrement: increment = 1; break;
2833 case EOpPreDecrement: increment = -1; break;
2834 default: UNIMPLEMENTED();
2835 }
2836 }
2837 }
2838
2839 if (index != NULL && comparator != EOpNull && increment != 0)
2840 {
2841 if (comparator == EOpLessThanEqual)
2842 {
2843 comparator = EOpLessThan;
2844 limit += 1;
2845 }
2846
2847 if (comparator == EOpLessThan)
2848 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002849 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002850
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002851 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002852 {
2853 return false; // Not an excessive loop
2854 }
2855
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002856 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2857 mExcessiveLoopIndex = index;
2858
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002859 out << "{int ";
2860 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002861 out << ";\n"
2862 "bool Break";
2863 index->traverse(this);
2864 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002865
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002866 bool firstLoopFragment = true;
2867
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002868 while (iterations > 0)
2869 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002870 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002871
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002872 if (!firstLoopFragment)
2873 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002874 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002875 index->traverse(this);
2876 out << ") {\n";
2877 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002878
2879 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2880 {
2881 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2882 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002883
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002884 // for(int index = initial; index < clampedLimit; index += increment)
2885
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002886 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002887 index->traverse(this);
2888 out << " = ";
2889 out << initial;
2890
2891 out << "; ";
2892 index->traverse(this);
2893 out << " < ";
2894 out << clampedLimit;
2895
2896 out << "; ";
2897 index->traverse(this);
2898 out << " += ";
2899 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002900 out << ")\n";
2901
Jamie Madill075edd82013-07-08 13:30:19 -04002902 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002903 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002904
2905 if (node->getBody())
2906 {
2907 node->getBody()->traverse(this);
2908 }
2909
Jamie Madill075edd82013-07-08 13:30:19 -04002910 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002911 out << ";}\n";
2912
2913 if (!firstLoopFragment)
2914 {
2915 out << "}\n";
2916 }
2917
2918 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002919
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002920 initial += MAX_LOOP_ITERATIONS * increment;
2921 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002922 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002923
2924 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002925
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002926 mExcessiveLoopIndex = restoreIndex;
2927
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002928 return true;
2929 }
2930 else UNIMPLEMENTED();
2931 }
2932
2933 return false; // Not handled as an excessive loop
2934}
2935
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002936void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002937{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002938 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002939
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002940 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002941 {
2942 out << preString;
2943 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002944 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002945 {
2946 out << inString;
2947 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002948 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002949 {
2950 out << postString;
2951 }
2952}
2953
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002954void OutputHLSL::outputLineDirective(int line)
2955{
2956 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2957 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002958 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002959 mBody << "#line " << line;
2960
2961 if (mContext.sourcePath)
2962 {
2963 mBody << " \"" << mContext.sourcePath << "\"";
2964 }
2965
2966 mBody << "\n";
2967 }
2968}
2969
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002970TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2971{
2972 TQualifier qualifier = symbol->getQualifier();
2973 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002974 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002975
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002976 if (name.empty()) // HLSL demands named arguments, also for prototypes
2977 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002978 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002979 }
2980 else
2981 {
2982 name = decorate(name);
2983 }
2984
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002985 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2986 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04002987 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2988 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002989 }
2990
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002991 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002992}
2993
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002994TString OutputHLSL::interpolationString(TQualifier qualifier)
2995{
2996 switch(qualifier)
2997 {
2998 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07002999 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003000 case EvqInvariantVaryingIn: return "";
3001 case EvqSmoothIn: return "linear";
3002 case EvqFlatIn: return "nointerpolation";
3003 case EvqCentroidIn: return "centroid";
3004 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003005 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003006 case EvqInvariantVaryingOut: return "";
3007 case EvqSmoothOut: return "linear";
3008 case EvqFlatOut: return "nointerpolation";
3009 case EvqCentroidOut: return "centroid";
3010 default: UNREACHABLE();
3011 }
3012
3013 return "";
3014}
3015
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003016TString OutputHLSL::qualifierString(TQualifier qualifier)
3017{
3018 switch(qualifier)
3019 {
3020 case EvqIn: return "in";
3021 case EvqOut: return "out";
3022 case EvqInOut: return "inout";
3023 case EvqConstReadOnly: return "const";
3024 default: UNREACHABLE();
3025 }
3026
3027 return "";
3028}
3029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003030TString OutputHLSL::typeString(const TType &type)
3031{
Jamie Madill98493dd2013-07-08 14:39:03 -04003032 const TStructure* structure = type.getStruct();
3033 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003034 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003035 const TString& typeName = structure->name();
3036 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003037 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003038 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003039 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003040 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003041 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003042 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003043 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003044 }
3045 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003046 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003047 int cols = type.getCols();
3048 int rows = type.getRows();
3049 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003050 }
3051 else
3052 {
3053 switch (type.getBasicType())
3054 {
3055 case EbtFloat:
3056 switch (type.getNominalSize())
3057 {
3058 case 1: return "float";
3059 case 2: return "float2";
3060 case 3: return "float3";
3061 case 4: return "float4";
3062 }
3063 case EbtInt:
3064 switch (type.getNominalSize())
3065 {
3066 case 1: return "int";
3067 case 2: return "int2";
3068 case 3: return "int3";
3069 case 4: return "int4";
3070 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003071 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003072 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003073 {
3074 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003075 case 2: return "uint2";
3076 case 3: return "uint3";
3077 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003078 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003079 case EbtBool:
3080 switch (type.getNominalSize())
3081 {
3082 case 1: return "bool";
3083 case 2: return "bool2";
3084 case 3: return "bool3";
3085 case 4: return "bool4";
3086 }
3087 case EbtVoid:
3088 return "void";
3089 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003090 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003091 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003092 case EbtSampler2DArray:
3093 case EbtISampler2DArray:
3094 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003095 return "sampler2D";
3096 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003097 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003098 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003099 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003100 case EbtSamplerExternalOES:
3101 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003102 default:
3103 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003104 }
3105 }
3106
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003107 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003108 return "<unknown type>";
3109}
3110
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003111TString OutputHLSL::textureString(const TType &type)
3112{
3113 switch (type.getBasicType())
3114 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003115 case EbtSampler2D: return "Texture2D";
3116 case EbtSamplerCube: return "TextureCube";
3117 case EbtSamplerExternalOES: return "Texture2D";
3118 case EbtSampler2DArray: return "Texture2DArray";
3119 case EbtSampler3D: return "Texture3D";
3120 case EbtISampler2D: return "Texture2D<int4>";
3121 case EbtISampler3D: return "Texture3D<int4>";
3122 case EbtISamplerCube: return "TextureCube<int4>";
3123 case EbtISampler2DArray: return "Texture2DArray<int4>";
3124 case EbtUSampler2D: return "Texture2D<uint4>";
3125 case EbtUSampler3D: return "Texture3D<uint4>";
3126 case EbtUSamplerCube: return "TextureCube<uint4>";
3127 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3128 case EbtSampler2DShadow: return "Texture2D";
3129 case EbtSamplerCubeShadow: return "TextureCube";
3130 case EbtSampler2DArrayShadow: return "Texture2DArray";
3131 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003132 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003133
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003134 return "<unknown texture type>";
3135}
3136
Nicolas Capenscb127d32013-07-15 17:26:18 -04003137TString OutputHLSL::samplerString(const TType &type)
3138{
3139 if (IsShadowSampler(type.getBasicType()))
3140 {
3141 return "SamplerComparisonState";
3142 }
3143 else
3144 {
3145 return "SamplerState";
3146 }
3147}
3148
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003149TString OutputHLSL::arrayString(const TType &type)
3150{
3151 if (!type.isArray())
3152 {
3153 return "";
3154 }
3155
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003156 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003157}
3158
3159TString OutputHLSL::initializer(const TType &type)
3160{
3161 TString string;
3162
Jamie Madill94bf7f22013-07-08 13:31:15 -04003163 size_t size = type.getObjectSize();
3164 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003165 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003166 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003167
Jamie Madill94bf7f22013-07-08 13:31:15 -04003168 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003169 {
3170 string += ", ";
3171 }
3172 }
3173
daniel@transgaming.comead23042010-04-29 03:35:36 +00003174 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003175}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003176
Jamie Madill98493dd2013-07-08 14:39:03 -04003177TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003178{
Jamie Madill98493dd2013-07-08 14:39:03 -04003179 const TFieldList &fields = structure.fields();
3180 const bool isNameless = (structure.name() == "");
3181 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003182 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3183
Jamie Madill98493dd2013-07-08 14:39:03 -04003184 TString string;
3185 string += declareString + "\n"
3186 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003187
Jamie Madillc835df62013-06-21 09:15:32 -04003188 int elementIndex = 0;
3189
Jamie Madill9cf6c072013-06-20 11:55:53 -04003190 for (unsigned int i = 0; i < fields.size(); i++)
3191 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003192 const TField &field = *fields[i];
3193 const TType &fieldType = *field.type();
3194 const TStructure *fieldStruct = fieldType.getStruct();
3195 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003196
Jamie Madillc835df62013-06-21 09:15:32 -04003197 if (useStd140Packing)
3198 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003199 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003200 }
3201
Jamie Madill98493dd2013-07-08 14:39:03 -04003202 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003203
3204 if (useStd140Packing)
3205 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003206 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003207 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003208 }
3209
3210 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003211 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003212
Jamie Madille4075c92013-06-21 09:15:32 -04003213 // Add remaining element index to the global map, for use with nested structs in standard layouts
3214 if (useStd140Packing)
3215 {
3216 mStd140StructElementIndexes[structName] = elementIndex;
3217 }
3218
Jamie Madill98493dd2013-07-08 14:39:03 -04003219 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003220}
3221
Jamie Madill98493dd2013-07-08 14:39:03 -04003222TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003223{
Jamie Madill98493dd2013-07-08 14:39:03 -04003224 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003225 {
3226 return "";
3227 }
3228
3229 TString prefix = "";
3230
3231 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3232 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003233
3234 if (useStd140Packing)
3235 {
3236 prefix += "std";
3237 }
3238
Jamie Madill9cf6c072013-06-20 11:55:53 -04003239 if (useHLSLRowMajorPacking)
3240 {
Jamie Madillc835df62013-06-21 09:15:32 -04003241 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003242 prefix += "rm";
3243 }
3244
Jamie Madill98493dd2013-07-08 14:39:03 -04003245 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003246}
3247
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003248void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003249{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003250 if (name == "")
3251 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003252 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003253 }
3254
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003255 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3256 {
3257 return; // Already added
3258 }
3259
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003260 TType ctorType = type;
3261 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003262 ctorType.setPrecision(EbpHigh);
3263 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003264
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003265 TString ctorName = type.getStruct() ? decorate(name) : name;
3266
3267 typedef std::vector<TType> ParameterArray;
3268 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003269
Jamie Madill98493dd2013-07-08 14:39:03 -04003270 const TStructure* structure = type.getStruct();
3271 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003272 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003273 mStructNames.insert(decorate(name));
3274
Jamie Madill98493dd2013-07-08 14:39:03 -04003275 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003276
Jamie Madill98493dd2013-07-08 14:39:03 -04003277 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003278 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003279 // Add row-major packed struct for interface blocks
3280 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003281 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003282 "#pragma pack_matrix(column_major)\n";
3283
Jamie Madillc835df62013-06-21 09:15:32 -04003284 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003285 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003286
3287 const TString &std140RowMajorPrefix = "std_rm";
3288 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003289 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003290 "#pragma pack_matrix(column_major)\n";
3291
Jamie Madill98493dd2013-07-08 14:39:03 -04003292 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003293 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003294 mStructDeclarations.push_back(std140String);
3295 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003296 }
3297
Jamie Madill98493dd2013-07-08 14:39:03 -04003298 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003299 for (unsigned int i = 0; i < fields.size(); i++)
3300 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003301 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003302 }
3303 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003304 else if (parameters)
3305 {
3306 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3307 {
3308 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3309 }
3310 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003311 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003312
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003313 TString constructor;
3314
3315 if (ctorType.getStruct())
3316 {
3317 constructor += ctorName + " " + ctorName + "_ctor(";
3318 }
3319 else // Built-in type
3320 {
3321 constructor += typeString(ctorType) + " " + ctorName + "(";
3322 }
3323
3324 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3325 {
3326 const TType &type = ctorParameters[parameter];
3327
3328 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3329
3330 if (parameter < ctorParameters.size() - 1)
3331 {
3332 constructor += ", ";
3333 }
3334 }
3335
3336 constructor += ")\n"
3337 "{\n";
3338
3339 if (ctorType.getStruct())
3340 {
3341 constructor += " " + ctorName + " structure = {";
3342 }
3343 else
3344 {
3345 constructor += " return " + typeString(ctorType) + "(";
3346 }
3347
3348 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3349 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003350 int rows = ctorType.getRows();
3351 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003352 const TType &parameter = ctorParameters[0];
3353
3354 if (parameter.isScalar())
3355 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003356 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003357 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003358 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003359 {
3360 constructor += TString((row == col) ? "x0" : "0.0");
3361
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003362 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003363 {
3364 constructor += ", ";
3365 }
3366 }
3367 }
3368 }
3369 else if (parameter.isMatrix())
3370 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003371 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003372 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003373 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003374 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003375 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003376 {
3377 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3378 }
3379 else
3380 {
3381 constructor += TString((row == col) ? "1.0" : "0.0");
3382 }
3383
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003384 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003385 {
3386 constructor += ", ";
3387 }
3388 }
3389 }
3390 }
3391 else UNREACHABLE();
3392 }
3393 else
3394 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003395 size_t remainingComponents = ctorType.getObjectSize();
3396 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003397
3398 while (remainingComponents > 0)
3399 {
3400 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003401 const size_t parameterSize = parameter.getObjectSize();
3402 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003403
3404 constructor += "x" + str(parameterIndex);
3405
3406 if (parameter.isScalar())
3407 {
3408 remainingComponents -= parameter.getObjectSize();
3409 }
3410 else if (parameter.isVector())
3411 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003412 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003413 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003414 ASSERT(parameterSize <= remainingComponents);
3415 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003416 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003417 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003418 {
3419 switch (remainingComponents)
3420 {
3421 case 1: constructor += ".x"; break;
3422 case 2: constructor += ".xy"; break;
3423 case 3: constructor += ".xyz"; break;
3424 case 4: constructor += ".xyzw"; break;
3425 default: UNREACHABLE();
3426 }
3427
3428 remainingComponents = 0;
3429 }
3430 else UNREACHABLE();
3431 }
3432 else if (parameter.isMatrix() || parameter.getStruct())
3433 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003434 ASSERT(remainingComponents == parameterSize || moreParameters);
3435 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003436
Jamie Madill94bf7f22013-07-08 13:31:15 -04003437 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003438 }
3439 else UNREACHABLE();
3440
3441 if (moreParameters)
3442 {
3443 parameterIndex++;
3444 }
3445
3446 if (remainingComponents)
3447 {
3448 constructor += ", ";
3449 }
3450 }
3451 }
3452
3453 if (ctorType.getStruct())
3454 {
3455 constructor += "};\n"
3456 " return structure;\n"
3457 "}\n";
3458 }
3459 else
3460 {
3461 constructor += ");\n"
3462 "}\n";
3463 }
3464
daniel@transgaming.com63691862010-04-29 03:32:42 +00003465 mConstructors.insert(constructor);
3466}
3467
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003468const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3469{
3470 TInfoSinkBase &out = mBody;
3471
Jamie Madill98493dd2013-07-08 14:39:03 -04003472 const TStructure* structure = type.getStruct();
3473 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003474 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003475 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003476
Jamie Madill98493dd2013-07-08 14:39:03 -04003477 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003478
Jamie Madill98493dd2013-07-08 14:39:03 -04003479 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003480 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003481 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003482
3483 constUnion = writeConstantUnion(*fieldType, constUnion);
3484
Jamie Madill98493dd2013-07-08 14:39:03 -04003485 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003486 {
3487 out << ", ";
3488 }
3489 }
3490
3491 out << ")";
3492 }
3493 else
3494 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003495 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003496 bool writeType = size > 1;
3497
3498 if (writeType)
3499 {
3500 out << typeString(type) << "(";
3501 }
3502
Jamie Madill94bf7f22013-07-08 13:31:15 -04003503 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003504 {
3505 switch (constUnion->getType())
3506 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003507 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003508 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003509 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003510 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003511 default: UNREACHABLE();
3512 }
3513
3514 if (i != size - 1)
3515 {
3516 out << ", ";
3517 }
3518 }
3519
3520 if (writeType)
3521 {
3522 out << ")";
3523 }
3524 }
3525
3526 return constUnion;
3527}
3528
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003529TString OutputHLSL::scopeString(unsigned int depthLimit)
3530{
3531 TString string;
3532
3533 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3534 {
3535 string += "_" + str(i);
3536 }
3537
3538 return string;
3539}
3540
3541TString OutputHLSL::scopedStruct(const TString &typeName)
3542{
3543 if (typeName == "")
3544 {
3545 return typeName;
3546 }
3547
3548 return typeName + scopeString(mScopeDepth);
3549}
3550
3551TString OutputHLSL::structLookup(const TString &typeName)
3552{
3553 for (int depth = mScopeDepth; depth >= 0; depth--)
3554 {
3555 TString scopedName = decorate(typeName + scopeString(depth));
3556
3557 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3558 {
3559 if (*structName == scopedName)
3560 {
3561 return scopedName;
3562 }
3563 }
3564 }
3565
3566 UNREACHABLE(); // Should have found a matching constructor
3567
3568 return typeName;
3569}
3570
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003571TString OutputHLSL::decorate(const TString &string)
3572{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003573 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003574 {
3575 return "_" + string;
3576 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003577
3578 return string;
3579}
3580
apatrick@chromium.org65756022012-01-17 21:45:38 +00003581TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003582{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003583 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003584 {
3585 return "ex_" + string;
3586 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003587
3588 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003589}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003590
Jamie Madill98493dd2013-07-08 14:39:03 -04003591TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003592{
Jamie Madill98493dd2013-07-08 14:39:03 -04003593 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003594 {
3595 return decorate(string);
3596 }
3597
3598 return string;
3599}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003600
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003601void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003602{
Jamie Madill98493dd2013-07-08 14:39:03 -04003603 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003604
3605 if (!structure)
3606 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003607 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003608 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3609 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3610 output.push_back(field);
3611 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003612 else
3613 {
Jamie Madill28167c62013-08-30 13:21:10 -04003614 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003615
3616 const TFieldList &fields = structure->fields();
3617
3618 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3619 {
3620 TField *field = fields[fieldIndex];
3621 TType *fieldType = field->type();
3622
3623 // make sure to copy matrix packing information
3624 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3625
3626 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3627 }
3628
3629 output.push_back(structField);
3630 }
3631}
3632
Jamie Madillc2141fb2013-08-30 13:21:08 -04003633Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003634{
3635 const TStructure *structure = type.getStruct();
3636
3637 if (!structure)
3638 {
3639 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3640 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003641 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003642 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003643
3644 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003645 }
3646 else
3647 {
Jamie Madill56093782013-08-30 13:21:11 -04003648 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3649 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003650
Jamie Madill98493dd2013-07-08 14:39:03 -04003651 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003652
Jamie Madill98493dd2013-07-08 14:39:03 -04003653 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003654 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003655 TField *field = fields[fieldIndex];
3656 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003657
Jamie Madill56093782013-08-30 13:21:11 -04003658 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003659 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003660
Jamie Madill56093782013-08-30 13:21:11 -04003661 // assign register offset information -- this will override the information in any sub-structures.
3662 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3663
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003664 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003665
3666 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003667 }
3668}
3669
Jamie Madill139b9092013-08-30 13:21:06 -04003670InterpolationType getInterpolationType(TQualifier qualifier)
3671{
3672 switch (qualifier)
3673 {
3674 case EvqFlatIn:
3675 case EvqFlatOut:
3676 return INTERPOLATION_FLAT;
3677
3678 case EvqSmoothIn:
3679 case EvqSmoothOut:
3680 case EvqVertexOut:
3681 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003682 case EvqVaryingIn:
3683 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003684 return INTERPOLATION_SMOOTH;
3685
3686 case EvqCentroidIn:
3687 case EvqCentroidOut:
3688 return INTERPOLATION_CENTROID;
3689
3690 default: UNREACHABLE();
3691 return INTERPOLATION_SMOOTH;
3692 }
3693}
3694
Jamie Madill94599662013-08-30 13:21:10 -04003695void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003696{
3697 const TStructure *structure = type.getStruct();
3698
Jamie Madill94599662013-08-30 13:21:10 -04003699 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003700 if (!structure)
3701 {
Jamie Madill139b9092013-08-30 13:21:06 -04003702 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003703 fieldsOut.push_back(varying);
3704 }
3705 else
3706 {
Jamie Madill28167c62013-08-30 13:21:10 -04003707 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003708 const TFieldList &fields = structure->fields();
3709
Jamie Madill28167c62013-08-30 13:21:10 -04003710 structVarying.structName = structure->name().c_str();
3711
Jamie Madill47fdd132013-08-30 13:21:04 -04003712 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3713 {
3714 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003715 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003716 }
3717
3718 fieldsOut.push_back(structVarying);
3719 }
3720}
3721
Jamie Madillc2141fb2013-08-30 13:21:08 -04003722int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003723{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003724 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3725
3726 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3727
3728 if (IsSampler(type.getBasicType()))
3729 {
3730 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3731 }
3732 else
3733 {
3734 mUniformRegister += HLSLVariableRegisterCount(uniform);
3735 }
3736
3737 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003738}
3739
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003740GLenum OutputHLSL::glVariableType(const TType &type)
3741{
3742 if (type.getBasicType() == EbtFloat)
3743 {
3744 if (type.isScalar())
3745 {
3746 return GL_FLOAT;
3747 }
3748 else if (type.isVector())
3749 {
3750 switch(type.getNominalSize())
3751 {
3752 case 2: return GL_FLOAT_VEC2;
3753 case 3: return GL_FLOAT_VEC3;
3754 case 4: return GL_FLOAT_VEC4;
3755 default: UNREACHABLE();
3756 }
3757 }
3758 else if (type.isMatrix())
3759 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003760 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003761 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003762 case 2:
3763 switch(type.getRows())
3764 {
3765 case 2: return GL_FLOAT_MAT2;
3766 case 3: return GL_FLOAT_MAT2x3;
3767 case 4: return GL_FLOAT_MAT2x4;
3768 default: UNREACHABLE();
3769 }
3770
3771 case 3:
3772 switch(type.getRows())
3773 {
3774 case 2: return GL_FLOAT_MAT3x2;
3775 case 3: return GL_FLOAT_MAT3;
3776 case 4: return GL_FLOAT_MAT3x4;
3777 default: UNREACHABLE();
3778 }
3779
3780 case 4:
3781 switch(type.getRows())
3782 {
3783 case 2: return GL_FLOAT_MAT4x2;
3784 case 3: return GL_FLOAT_MAT4x3;
3785 case 4: return GL_FLOAT_MAT4;
3786 default: UNREACHABLE();
3787 }
3788
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003789 default: UNREACHABLE();
3790 }
3791 }
3792 else UNREACHABLE();
3793 }
3794 else if (type.getBasicType() == EbtInt)
3795 {
3796 if (type.isScalar())
3797 {
3798 return GL_INT;
3799 }
3800 else if (type.isVector())
3801 {
3802 switch(type.getNominalSize())
3803 {
3804 case 2: return GL_INT_VEC2;
3805 case 3: return GL_INT_VEC3;
3806 case 4: return GL_INT_VEC4;
3807 default: UNREACHABLE();
3808 }
3809 }
3810 else UNREACHABLE();
3811 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003812 else if (type.getBasicType() == EbtUInt)
3813 {
3814 if (type.isScalar())
3815 {
3816 return GL_UNSIGNED_INT;
3817 }
3818 else if (type.isVector())
3819 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003820 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003821 {
3822 case 2: return GL_UNSIGNED_INT_VEC2;
3823 case 3: return GL_UNSIGNED_INT_VEC3;
3824 case 4: return GL_UNSIGNED_INT_VEC4;
3825 default: UNREACHABLE();
3826 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003827 }
3828 else UNREACHABLE();
3829 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003830 else if (type.getBasicType() == EbtBool)
3831 {
3832 if (type.isScalar())
3833 {
3834 return GL_BOOL;
3835 }
3836 else if (type.isVector())
3837 {
3838 switch(type.getNominalSize())
3839 {
3840 case 2: return GL_BOOL_VEC2;
3841 case 3: return GL_BOOL_VEC3;
3842 case 4: return GL_BOOL_VEC4;
3843 default: UNREACHABLE();
3844 }
3845 }
3846 else UNREACHABLE();
3847 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003848
3849 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003850 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003851 case EbtSampler2D: return GL_SAMPLER_2D;
3852 case EbtSampler3D: return GL_SAMPLER_3D;
3853 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3854 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3855 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3856 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3857 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3858 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3859 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3860 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3861 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3862 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3863 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3864 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3865 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3866 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003867 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003868
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003869
3870 return GL_NONE;
3871}
3872
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003873GLenum OutputHLSL::glVariablePrecision(const TType &type)
3874{
3875 if (type.getBasicType() == EbtFloat)
3876 {
3877 switch (type.getPrecision())
3878 {
3879 case EbpHigh: return GL_HIGH_FLOAT;
3880 case EbpMedium: return GL_MEDIUM_FLOAT;
3881 case EbpLow: return GL_LOW_FLOAT;
3882 case EbpUndefined:
3883 // Should be defined as the default precision by the parser
3884 default: UNREACHABLE();
3885 }
3886 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003887 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003888 {
3889 switch (type.getPrecision())
3890 {
3891 case EbpHigh: return GL_HIGH_INT;
3892 case EbpMedium: return GL_MEDIUM_INT;
3893 case EbpLow: return GL_LOW_INT;
3894 case EbpUndefined:
3895 // Should be defined as the default precision by the parser
3896 default: UNREACHABLE();
3897 }
3898 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003899
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003900 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003901 return GL_NONE;
3902}
3903
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003904bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3905{
3906 switch(qualifier)
3907 {
3908 case EvqVaryingOut:
3909 case EvqInvariantVaryingOut:
3910 case EvqSmoothOut:
3911 case EvqFlatOut:
3912 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07003913 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003914 return true;
3915 }
3916
3917 return false;
3918}
3919
3920bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3921{
3922 switch(qualifier)
3923 {
3924 case EvqVaryingIn:
3925 case EvqInvariantVaryingIn:
3926 case EvqSmoothIn:
3927 case EvqFlatIn:
3928 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07003929 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003930 return true;
3931 }
3932
3933 return false;
3934}
3935
3936bool OutputHLSL::isVarying(TQualifier qualifier)
3937{
3938 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3939}
3940
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003941}