blob: d93b68de93c782430abb98c46233c40cba4a68a8 [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
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/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"
alokp@chromium.org91b72322010-06-02 15:50:56 +000011#include "compiler/debug.h"
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000012#include "compiler/DetectDiscontinuity.h"
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000013#include "compiler/InfoSink.h"
14#include "compiler/SearchSymbol.h"
15#include "compiler/UnfoldShortCircuit.h"
Jamie Madill440dc742013-06-20 11:55:55 -040016#include "compiler/HLSLLayoutEncoder.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000018#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000019#include <cfloat>
20#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000021
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022namespace sh
23{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000024// Integer to TString conversion
25TString str(int i)
26{
27 char buffer[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000028 snprintf(buffer, sizeof(buffer), "%d", i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000029 return buffer;
30}
31
Nicolas Capense0ba27a2013-06-24 16:10:52 -040032TString OutputHLSL::TextureFunction::name() const
33{
34 TString name = "gl_texture";
35
36 if (sampler == EbtSampler2D ||
37 sampler == EbtISampler2D ||
Nicolas Capensfb50dff2013-06-24 16:16:23 -040038 sampler == EbtUSampler2D ||
39 sampler == EbtSampler2DArray ||
40 sampler == EbtISampler2DArray ||
41 sampler == EbtUSampler2DArray)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040042 {
43 name += "2D";
44 }
45 else if (sampler == EbtSampler3D ||
46 sampler == EbtISampler3D ||
47 sampler == EbtUSampler3D)
48 {
49 name += "3D";
50 }
51 else if (sampler == EbtSamplerCube ||
52 sampler == EbtISamplerCube ||
53 sampler == EbtUSamplerCube)
54 {
55 name += "Cube";
56 }
57 else UNREACHABLE();
58
59 if (proj)
60 {
61 name += "Proj";
62 }
63
64 switch(mipmap)
65 {
66 case IMPLICIT: break;
67 case BIAS: break;
68 case LOD: name += "Lod"; break;
69 case LOD0: name += "Lod0"; break;
70 default: UNREACHABLE();
71 }
72
73 return name + "(";
74}
75
76bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
77{
78 if (sampler < rhs.sampler) return true;
79 if (coords < rhs.coords) return true;
80 if (!proj && rhs.proj) return true;
81 if (mipmap < rhs.mipmap) return true;
82
83 return false;
84}
85
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000086OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000087 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000089 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000090 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000091
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000092 mUsesFragColor = false;
93 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000094 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000095 mUsesFragCoord = false;
96 mUsesPointCoord = false;
97 mUsesFrontFacing = false;
98 mUsesPointSize = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000099 mUsesXor = false;
100 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000101 mUsesMod2v = false;
102 mUsesMod2f = false;
103 mUsesMod3v = false;
104 mUsesMod3f = false;
105 mUsesMod4v = false;
106 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000107 mUsesFaceforward1 = false;
108 mUsesFaceforward2 = false;
109 mUsesFaceforward3 = false;
110 mUsesFaceforward4 = false;
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000111
112 for (unsigned int col = 0; col <= 4; col++)
113 {
114 for (unsigned int row = 0; row <= 4; row++)
115 {
116 mUsesEqualMat[col][row] = false;
117 }
118 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000119 mUsesEqualVec2 = false;
120 mUsesEqualVec3 = false;
121 mUsesEqualVec4 = false;
122 mUsesEqualIVec2 = false;
123 mUsesEqualIVec3 = false;
124 mUsesEqualIVec4 = false;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +0000125 mUsesEqualUVec2 = false;
126 mUsesEqualUVec3 = false;
127 mUsesEqualUVec4 = false;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000128 mUsesEqualBVec2 = false;
129 mUsesEqualBVec3 = false;
130 mUsesEqualBVec4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000131 mUsesAtan2_1 = false;
132 mUsesAtan2_2 = false;
133 mUsesAtan2_3 = false;
134 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000135
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000136 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
137
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000138 mScopeDepth = 0;
139
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000140 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000141
142 mContainsLoopDiscontinuity = false;
143 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000144 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000145
146 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000147
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000148 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000149 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000150 if (mContext.shaderType == SH_FRAGMENT_SHADER)
151 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000152 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000153 }
154 else
155 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000156 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000157 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000158 }
159 else
160 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000161 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000162 }
163
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000164 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000165 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400166 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167}
168
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000169OutputHLSL::~OutputHLSL()
170{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000171 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000172}
173
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000174void OutputHLSL::output()
175{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000176 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000177
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000178 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 +0000179 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000180
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000181 mContext.infoSink().obj << mHeader.c_str();
182 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000183}
184
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000185TInfoSinkBase &OutputHLSL::getBodyStream()
186{
187 return mBody;
188}
189
daniel@transgaming.com043da132012-12-20 21:12:22 +0000190const ActiveUniforms &OutputHLSL::getUniforms()
191{
192 return mActiveUniforms;
193}
194
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000195const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
196{
197 return mActiveInterfaceBlocks;
198}
199
Jamie Madill46131a32013-06-20 11:55:50 -0400200const ActiveShaderVariables &OutputHLSL::getOutputVariables() const
201{
202 return mActiveOutputVariables;
203}
204
Jamie Madilldefb6742013-06-20 11:55:51 -0400205const ActiveShaderVariables &OutputHLSL::getAttributes() const
206{
207 return mActiveAttributes;
208}
209
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000210int OutputHLSL::vectorSize(const TType &type) const
211{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000212 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000213 int arraySize = type.isArray() ? type.getArraySize() : 1;
214
215 return elementSize * arraySize;
216}
217
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000218TString OutputHLSL::interfaceBlockUniformName(const TType &interfaceBlockType, const TType &uniformType)
219{
220 if (interfaceBlockType.hasInstanceName())
221 {
222 return interfaceBlockType.getTypeName() + "." + uniformType.getFieldName();
223 }
224 else
225 {
226 return uniformType.getFieldName();
227 }
228}
229
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000230TString OutputHLSL::decoratePrivate(const TString &privateText)
231{
232 return "dx_" + privateText;
233}
234
235TString OutputHLSL::interfaceBlockStructName(const TType &interfaceBlockType)
236{
237 return decoratePrivate(interfaceBlockType.getTypeName()) + "_type";
238}
239
240TString OutputHLSL::interfaceBlockInstanceString(const TType& interfaceBlockType, unsigned int arrayIndex)
241{
242 if (!interfaceBlockType.hasInstanceName())
243 {
244 return "";
245 }
246 else if (interfaceBlockType.isArray())
247 {
248 return decoratePrivate(interfaceBlockType.getInstanceName()) + "_" + str(arrayIndex);
249 }
250 else
251 {
252 return decorate(interfaceBlockType.getInstanceName());
253 }
254}
255
Jamie Madillc835df62013-06-21 09:15:32 -0400256TString OutputHLSL::interfaceBlockMemberTypeString(const TType &memberType, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000257{
Jamie Madill529077d2013-06-20 11:55:54 -0400258 const TLayoutMatrixPacking matrixPacking = memberType.getLayoutQualifier().matrixPacking;
259 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000260
261 if (memberType.isMatrix())
262 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400263 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400264 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
265 return matrixPackString + " " + typeString(memberType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000266 }
267 else if (memberType.getBasicType() == EbtStruct)
268 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400269 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madillc835df62013-06-21 09:15:32 -0400270 return structureTypeName(memberType, matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000271 }
272 else
273 {
274 return typeString(memberType);
275 }
276}
277
Jamie Madill574d9dd2013-06-20 11:55:56 -0400278TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
279{
280 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
281 {
282 // no padding needed, HLSL will align the field to a new register
283 *elementIndex = 0;
284 return "";
285 }
286
287 const GLenum glType = glVariableType(type);
288 const int numComponents = gl::UniformComponentCount(glType);
289
290 if (numComponents >= 4)
291 {
292 // no padding needed, HLSL will align the field to a new register
293 *elementIndex = 0;
294 return "";
295 }
296
297 if (*elementIndex + numComponents > 4)
298 {
299 // no padding needed, HLSL will align the field to a new register
300 *elementIndex = numComponents;
301 return "";
302 }
303
304 TString padding;
305
306 const int alignment = numComponents == 3 ? 4 : numComponents;
307 const int paddingOffset = (*elementIndex % alignment);
308
309 if (paddingOffset != 0)
310 {
311 // padding is neccessary
312 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
313 {
314 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
315 }
316
317 *elementIndex += (alignment - paddingOffset);
318 }
319
320 *elementIndex += numComponents;
321 *elementIndex %= 4;
322
323 return padding;
324}
325
Jamie Madille4075c92013-06-21 09:15:32 -0400326TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400327{
Jamie Madillc835df62013-06-21 09:15:32 -0400328 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400329 {
330 return "";
331 }
332
Jamie Madill574d9dd2013-06-20 11:55:56 -0400333 int numComponents = 0;
334
335 if (type.isMatrix())
336 {
Jamie Madille4075c92013-06-21 09:15:32 -0400337 // This method can also be called from structureString, which does not use layout qualifiers.
338 // Thus, use the method parameter for determining the matrix packing.
339 //
340 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
341 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
342 //
343 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400344 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400345 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
346 }
Jamie Madillc835df62013-06-21 09:15:32 -0400347 else if (type.getBasicType() == EbtStruct)
348 {
Jamie Madille4075c92013-06-21 09:15:32 -0400349 const TString &structName = structureTypeName(type, useHLSLRowMajorPacking, true);
350 numComponents = mStd140StructElementIndexes[structName];
351
352 if (numComponents == 0)
353 {
354 return "";
355 }
Jamie Madillc835df62013-06-21 09:15:32 -0400356 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400357 else
358 {
Jamie Madillc835df62013-06-21 09:15:32 -0400359 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400360 numComponents = gl::UniformComponentCount(glType);
361 }
362
363 TString padding;
364 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
365 {
366 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
367 }
368 return padding;
369}
370
371TString OutputHLSL::interfaceBlockMemberString(const TTypeList &typeList, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000372{
373 TString hlsl;
374
Jamie Madill574d9dd2013-06-20 11:55:56 -0400375 int elementIndex = 0;
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000376
377 for (unsigned int typeIndex = 0; typeIndex < typeList.size(); typeIndex++)
378 {
379 const TType &memberType = *typeList[typeIndex].type;
Jamie Madill574d9dd2013-06-20 11:55:56 -0400380
381 if (blockStorage == EbsStd140)
382 {
Jamie Madillc835df62013-06-21 09:15:32 -0400383 // 2 and 3 component vector types in some cases need pre-padding
384 hlsl += std140PrePaddingString(memberType, &elementIndex);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400385 }
386
Jamie Madillc835df62013-06-21 09:15:32 -0400387 hlsl += " " + interfaceBlockMemberTypeString(memberType, blockStorage) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000388 " " + decorate(memberType.getFieldName()) + arrayString(memberType) + ";\n";
Jamie Madill574d9dd2013-06-20 11:55:56 -0400389
390 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
391 if (blockStorage == EbsStd140)
392 {
Jamie Madille4075c92013-06-21 09:15:32 -0400393 const bool useHLSLRowMajorPacking = (memberType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
394 hlsl += std140PostPaddingString(memberType, useHLSLRowMajorPacking);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400395 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000396 }
397
398 return hlsl;
399}
400
401TString OutputHLSL::interfaceBlockStructString(const TType &interfaceBlockType)
402{
403 const TTypeList &typeList = *interfaceBlockType.getStruct();
Jamie Madill574d9dd2013-06-20 11:55:56 -0400404 const TLayoutBlockStorage blockStorage = interfaceBlockType.getLayoutQualifier().blockStorage;
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000405
406 return "struct " + interfaceBlockStructName(interfaceBlockType) + "\n"
407 "{\n" +
Jamie Madill574d9dd2013-06-20 11:55:56 -0400408 interfaceBlockMemberString(typeList, blockStorage) +
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000409 "};\n\n";
410}
411
412TString OutputHLSL::interfaceBlockString(const TType &interfaceBlockType, unsigned int registerIndex, unsigned int arrayIndex)
413{
414 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
415 const TString &blockName = interfaceBlockType.getTypeName() + arrayIndexString;
416 TString hlsl;
417
418 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
419 "{\n";
420
421 if (interfaceBlockType.hasInstanceName())
422 {
423 hlsl += " " + interfaceBlockStructName(interfaceBlockType) + " " + interfaceBlockInstanceString(interfaceBlockType, arrayIndex) + ";\n";
424 }
425 else
426 {
427 const TTypeList &typeList = *interfaceBlockType.getStruct();
Jamie Madill574d9dd2013-06-20 11:55:56 -0400428 const TLayoutBlockStorage blockStorage = interfaceBlockType.getLayoutQualifier().blockStorage;
429 hlsl += interfaceBlockMemberString(typeList, blockStorage);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000430 }
431
432 hlsl += "};\n\n";
433
434 return hlsl;
435}
436
Jamie Madill440dc742013-06-20 11:55:55 -0400437// Use the same layout for packed and shared
438void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
439{
440 interfaceBlock->layout = newLayout;
441 interfaceBlock->blockInfo.clear();
442
443 switch (newLayout)
444 {
445 case BLOCKLAYOUT_SHARED:
446 case BLOCKLAYOUT_PACKED:
447 {
448 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
449 hlslEncoder.encodeFields(interfaceBlock->activeUniforms);
450 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
451 }
452 break;
453
454 case BLOCKLAYOUT_STANDARD:
455 {
456 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
457 stdEncoder.encodeFields(interfaceBlock->activeUniforms);
458 interfaceBlock->dataSize = stdEncoder.getBlockSize();
459 }
460 break;
461
462 default:
463 UNREACHABLE();
464 break;
465 }
466}
467
Jamie Madill574d9dd2013-06-20 11:55:56 -0400468BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
469{
470 switch (blockStorage)
471 {
472 case EbsPacked: return BLOCKLAYOUT_PACKED;
473 case EbsShared: return BLOCKLAYOUT_SHARED;
474 case EbsStd140: return BLOCKLAYOUT_STANDARD;
475 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
476 }
477}
478
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000479void OutputHLSL::header()
480{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000481 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000483 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000484 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000485 TString varyings;
486 TString attributes;
487
488 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
489 {
490 const TType &type = uniform->second->getType();
491 const TString &name = uniform->second->getSymbol();
492
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000493 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
494 {
495 int index = samplerRegister(mReferencedUniforms[name]);
496
497 uniforms += "uniform SamplerState sampler_" + decorateUniform(name, type) + arrayString(type) +
498 " : register(s" + str(index) + ");\n";
499
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000500 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000501 " : register(t" + str(index) + ");\n";
502 }
503 else
504 {
505 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
506 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
507 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000508 }
509
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000510 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
511 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000512 const TType &nodeType = interfaceBlockIt->second->getType();
513 const TType &interfaceBlockType = nodeType.isInterfaceBlockMember() ? *nodeType.getInterfaceBlockType() : nodeType;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000514 const TString &blockName = interfaceBlockType.getTypeName();
515 const TTypeList &typeList = *interfaceBlockType.getStruct();
516
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000517 const unsigned int arraySize = interfaceBlockType.isArray() ? interfaceBlockType.getArraySize() : 0;
518 sh::InterfaceBlock interfaceBlock(blockName.c_str(), arraySize, mInterfaceBlockRegister);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000519 for (unsigned int typeIndex = 0; typeIndex < typeList.size(); typeIndex++)
520 {
521 const TType &memberType = *typeList[typeIndex].type;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000522 const TString &fullUniformName = interfaceBlockUniformName(interfaceBlockType, memberType);
523 declareUniformToList(memberType, fullUniformName, typeIndex, interfaceBlock.activeUniforms);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000524 }
525
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000526 mInterfaceBlockRegister += std::max(1u, interfaceBlock.arraySize);
527
Jamie Madill574d9dd2013-06-20 11:55:56 -0400528 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlockType.getLayoutQualifier().blockStorage);
529 setBlockLayout(&interfaceBlock, blockLayoutType);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000530 mActiveInterfaceBlocks.push_back(interfaceBlock);
531
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000532 if (interfaceBlockType.hasInstanceName())
533 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000534 interfaceBlocks += interfaceBlockStructString(interfaceBlockType);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000535 }
536
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000537 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000538 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000539 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
540 {
541 interfaceBlocks += interfaceBlockString(interfaceBlockType, interfaceBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000542 }
543 }
544 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000545 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000546 interfaceBlocks += interfaceBlockString(interfaceBlockType, interfaceBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000547 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000548 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000549
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000550 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
551 {
552 const TType &type = varying->second->getType();
553 const TString &name = varying->second->getSymbol();
554
555 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000556 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
557 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000558 }
559
560 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
561 {
562 const TType &type = attribute->second->getType();
563 const TString &name = attribute->second->getSymbol();
564
565 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400566
567 ShaderVariable shaderVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
568 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
569 mActiveAttributes.push_back(shaderVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000570 }
571
Jamie Madill529077d2013-06-20 11:55:54 -0400572 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
573 {
574 out << *structDeclaration;
575 }
576
577 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
578 {
579 out << *constructor;
580 }
581
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400582 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000584 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000585 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000586
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000587 out << "// Varyings\n";
588 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400589 out << "\n";
590
591 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000592 {
Jamie Madill46131a32013-06-20 11:55:50 -0400593 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000594 {
Jamie Madill46131a32013-06-20 11:55:50 -0400595 const TString &variableName = outputVariableIt->first;
596 const TType &variableType = outputVariableIt->second->getType();
597 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
598
599 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
600 " = " + initializer(variableType) + ";\n";
601
602 ShaderVariable outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
603 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
604 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000605 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000606 }
Jamie Madill46131a32013-06-20 11:55:50 -0400607 else
608 {
609 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
610
611 out << "static float4 gl_Color[" << numColorValues << "] =\n"
612 "{\n";
613 for (unsigned int i = 0; i < numColorValues; i++)
614 {
615 out << " float4(0, 0, 0, 0)";
616 if (i + 1 != numColorValues)
617 {
618 out << ",";
619 }
620 out << "\n";
621 }
622
623 out << "};\n";
624 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000625
626 if (mUsesFragCoord)
627 {
628 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
629 }
630
631 if (mUsesPointCoord)
632 {
633 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
634 }
635
636 if (mUsesFrontFacing)
637 {
638 out << "static bool gl_FrontFacing = false;\n";
639 }
640
641 out << "\n";
642
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000643 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000644 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000645 out << "struct gl_DepthRangeParameters\n"
646 "{\n"
647 " float near;\n"
648 " float far;\n"
649 " float diff;\n"
650 "};\n"
651 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000652 }
653
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000654 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000655 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000656 out << "cbuffer DriverConstants : register(b1)\n"
657 "{\n";
658
659 if (mUsesDepthRange)
660 {
661 out << " float3 dx_DepthRange : packoffset(c0);\n";
662 }
663
664 if (mUsesFragCoord)
665 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000666 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000667 }
668
669 if (mUsesFragCoord || mUsesFrontFacing)
670 {
671 out << " float3 dx_DepthFront : packoffset(c2);\n";
672 }
673
674 out << "};\n";
675 }
676 else
677 {
678 if (mUsesDepthRange)
679 {
680 out << "uniform float3 dx_DepthRange : register(c0);";
681 }
682
683 if (mUsesFragCoord)
684 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000685 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000686 }
687
688 if (mUsesFragCoord || mUsesFrontFacing)
689 {
690 out << "uniform float3 dx_DepthFront : register(c2);\n";
691 }
692 }
693
694 out << "\n";
695
696 if (mUsesDepthRange)
697 {
698 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
699 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000700 }
701
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000702 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000703 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000704
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000705 if (!interfaceBlocks.empty())
706 {
707 out << interfaceBlocks;
708 out << "\n";
709 }
710
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000711 if (usingMRTExtension && mNumRenderTargets > 1)
712 {
713 out << "#define GL_USES_MRT\n";
714 }
715
716 if (mUsesFragColor)
717 {
718 out << "#define GL_USES_FRAG_COLOR\n";
719 }
720
721 if (mUsesFragData)
722 {
723 out << "#define GL_USES_FRAG_DATA\n";
724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000726 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000728 out << "// Attributes\n";
729 out << attributes;
730 out << "\n"
731 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
732
733 if (mUsesPointSize)
734 {
735 out << "static float gl_PointSize = float(1);\n";
736 }
737
738 out << "\n"
739 "// Varyings\n";
740 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000741 out << "\n";
742
743 if (mUsesDepthRange)
744 {
745 out << "struct gl_DepthRangeParameters\n"
746 "{\n"
747 " float near;\n"
748 " float far;\n"
749 " float diff;\n"
750 "};\n"
751 "\n";
752 }
753
754 if (mOutputType == SH_HLSL11_OUTPUT)
755 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000756 if (mUsesDepthRange)
757 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000758 out << "cbuffer DriverConstants : register(b1)\n"
759 "{\n"
760 " float3 dx_DepthRange : packoffset(c0);\n"
761 "};\n"
762 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000763 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000764 }
765 else
766 {
767 if (mUsesDepthRange)
768 {
769 out << "uniform float3 dx_DepthRange : register(c0);\n";
770 }
771
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000772 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000773 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000774 }
775
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000776 if (mUsesDepthRange)
777 {
778 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
779 "\n";
780 }
781
782 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000783 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000784
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000785 if (!interfaceBlocks.empty())
786 {
787 out << interfaceBlocks;
788 out << "\n";
789 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400790 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000791
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400792 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
793 {
794 // Return type
795 switch(textureFunction->sampler)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000796 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400797 case EbtSampler2D: out << "float4 "; break;
798 case EbtSampler3D: out << "float4 "; break;
799 case EbtSamplerCube: out << "float4 "; break;
800 case EbtSampler2DArray: out << "float4 "; break;
801 case EbtISampler2D: out << "int4 "; break;
802 case EbtISampler3D: out << "int4 "; break;
803 case EbtISamplerCube: out << "int4 "; break;
804 case EbtISampler2DArray: out << "int4 "; break;
805 case EbtUSampler2D: out << "uint4 "; break;
806 case EbtUSampler3D: out << "uint4 "; break;
807 case EbtUSamplerCube: out << "uint4 "; break;
808 case EbtUSampler2DArray: out << "uint4 "; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400809 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000810 }
811
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400812 // Function name
813 out << textureFunction->name();
814
815 // Argument list
816 int hlslCoords = 4;
817
818 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000819 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400820 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000821 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400822 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
823 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
824 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000825 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400826
827 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000828 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400829 case TextureFunction::IMPLICIT: break;
830 case TextureFunction::BIAS: hlslCoords = 4; break;
831 case TextureFunction::LOD: hlslCoords = 4; break;
832 case TextureFunction::LOD0: hlslCoords = 4; break;
833 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000834 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400835 }
836 else if (mOutputType == SH_HLSL11_OUTPUT)
837 {
838 switch(textureFunction->sampler)
839 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400840 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
841 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
842 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
843 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 2; break;
844 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
845 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
846 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
847 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 2; break;
848 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
849 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
850 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
851 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 2; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400852 default: UNREACHABLE();
853 }
854 }
855 else UNREACHABLE();
856
857 switch(textureFunction->coords)
858 {
859 case 2: out << ", float2 t"; break;
860 case 3: out << ", float3 t"; break;
861 case 4: out << ", float4 t"; break;
862 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000863 }
864
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400865 switch(textureFunction->mipmap)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000866 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400867 case TextureFunction::IMPLICIT: break;
868 case TextureFunction::BIAS: out << ", float bias"; break;
869 case TextureFunction::LOD: out << ", float lod"; break;
870 case TextureFunction::LOD0: break;
871 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000872 }
873
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400874 out << ")\n"
875 "{\n"
876 " return ";
877
878 // HLSL intrinsic
879 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000880 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400881 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000882 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400883 case EbtSampler2D: out << "tex2D"; break;
884 case EbtSamplerCube: out << "texCUBE"; break;
885 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000886 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400887 }
888 else if (mOutputType == SH_HLSL11_OUTPUT)
889 {
890 out << "x.Sample";
891 }
892 else UNREACHABLE();
893
894 if (mOutputType == SH_HLSL9_OUTPUT)
895 {
896 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000897 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400898 case TextureFunction::IMPLICIT: out << "(s, "; break;
899 case TextureFunction::BIAS: out << "bias(s, "; break;
900 case TextureFunction::LOD: out << "lod(s, "; break;
901 case TextureFunction::LOD0: out << "lod(s, "; break;
902 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000903 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400904 }
905 else if (mOutputType == SH_HLSL11_OUTPUT)
906 {
907 switch(textureFunction->mipmap)
908 {
909 case TextureFunction::IMPLICIT: out << "(s, "; break;
910 case TextureFunction::BIAS: out << "Bias(s, "; break;
911 case TextureFunction::LOD: out << "Level(s, "; break;
912 case TextureFunction::LOD0: out << "Level(s, "; break;
913 default: UNREACHABLE();
914 }
915 }
916 else UNREACHABLE();
917
918 switch(hlslCoords)
919 {
920 case 2: out << "float2("; break;
921 case 3: out << "float3("; break;
922 case 4: out << "float4("; break;
923 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000924 }
925
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400926 TString proj = "";
927
928 if (textureFunction->proj)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400929 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400930 switch(textureFunction->coords)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400931 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400932 case 3: proj = " / t.z"; break;
933 case 4: proj = " / t.w"; break;
934 default: UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400935 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400936 }
937
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400938 out << "t.x" + proj + ", t.y" + proj;
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400939
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400940 if (mOutputType == SH_HLSL9_OUTPUT)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400941 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400942 if (hlslCoords >= 3)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400943 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400944 if (textureFunction->coords < 3)
945 {
946 out << ", 0";
947 }
948 else
949 {
950 out << ", t.z" + proj;
951 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400952 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400953
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400954 if (hlslCoords == 4)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400955 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400956 switch(textureFunction->mipmap)
957 {
958 case TextureFunction::BIAS: out << ", bias"; break;
959 case TextureFunction::LOD: out << ", lod"; break;
960 case TextureFunction::LOD0: out << ", 0"; break;
961 default: UNREACHABLE();
962 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400963 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400964
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400965 out << "));\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000966 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400967 else if (mOutputType == SH_HLSL11_OUTPUT)
968 {
969 if (hlslCoords >= 3)
970 {
971 out << ", t.z" + proj;
972 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000973
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400974 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000975 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400976 case TextureFunction::IMPLICIT: out << "));"; break;
977 case TextureFunction::BIAS: out << "), bias);"; break;
978 case TextureFunction::LOD: out << "), lod);"; break;
979 case TextureFunction::LOD0: out << "), 0);"; break;
980 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000981 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000982 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400983 else UNREACHABLE();
984
985 out << "}\n"
986 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987 }
988
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000989 if (mUsesFragCoord)
990 {
991 out << "#define GL_USES_FRAG_COORD\n";
992 }
993
994 if (mUsesPointCoord)
995 {
996 out << "#define GL_USES_POINT_COORD\n";
997 }
998
999 if (mUsesFrontFacing)
1000 {
1001 out << "#define GL_USES_FRONT_FACING\n";
1002 }
1003
1004 if (mUsesPointSize)
1005 {
1006 out << "#define GL_USES_POINT_SIZE\n";
1007 }
1008
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001009 if (mUsesDepthRange)
1010 {
1011 out << "#define GL_USES_DEPTH_RANGE\n";
1012 }
1013
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001014 if (mUsesXor)
1015 {
1016 out << "bool xor(bool p, bool q)\n"
1017 "{\n"
1018 " return (p || q) && !(p && q);\n"
1019 "}\n"
1020 "\n";
1021 }
1022
1023 if (mUsesMod1)
1024 {
1025 out << "float mod(float x, float y)\n"
1026 "{\n"
1027 " return x - y * floor(x / y);\n"
1028 "}\n"
1029 "\n";
1030 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001031
1032 if (mUsesMod2v)
1033 {
1034 out << "float2 mod(float2 x, float2 y)\n"
1035 "{\n"
1036 " return x - y * floor(x / y);\n"
1037 "}\n"
1038 "\n";
1039 }
1040
1041 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001042 {
1043 out << "float2 mod(float2 x, float y)\n"
1044 "{\n"
1045 " return x - y * floor(x / y);\n"
1046 "}\n"
1047 "\n";
1048 }
1049
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001050 if (mUsesMod3v)
1051 {
1052 out << "float3 mod(float3 x, float3 y)\n"
1053 "{\n"
1054 " return x - y * floor(x / y);\n"
1055 "}\n"
1056 "\n";
1057 }
1058
1059 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001060 {
1061 out << "float3 mod(float3 x, float y)\n"
1062 "{\n"
1063 " return x - y * floor(x / y);\n"
1064 "}\n"
1065 "\n";
1066 }
1067
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001068 if (mUsesMod4v)
1069 {
1070 out << "float4 mod(float4 x, float4 y)\n"
1071 "{\n"
1072 " return x - y * floor(x / y);\n"
1073 "}\n"
1074 "\n";
1075 }
1076
1077 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001078 {
1079 out << "float4 mod(float4 x, float y)\n"
1080 "{\n"
1081 " return x - y * floor(x / y);\n"
1082 "}\n"
1083 "\n";
1084 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001085
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001086 if (mUsesFaceforward1)
1087 {
1088 out << "float faceforward(float N, float I, float Nref)\n"
1089 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001090 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001091 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001092 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001093 " }\n"
1094 " else\n"
1095 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001096 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001097 " }\n"
1098 "}\n"
1099 "\n";
1100 }
1101
1102 if (mUsesFaceforward2)
1103 {
1104 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1105 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001106 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001107 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001108 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001109 " }\n"
1110 " else\n"
1111 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001112 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001113 " }\n"
1114 "}\n"
1115 "\n";
1116 }
1117
1118 if (mUsesFaceforward3)
1119 {
1120 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1121 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001122 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001123 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001124 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001125 " }\n"
1126 " else\n"
1127 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001128 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001129 " }\n"
1130 "}\n"
1131 "\n";
1132 }
1133
1134 if (mUsesFaceforward4)
1135 {
1136 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1137 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001138 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001139 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001140 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001141 " }\n"
1142 " else\n"
1143 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001144 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001145 " }\n"
1146 "}\n"
1147 "\n";
1148 }
1149
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001150 for (unsigned int cols = 2; cols <= 4; cols++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001151 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001152 for (unsigned int rows = 2; rows <= 4; rows++)
1153 {
1154 if (mUsesEqualMat[cols][rows])
1155 {
1156 TString matrixType = "float" + str(cols) + "x" + str(rows);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001157
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001158 out << "bool equal(" + matrixType + " m, " + matrixType + " n)\n"
1159 "{\n";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001160
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001161 for (unsigned int row = 0; row < rows; row++)
1162 {
1163 if (row == 0)
1164 {
1165 out << " return ";
1166 }
1167 else
1168 {
1169 out << " ";
1170 }
1171
1172 for (unsigned int col = 0; col < cols; col++)
1173 {
1174 TString index = "[" + str(col) + "][" + str(row) + "]";
1175 out << "m" + index + " == n" + index;
1176
1177 if (col == cols-1 && row == rows-1)
1178 {
1179 out << ";\n";
1180 }
1181 else if (col == cols-1)
1182 {
1183 out << " &&\n";
1184 }
1185 else
1186 {
1187 out << " && ";
1188 }
1189 }
1190 }
1191
1192 out << "}\n";
1193 }
1194 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001195 }
1196
1197 if (mUsesEqualVec2)
1198 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001199 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001200 "{\n"
1201 " return v.x == u.x && v.y == u.y;\n"
1202 "}\n";
1203 }
1204
1205 if (mUsesEqualVec3)
1206 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001207 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001208 "{\n"
1209 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1210 "}\n";
1211 }
1212
1213 if (mUsesEqualVec4)
1214 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001215 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001216 "{\n"
1217 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1218 "}\n";
1219 }
1220
1221 if (mUsesEqualIVec2)
1222 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001223 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001224 "{\n"
1225 " return v.x == u.x && v.y == u.y;\n"
1226 "}\n";
1227 }
1228
1229 if (mUsesEqualIVec3)
1230 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001231 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001232 "{\n"
1233 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1234 "}\n";
1235 }
1236
1237 if (mUsesEqualIVec4)
1238 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001239 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001240 "{\n"
1241 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1242 "}\n";
1243 }
1244
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001245 if (mUsesEqualUVec2)
1246 {
1247 out << "bool equal(uint2 v, uint2 u)\n"
1248 "{\n"
1249 " return v.x == u.x && v.y == u.y;\n"
1250 "}\n";
1251 }
1252
1253 if (mUsesEqualUVec3)
1254 {
1255 out << "bool equal(uint3 v, uint3 u)\n"
1256 "{\n"
1257 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1258 "}\n";
1259 }
1260
1261 if (mUsesEqualUVec4)
1262 {
1263 out << "bool equal(uint4 v, uint4 u)\n"
1264 "{\n"
1265 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1266 "}\n";
1267 }
1268
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001269 if (mUsesEqualBVec2)
1270 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001271 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001272 "{\n"
1273 " return v.x == u.x && v.y == u.y;\n"
1274 "}\n";
1275 }
1276
1277 if (mUsesEqualBVec3)
1278 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001279 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001280 "{\n"
1281 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1282 "}\n";
1283 }
1284
1285 if (mUsesEqualBVec4)
1286 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001287 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001288 "{\n"
1289 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1290 "}\n";
1291 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001292
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001293 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001294 {
1295 out << "float atanyx(float y, float x)\n"
1296 "{\n"
1297 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1298 " return atan2(y, x);\n"
1299 "}\n";
1300 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001301
1302 if (mUsesAtan2_2)
1303 {
1304 out << "float2 atanyx(float2 y, float2 x)\n"
1305 "{\n"
1306 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1307 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1308 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1309 "}\n";
1310 }
1311
1312 if (mUsesAtan2_3)
1313 {
1314 out << "float3 atanyx(float3 y, float3 x)\n"
1315 "{\n"
1316 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1317 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1318 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1319 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1320 "}\n";
1321 }
1322
1323 if (mUsesAtan2_4)
1324 {
1325 out << "float4 atanyx(float4 y, float4 x)\n"
1326 "{\n"
1327 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1328 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1329 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1330 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1331 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1332 "}\n";
1333 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001334}
1335
1336void OutputHLSL::visitSymbol(TIntermSymbol *node)
1337{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001338 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001339
1340 TString name = node->getSymbol();
1341
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001342 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001343 {
1344 mUsesDepthRange = true;
1345 out << name;
1346 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001347 else
1348 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001349 TQualifier qualifier = node->getQualifier();
1350
1351 if (qualifier == EvqUniform)
1352 {
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001353 if (node->getType().isInterfaceBlockMember())
1354 {
1355 const TString& interfaceBlockTypeName = node->getType().getInterfaceBlockType()->getTypeName();
1356 mReferencedInterfaceBlocks[interfaceBlockTypeName] = node;
1357 out << decorateUniform(name, node->getType());
1358 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001359 else if (node->getBasicType() == EbtInterfaceBlock)
1360 {
1361 const TString& interfaceBlockTypeName = node->getType().getTypeName();
1362 mReferencedInterfaceBlocks[interfaceBlockTypeName] = node;
1363 out << decorateUniform(name, node->getType());
1364 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001365 else
1366 {
1367 mReferencedUniforms[name] = node;
1368 out << decorateUniform(name, node->getType());
1369 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001370 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001371 else if (qualifier == EvqAttribute || qualifier == EvqVertexInput)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001372 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001373 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001374 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001375 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001376 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001377 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001378 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001379 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001380 }
Jamie Madill46131a32013-06-20 11:55:50 -04001381 else if (qualifier == EvqFragmentOutput)
1382 {
1383 mReferencedOutputVariables[name] = node;
1384 out << "out_" << name;
1385 }
1386 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001387 {
1388 out << "gl_Color[0]";
1389 mUsesFragColor = true;
1390 }
1391 else if (qualifier == EvqFragData)
1392 {
1393 out << "gl_Color";
1394 mUsesFragData = true;
1395 }
1396 else if (qualifier == EvqFragCoord)
1397 {
1398 mUsesFragCoord = true;
1399 out << name;
1400 }
1401 else if (qualifier == EvqPointCoord)
1402 {
1403 mUsesPointCoord = true;
1404 out << name;
1405 }
1406 else if (qualifier == EvqFrontFacing)
1407 {
1408 mUsesFrontFacing = true;
1409 out << name;
1410 }
1411 else if (qualifier == EvqPointSize)
1412 {
1413 mUsesPointSize = true;
1414 out << name;
1415 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001416 else
1417 {
1418 out << decorate(name);
1419 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001420 }
1421}
1422
1423bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1424{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001425 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001426
1427 switch (node->getOp())
1428 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001429 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001430 case EOpInitialize:
1431 if (visit == PreVisit)
1432 {
1433 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1434 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1435 // new variable is created before the assignment is evaluated), so we need to convert
1436 // this to "float t = x, x = t;".
1437
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001438 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1439 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001440
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001441 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1442 expression->traverse(&searchSymbol);
1443 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001444
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001445 if (sameSymbol)
1446 {
1447 // Type already printed
1448 out << "t" + str(mUniqueIndex) + " = ";
1449 expression->traverse(this);
1450 out << ", ";
1451 symbolNode->traverse(this);
1452 out << " = t" + str(mUniqueIndex);
1453
1454 mUniqueIndex++;
1455 return false;
1456 }
1457 }
1458 else if (visit == InVisit)
1459 {
1460 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001461 }
1462 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001463 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1464 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1465 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1466 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1467 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1468 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001469 if (visit == PreVisit)
1470 {
1471 out << "(";
1472 }
1473 else if (visit == InVisit)
1474 {
1475 out << " = mul(";
1476 node->getLeft()->traverse(this);
1477 out << ", transpose(";
1478 }
1479 else
1480 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001481 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001482 }
1483 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001484 case EOpMatrixTimesMatrixAssign:
1485 if (visit == PreVisit)
1486 {
1487 out << "(";
1488 }
1489 else if (visit == InVisit)
1490 {
1491 out << " = mul(";
1492 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001493 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001494 }
1495 else
1496 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001497 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001498 }
1499 break;
1500 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001501 case EOpIndexDirect:
1502 if (node->getLeft()->getBasicType() == EbtInterfaceBlock)
1503 {
1504 if (visit == PreVisit)
1505 {
1506 const TType &interfaceBlockType = node->getLeft()->getType();
1507 mReferencedInterfaceBlocks[interfaceBlockType.getInstanceName()] = node->getLeft()->getAsSymbolNode();
1508 out << interfaceBlockInstanceString(interfaceBlockType, node->getRight()->getAsConstantUnion()->getIConst(0));
1509 return false;
1510 }
1511 }
1512 else
1513 {
1514 outputTriplet(visit, "", "[", "]");
1515 }
1516 break;
1517 case EOpIndexIndirect:
1518 // We do not currently support indirect references to interface blocks
1519 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1520 outputTriplet(visit, "", "[", "]");
1521 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001522 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001523 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001524 if (visit == InVisit)
1525 {
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001526 out << "." + decorateField(node->getType().getFieldName(), node->getLeft()->getType());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001527
1528 return false;
1529 }
1530 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001531 case EOpVectorSwizzle:
1532 if (visit == InVisit)
1533 {
1534 out << ".";
1535
1536 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1537
1538 if (swizzle)
1539 {
1540 TIntermSequence &sequence = swizzle->getSequence();
1541
1542 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1543 {
1544 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1545
1546 if (element)
1547 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001548 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001549
1550 switch (i)
1551 {
1552 case 0: out << "x"; break;
1553 case 1: out << "y"; break;
1554 case 2: out << "z"; break;
1555 case 3: out << "w"; break;
1556 default: UNREACHABLE();
1557 }
1558 }
1559 else UNREACHABLE();
1560 }
1561 }
1562 else UNREACHABLE();
1563
1564 return false; // Fully processed
1565 }
1566 break;
1567 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1568 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1569 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1570 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001571 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001572 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001573 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001574 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001575 if (node->getOp() == EOpEqual)
1576 {
1577 outputTriplet(visit, "(", " == ", ")");
1578 }
1579 else
1580 {
1581 outputTriplet(visit, "(", " != ", ")");
1582 }
1583 }
1584 else if (node->getLeft()->getBasicType() == EbtStruct)
1585 {
1586 if (node->getOp() == EOpEqual)
1587 {
1588 out << "(";
1589 }
1590 else
1591 {
1592 out << "!(";
1593 }
1594
1595 const TTypeList *fields = node->getLeft()->getType().getStruct();
1596
1597 for (size_t i = 0; i < fields->size(); i++)
1598 {
1599 const TType *fieldType = (*fields)[i].type;
1600
1601 node->getLeft()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001602 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType()) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001603 node->getRight()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001604 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001605
1606 if (i < fields->size() - 1)
1607 {
1608 out << " && ";
1609 }
1610 }
1611
1612 out << ")";
1613
1614 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001615 }
1616 else
1617 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001618 if (node->getLeft()->isMatrix())
1619 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001620 mUsesEqualMat[node->getLeft()->getCols()][node->getLeft()->getRows()] = true;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001621 }
1622 else if (node->getLeft()->isVector())
1623 {
1624 switch (node->getLeft()->getBasicType())
1625 {
1626 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001627 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001628 {
1629 case 2: mUsesEqualVec2 = true; break;
1630 case 3: mUsesEqualVec3 = true; break;
1631 case 4: mUsesEqualVec4 = true; break;
1632 default: UNREACHABLE();
1633 }
1634 break;
1635 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001636 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001637 {
1638 case 2: mUsesEqualIVec2 = true; break;
1639 case 3: mUsesEqualIVec3 = true; break;
1640 case 4: mUsesEqualIVec4 = true; break;
1641 default: UNREACHABLE();
1642 }
1643 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001644 case EbtUInt:
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001645 switch (node->getLeft()->getNominalSize())
1646 {
1647 case 2: mUsesEqualUVec2 = true; break;
1648 case 3: mUsesEqualUVec3 = true; break;
1649 case 4: mUsesEqualUVec4 = true; break;
1650 default: UNREACHABLE();
1651 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001652 break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001653 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001654 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001655 {
1656 case 2: mUsesEqualBVec2 = true; break;
1657 case 3: mUsesEqualBVec3 = true; break;
1658 case 4: mUsesEqualBVec4 = true; break;
1659 default: UNREACHABLE();
1660 }
1661 break;
1662 default: UNREACHABLE();
1663 }
1664 }
1665 else UNREACHABLE();
1666
1667 if (node->getOp() == EOpEqual)
1668 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001669 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001670 }
1671 else
1672 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001673 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001674 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001675 }
1676 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1678 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1679 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1680 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1681 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001682 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001683 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1684 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001685 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001686 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001687 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001688 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001689 case EOpLogicalXor:
1690 mUsesXor = true;
1691 outputTriplet(visit, "xor(", ", ", ")");
1692 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001693 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001694 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001695 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696 default: UNREACHABLE();
1697 }
1698
1699 return true;
1700}
1701
1702bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1703{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 switch (node->getOp())
1705 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001706 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1707 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1708 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1709 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1710 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1711 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1712 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001713 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001714 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001715 case EOpConvFloatToBool:
1716 switch (node->getOperand()->getType().getNominalSize())
1717 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001718 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1719 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1720 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1721 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001722 default: UNREACHABLE();
1723 }
1724 break;
1725 case EOpConvBoolToFloat:
1726 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001727 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728 switch (node->getOperand()->getType().getNominalSize())
1729 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001730 case 1: outputTriplet(visit, "float(", "", ")"); break;
1731 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1732 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1733 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734 default: UNREACHABLE();
1735 }
1736 break;
1737 case EOpConvFloatToInt:
1738 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001739 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740 switch (node->getOperand()->getType().getNominalSize())
1741 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001742 case 1: outputTriplet(visit, "int(", "", ")"); break;
1743 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1744 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1745 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746 default: UNREACHABLE();
1747 }
1748 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001749 case EOpConvFloatToUInt:
1750 case EOpConvBoolToUInt:
1751 case EOpConvIntToUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001752 switch (node->getOperand()->getType().getCols())
1753 {
1754 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001755 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1756 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1757 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001758 default: UNREACHABLE();
1759 }
1760 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001761 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1762 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1763 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1764 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1765 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1766 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1767 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1768 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1769 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1770 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1771 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1772 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1773 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1774 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1775 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1776 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1777 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1778 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1779 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1780 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1781 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001782 case EOpDFdx:
1783 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1784 {
1785 outputTriplet(visit, "(", "", ", 0.0)");
1786 }
1787 else
1788 {
1789 outputTriplet(visit, "ddx(", "", ")");
1790 }
1791 break;
1792 case EOpDFdy:
1793 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1794 {
1795 outputTriplet(visit, "(", "", ", 0.0)");
1796 }
1797 else
1798 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001799 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001800 }
1801 break;
1802 case EOpFwidth:
1803 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1804 {
1805 outputTriplet(visit, "(", "", ", 0.0)");
1806 }
1807 else
1808 {
1809 outputTriplet(visit, "fwidth(", "", ")");
1810 }
1811 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001812 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1813 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814 default: UNREACHABLE();
1815 }
1816
1817 return true;
1818}
1819
1820bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1821{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001822 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001823
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824 switch (node->getOp())
1825 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001826 case EOpSequence:
1827 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001828 if (mInsideFunction)
1829 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001830 outputLineDirective(node->getLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001831 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001832
1833 mScopeDepth++;
1834
1835 if (mScopeBracket.size() < mScopeDepth)
1836 {
1837 mScopeBracket.push_back(0); // New scope level
1838 }
1839 else
1840 {
1841 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1842 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001843 }
1844
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001845 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1846 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001847 outputLineDirective((*sit)->getLine());
1848
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001849 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001850
1851 out << ";\n";
1852 }
1853
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001854 if (mInsideFunction)
1855 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001856 outputLineDirective(node->getEndLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001857 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001858
1859 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001860 }
1861
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001862 return false;
1863 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864 case EOpDeclaration:
1865 if (visit == PreVisit)
1866 {
1867 TIntermSequence &sequence = node->getSequence();
1868 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001870 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001872 if (variable->getType().getStruct())
1873 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001874 addConstructor(variable->getType(), scopedStruct(variable->getType().getTypeName()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001875 }
1876
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001877 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001879 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001880 {
1881 out << "static ";
1882 }
1883
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001884 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001886 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001888 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001890 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001892 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001893 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00001894 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001895 }
1896 else
1897 {
1898 (*sit)->traverse(this);
1899 }
1900
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001901 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001902 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001903 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904 }
1905 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001906 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001907 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1908 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001909 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001910 }
1911 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001913 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001914 {
1915 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1916 {
1917 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1918
1919 if (symbol)
1920 {
1921 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1922 mReferencedVaryings[symbol->getSymbol()] = symbol;
1923 }
1924 else
1925 {
1926 (*sit)->traverse(this);
1927 }
1928 }
1929 }
1930
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001931 return false;
1932 }
1933 else if (visit == InVisit)
1934 {
1935 out << ", ";
1936 }
1937 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001938 case EOpPrototype:
1939 if (visit == PreVisit)
1940 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001941 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001942
1943 TIntermSequence &arguments = node->getSequence();
1944
1945 for (unsigned int i = 0; i < arguments.size(); i++)
1946 {
1947 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1948
1949 if (symbol)
1950 {
1951 out << argumentString(symbol);
1952
1953 if (i < arguments.size() - 1)
1954 {
1955 out << ", ";
1956 }
1957 }
1958 else UNREACHABLE();
1959 }
1960
1961 out << ");\n";
1962
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001963 // Also prototype the Lod0 variant if needed
1964 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1965 {
1966 mOutputLod0Function = true;
1967 node->traverse(this);
1968 mOutputLod0Function = false;
1969 }
1970
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001971 return false;
1972 }
1973 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001974 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001975 case EOpFunction:
1976 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001977 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001979 out << typeString(node->getType()) << " ";
1980
1981 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001983 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001984 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001985 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001986 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001987 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001988 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001989
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001990 TIntermSequence &sequence = node->getSequence();
1991 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1992
1993 for (unsigned int i = 0; i < arguments.size(); i++)
1994 {
1995 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1996
1997 if (symbol)
1998 {
1999 if (symbol->getType().getStruct())
2000 {
2001 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getTypeName()), NULL);
2002 }
2003
2004 out << argumentString(symbol);
2005
2006 if (i < arguments.size() - 1)
2007 {
2008 out << ", ";
2009 }
2010 }
2011 else UNREACHABLE();
2012 }
2013
2014 out << ")\n"
2015 "{\n";
2016
2017 if (sequence.size() > 1)
2018 {
2019 mInsideFunction = true;
2020 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002021 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002023
2024 out << "}\n";
2025
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002026 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2027 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002028 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002029 {
2030 mOutputLod0Function = true;
2031 node->traverse(this);
2032 mOutputLod0Function = false;
2033 }
2034 }
2035
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002036 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037 }
2038 break;
2039 case EOpFunctionCall:
2040 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002041 TString name = TFunction::unmangleName(node->getName());
2042 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002043 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002044
2045 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002047 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002048 }
2049 else
2050 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002051 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2052
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002053 TextureFunction textureFunction;
2054 textureFunction.sampler = samplerType;
2055 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
2056 textureFunction.mipmap = TextureFunction::IMPLICIT;
2057
2058 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002059 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002060 textureFunction.mipmap = TextureFunction::IMPLICIT;
2061 textureFunction.proj = false;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002062 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002063 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002064 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002065 textureFunction.mipmap = TextureFunction::IMPLICIT;
2066 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002067 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002068 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002069 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002070 textureFunction.mipmap = TextureFunction::LOD;
2071 textureFunction.proj = false;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002072 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002073 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002074 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002075 textureFunction.mipmap = TextureFunction::LOD;
2076 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002077 }
2078 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002079
2080 if (textureFunction.mipmap != TextureFunction::LOD)
2081 {
2082 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2083 {
2084 textureFunction.mipmap = TextureFunction::LOD0;
2085 }
2086 else if (arguments.size() == 3)
2087 {
2088 textureFunction.mipmap = TextureFunction::BIAS;
2089 }
2090 }
2091
2092 mUsesTexture.insert(textureFunction);
2093
2094 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002096
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002097 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2098 {
2099 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2100 {
2101 out << "texture_";
2102 (*arg)->traverse(this);
2103 out << ", sampler_";
2104 }
2105
2106 (*arg)->traverse(this);
2107
2108 if (arg < arguments.end() - 1)
2109 {
2110 out << ", ";
2111 }
2112 }
2113
2114 out << ")";
2115
2116 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117 }
2118 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002119 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002120 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002121 addConstructor(node->getType(), "vec1", &node->getSequence());
2122 outputTriplet(visit, "vec1(", "", ")");
2123 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002124 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002125 addConstructor(node->getType(), "vec2", &node->getSequence());
2126 outputTriplet(visit, "vec2(", ", ", ")");
2127 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002128 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002129 addConstructor(node->getType(), "vec3", &node->getSequence());
2130 outputTriplet(visit, "vec3(", ", ", ")");
2131 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002132 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002133 addConstructor(node->getType(), "vec4", &node->getSequence());
2134 outputTriplet(visit, "vec4(", ", ", ")");
2135 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002136 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002137 addConstructor(node->getType(), "bvec1", &node->getSequence());
2138 outputTriplet(visit, "bvec1(", "", ")");
2139 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002140 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002141 addConstructor(node->getType(), "bvec2", &node->getSequence());
2142 outputTriplet(visit, "bvec2(", ", ", ")");
2143 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002144 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002145 addConstructor(node->getType(), "bvec3", &node->getSequence());
2146 outputTriplet(visit, "bvec3(", ", ", ")");
2147 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002148 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002149 addConstructor(node->getType(), "bvec4", &node->getSequence());
2150 outputTriplet(visit, "bvec4(", ", ", ")");
2151 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002152 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002153 addConstructor(node->getType(), "ivec1", &node->getSequence());
2154 outputTriplet(visit, "ivec1(", "", ")");
2155 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002156 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002157 addConstructor(node->getType(), "ivec2", &node->getSequence());
2158 outputTriplet(visit, "ivec2(", ", ", ")");
2159 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002160 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002161 addConstructor(node->getType(), "ivec3", &node->getSequence());
2162 outputTriplet(visit, "ivec3(", ", ", ")");
2163 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002164 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002165 addConstructor(node->getType(), "ivec4", &node->getSequence());
2166 outputTriplet(visit, "ivec4(", ", ", ")");
2167 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002168 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002169 addConstructor(node->getType(), "uvec1", &node->getSequence());
2170 outputTriplet(visit, "uvec1(", "", ")");
2171 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002172 case EOpConstructUVec2:
2173 addConstructor(node->getType(), "uvec2", &node->getSequence());
2174 outputTriplet(visit, "uvec2(", ", ", ")");
2175 break;
2176 case EOpConstructUVec3:
2177 addConstructor(node->getType(), "uvec3", &node->getSequence());
2178 outputTriplet(visit, "uvec3(", ", ", ")");
2179 break;
2180 case EOpConstructUVec4:
2181 addConstructor(node->getType(), "uvec4", &node->getSequence());
2182 outputTriplet(visit, "uvec4(", ", ", ")");
2183 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002184 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002185 addConstructor(node->getType(), "mat2", &node->getSequence());
2186 outputTriplet(visit, "mat2(", ", ", ")");
2187 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002188 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002189 addConstructor(node->getType(), "mat3", &node->getSequence());
2190 outputTriplet(visit, "mat3(", ", ", ")");
2191 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002192 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002193 addConstructor(node->getType(), "mat4", &node->getSequence());
2194 outputTriplet(visit, "mat4(", ", ", ")");
2195 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002196 case EOpConstructStruct:
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002197 addConstructor(node->getType(), scopedStruct(node->getType().getTypeName()), &node->getSequence());
2198 outputTriplet(visit, structLookup(node->getType().getTypeName()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002199 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002200 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2201 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2202 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2203 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2204 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2205 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002206 case EOpMod:
2207 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002208 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002209 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2210 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2211 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002212 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002213 case 11: mUsesMod1 = true; break;
2214 case 22: mUsesMod2v = true; break;
2215 case 21: mUsesMod2f = true; break;
2216 case 33: mUsesMod3v = true; break;
2217 case 31: mUsesMod3f = true; break;
2218 case 44: mUsesMod4v = true; break;
2219 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002220 default: UNREACHABLE();
2221 }
2222
2223 outputTriplet(visit, "mod(", ", ", ")");
2224 }
2225 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002226 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002227 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002228 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002229 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2230 {
2231 case 1: mUsesAtan2_1 = true; break;
2232 case 2: mUsesAtan2_2 = true; break;
2233 case 3: mUsesAtan2_3 = true; break;
2234 case 4: mUsesAtan2_4 = true; break;
2235 default: UNREACHABLE();
2236 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002237 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238 break;
2239 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2240 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2241 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2242 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2243 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2244 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2245 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2246 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2247 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002248 case EOpFaceForward:
2249 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002250 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002251 {
2252 case 1: mUsesFaceforward1 = true; break;
2253 case 2: mUsesFaceforward2 = true; break;
2254 case 3: mUsesFaceforward3 = true; break;
2255 case 4: mUsesFaceforward4 = true; break;
2256 default: UNREACHABLE();
2257 }
2258
2259 outputTriplet(visit, "faceforward(", ", ", ")");
2260 }
2261 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2263 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2264 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265 default: UNREACHABLE();
2266 }
2267
2268 return true;
2269}
2270
2271bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2272{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002273 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002275 if (node->usesTernaryOperator())
2276 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002277 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002278 }
2279 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002281 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002282
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002283 out << "if(";
2284
2285 node->getCondition()->traverse(this);
2286
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002287 out << ")\n";
2288
2289 outputLineDirective(node->getLine());
2290 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291
daniel@transgaming.combb885322010-04-15 20:45:24 +00002292 if (node->getTrueBlock())
2293 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002294 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002295 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002297 outputLineDirective(node->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002298 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002299
2300 if (node->getFalseBlock())
2301 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002302 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002303
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002304 outputLineDirective(node->getFalseBlock()->getLine());
2305 out << "{\n";
2306
2307 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002308 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002309
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002310 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002311 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002312 }
2313 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002314
2315 return false;
2316}
2317
2318void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2319{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002320 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321}
2322
2323bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2324{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002325 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2326
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002327 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002328 {
2329 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2330 }
2331
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002332 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002333 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002334 if (handleExcessiveLoop(node))
2335 {
2336 return false;
2337 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002338 }
2339
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002340 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341
alokp@chromium.org52813552010-11-16 18:36:09 +00002342 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002343 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002344 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002345
2346 outputLineDirective(node->getLine());
2347 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 }
2349 else
2350 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002351 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352
2353 if (node->getInit())
2354 {
2355 node->getInit()->traverse(this);
2356 }
2357
2358 out << "; ";
2359
alokp@chromium.org52813552010-11-16 18:36:09 +00002360 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002362 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 }
2364
2365 out << "; ";
2366
alokp@chromium.org52813552010-11-16 18:36:09 +00002367 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002369 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 }
2371
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002372 out << ")\n";
2373
2374 outputLineDirective(node->getLine());
2375 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 }
2377
2378 if (node->getBody())
2379 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002380 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 }
2382
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002383 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002384 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385
alokp@chromium.org52813552010-11-16 18:36:09 +00002386 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002388 outputLineDirective(node->getCondition()->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 out << "while(\n";
2390
alokp@chromium.org52813552010-11-16 18:36:09 +00002391 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392
daniel@transgaming.com73536982012-03-21 20:45:49 +00002393 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394 }
2395
daniel@transgaming.com73536982012-03-21 20:45:49 +00002396 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002397
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002398 mInsideDiscontinuousLoop = wasDiscontinuous;
2399
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 return false;
2401}
2402
2403bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2404{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002405 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406
2407 switch (node->getFlowOp())
2408 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002409 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002410 case EOpBreak:
2411 if (visit == PreVisit)
2412 {
2413 if (mExcessiveLoopIndex)
2414 {
2415 out << "{Break";
2416 mExcessiveLoopIndex->traverse(this);
2417 out << " = true; break;}\n";
2418 }
2419 else
2420 {
2421 out << "break;\n";
2422 }
2423 }
2424 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002425 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 case EOpReturn:
2427 if (visit == PreVisit)
2428 {
2429 if (node->getExpression())
2430 {
2431 out << "return ";
2432 }
2433 else
2434 {
2435 out << "return;\n";
2436 }
2437 }
2438 else if (visit == PostVisit)
2439 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002440 if (node->getExpression())
2441 {
2442 out << ";\n";
2443 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 }
2445 break;
2446 default: UNREACHABLE();
2447 }
2448
2449 return true;
2450}
2451
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002452void OutputHLSL::traverseStatements(TIntermNode *node)
2453{
2454 if (isSingleStatement(node))
2455 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002456 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002457 }
2458
2459 node->traverse(this);
2460}
2461
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002462bool OutputHLSL::isSingleStatement(TIntermNode *node)
2463{
2464 TIntermAggregate *aggregate = node->getAsAggregate();
2465
2466 if (aggregate)
2467 {
2468 if (aggregate->getOp() == EOpSequence)
2469 {
2470 return false;
2471 }
2472 else
2473 {
2474 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2475 {
2476 if (!isSingleStatement(*sit))
2477 {
2478 return false;
2479 }
2480 }
2481
2482 return true;
2483 }
2484 }
2485
2486 return true;
2487}
2488
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002489// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2490// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002491bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2492{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002493 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002494 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002495
2496 // Parse loops of the form:
2497 // for(int index = initial; index [comparator] limit; index += increment)
2498 TIntermSymbol *index = NULL;
2499 TOperator comparator = EOpNull;
2500 int initial = 0;
2501 int limit = 0;
2502 int increment = 0;
2503
2504 // Parse index name and intial value
2505 if (node->getInit())
2506 {
2507 TIntermAggregate *init = node->getInit()->getAsAggregate();
2508
2509 if (init)
2510 {
2511 TIntermSequence &sequence = init->getSequence();
2512 TIntermTyped *variable = sequence[0]->getAsTyped();
2513
2514 if (variable && variable->getQualifier() == EvqTemporary)
2515 {
2516 TIntermBinary *assign = variable->getAsBinaryNode();
2517
2518 if (assign->getOp() == EOpInitialize)
2519 {
2520 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2521 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2522
2523 if (symbol && constant)
2524 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002525 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002526 {
2527 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002528 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002529 }
2530 }
2531 }
2532 }
2533 }
2534 }
2535
2536 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002537 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002538 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002539 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002540
2541 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2542 {
2543 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2544
2545 if (constant)
2546 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002547 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002548 {
2549 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002550 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002551 }
2552 }
2553 }
2554 }
2555
2556 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002557 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002558 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002559 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2560 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002561
2562 if (binaryTerminal)
2563 {
2564 TOperator op = binaryTerminal->getOp();
2565 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2566
2567 if (constant)
2568 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002569 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002570 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002571 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002572
2573 switch (op)
2574 {
2575 case EOpAddAssign: increment = value; break;
2576 case EOpSubAssign: increment = -value; break;
2577 default: UNIMPLEMENTED();
2578 }
2579 }
2580 }
2581 }
2582 else if (unaryTerminal)
2583 {
2584 TOperator op = unaryTerminal->getOp();
2585
2586 switch (op)
2587 {
2588 case EOpPostIncrement: increment = 1; break;
2589 case EOpPostDecrement: increment = -1; break;
2590 case EOpPreIncrement: increment = 1; break;
2591 case EOpPreDecrement: increment = -1; break;
2592 default: UNIMPLEMENTED();
2593 }
2594 }
2595 }
2596
2597 if (index != NULL && comparator != EOpNull && increment != 0)
2598 {
2599 if (comparator == EOpLessThanEqual)
2600 {
2601 comparator = EOpLessThan;
2602 limit += 1;
2603 }
2604
2605 if (comparator == EOpLessThan)
2606 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002607 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002608
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002609 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002610 {
2611 return false; // Not an excessive loop
2612 }
2613
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002614 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2615 mExcessiveLoopIndex = index;
2616
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002617 out << "{int ";
2618 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002619 out << ";\n"
2620 "bool Break";
2621 index->traverse(this);
2622 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002623
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002624 bool firstLoopFragment = true;
2625
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002626 while (iterations > 0)
2627 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002628 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002629
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002630 if (!firstLoopFragment)
2631 {
2632 out << "if(!Break";
2633 index->traverse(this);
2634 out << ") {\n";
2635 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002636
2637 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2638 {
2639 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2640 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002641
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002642 // for(int index = initial; index < clampedLimit; index += increment)
2643
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002644 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002645 index->traverse(this);
2646 out << " = ";
2647 out << initial;
2648
2649 out << "; ";
2650 index->traverse(this);
2651 out << " < ";
2652 out << clampedLimit;
2653
2654 out << "; ";
2655 index->traverse(this);
2656 out << " += ";
2657 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002658 out << ")\n";
2659
2660 outputLineDirective(node->getLine());
2661 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002662
2663 if (node->getBody())
2664 {
2665 node->getBody()->traverse(this);
2666 }
2667
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002668 outputLineDirective(node->getLine());
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002669 out << ";}\n";
2670
2671 if (!firstLoopFragment)
2672 {
2673 out << "}\n";
2674 }
2675
2676 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002677
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002678 initial += MAX_LOOP_ITERATIONS * increment;
2679 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002680 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002681
2682 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002683
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002684 mExcessiveLoopIndex = restoreIndex;
2685
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002686 return true;
2687 }
2688 else UNIMPLEMENTED();
2689 }
2690
2691 return false; // Not handled as an excessive loop
2692}
2693
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002694void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002695{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002696 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002697
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002698 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699 {
2700 out << preString;
2701 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002702 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703 {
2704 out << inString;
2705 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002706 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707 {
2708 out << postString;
2709 }
2710}
2711
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002712void OutputHLSL::outputLineDirective(int line)
2713{
2714 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2715 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002716 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002717 mBody << "#line " << line;
2718
2719 if (mContext.sourcePath)
2720 {
2721 mBody << " \"" << mContext.sourcePath << "\"";
2722 }
2723
2724 mBody << "\n";
2725 }
2726}
2727
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002728TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2729{
2730 TQualifier qualifier = symbol->getQualifier();
2731 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002732 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002733
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002734 if (name.empty()) // HLSL demands named arguments, also for prototypes
2735 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002736 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002737 }
2738 else
2739 {
2740 name = decorate(name);
2741 }
2742
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002743 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2744 {
2745 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2746 qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type);
2747 }
2748
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002749 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002750}
2751
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002752TString OutputHLSL::interpolationString(TQualifier qualifier)
2753{
2754 switch(qualifier)
2755 {
2756 case EvqVaryingIn: return "";
2757 case EvqInvariantVaryingIn: return "";
2758 case EvqSmoothIn: return "linear";
2759 case EvqFlatIn: return "nointerpolation";
2760 case EvqCentroidIn: return "centroid";
2761 case EvqVaryingOut: return "";
2762 case EvqInvariantVaryingOut: return "";
2763 case EvqSmoothOut: return "linear";
2764 case EvqFlatOut: return "nointerpolation";
2765 case EvqCentroidOut: return "centroid";
2766 default: UNREACHABLE();
2767 }
2768
2769 return "";
2770}
2771
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002772TString OutputHLSL::qualifierString(TQualifier qualifier)
2773{
2774 switch(qualifier)
2775 {
2776 case EvqIn: return "in";
2777 case EvqOut: return "out";
2778 case EvqInOut: return "inout";
2779 case EvqConstReadOnly: return "const";
2780 default: UNREACHABLE();
2781 }
2782
2783 return "";
2784}
2785
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786TString OutputHLSL::typeString(const TType &type)
2787{
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002788 if (type.getBasicType() == EbtStruct)
2789 {
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002790 if (type.getTypeName() != "")
2791 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002792 return structLookup(type.getTypeName());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002793 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002794 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002795 {
Jamie Madillc835df62013-06-21 09:15:32 -04002796 return structureString(type, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002797 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002798 }
2799 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002800 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00002801 int cols = type.getCols();
2802 int rows = type.getRows();
2803 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804 }
2805 else
2806 {
2807 switch (type.getBasicType())
2808 {
2809 case EbtFloat:
2810 switch (type.getNominalSize())
2811 {
2812 case 1: return "float";
2813 case 2: return "float2";
2814 case 3: return "float3";
2815 case 4: return "float4";
2816 }
2817 case EbtInt:
2818 switch (type.getNominalSize())
2819 {
2820 case 1: return "int";
2821 case 2: return "int2";
2822 case 3: return "int3";
2823 case 4: return "int4";
2824 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002825 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04002826 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002827 {
2828 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002829 case 2: return "uint2";
2830 case 3: return "uint3";
2831 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002832 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002833 case EbtBool:
2834 switch (type.getNominalSize())
2835 {
2836 case 1: return "bool";
2837 case 2: return "bool2";
2838 case 3: return "bool3";
2839 case 4: return "bool4";
2840 }
2841 case EbtVoid:
2842 return "void";
2843 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002844 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04002845 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04002846 case EbtSampler2DArray:
2847 case EbtISampler2DArray:
2848 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002849 return "sampler2D";
2850 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002851 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04002852 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002853 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00002854 case EbtSamplerExternalOES:
2855 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002856 default:
2857 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002858 }
2859 }
2860
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002861 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002862 return "<unknown type>";
2863}
2864
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002865TString OutputHLSL::textureString(const TType &type)
2866{
2867 switch (type.getBasicType())
2868 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -04002869 case EbtSampler2D: return "Texture2D";
2870 case EbtSamplerCube: return "TextureCube";
2871 case EbtSamplerExternalOES: return "Texture2D";
2872 case EbtSampler2DArray: return "Texture2DArray";
2873 case EbtISampler2D: return "Texture2D<int4>";
2874 case EbtISamplerCube: return "TextureCube<int4>";
2875 case EbtISampler2DArray: return "Texture2DArray<int4>";
2876 case EbtUSampler2D: return "Texture2D<uint4>";
2877 case EbtUSamplerCube: return "TextureCube<uint4>";
2878 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002879 default:
2880 break;
2881 }
2882
2883 UNREACHABLE();
2884 return "<unknown texture type>";
2885}
2886
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002887TString OutputHLSL::arrayString(const TType &type)
2888{
2889 if (!type.isArray())
2890 {
2891 return "";
2892 }
2893
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002894 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002895}
2896
2897TString OutputHLSL::initializer(const TType &type)
2898{
2899 TString string;
2900
daniel@transgaming.comead23042010-04-29 03:35:36 +00002901 for (int component = 0; component < type.getObjectSize(); component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002902 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002903 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002904
daniel@transgaming.comead23042010-04-29 03:35:36 +00002905 if (component < type.getObjectSize() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002906 {
2907 string += ", ";
2908 }
2909 }
2910
daniel@transgaming.comead23042010-04-29 03:35:36 +00002911 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002912}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002913
Jamie Madillc835df62013-06-21 09:15:32 -04002914TString OutputHLSL::structureString(const TType &structType, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04002915{
2916 ASSERT(structType.getStruct());
2917
2918 const TTypeList &fields = *structType.getStruct();
2919 const bool isNameless = (structType.getTypeName() == "");
Jamie Madillc835df62013-06-21 09:15:32 -04002920 const TString &structName = structureTypeName(structType, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04002921
2922 const TString declareString = (isNameless ? "struct" : "struct " + structName);
2923
2924 TString structure;
2925 structure += declareString + "\n"
2926 "{\n";
2927
Jamie Madillc835df62013-06-21 09:15:32 -04002928 int elementIndex = 0;
2929
Jamie Madill9cf6c072013-06-20 11:55:53 -04002930 for (unsigned int i = 0; i < fields.size(); i++)
2931 {
2932 const TType &field = *fields[i].type;
2933
Jamie Madillc835df62013-06-21 09:15:32 -04002934 if (useStd140Packing)
2935 {
2936 structure += std140PrePaddingString(field, &elementIndex);
2937 }
2938
2939 structure += " " + structureTypeName(field, useHLSLRowMajorPacking, useStd140Packing) + " " +
Jamie Madill9cf6c072013-06-20 11:55:53 -04002940 decorateField(field.getFieldName(), structType) + arrayString(field) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04002941
2942 if (useStd140Packing)
2943 {
Jamie Madille4075c92013-06-21 09:15:32 -04002944 structure += std140PostPaddingString(field, useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04002945 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04002946 }
2947
2948 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
2949 structure += (isNameless ? "} " : "};\n");
2950
Jamie Madille4075c92013-06-21 09:15:32 -04002951 // Add remaining element index to the global map, for use with nested structs in standard layouts
2952 if (useStd140Packing)
2953 {
2954 mStd140StructElementIndexes[structName] = elementIndex;
2955 }
2956
Jamie Madill9cf6c072013-06-20 11:55:53 -04002957 return structure;
2958}
2959
Jamie Madillc835df62013-06-21 09:15:32 -04002960TString OutputHLSL::structureTypeName(const TType &structType, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04002961{
2962 if (structType.getBasicType() != EbtStruct)
2963 {
2964 return typeString(structType);
2965 }
2966
2967 if (structType.getTypeName() == "")
2968 {
2969 return "";
2970 }
2971
2972 TString prefix = "";
2973
2974 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
2975 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04002976
2977 if (useStd140Packing)
2978 {
2979 prefix += "std";
2980 }
2981
Jamie Madill9cf6c072013-06-20 11:55:53 -04002982 if (useHLSLRowMajorPacking)
2983 {
Jamie Madillc835df62013-06-21 09:15:32 -04002984 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04002985 prefix += "rm";
2986 }
2987
2988 return prefix + typeString(structType);
2989}
2990
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002991void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00002992{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002993 if (name == "")
2994 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002995 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002996 }
2997
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00002998 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
2999 {
3000 return; // Already added
3001 }
3002
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003003 TType ctorType = type;
3004 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003005 ctorType.setPrecision(EbpHigh);
3006 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003007
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003008 TString ctorName = type.getStruct() ? decorate(name) : name;
3009
3010 typedef std::vector<TType> ParameterArray;
3011 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003012
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003013 if (type.getStruct())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003014 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003015 mStructNames.insert(decorate(name));
3016
Jamie Madillc835df62013-06-21 09:15:32 -04003017 const TString &structure = structureString(type, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003018
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003019 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003020 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003021 // Add row-major packed struct for interface blocks
3022 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madillc835df62013-06-21 09:15:32 -04003023 structureString(type, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003024 "#pragma pack_matrix(column_major)\n";
3025
Jamie Madillc835df62013-06-21 09:15:32 -04003026 const TString &std140Prefix = "std";
3027 TString std140String = structureString(type, false, true);
3028
3029 const TString &std140RowMajorPrefix = "std_rm";
3030 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
3031 structureString(type, true, true) +
3032 "#pragma pack_matrix(column_major)\n";
3033
Jamie Madill9cf6c072013-06-20 11:55:53 -04003034 mStructDeclarations.push_back(structure);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003035 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003036 mStructDeclarations.push_back(std140String);
3037 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003038 }
3039
Jamie Madill9cf6c072013-06-20 11:55:53 -04003040 const TTypeList &fields = *type.getStruct();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003041 for (unsigned int i = 0; i < fields.size(); i++)
3042 {
3043 ctorParameters.push_back(*fields[i].type);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003044 }
3045 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003046 else if (parameters)
3047 {
3048 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3049 {
3050 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3051 }
3052 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003053 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003054
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003055 TString constructor;
3056
3057 if (ctorType.getStruct())
3058 {
3059 constructor += ctorName + " " + ctorName + "_ctor(";
3060 }
3061 else // Built-in type
3062 {
3063 constructor += typeString(ctorType) + " " + ctorName + "(";
3064 }
3065
3066 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3067 {
3068 const TType &type = ctorParameters[parameter];
3069
3070 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3071
3072 if (parameter < ctorParameters.size() - 1)
3073 {
3074 constructor += ", ";
3075 }
3076 }
3077
3078 constructor += ")\n"
3079 "{\n";
3080
3081 if (ctorType.getStruct())
3082 {
3083 constructor += " " + ctorName + " structure = {";
3084 }
3085 else
3086 {
3087 constructor += " return " + typeString(ctorType) + "(";
3088 }
3089
3090 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3091 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003092 int rows = ctorType.getRows();
3093 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003094 const TType &parameter = ctorParameters[0];
3095
3096 if (parameter.isScalar())
3097 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003098 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003099 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003100 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003101 {
3102 constructor += TString((row == col) ? "x0" : "0.0");
3103
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003104 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003105 {
3106 constructor += ", ";
3107 }
3108 }
3109 }
3110 }
3111 else if (parameter.isMatrix())
3112 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003113 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003114 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003115 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003116 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003117 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003118 {
3119 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3120 }
3121 else
3122 {
3123 constructor += TString((row == col) ? "1.0" : "0.0");
3124 }
3125
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003126 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003127 {
3128 constructor += ", ";
3129 }
3130 }
3131 }
3132 }
3133 else UNREACHABLE();
3134 }
3135 else
3136 {
3137 int remainingComponents = ctorType.getObjectSize();
3138 int parameterIndex = 0;
3139
3140 while (remainingComponents > 0)
3141 {
3142 const TType &parameter = ctorParameters[parameterIndex];
3143 bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
3144
3145 constructor += "x" + str(parameterIndex);
3146
3147 if (parameter.isScalar())
3148 {
3149 remainingComponents -= parameter.getObjectSize();
3150 }
3151 else if (parameter.isVector())
3152 {
3153 if (remainingComponents == parameter.getObjectSize() || moreParameters)
3154 {
3155 remainingComponents -= parameter.getObjectSize();
3156 }
Jamie Madilla9f52472013-06-06 11:56:43 -04003157 else if (remainingComponents < parameter.getNominalSize())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003158 {
3159 switch (remainingComponents)
3160 {
3161 case 1: constructor += ".x"; break;
3162 case 2: constructor += ".xy"; break;
3163 case 3: constructor += ".xyz"; break;
3164 case 4: constructor += ".xyzw"; break;
3165 default: UNREACHABLE();
3166 }
3167
3168 remainingComponents = 0;
3169 }
3170 else UNREACHABLE();
3171 }
3172 else if (parameter.isMatrix() || parameter.getStruct())
3173 {
3174 ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
3175
3176 remainingComponents -= parameter.getObjectSize();
3177 }
3178 else UNREACHABLE();
3179
3180 if (moreParameters)
3181 {
3182 parameterIndex++;
3183 }
3184
3185 if (remainingComponents)
3186 {
3187 constructor += ", ";
3188 }
3189 }
3190 }
3191
3192 if (ctorType.getStruct())
3193 {
3194 constructor += "};\n"
3195 " return structure;\n"
3196 "}\n";
3197 }
3198 else
3199 {
3200 constructor += ");\n"
3201 "}\n";
3202 }
3203
daniel@transgaming.com63691862010-04-29 03:32:42 +00003204 mConstructors.insert(constructor);
3205}
3206
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003207const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3208{
3209 TInfoSinkBase &out = mBody;
3210
3211 if (type.getBasicType() == EbtStruct)
3212 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003213 out << structLookup(type.getTypeName()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003214
3215 const TTypeList *structure = type.getStruct();
3216
3217 for (size_t i = 0; i < structure->size(); i++)
3218 {
3219 const TType *fieldType = (*structure)[i].type;
3220
3221 constUnion = writeConstantUnion(*fieldType, constUnion);
3222
3223 if (i != structure->size() - 1)
3224 {
3225 out << ", ";
3226 }
3227 }
3228
3229 out << ")";
3230 }
3231 else
3232 {
3233 int size = type.getObjectSize();
3234 bool writeType = size > 1;
3235
3236 if (writeType)
3237 {
3238 out << typeString(type) << "(";
3239 }
3240
3241 for (int i = 0; i < size; i++, constUnion++)
3242 {
3243 switch (constUnion->getType())
3244 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003245 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003246 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003247 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003248 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003249 default: UNREACHABLE();
3250 }
3251
3252 if (i != size - 1)
3253 {
3254 out << ", ";
3255 }
3256 }
3257
3258 if (writeType)
3259 {
3260 out << ")";
3261 }
3262 }
3263
3264 return constUnion;
3265}
3266
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003267TString OutputHLSL::scopeString(unsigned int depthLimit)
3268{
3269 TString string;
3270
3271 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3272 {
3273 string += "_" + str(i);
3274 }
3275
3276 return string;
3277}
3278
3279TString OutputHLSL::scopedStruct(const TString &typeName)
3280{
3281 if (typeName == "")
3282 {
3283 return typeName;
3284 }
3285
3286 return typeName + scopeString(mScopeDepth);
3287}
3288
3289TString OutputHLSL::structLookup(const TString &typeName)
3290{
3291 for (int depth = mScopeDepth; depth >= 0; depth--)
3292 {
3293 TString scopedName = decorate(typeName + scopeString(depth));
3294
3295 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3296 {
3297 if (*structName == scopedName)
3298 {
3299 return scopedName;
3300 }
3301 }
3302 }
3303
3304 UNREACHABLE(); // Should have found a matching constructor
3305
3306 return typeName;
3307}
3308
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003309TString OutputHLSL::decorate(const TString &string)
3310{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003311 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003312 {
3313 return "_" + string;
3314 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003315
3316 return string;
3317}
3318
apatrick@chromium.org65756022012-01-17 21:45:38 +00003319TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003320{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003321 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003322 {
3323 return "ex_" + string;
3324 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003325
3326 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003327}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003328
3329TString OutputHLSL::decorateField(const TString &string, const TType &structure)
3330{
3331 if (structure.getTypeName().compare(0, 3, "gl_") != 0)
3332 {
3333 return decorate(string);
3334 }
3335
3336 return string;
3337}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003338
3339TString OutputHLSL::registerString(TIntermSymbol *operand)
3340{
3341 ASSERT(operand->getQualifier() == EvqUniform);
3342
3343 if (IsSampler(operand->getBasicType()))
3344 {
3345 return "s" + str(samplerRegister(operand));
3346 }
3347
3348 return "c" + str(uniformRegister(operand));
3349}
3350
3351int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3352{
3353 const TType &type = sampler->getType();
3354 ASSERT(IsSampler(type.getBasicType()));
3355
3356 int index = mSamplerRegister;
3357 mSamplerRegister += sampler->totalRegisterCount();
3358
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003359 declareUniform(type, sampler->getSymbol(), index);
3360
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003361 return index;
3362}
3363
3364int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3365{
3366 const TType &type = uniform->getType();
3367 ASSERT(!IsSampler(type.getBasicType()));
3368
3369 int index = mUniformRegister;
3370 mUniformRegister += uniform->totalRegisterCount();
3371
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003372 declareUniform(type, uniform->getSymbol(), index);
3373
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003374 return index;
3375}
3376
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003377void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int index, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003378{
3379 const TTypeList *structure = type.getStruct();
3380
3381 if (!structure)
3382 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003383 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3384 output.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), (unsigned int)index, isRowMajorMatrix));
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003385 }
3386 else
3387 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003388 Uniform structUniform(GL_NONE, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), (unsigned int)index, false);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003389
3390 int fieldIndex = index;
3391
3392 for (size_t i = 0; i < structure->size(); i++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003393 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003394 TType fieldType = *(*structure)[i].type;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003395 const TString &fieldName = fieldType.getFieldName();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003396
Jamie Madill010fffa2013-06-20 11:55:53 -04003397 // make sure to copy matrix packing information
3398 fieldType.setLayoutQualifier(type.getLayoutQualifier());
3399
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003400 declareUniformToList(fieldType, fieldName, fieldIndex, structUniform.fields);
3401 fieldIndex += fieldType.totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003402 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003403
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003404 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003405 }
3406}
3407
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003408void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3409{
3410 declareUniformToList(type, name, index, mActiveUniforms);
3411}
3412
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003413GLenum OutputHLSL::glVariableType(const TType &type)
3414{
3415 if (type.getBasicType() == EbtFloat)
3416 {
3417 if (type.isScalar())
3418 {
3419 return GL_FLOAT;
3420 }
3421 else if (type.isVector())
3422 {
3423 switch(type.getNominalSize())
3424 {
3425 case 2: return GL_FLOAT_VEC2;
3426 case 3: return GL_FLOAT_VEC3;
3427 case 4: return GL_FLOAT_VEC4;
3428 default: UNREACHABLE();
3429 }
3430 }
3431 else if (type.isMatrix())
3432 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003433 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003434 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003435 case 2:
3436 switch(type.getRows())
3437 {
3438 case 2: return GL_FLOAT_MAT2;
3439 case 3: return GL_FLOAT_MAT2x3;
3440 case 4: return GL_FLOAT_MAT2x4;
3441 default: UNREACHABLE();
3442 }
3443
3444 case 3:
3445 switch(type.getRows())
3446 {
3447 case 2: return GL_FLOAT_MAT3x2;
3448 case 3: return GL_FLOAT_MAT3;
3449 case 4: return GL_FLOAT_MAT3x4;
3450 default: UNREACHABLE();
3451 }
3452
3453 case 4:
3454 switch(type.getRows())
3455 {
3456 case 2: return GL_FLOAT_MAT4x2;
3457 case 3: return GL_FLOAT_MAT4x3;
3458 case 4: return GL_FLOAT_MAT4;
3459 default: UNREACHABLE();
3460 }
3461
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003462 default: UNREACHABLE();
3463 }
3464 }
3465 else UNREACHABLE();
3466 }
3467 else if (type.getBasicType() == EbtInt)
3468 {
3469 if (type.isScalar())
3470 {
3471 return GL_INT;
3472 }
3473 else if (type.isVector())
3474 {
3475 switch(type.getNominalSize())
3476 {
3477 case 2: return GL_INT_VEC2;
3478 case 3: return GL_INT_VEC3;
3479 case 4: return GL_INT_VEC4;
3480 default: UNREACHABLE();
3481 }
3482 }
3483 else UNREACHABLE();
3484 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003485 else if (type.getBasicType() == EbtUInt)
3486 {
3487 if (type.isScalar())
3488 {
3489 return GL_UNSIGNED_INT;
3490 }
3491 else if (type.isVector())
3492 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003493 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003494 {
3495 case 2: return GL_UNSIGNED_INT_VEC2;
3496 case 3: return GL_UNSIGNED_INT_VEC3;
3497 case 4: return GL_UNSIGNED_INT_VEC4;
3498 default: UNREACHABLE();
3499 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003500 }
3501 else UNREACHABLE();
3502 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003503 else if (type.getBasicType() == EbtBool)
3504 {
3505 if (type.isScalar())
3506 {
3507 return GL_BOOL;
3508 }
3509 else if (type.isVector())
3510 {
3511 switch(type.getNominalSize())
3512 {
3513 case 2: return GL_BOOL_VEC2;
3514 case 3: return GL_BOOL_VEC3;
3515 case 4: return GL_BOOL_VEC4;
3516 default: UNREACHABLE();
3517 }
3518 }
3519 else UNREACHABLE();
3520 }
3521 else if (type.getBasicType() == EbtSampler2D)
3522 {
3523 return GL_SAMPLER_2D;
3524 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003525 else if (type.getBasicType() == EbtSampler3D)
3526 {
3527 return GL_SAMPLER_3D;
3528 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003529 else if (type.getBasicType() == EbtSamplerCube)
3530 {
3531 return GL_SAMPLER_CUBE;
3532 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003533 else if (type.getBasicType() == EbtSampler2DArray)
3534 {
3535 return GL_SAMPLER_2D_ARRAY;
3536 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003537 else if (type.getBasicType() == EbtISampler2D)
3538 {
3539 return GL_INT_SAMPLER_2D;
3540 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003541 else if (type.getBasicType() == EbtISampler3D)
3542 {
3543 return GL_INT_SAMPLER_3D;
3544 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003545 else if (type.getBasicType() == EbtISamplerCube)
3546 {
3547 return GL_INT_SAMPLER_CUBE;
3548 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003549 else if (type.getBasicType() == EbtISampler2DArray)
3550 {
3551 return GL_INT_SAMPLER_2D_ARRAY;
3552 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003553 else if (type.getBasicType() == EbtUSampler2D)
3554 {
3555 return GL_UNSIGNED_INT_SAMPLER_2D;
3556 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003557 else if (type.getBasicType() == EbtUSampler3D)
3558 {
3559 return GL_UNSIGNED_INT_SAMPLER_3D;
3560 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003561 else if (type.getBasicType() == EbtUSamplerCube)
3562 {
3563 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3564 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003565 else if (type.getBasicType() == EbtUSampler2DArray)
3566 {
3567 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3568 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003569 else UNREACHABLE();
3570
3571 return GL_NONE;
3572}
3573
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003574GLenum OutputHLSL::glVariablePrecision(const TType &type)
3575{
3576 if (type.getBasicType() == EbtFloat)
3577 {
3578 switch (type.getPrecision())
3579 {
3580 case EbpHigh: return GL_HIGH_FLOAT;
3581 case EbpMedium: return GL_MEDIUM_FLOAT;
3582 case EbpLow: return GL_LOW_FLOAT;
3583 case EbpUndefined:
3584 // Should be defined as the default precision by the parser
3585 default: UNREACHABLE();
3586 }
3587 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003588 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003589 {
3590 switch (type.getPrecision())
3591 {
3592 case EbpHigh: return GL_HIGH_INT;
3593 case EbpMedium: return GL_MEDIUM_INT;
3594 case EbpLow: return GL_LOW_INT;
3595 case EbpUndefined:
3596 // Should be defined as the default precision by the parser
3597 default: UNREACHABLE();
3598 }
3599 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003600
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003601 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003602 return GL_NONE;
3603}
3604
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003605bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3606{
3607 switch(qualifier)
3608 {
3609 case EvqVaryingOut:
3610 case EvqInvariantVaryingOut:
3611 case EvqSmoothOut:
3612 case EvqFlatOut:
3613 case EvqCentroidOut:
3614 return true;
3615 }
3616
3617 return false;
3618}
3619
3620bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3621{
3622 switch(qualifier)
3623 {
3624 case EvqVaryingIn:
3625 case EvqInvariantVaryingIn:
3626 case EvqSmoothIn:
3627 case EvqFlatIn:
3628 case EvqCentroidIn:
3629 return true;
3630 }
3631
3632 return false;
3633}
3634
3635bool OutputHLSL::isVarying(TQualifier qualifier)
3636{
3637 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3638}
3639
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003640}