blob: 4737400aafad5f8dfa582ca45cc9a0355fe13232 [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
326TString OutputHLSL::std140PostPaddingString(const TType &type)
327{
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 {
337 const bool isRowMajorMatrix = (type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madillc835df62013-06-21 09:15:32 -0400338 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400339 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
340 }
Jamie Madillc835df62013-06-21 09:15:32 -0400341 else if (type.getBasicType() == EbtStruct)
342 {
343 // TODO
344 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400345 else
346 {
Jamie Madillc835df62013-06-21 09:15:32 -0400347 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400348 numComponents = gl::UniformComponentCount(glType);
349 }
350
351 TString padding;
352 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
353 {
354 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
355 }
356 return padding;
357}
358
359TString OutputHLSL::interfaceBlockMemberString(const TTypeList &typeList, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000360{
361 TString hlsl;
362
Jamie Madill574d9dd2013-06-20 11:55:56 -0400363 int elementIndex = 0;
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000364
365 for (unsigned int typeIndex = 0; typeIndex < typeList.size(); typeIndex++)
366 {
367 const TType &memberType = *typeList[typeIndex].type;
Jamie Madill574d9dd2013-06-20 11:55:56 -0400368
369 if (blockStorage == EbsStd140)
370 {
Jamie Madillc835df62013-06-21 09:15:32 -0400371 // 2 and 3 component vector types in some cases need pre-padding
372 hlsl += std140PrePaddingString(memberType, &elementIndex);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400373 }
374
Jamie Madillc835df62013-06-21 09:15:32 -0400375 hlsl += " " + interfaceBlockMemberTypeString(memberType, blockStorage) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000376 " " + decorate(memberType.getFieldName()) + arrayString(memberType) + ";\n";
Jamie Madill574d9dd2013-06-20 11:55:56 -0400377
378 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
379 if (blockStorage == EbsStd140)
380 {
381 hlsl += std140PostPaddingString(memberType);
382 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000383 }
384
385 return hlsl;
386}
387
388TString OutputHLSL::interfaceBlockStructString(const TType &interfaceBlockType)
389{
390 const TTypeList &typeList = *interfaceBlockType.getStruct();
Jamie Madill574d9dd2013-06-20 11:55:56 -0400391 const TLayoutBlockStorage blockStorage = interfaceBlockType.getLayoutQualifier().blockStorage;
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000392
393 return "struct " + interfaceBlockStructName(interfaceBlockType) + "\n"
394 "{\n" +
Jamie Madill574d9dd2013-06-20 11:55:56 -0400395 interfaceBlockMemberString(typeList, blockStorage) +
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000396 "};\n\n";
397}
398
399TString OutputHLSL::interfaceBlockString(const TType &interfaceBlockType, unsigned int registerIndex, unsigned int arrayIndex)
400{
401 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
402 const TString &blockName = interfaceBlockType.getTypeName() + arrayIndexString;
403 TString hlsl;
404
405 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
406 "{\n";
407
408 if (interfaceBlockType.hasInstanceName())
409 {
410 hlsl += " " + interfaceBlockStructName(interfaceBlockType) + " " + interfaceBlockInstanceString(interfaceBlockType, arrayIndex) + ";\n";
411 }
412 else
413 {
414 const TTypeList &typeList = *interfaceBlockType.getStruct();
Jamie Madill574d9dd2013-06-20 11:55:56 -0400415 const TLayoutBlockStorage blockStorage = interfaceBlockType.getLayoutQualifier().blockStorage;
416 hlsl += interfaceBlockMemberString(typeList, blockStorage);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000417 }
418
419 hlsl += "};\n\n";
420
421 return hlsl;
422}
423
Jamie Madill440dc742013-06-20 11:55:55 -0400424// Use the same layout for packed and shared
425void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
426{
427 interfaceBlock->layout = newLayout;
428 interfaceBlock->blockInfo.clear();
429
430 switch (newLayout)
431 {
432 case BLOCKLAYOUT_SHARED:
433 case BLOCKLAYOUT_PACKED:
434 {
435 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
436 hlslEncoder.encodeFields(interfaceBlock->activeUniforms);
437 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
438 }
439 break;
440
441 case BLOCKLAYOUT_STANDARD:
442 {
443 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
444 stdEncoder.encodeFields(interfaceBlock->activeUniforms);
445 interfaceBlock->dataSize = stdEncoder.getBlockSize();
446 }
447 break;
448
449 default:
450 UNREACHABLE();
451 break;
452 }
453}
454
Jamie Madill574d9dd2013-06-20 11:55:56 -0400455BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
456{
457 switch (blockStorage)
458 {
459 case EbsPacked: return BLOCKLAYOUT_PACKED;
460 case EbsShared: return BLOCKLAYOUT_SHARED;
461 case EbsStd140: return BLOCKLAYOUT_STANDARD;
462 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
463 }
464}
465
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000466void OutputHLSL::header()
467{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000468 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000470 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000471 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000472 TString varyings;
473 TString attributes;
474
475 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
476 {
477 const TType &type = uniform->second->getType();
478 const TString &name = uniform->second->getSymbol();
479
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000480 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
481 {
482 int index = samplerRegister(mReferencedUniforms[name]);
483
484 uniforms += "uniform SamplerState sampler_" + decorateUniform(name, type) + arrayString(type) +
485 " : register(s" + str(index) + ");\n";
486
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000487 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000488 " : register(t" + str(index) + ");\n";
489 }
490 else
491 {
492 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
493 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
494 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000495 }
496
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000497 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
498 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000499 const TType &nodeType = interfaceBlockIt->second->getType();
500 const TType &interfaceBlockType = nodeType.isInterfaceBlockMember() ? *nodeType.getInterfaceBlockType() : nodeType;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000501 const TString &blockName = interfaceBlockType.getTypeName();
502 const TTypeList &typeList = *interfaceBlockType.getStruct();
503
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000504 const unsigned int arraySize = interfaceBlockType.isArray() ? interfaceBlockType.getArraySize() : 0;
505 sh::InterfaceBlock interfaceBlock(blockName.c_str(), arraySize, mInterfaceBlockRegister);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000506 for (unsigned int typeIndex = 0; typeIndex < typeList.size(); typeIndex++)
507 {
508 const TType &memberType = *typeList[typeIndex].type;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000509 const TString &fullUniformName = interfaceBlockUniformName(interfaceBlockType, memberType);
510 declareUniformToList(memberType, fullUniformName, typeIndex, interfaceBlock.activeUniforms);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000511 }
512
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000513 mInterfaceBlockRegister += std::max(1u, interfaceBlock.arraySize);
514
Jamie Madill574d9dd2013-06-20 11:55:56 -0400515 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlockType.getLayoutQualifier().blockStorage);
516 setBlockLayout(&interfaceBlock, blockLayoutType);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000517 mActiveInterfaceBlocks.push_back(interfaceBlock);
518
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000519 if (interfaceBlockType.hasInstanceName())
520 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000521 interfaceBlocks += interfaceBlockStructString(interfaceBlockType);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000522 }
523
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000524 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000525 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000526 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
527 {
528 interfaceBlocks += interfaceBlockString(interfaceBlockType, interfaceBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000529 }
530 }
531 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000532 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000533 interfaceBlocks += interfaceBlockString(interfaceBlockType, interfaceBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000534 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000535 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000536
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000537 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
538 {
539 const TType &type = varying->second->getType();
540 const TString &name = varying->second->getSymbol();
541
542 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000543 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
544 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000545 }
546
547 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
548 {
549 const TType &type = attribute->second->getType();
550 const TString &name = attribute->second->getSymbol();
551
552 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400553
554 ShaderVariable shaderVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
555 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
556 mActiveAttributes.push_back(shaderVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000557 }
558
Jamie Madill529077d2013-06-20 11:55:54 -0400559 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
560 {
561 out << *structDeclaration;
562 }
563
564 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
565 {
566 out << *constructor;
567 }
568
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400569 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000571 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000572 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000573
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000574 out << "// Varyings\n";
575 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400576 out << "\n";
577
578 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000579 {
Jamie Madill46131a32013-06-20 11:55:50 -0400580 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000581 {
Jamie Madill46131a32013-06-20 11:55:50 -0400582 const TString &variableName = outputVariableIt->first;
583 const TType &variableType = outputVariableIt->second->getType();
584 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
585
586 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
587 " = " + initializer(variableType) + ";\n";
588
589 ShaderVariable outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
590 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
591 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000592 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000593 }
Jamie Madill46131a32013-06-20 11:55:50 -0400594 else
595 {
596 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
597
598 out << "static float4 gl_Color[" << numColorValues << "] =\n"
599 "{\n";
600 for (unsigned int i = 0; i < numColorValues; i++)
601 {
602 out << " float4(0, 0, 0, 0)";
603 if (i + 1 != numColorValues)
604 {
605 out << ",";
606 }
607 out << "\n";
608 }
609
610 out << "};\n";
611 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000612
613 if (mUsesFragCoord)
614 {
615 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
616 }
617
618 if (mUsesPointCoord)
619 {
620 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
621 }
622
623 if (mUsesFrontFacing)
624 {
625 out << "static bool gl_FrontFacing = false;\n";
626 }
627
628 out << "\n";
629
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000630 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000631 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000632 out << "struct gl_DepthRangeParameters\n"
633 "{\n"
634 " float near;\n"
635 " float far;\n"
636 " float diff;\n"
637 "};\n"
638 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000639 }
640
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000641 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000642 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000643 out << "cbuffer DriverConstants : register(b1)\n"
644 "{\n";
645
646 if (mUsesDepthRange)
647 {
648 out << " float3 dx_DepthRange : packoffset(c0);\n";
649 }
650
651 if (mUsesFragCoord)
652 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000653 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000654 }
655
656 if (mUsesFragCoord || mUsesFrontFacing)
657 {
658 out << " float3 dx_DepthFront : packoffset(c2);\n";
659 }
660
661 out << "};\n";
662 }
663 else
664 {
665 if (mUsesDepthRange)
666 {
667 out << "uniform float3 dx_DepthRange : register(c0);";
668 }
669
670 if (mUsesFragCoord)
671 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000672 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000673 }
674
675 if (mUsesFragCoord || mUsesFrontFacing)
676 {
677 out << "uniform float3 dx_DepthFront : register(c2);\n";
678 }
679 }
680
681 out << "\n";
682
683 if (mUsesDepthRange)
684 {
685 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
686 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000687 }
688
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000689 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000690 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000691
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000692 if (!interfaceBlocks.empty())
693 {
694 out << interfaceBlocks;
695 out << "\n";
696 }
697
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000698 if (usingMRTExtension && mNumRenderTargets > 1)
699 {
700 out << "#define GL_USES_MRT\n";
701 }
702
703 if (mUsesFragColor)
704 {
705 out << "#define GL_USES_FRAG_COLOR\n";
706 }
707
708 if (mUsesFragData)
709 {
710 out << "#define GL_USES_FRAG_DATA\n";
711 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000712 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000713 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000714 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000715 out << "// Attributes\n";
716 out << attributes;
717 out << "\n"
718 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
719
720 if (mUsesPointSize)
721 {
722 out << "static float gl_PointSize = float(1);\n";
723 }
724
725 out << "\n"
726 "// Varyings\n";
727 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000728 out << "\n";
729
730 if (mUsesDepthRange)
731 {
732 out << "struct gl_DepthRangeParameters\n"
733 "{\n"
734 " float near;\n"
735 " float far;\n"
736 " float diff;\n"
737 "};\n"
738 "\n";
739 }
740
741 if (mOutputType == SH_HLSL11_OUTPUT)
742 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000743 if (mUsesDepthRange)
744 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000745 out << "cbuffer DriverConstants : register(b1)\n"
746 "{\n"
747 " float3 dx_DepthRange : packoffset(c0);\n"
748 "};\n"
749 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000750 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000751 }
752 else
753 {
754 if (mUsesDepthRange)
755 {
756 out << "uniform float3 dx_DepthRange : register(c0);\n";
757 }
758
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000759 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000760 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000761 }
762
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000763 if (mUsesDepthRange)
764 {
765 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
766 "\n";
767 }
768
769 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000771
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000772 if (!interfaceBlocks.empty())
773 {
774 out << interfaceBlocks;
775 out << "\n";
776 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400777 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000778
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400779 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
780 {
781 // Return type
782 switch(textureFunction->sampler)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000783 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400784 case EbtSampler2D: out << "float4 "; break;
785 case EbtSampler3D: out << "float4 "; break;
786 case EbtSamplerCube: out << "float4 "; break;
787 case EbtSampler2DArray: out << "float4 "; break;
788 case EbtISampler2D: out << "int4 "; break;
789 case EbtISampler3D: out << "int4 "; break;
790 case EbtISamplerCube: out << "int4 "; break;
791 case EbtISampler2DArray: out << "int4 "; break;
792 case EbtUSampler2D: out << "uint4 "; break;
793 case EbtUSampler3D: out << "uint4 "; break;
794 case EbtUSamplerCube: out << "uint4 "; break;
795 case EbtUSampler2DArray: out << "uint4 "; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400796 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000797 }
798
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400799 // Function name
800 out << textureFunction->name();
801
802 // Argument list
803 int hlslCoords = 4;
804
805 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000806 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400807 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000808 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400809 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
810 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
811 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000812 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400813
814 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000815 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400816 case TextureFunction::IMPLICIT: break;
817 case TextureFunction::BIAS: hlslCoords = 4; break;
818 case TextureFunction::LOD: hlslCoords = 4; break;
819 case TextureFunction::LOD0: hlslCoords = 4; break;
820 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000821 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400822 }
823 else if (mOutputType == SH_HLSL11_OUTPUT)
824 {
825 switch(textureFunction->sampler)
826 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400827 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
828 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
829 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
830 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 2; break;
831 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
832 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
833 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
834 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 2; break;
835 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
836 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
837 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
838 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 2; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400839 default: UNREACHABLE();
840 }
841 }
842 else UNREACHABLE();
843
844 switch(textureFunction->coords)
845 {
846 case 2: out << ", float2 t"; break;
847 case 3: out << ", float3 t"; break;
848 case 4: out << ", float4 t"; break;
849 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000850 }
851
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400852 switch(textureFunction->mipmap)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000853 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400854 case TextureFunction::IMPLICIT: break;
855 case TextureFunction::BIAS: out << ", float bias"; break;
856 case TextureFunction::LOD: out << ", float lod"; break;
857 case TextureFunction::LOD0: break;
858 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000859 }
860
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400861 out << ")\n"
862 "{\n"
863 " return ";
864
865 // HLSL intrinsic
866 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000867 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400868 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000869 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400870 case EbtSampler2D: out << "tex2D"; break;
871 case EbtSamplerCube: out << "texCUBE"; break;
872 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000873 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400874 }
875 else if (mOutputType == SH_HLSL11_OUTPUT)
876 {
877 out << "x.Sample";
878 }
879 else UNREACHABLE();
880
881 if (mOutputType == SH_HLSL9_OUTPUT)
882 {
883 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000884 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400885 case TextureFunction::IMPLICIT: out << "(s, "; break;
886 case TextureFunction::BIAS: out << "bias(s, "; break;
887 case TextureFunction::LOD: out << "lod(s, "; break;
888 case TextureFunction::LOD0: out << "lod(s, "; break;
889 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000890 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400891 }
892 else if (mOutputType == SH_HLSL11_OUTPUT)
893 {
894 switch(textureFunction->mipmap)
895 {
896 case TextureFunction::IMPLICIT: out << "(s, "; break;
897 case TextureFunction::BIAS: out << "Bias(s, "; break;
898 case TextureFunction::LOD: out << "Level(s, "; break;
899 case TextureFunction::LOD0: out << "Level(s, "; break;
900 default: UNREACHABLE();
901 }
902 }
903 else UNREACHABLE();
904
905 switch(hlslCoords)
906 {
907 case 2: out << "float2("; break;
908 case 3: out << "float3("; break;
909 case 4: out << "float4("; break;
910 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000911 }
912
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400913 TString proj = "";
914
915 if (textureFunction->proj)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400916 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400917 switch(textureFunction->coords)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400918 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400919 case 3: proj = " / t.z"; break;
920 case 4: proj = " / t.w"; break;
921 default: UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400922 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400923 }
924
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400925 out << "t.x" + proj + ", t.y" + proj;
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400926
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400927 if (mOutputType == SH_HLSL9_OUTPUT)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400928 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400929 if (hlslCoords >= 3)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400930 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400931 if (textureFunction->coords < 3)
932 {
933 out << ", 0";
934 }
935 else
936 {
937 out << ", t.z" + proj;
938 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400939 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400940
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400941 if (hlslCoords == 4)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400942 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400943 switch(textureFunction->mipmap)
944 {
945 case TextureFunction::BIAS: out << ", bias"; break;
946 case TextureFunction::LOD: out << ", lod"; break;
947 case TextureFunction::LOD0: out << ", 0"; break;
948 default: UNREACHABLE();
949 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400950 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400951
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400952 out << "));\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000953 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400954 else if (mOutputType == SH_HLSL11_OUTPUT)
955 {
956 if (hlslCoords >= 3)
957 {
958 out << ", t.z" + proj;
959 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000960
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400961 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000962 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400963 case TextureFunction::IMPLICIT: out << "));"; break;
964 case TextureFunction::BIAS: out << "), bias);"; break;
965 case TextureFunction::LOD: out << "), lod);"; break;
966 case TextureFunction::LOD0: out << "), 0);"; break;
967 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000968 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000969 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400970 else UNREACHABLE();
971
972 out << "}\n"
973 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974 }
975
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000976 if (mUsesFragCoord)
977 {
978 out << "#define GL_USES_FRAG_COORD\n";
979 }
980
981 if (mUsesPointCoord)
982 {
983 out << "#define GL_USES_POINT_COORD\n";
984 }
985
986 if (mUsesFrontFacing)
987 {
988 out << "#define GL_USES_FRONT_FACING\n";
989 }
990
991 if (mUsesPointSize)
992 {
993 out << "#define GL_USES_POINT_SIZE\n";
994 }
995
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000996 if (mUsesDepthRange)
997 {
998 out << "#define GL_USES_DEPTH_RANGE\n";
999 }
1000
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001001 if (mUsesXor)
1002 {
1003 out << "bool xor(bool p, bool q)\n"
1004 "{\n"
1005 " return (p || q) && !(p && q);\n"
1006 "}\n"
1007 "\n";
1008 }
1009
1010 if (mUsesMod1)
1011 {
1012 out << "float mod(float x, float y)\n"
1013 "{\n"
1014 " return x - y * floor(x / y);\n"
1015 "}\n"
1016 "\n";
1017 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001018
1019 if (mUsesMod2v)
1020 {
1021 out << "float2 mod(float2 x, float2 y)\n"
1022 "{\n"
1023 " return x - y * floor(x / y);\n"
1024 "}\n"
1025 "\n";
1026 }
1027
1028 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001029 {
1030 out << "float2 mod(float2 x, float y)\n"
1031 "{\n"
1032 " return x - y * floor(x / y);\n"
1033 "}\n"
1034 "\n";
1035 }
1036
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001037 if (mUsesMod3v)
1038 {
1039 out << "float3 mod(float3 x, float3 y)\n"
1040 "{\n"
1041 " return x - y * floor(x / y);\n"
1042 "}\n"
1043 "\n";
1044 }
1045
1046 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001047 {
1048 out << "float3 mod(float3 x, float y)\n"
1049 "{\n"
1050 " return x - y * floor(x / y);\n"
1051 "}\n"
1052 "\n";
1053 }
1054
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001055 if (mUsesMod4v)
1056 {
1057 out << "float4 mod(float4 x, float4 y)\n"
1058 "{\n"
1059 " return x - y * floor(x / y);\n"
1060 "}\n"
1061 "\n";
1062 }
1063
1064 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001065 {
1066 out << "float4 mod(float4 x, float y)\n"
1067 "{\n"
1068 " return x - y * floor(x / y);\n"
1069 "}\n"
1070 "\n";
1071 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001072
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001073 if (mUsesFaceforward1)
1074 {
1075 out << "float faceforward(float N, float I, float Nref)\n"
1076 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001077 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001078 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001079 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001080 " }\n"
1081 " else\n"
1082 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001083 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001084 " }\n"
1085 "}\n"
1086 "\n";
1087 }
1088
1089 if (mUsesFaceforward2)
1090 {
1091 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1092 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001093 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001094 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001095 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001096 " }\n"
1097 " else\n"
1098 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001099 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001100 " }\n"
1101 "}\n"
1102 "\n";
1103 }
1104
1105 if (mUsesFaceforward3)
1106 {
1107 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1108 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001109 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001110 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001111 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001112 " }\n"
1113 " else\n"
1114 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001115 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001116 " }\n"
1117 "}\n"
1118 "\n";
1119 }
1120
1121 if (mUsesFaceforward4)
1122 {
1123 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1124 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001125 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001126 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001127 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001128 " }\n"
1129 " else\n"
1130 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001131 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001132 " }\n"
1133 "}\n"
1134 "\n";
1135 }
1136
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001137 for (unsigned int cols = 2; cols <= 4; cols++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001138 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001139 for (unsigned int rows = 2; rows <= 4; rows++)
1140 {
1141 if (mUsesEqualMat[cols][rows])
1142 {
1143 TString matrixType = "float" + str(cols) + "x" + str(rows);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001144
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001145 out << "bool equal(" + matrixType + " m, " + matrixType + " n)\n"
1146 "{\n";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001147
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001148 for (unsigned int row = 0; row < rows; row++)
1149 {
1150 if (row == 0)
1151 {
1152 out << " return ";
1153 }
1154 else
1155 {
1156 out << " ";
1157 }
1158
1159 for (unsigned int col = 0; col < cols; col++)
1160 {
1161 TString index = "[" + str(col) + "][" + str(row) + "]";
1162 out << "m" + index + " == n" + index;
1163
1164 if (col == cols-1 && row == rows-1)
1165 {
1166 out << ";\n";
1167 }
1168 else if (col == cols-1)
1169 {
1170 out << " &&\n";
1171 }
1172 else
1173 {
1174 out << " && ";
1175 }
1176 }
1177 }
1178
1179 out << "}\n";
1180 }
1181 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001182 }
1183
1184 if (mUsesEqualVec2)
1185 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001186 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001187 "{\n"
1188 " return v.x == u.x && v.y == u.y;\n"
1189 "}\n";
1190 }
1191
1192 if (mUsesEqualVec3)
1193 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001194 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001195 "{\n"
1196 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1197 "}\n";
1198 }
1199
1200 if (mUsesEqualVec4)
1201 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001202 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001203 "{\n"
1204 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1205 "}\n";
1206 }
1207
1208 if (mUsesEqualIVec2)
1209 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001210 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001211 "{\n"
1212 " return v.x == u.x && v.y == u.y;\n"
1213 "}\n";
1214 }
1215
1216 if (mUsesEqualIVec3)
1217 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001218 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001219 "{\n"
1220 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1221 "}\n";
1222 }
1223
1224 if (mUsesEqualIVec4)
1225 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001226 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001227 "{\n"
1228 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1229 "}\n";
1230 }
1231
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001232 if (mUsesEqualUVec2)
1233 {
1234 out << "bool equal(uint2 v, uint2 u)\n"
1235 "{\n"
1236 " return v.x == u.x && v.y == u.y;\n"
1237 "}\n";
1238 }
1239
1240 if (mUsesEqualUVec3)
1241 {
1242 out << "bool equal(uint3 v, uint3 u)\n"
1243 "{\n"
1244 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1245 "}\n";
1246 }
1247
1248 if (mUsesEqualUVec4)
1249 {
1250 out << "bool equal(uint4 v, uint4 u)\n"
1251 "{\n"
1252 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1253 "}\n";
1254 }
1255
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001256 if (mUsesEqualBVec2)
1257 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001258 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001259 "{\n"
1260 " return v.x == u.x && v.y == u.y;\n"
1261 "}\n";
1262 }
1263
1264 if (mUsesEqualBVec3)
1265 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001266 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001267 "{\n"
1268 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1269 "}\n";
1270 }
1271
1272 if (mUsesEqualBVec4)
1273 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001274 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001275 "{\n"
1276 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1277 "}\n";
1278 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001279
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001280 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001281 {
1282 out << "float atanyx(float y, float x)\n"
1283 "{\n"
1284 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1285 " return atan2(y, x);\n"
1286 "}\n";
1287 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001288
1289 if (mUsesAtan2_2)
1290 {
1291 out << "float2 atanyx(float2 y, float2 x)\n"
1292 "{\n"
1293 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1294 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1295 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1296 "}\n";
1297 }
1298
1299 if (mUsesAtan2_3)
1300 {
1301 out << "float3 atanyx(float3 y, float3 x)\n"
1302 "{\n"
1303 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1304 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1305 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1306 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1307 "}\n";
1308 }
1309
1310 if (mUsesAtan2_4)
1311 {
1312 out << "float4 atanyx(float4 y, float4 x)\n"
1313 "{\n"
1314 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1315 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1316 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1317 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1318 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1319 "}\n";
1320 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001321}
1322
1323void OutputHLSL::visitSymbol(TIntermSymbol *node)
1324{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001325 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001326
1327 TString name = node->getSymbol();
1328
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001329 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001330 {
1331 mUsesDepthRange = true;
1332 out << name;
1333 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001334 else
1335 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001336 TQualifier qualifier = node->getQualifier();
1337
1338 if (qualifier == EvqUniform)
1339 {
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001340 if (node->getType().isInterfaceBlockMember())
1341 {
1342 const TString& interfaceBlockTypeName = node->getType().getInterfaceBlockType()->getTypeName();
1343 mReferencedInterfaceBlocks[interfaceBlockTypeName] = node;
1344 out << decorateUniform(name, node->getType());
1345 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001346 else if (node->getBasicType() == EbtInterfaceBlock)
1347 {
1348 const TString& interfaceBlockTypeName = node->getType().getTypeName();
1349 mReferencedInterfaceBlocks[interfaceBlockTypeName] = node;
1350 out << decorateUniform(name, node->getType());
1351 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001352 else
1353 {
1354 mReferencedUniforms[name] = node;
1355 out << decorateUniform(name, node->getType());
1356 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001357 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001358 else if (qualifier == EvqAttribute || qualifier == EvqVertexInput)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001359 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001360 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001361 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001362 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001363 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001364 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001365 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001366 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001367 }
Jamie Madill46131a32013-06-20 11:55:50 -04001368 else if (qualifier == EvqFragmentOutput)
1369 {
1370 mReferencedOutputVariables[name] = node;
1371 out << "out_" << name;
1372 }
1373 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001374 {
1375 out << "gl_Color[0]";
1376 mUsesFragColor = true;
1377 }
1378 else if (qualifier == EvqFragData)
1379 {
1380 out << "gl_Color";
1381 mUsesFragData = true;
1382 }
1383 else if (qualifier == EvqFragCoord)
1384 {
1385 mUsesFragCoord = true;
1386 out << name;
1387 }
1388 else if (qualifier == EvqPointCoord)
1389 {
1390 mUsesPointCoord = true;
1391 out << name;
1392 }
1393 else if (qualifier == EvqFrontFacing)
1394 {
1395 mUsesFrontFacing = true;
1396 out << name;
1397 }
1398 else if (qualifier == EvqPointSize)
1399 {
1400 mUsesPointSize = true;
1401 out << name;
1402 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001403 else
1404 {
1405 out << decorate(name);
1406 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001407 }
1408}
1409
1410bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1411{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001412 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001413
1414 switch (node->getOp())
1415 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001416 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001417 case EOpInitialize:
1418 if (visit == PreVisit)
1419 {
1420 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1421 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1422 // new variable is created before the assignment is evaluated), so we need to convert
1423 // this to "float t = x, x = t;".
1424
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001425 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1426 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001427
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001428 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1429 expression->traverse(&searchSymbol);
1430 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001431
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001432 if (sameSymbol)
1433 {
1434 // Type already printed
1435 out << "t" + str(mUniqueIndex) + " = ";
1436 expression->traverse(this);
1437 out << ", ";
1438 symbolNode->traverse(this);
1439 out << " = t" + str(mUniqueIndex);
1440
1441 mUniqueIndex++;
1442 return false;
1443 }
1444 }
1445 else if (visit == InVisit)
1446 {
1447 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001448 }
1449 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001450 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1451 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1452 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1453 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1454 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1455 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001456 if (visit == PreVisit)
1457 {
1458 out << "(";
1459 }
1460 else if (visit == InVisit)
1461 {
1462 out << " = mul(";
1463 node->getLeft()->traverse(this);
1464 out << ", transpose(";
1465 }
1466 else
1467 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001468 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001469 }
1470 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001471 case EOpMatrixTimesMatrixAssign:
1472 if (visit == PreVisit)
1473 {
1474 out << "(";
1475 }
1476 else if (visit == InVisit)
1477 {
1478 out << " = mul(";
1479 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001480 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001481 }
1482 else
1483 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001484 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001485 }
1486 break;
1487 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001488 case EOpIndexDirect:
1489 if (node->getLeft()->getBasicType() == EbtInterfaceBlock)
1490 {
1491 if (visit == PreVisit)
1492 {
1493 const TType &interfaceBlockType = node->getLeft()->getType();
1494 mReferencedInterfaceBlocks[interfaceBlockType.getInstanceName()] = node->getLeft()->getAsSymbolNode();
1495 out << interfaceBlockInstanceString(interfaceBlockType, node->getRight()->getAsConstantUnion()->getIConst(0));
1496 return false;
1497 }
1498 }
1499 else
1500 {
1501 outputTriplet(visit, "", "[", "]");
1502 }
1503 break;
1504 case EOpIndexIndirect:
1505 // We do not currently support indirect references to interface blocks
1506 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1507 outputTriplet(visit, "", "[", "]");
1508 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001509 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001510 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001511 if (visit == InVisit)
1512 {
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001513 out << "." + decorateField(node->getType().getFieldName(), node->getLeft()->getType());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001514
1515 return false;
1516 }
1517 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001518 case EOpVectorSwizzle:
1519 if (visit == InVisit)
1520 {
1521 out << ".";
1522
1523 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1524
1525 if (swizzle)
1526 {
1527 TIntermSequence &sequence = swizzle->getSequence();
1528
1529 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1530 {
1531 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1532
1533 if (element)
1534 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001535 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001536
1537 switch (i)
1538 {
1539 case 0: out << "x"; break;
1540 case 1: out << "y"; break;
1541 case 2: out << "z"; break;
1542 case 3: out << "w"; break;
1543 default: UNREACHABLE();
1544 }
1545 }
1546 else UNREACHABLE();
1547 }
1548 }
1549 else UNREACHABLE();
1550
1551 return false; // Fully processed
1552 }
1553 break;
1554 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1555 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1556 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1557 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001558 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001559 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001560 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001561 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001562 if (node->getOp() == EOpEqual)
1563 {
1564 outputTriplet(visit, "(", " == ", ")");
1565 }
1566 else
1567 {
1568 outputTriplet(visit, "(", " != ", ")");
1569 }
1570 }
1571 else if (node->getLeft()->getBasicType() == EbtStruct)
1572 {
1573 if (node->getOp() == EOpEqual)
1574 {
1575 out << "(";
1576 }
1577 else
1578 {
1579 out << "!(";
1580 }
1581
1582 const TTypeList *fields = node->getLeft()->getType().getStruct();
1583
1584 for (size_t i = 0; i < fields->size(); i++)
1585 {
1586 const TType *fieldType = (*fields)[i].type;
1587
1588 node->getLeft()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001589 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType()) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001590 node->getRight()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001591 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001592
1593 if (i < fields->size() - 1)
1594 {
1595 out << " && ";
1596 }
1597 }
1598
1599 out << ")";
1600
1601 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001602 }
1603 else
1604 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001605 if (node->getLeft()->isMatrix())
1606 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001607 mUsesEqualMat[node->getLeft()->getCols()][node->getLeft()->getRows()] = true;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001608 }
1609 else if (node->getLeft()->isVector())
1610 {
1611 switch (node->getLeft()->getBasicType())
1612 {
1613 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001614 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001615 {
1616 case 2: mUsesEqualVec2 = true; break;
1617 case 3: mUsesEqualVec3 = true; break;
1618 case 4: mUsesEqualVec4 = true; break;
1619 default: UNREACHABLE();
1620 }
1621 break;
1622 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001623 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001624 {
1625 case 2: mUsesEqualIVec2 = true; break;
1626 case 3: mUsesEqualIVec3 = true; break;
1627 case 4: mUsesEqualIVec4 = true; break;
1628 default: UNREACHABLE();
1629 }
1630 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001631 case EbtUInt:
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001632 switch (node->getLeft()->getNominalSize())
1633 {
1634 case 2: mUsesEqualUVec2 = true; break;
1635 case 3: mUsesEqualUVec3 = true; break;
1636 case 4: mUsesEqualUVec4 = true; break;
1637 default: UNREACHABLE();
1638 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001639 break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001640 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001641 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001642 {
1643 case 2: mUsesEqualBVec2 = true; break;
1644 case 3: mUsesEqualBVec3 = true; break;
1645 case 4: mUsesEqualBVec4 = true; break;
1646 default: UNREACHABLE();
1647 }
1648 break;
1649 default: UNREACHABLE();
1650 }
1651 }
1652 else UNREACHABLE();
1653
1654 if (node->getOp() == EOpEqual)
1655 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001656 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001657 }
1658 else
1659 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001660 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001661 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001662 }
1663 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1665 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1666 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1667 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1668 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001669 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001670 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1671 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001672 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001673 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001674 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001675 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001676 case EOpLogicalXor:
1677 mUsesXor = true;
1678 outputTriplet(visit, "xor(", ", ", ")");
1679 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001680 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001681 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001682 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001683 default: UNREACHABLE();
1684 }
1685
1686 return true;
1687}
1688
1689bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1690{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691 switch (node->getOp())
1692 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001693 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1694 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1695 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1696 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1697 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1698 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1699 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001701 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001702 case EOpConvFloatToBool:
1703 switch (node->getOperand()->getType().getNominalSize())
1704 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001705 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1706 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1707 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1708 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709 default: UNREACHABLE();
1710 }
1711 break;
1712 case EOpConvBoolToFloat:
1713 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001714 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001715 switch (node->getOperand()->getType().getNominalSize())
1716 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001717 case 1: outputTriplet(visit, "float(", "", ")"); break;
1718 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1719 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1720 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721 default: UNREACHABLE();
1722 }
1723 break;
1724 case EOpConvFloatToInt:
1725 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001726 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 switch (node->getOperand()->getType().getNominalSize())
1728 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001729 case 1: outputTriplet(visit, "int(", "", ")"); break;
1730 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1731 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1732 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733 default: UNREACHABLE();
1734 }
1735 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001736 case EOpConvFloatToUInt:
1737 case EOpConvBoolToUInt:
1738 case EOpConvIntToUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001739 switch (node->getOperand()->getType().getCols())
1740 {
1741 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001742 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1743 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1744 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001745 default: UNREACHABLE();
1746 }
1747 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001748 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1749 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1750 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1751 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1752 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1753 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1754 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1755 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1756 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1757 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1758 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1759 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1760 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1761 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1762 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1763 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1764 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1765 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1766 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1767 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1768 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001769 case EOpDFdx:
1770 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1771 {
1772 outputTriplet(visit, "(", "", ", 0.0)");
1773 }
1774 else
1775 {
1776 outputTriplet(visit, "ddx(", "", ")");
1777 }
1778 break;
1779 case EOpDFdy:
1780 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1781 {
1782 outputTriplet(visit, "(", "", ", 0.0)");
1783 }
1784 else
1785 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001786 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001787 }
1788 break;
1789 case EOpFwidth:
1790 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1791 {
1792 outputTriplet(visit, "(", "", ", 0.0)");
1793 }
1794 else
1795 {
1796 outputTriplet(visit, "fwidth(", "", ")");
1797 }
1798 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001799 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1800 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001801 default: UNREACHABLE();
1802 }
1803
1804 return true;
1805}
1806
1807bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1808{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001809 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811 switch (node->getOp())
1812 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001813 case EOpSequence:
1814 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001815 if (mInsideFunction)
1816 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001817 outputLineDirective(node->getLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001818 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001819
1820 mScopeDepth++;
1821
1822 if (mScopeBracket.size() < mScopeDepth)
1823 {
1824 mScopeBracket.push_back(0); // New scope level
1825 }
1826 else
1827 {
1828 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1829 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001830 }
1831
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001832 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1833 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001834 outputLineDirective((*sit)->getLine());
1835
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001836 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001837
1838 out << ";\n";
1839 }
1840
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001841 if (mInsideFunction)
1842 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001843 outputLineDirective(node->getEndLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001844 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001845
1846 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001847 }
1848
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001849 return false;
1850 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001851 case EOpDeclaration:
1852 if (visit == PreVisit)
1853 {
1854 TIntermSequence &sequence = node->getSequence();
1855 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001857 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001858 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001859 if (variable->getType().getStruct())
1860 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001861 addConstructor(variable->getType(), scopedStruct(variable->getType().getTypeName()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001862 }
1863
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001864 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001865 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001866 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001867 {
1868 out << "static ";
1869 }
1870
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001871 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001872
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001873 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001875 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001876
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001877 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001879 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001880 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00001881 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001882 }
1883 else
1884 {
1885 (*sit)->traverse(this);
1886 }
1887
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001888 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001889 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001890 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891 }
1892 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001894 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1895 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001896 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001897 }
1898 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001899 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001900 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001901 {
1902 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1903 {
1904 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1905
1906 if (symbol)
1907 {
1908 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1909 mReferencedVaryings[symbol->getSymbol()] = symbol;
1910 }
1911 else
1912 {
1913 (*sit)->traverse(this);
1914 }
1915 }
1916 }
1917
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 return false;
1919 }
1920 else if (visit == InVisit)
1921 {
1922 out << ", ";
1923 }
1924 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001925 case EOpPrototype:
1926 if (visit == PreVisit)
1927 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001928 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001929
1930 TIntermSequence &arguments = node->getSequence();
1931
1932 for (unsigned int i = 0; i < arguments.size(); i++)
1933 {
1934 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1935
1936 if (symbol)
1937 {
1938 out << argumentString(symbol);
1939
1940 if (i < arguments.size() - 1)
1941 {
1942 out << ", ";
1943 }
1944 }
1945 else UNREACHABLE();
1946 }
1947
1948 out << ");\n";
1949
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001950 // Also prototype the Lod0 variant if needed
1951 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1952 {
1953 mOutputLod0Function = true;
1954 node->traverse(this);
1955 mOutputLod0Function = false;
1956 }
1957
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001958 return false;
1959 }
1960 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001961 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962 case EOpFunction:
1963 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001964 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001966 out << typeString(node->getType()) << " ";
1967
1968 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001969 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001970 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001972 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001974 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001975 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001976
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001977 TIntermSequence &sequence = node->getSequence();
1978 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1979
1980 for (unsigned int i = 0; i < arguments.size(); i++)
1981 {
1982 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1983
1984 if (symbol)
1985 {
1986 if (symbol->getType().getStruct())
1987 {
1988 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getTypeName()), NULL);
1989 }
1990
1991 out << argumentString(symbol);
1992
1993 if (i < arguments.size() - 1)
1994 {
1995 out << ", ";
1996 }
1997 }
1998 else UNREACHABLE();
1999 }
2000
2001 out << ")\n"
2002 "{\n";
2003
2004 if (sequence.size() > 1)
2005 {
2006 mInsideFunction = true;
2007 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002008 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002010
2011 out << "}\n";
2012
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002013 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2014 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002015 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002016 {
2017 mOutputLod0Function = true;
2018 node->traverse(this);
2019 mOutputLod0Function = false;
2020 }
2021 }
2022
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002023 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024 }
2025 break;
2026 case EOpFunctionCall:
2027 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002028 TString name = TFunction::unmangleName(node->getName());
2029 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002030 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002031
2032 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002034 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035 }
2036 else
2037 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002038 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2039
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002040 TextureFunction textureFunction;
2041 textureFunction.sampler = samplerType;
2042 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
2043 textureFunction.mipmap = TextureFunction::IMPLICIT;
2044
2045 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002046 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002047 textureFunction.mipmap = TextureFunction::IMPLICIT;
2048 textureFunction.proj = false;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002049 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002050 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002051 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002052 textureFunction.mipmap = TextureFunction::IMPLICIT;
2053 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002054 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002055 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002056 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002057 textureFunction.mipmap = TextureFunction::LOD;
2058 textureFunction.proj = false;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002059 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002060 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002061 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002062 textureFunction.mipmap = TextureFunction::LOD;
2063 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002064 }
2065 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002066
2067 if (textureFunction.mipmap != TextureFunction::LOD)
2068 {
2069 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2070 {
2071 textureFunction.mipmap = TextureFunction::LOD0;
2072 }
2073 else if (arguments.size() == 3)
2074 {
2075 textureFunction.mipmap = TextureFunction::BIAS;
2076 }
2077 }
2078
2079 mUsesTexture.insert(textureFunction);
2080
2081 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002082 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002083
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002084 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2085 {
2086 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2087 {
2088 out << "texture_";
2089 (*arg)->traverse(this);
2090 out << ", sampler_";
2091 }
2092
2093 (*arg)->traverse(this);
2094
2095 if (arg < arguments.end() - 1)
2096 {
2097 out << ", ";
2098 }
2099 }
2100
2101 out << ")";
2102
2103 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 }
2105 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002106 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002107 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002108 addConstructor(node->getType(), "vec1", &node->getSequence());
2109 outputTriplet(visit, "vec1(", "", ")");
2110 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002111 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002112 addConstructor(node->getType(), "vec2", &node->getSequence());
2113 outputTriplet(visit, "vec2(", ", ", ")");
2114 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002115 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002116 addConstructor(node->getType(), "vec3", &node->getSequence());
2117 outputTriplet(visit, "vec3(", ", ", ")");
2118 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002119 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002120 addConstructor(node->getType(), "vec4", &node->getSequence());
2121 outputTriplet(visit, "vec4(", ", ", ")");
2122 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002123 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002124 addConstructor(node->getType(), "bvec1", &node->getSequence());
2125 outputTriplet(visit, "bvec1(", "", ")");
2126 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002127 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002128 addConstructor(node->getType(), "bvec2", &node->getSequence());
2129 outputTriplet(visit, "bvec2(", ", ", ")");
2130 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002131 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002132 addConstructor(node->getType(), "bvec3", &node->getSequence());
2133 outputTriplet(visit, "bvec3(", ", ", ")");
2134 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002135 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002136 addConstructor(node->getType(), "bvec4", &node->getSequence());
2137 outputTriplet(visit, "bvec4(", ", ", ")");
2138 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002139 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002140 addConstructor(node->getType(), "ivec1", &node->getSequence());
2141 outputTriplet(visit, "ivec1(", "", ")");
2142 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002143 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002144 addConstructor(node->getType(), "ivec2", &node->getSequence());
2145 outputTriplet(visit, "ivec2(", ", ", ")");
2146 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002147 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002148 addConstructor(node->getType(), "ivec3", &node->getSequence());
2149 outputTriplet(visit, "ivec3(", ", ", ")");
2150 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002151 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002152 addConstructor(node->getType(), "ivec4", &node->getSequence());
2153 outputTriplet(visit, "ivec4(", ", ", ")");
2154 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002155 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002156 addConstructor(node->getType(), "uvec1", &node->getSequence());
2157 outputTriplet(visit, "uvec1(", "", ")");
2158 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002159 case EOpConstructUVec2:
2160 addConstructor(node->getType(), "uvec2", &node->getSequence());
2161 outputTriplet(visit, "uvec2(", ", ", ")");
2162 break;
2163 case EOpConstructUVec3:
2164 addConstructor(node->getType(), "uvec3", &node->getSequence());
2165 outputTriplet(visit, "uvec3(", ", ", ")");
2166 break;
2167 case EOpConstructUVec4:
2168 addConstructor(node->getType(), "uvec4", &node->getSequence());
2169 outputTriplet(visit, "uvec4(", ", ", ")");
2170 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002171 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002172 addConstructor(node->getType(), "mat2", &node->getSequence());
2173 outputTriplet(visit, "mat2(", ", ", ")");
2174 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002175 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002176 addConstructor(node->getType(), "mat3", &node->getSequence());
2177 outputTriplet(visit, "mat3(", ", ", ")");
2178 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002179 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002180 addConstructor(node->getType(), "mat4", &node->getSequence());
2181 outputTriplet(visit, "mat4(", ", ", ")");
2182 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002183 case EOpConstructStruct:
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002184 addConstructor(node->getType(), scopedStruct(node->getType().getTypeName()), &node->getSequence());
2185 outputTriplet(visit, structLookup(node->getType().getTypeName()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002186 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002187 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2188 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2189 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2190 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2191 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2192 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002193 case EOpMod:
2194 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002195 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002196 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2197 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2198 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002199 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002200 case 11: mUsesMod1 = true; break;
2201 case 22: mUsesMod2v = true; break;
2202 case 21: mUsesMod2f = true; break;
2203 case 33: mUsesMod3v = true; break;
2204 case 31: mUsesMod3f = true; break;
2205 case 44: mUsesMod4v = true; break;
2206 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002207 default: UNREACHABLE();
2208 }
2209
2210 outputTriplet(visit, "mod(", ", ", ")");
2211 }
2212 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002213 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002215 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002216 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2217 {
2218 case 1: mUsesAtan2_1 = true; break;
2219 case 2: mUsesAtan2_2 = true; break;
2220 case 3: mUsesAtan2_3 = true; break;
2221 case 4: mUsesAtan2_4 = true; break;
2222 default: UNREACHABLE();
2223 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002224 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002225 break;
2226 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2227 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2228 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2229 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2230 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2231 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2232 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2233 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2234 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002235 case EOpFaceForward:
2236 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002237 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002238 {
2239 case 1: mUsesFaceforward1 = true; break;
2240 case 2: mUsesFaceforward2 = true; break;
2241 case 3: mUsesFaceforward3 = true; break;
2242 case 4: mUsesFaceforward4 = true; break;
2243 default: UNREACHABLE();
2244 }
2245
2246 outputTriplet(visit, "faceforward(", ", ", ")");
2247 }
2248 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002249 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2250 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2251 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 default: UNREACHABLE();
2253 }
2254
2255 return true;
2256}
2257
2258bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2259{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002260 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002261
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002262 if (node->usesTernaryOperator())
2263 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002264 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002265 }
2266 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002268 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002269
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002270 out << "if(";
2271
2272 node->getCondition()->traverse(this);
2273
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002274 out << ")\n";
2275
2276 outputLineDirective(node->getLine());
2277 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278
daniel@transgaming.combb885322010-04-15 20:45:24 +00002279 if (node->getTrueBlock())
2280 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002281 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002282 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002284 outputLineDirective(node->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002285 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002286
2287 if (node->getFalseBlock())
2288 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002289 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002290
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002291 outputLineDirective(node->getFalseBlock()->getLine());
2292 out << "{\n";
2293
2294 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002295 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002296
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002297 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002298 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002299 }
2300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
2302 return false;
2303}
2304
2305void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2306{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002307 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002308}
2309
2310bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2311{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002312 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2313
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002314 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002315 {
2316 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2317 }
2318
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002319 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002320 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002321 if (handleExcessiveLoop(node))
2322 {
2323 return false;
2324 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002325 }
2326
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002327 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328
alokp@chromium.org52813552010-11-16 18:36:09 +00002329 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002331 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002332
2333 outputLineDirective(node->getLine());
2334 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335 }
2336 else
2337 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002338 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339
2340 if (node->getInit())
2341 {
2342 node->getInit()->traverse(this);
2343 }
2344
2345 out << "; ";
2346
alokp@chromium.org52813552010-11-16 18:36:09 +00002347 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002349 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350 }
2351
2352 out << "; ";
2353
alokp@chromium.org52813552010-11-16 18:36:09 +00002354 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002356 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357 }
2358
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002359 out << ")\n";
2360
2361 outputLineDirective(node->getLine());
2362 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 }
2364
2365 if (node->getBody())
2366 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002367 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368 }
2369
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002370 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002371 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372
alokp@chromium.org52813552010-11-16 18:36:09 +00002373 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002375 outputLineDirective(node->getCondition()->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 out << "while(\n";
2377
alokp@chromium.org52813552010-11-16 18:36:09 +00002378 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379
daniel@transgaming.com73536982012-03-21 20:45:49 +00002380 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 }
2382
daniel@transgaming.com73536982012-03-21 20:45:49 +00002383 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002384
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002385 mInsideDiscontinuousLoop = wasDiscontinuous;
2386
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 return false;
2388}
2389
2390bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2391{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002392 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393
2394 switch (node->getFlowOp())
2395 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002396 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002397 case EOpBreak:
2398 if (visit == PreVisit)
2399 {
2400 if (mExcessiveLoopIndex)
2401 {
2402 out << "{Break";
2403 mExcessiveLoopIndex->traverse(this);
2404 out << " = true; break;}\n";
2405 }
2406 else
2407 {
2408 out << "break;\n";
2409 }
2410 }
2411 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002412 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 case EOpReturn:
2414 if (visit == PreVisit)
2415 {
2416 if (node->getExpression())
2417 {
2418 out << "return ";
2419 }
2420 else
2421 {
2422 out << "return;\n";
2423 }
2424 }
2425 else if (visit == PostVisit)
2426 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002427 if (node->getExpression())
2428 {
2429 out << ";\n";
2430 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431 }
2432 break;
2433 default: UNREACHABLE();
2434 }
2435
2436 return true;
2437}
2438
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002439void OutputHLSL::traverseStatements(TIntermNode *node)
2440{
2441 if (isSingleStatement(node))
2442 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002443 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002444 }
2445
2446 node->traverse(this);
2447}
2448
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002449bool OutputHLSL::isSingleStatement(TIntermNode *node)
2450{
2451 TIntermAggregate *aggregate = node->getAsAggregate();
2452
2453 if (aggregate)
2454 {
2455 if (aggregate->getOp() == EOpSequence)
2456 {
2457 return false;
2458 }
2459 else
2460 {
2461 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2462 {
2463 if (!isSingleStatement(*sit))
2464 {
2465 return false;
2466 }
2467 }
2468
2469 return true;
2470 }
2471 }
2472
2473 return true;
2474}
2475
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002476// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2477// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002478bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2479{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002480 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002481 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002482
2483 // Parse loops of the form:
2484 // for(int index = initial; index [comparator] limit; index += increment)
2485 TIntermSymbol *index = NULL;
2486 TOperator comparator = EOpNull;
2487 int initial = 0;
2488 int limit = 0;
2489 int increment = 0;
2490
2491 // Parse index name and intial value
2492 if (node->getInit())
2493 {
2494 TIntermAggregate *init = node->getInit()->getAsAggregate();
2495
2496 if (init)
2497 {
2498 TIntermSequence &sequence = init->getSequence();
2499 TIntermTyped *variable = sequence[0]->getAsTyped();
2500
2501 if (variable && variable->getQualifier() == EvqTemporary)
2502 {
2503 TIntermBinary *assign = variable->getAsBinaryNode();
2504
2505 if (assign->getOp() == EOpInitialize)
2506 {
2507 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2508 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2509
2510 if (symbol && constant)
2511 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002512 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002513 {
2514 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002515 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002516 }
2517 }
2518 }
2519 }
2520 }
2521 }
2522
2523 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002524 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002525 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002526 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002527
2528 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2529 {
2530 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2531
2532 if (constant)
2533 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002534 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002535 {
2536 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002537 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002538 }
2539 }
2540 }
2541 }
2542
2543 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002544 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002545 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002546 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2547 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002548
2549 if (binaryTerminal)
2550 {
2551 TOperator op = binaryTerminal->getOp();
2552 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2553
2554 if (constant)
2555 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002556 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002557 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002558 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002559
2560 switch (op)
2561 {
2562 case EOpAddAssign: increment = value; break;
2563 case EOpSubAssign: increment = -value; break;
2564 default: UNIMPLEMENTED();
2565 }
2566 }
2567 }
2568 }
2569 else if (unaryTerminal)
2570 {
2571 TOperator op = unaryTerminal->getOp();
2572
2573 switch (op)
2574 {
2575 case EOpPostIncrement: increment = 1; break;
2576 case EOpPostDecrement: increment = -1; break;
2577 case EOpPreIncrement: increment = 1; break;
2578 case EOpPreDecrement: increment = -1; break;
2579 default: UNIMPLEMENTED();
2580 }
2581 }
2582 }
2583
2584 if (index != NULL && comparator != EOpNull && increment != 0)
2585 {
2586 if (comparator == EOpLessThanEqual)
2587 {
2588 comparator = EOpLessThan;
2589 limit += 1;
2590 }
2591
2592 if (comparator == EOpLessThan)
2593 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002594 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002595
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002596 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002597 {
2598 return false; // Not an excessive loop
2599 }
2600
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002601 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2602 mExcessiveLoopIndex = index;
2603
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002604 out << "{int ";
2605 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002606 out << ";\n"
2607 "bool Break";
2608 index->traverse(this);
2609 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002610
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002611 bool firstLoopFragment = true;
2612
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002613 while (iterations > 0)
2614 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002615 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002616
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002617 if (!firstLoopFragment)
2618 {
2619 out << "if(!Break";
2620 index->traverse(this);
2621 out << ") {\n";
2622 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002623
2624 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2625 {
2626 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2627 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002628
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002629 // for(int index = initial; index < clampedLimit; index += increment)
2630
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002631 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002632 index->traverse(this);
2633 out << " = ";
2634 out << initial;
2635
2636 out << "; ";
2637 index->traverse(this);
2638 out << " < ";
2639 out << clampedLimit;
2640
2641 out << "; ";
2642 index->traverse(this);
2643 out << " += ";
2644 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002645 out << ")\n";
2646
2647 outputLineDirective(node->getLine());
2648 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002649
2650 if (node->getBody())
2651 {
2652 node->getBody()->traverse(this);
2653 }
2654
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002655 outputLineDirective(node->getLine());
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002656 out << ";}\n";
2657
2658 if (!firstLoopFragment)
2659 {
2660 out << "}\n";
2661 }
2662
2663 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002664
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002665 initial += MAX_LOOP_ITERATIONS * increment;
2666 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002667 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002668
2669 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002670
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002671 mExcessiveLoopIndex = restoreIndex;
2672
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002673 return true;
2674 }
2675 else UNIMPLEMENTED();
2676 }
2677
2678 return false; // Not handled as an excessive loop
2679}
2680
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002681void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002682{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002683 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002684
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002685 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686 {
2687 out << preString;
2688 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002689 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690 {
2691 out << inString;
2692 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002693 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694 {
2695 out << postString;
2696 }
2697}
2698
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002699void OutputHLSL::outputLineDirective(int line)
2700{
2701 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2702 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002703 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002704 mBody << "#line " << line;
2705
2706 if (mContext.sourcePath)
2707 {
2708 mBody << " \"" << mContext.sourcePath << "\"";
2709 }
2710
2711 mBody << "\n";
2712 }
2713}
2714
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002715TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2716{
2717 TQualifier qualifier = symbol->getQualifier();
2718 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002719 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002720
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002721 if (name.empty()) // HLSL demands named arguments, also for prototypes
2722 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002723 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002724 }
2725 else
2726 {
2727 name = decorate(name);
2728 }
2729
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002730 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2731 {
2732 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2733 qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type);
2734 }
2735
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002736 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002737}
2738
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002739TString OutputHLSL::interpolationString(TQualifier qualifier)
2740{
2741 switch(qualifier)
2742 {
2743 case EvqVaryingIn: return "";
2744 case EvqInvariantVaryingIn: return "";
2745 case EvqSmoothIn: return "linear";
2746 case EvqFlatIn: return "nointerpolation";
2747 case EvqCentroidIn: return "centroid";
2748 case EvqVaryingOut: return "";
2749 case EvqInvariantVaryingOut: return "";
2750 case EvqSmoothOut: return "linear";
2751 case EvqFlatOut: return "nointerpolation";
2752 case EvqCentroidOut: return "centroid";
2753 default: UNREACHABLE();
2754 }
2755
2756 return "";
2757}
2758
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002759TString OutputHLSL::qualifierString(TQualifier qualifier)
2760{
2761 switch(qualifier)
2762 {
2763 case EvqIn: return "in";
2764 case EvqOut: return "out";
2765 case EvqInOut: return "inout";
2766 case EvqConstReadOnly: return "const";
2767 default: UNREACHABLE();
2768 }
2769
2770 return "";
2771}
2772
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002773TString OutputHLSL::typeString(const TType &type)
2774{
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002775 if (type.getBasicType() == EbtStruct)
2776 {
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002777 if (type.getTypeName() != "")
2778 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002779 return structLookup(type.getTypeName());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002780 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002781 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002782 {
Jamie Madillc835df62013-06-21 09:15:32 -04002783 return structureString(type, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002784 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002785 }
2786 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002787 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00002788 int cols = type.getCols();
2789 int rows = type.getRows();
2790 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002791 }
2792 else
2793 {
2794 switch (type.getBasicType())
2795 {
2796 case EbtFloat:
2797 switch (type.getNominalSize())
2798 {
2799 case 1: return "float";
2800 case 2: return "float2";
2801 case 3: return "float3";
2802 case 4: return "float4";
2803 }
2804 case EbtInt:
2805 switch (type.getNominalSize())
2806 {
2807 case 1: return "int";
2808 case 2: return "int2";
2809 case 3: return "int3";
2810 case 4: return "int4";
2811 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002812 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04002813 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002814 {
2815 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002816 case 2: return "uint2";
2817 case 3: return "uint3";
2818 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002819 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002820 case EbtBool:
2821 switch (type.getNominalSize())
2822 {
2823 case 1: return "bool";
2824 case 2: return "bool2";
2825 case 3: return "bool3";
2826 case 4: return "bool4";
2827 }
2828 case EbtVoid:
2829 return "void";
2830 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002831 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04002832 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04002833 case EbtSampler2DArray:
2834 case EbtISampler2DArray:
2835 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002836 return "sampler2D";
2837 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002838 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04002839 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002840 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00002841 case EbtSamplerExternalOES:
2842 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002843 default:
2844 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002845 }
2846 }
2847
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002848 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002849 return "<unknown type>";
2850}
2851
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002852TString OutputHLSL::textureString(const TType &type)
2853{
2854 switch (type.getBasicType())
2855 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -04002856 case EbtSampler2D: return "Texture2D";
2857 case EbtSamplerCube: return "TextureCube";
2858 case EbtSamplerExternalOES: return "Texture2D";
2859 case EbtSampler2DArray: return "Texture2DArray";
2860 case EbtISampler2D: return "Texture2D<int4>";
2861 case EbtISamplerCube: return "TextureCube<int4>";
2862 case EbtISampler2DArray: return "Texture2DArray<int4>";
2863 case EbtUSampler2D: return "Texture2D<uint4>";
2864 case EbtUSamplerCube: return "TextureCube<uint4>";
2865 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002866 default:
2867 break;
2868 }
2869
2870 UNREACHABLE();
2871 return "<unknown texture type>";
2872}
2873
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874TString OutputHLSL::arrayString(const TType &type)
2875{
2876 if (!type.isArray())
2877 {
2878 return "";
2879 }
2880
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002881 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002882}
2883
2884TString OutputHLSL::initializer(const TType &type)
2885{
2886 TString string;
2887
daniel@transgaming.comead23042010-04-29 03:35:36 +00002888 for (int component = 0; component < type.getObjectSize(); component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002889 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002890 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002891
daniel@transgaming.comead23042010-04-29 03:35:36 +00002892 if (component < type.getObjectSize() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002893 {
2894 string += ", ";
2895 }
2896 }
2897
daniel@transgaming.comead23042010-04-29 03:35:36 +00002898 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002899}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002900
Jamie Madillc835df62013-06-21 09:15:32 -04002901TString OutputHLSL::structureString(const TType &structType, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04002902{
2903 ASSERT(structType.getStruct());
2904
2905 const TTypeList &fields = *structType.getStruct();
2906 const bool isNameless = (structType.getTypeName() == "");
Jamie Madillc835df62013-06-21 09:15:32 -04002907 const TString &structName = structureTypeName(structType, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04002908
2909 const TString declareString = (isNameless ? "struct" : "struct " + structName);
2910
2911 TString structure;
2912 structure += declareString + "\n"
2913 "{\n";
2914
Jamie Madillc835df62013-06-21 09:15:32 -04002915 int elementIndex = 0;
2916
Jamie Madill9cf6c072013-06-20 11:55:53 -04002917 for (unsigned int i = 0; i < fields.size(); i++)
2918 {
2919 const TType &field = *fields[i].type;
2920
Jamie Madillc835df62013-06-21 09:15:32 -04002921 if (useStd140Packing)
2922 {
2923 structure += std140PrePaddingString(field, &elementIndex);
2924 }
2925
2926 structure += " " + structureTypeName(field, useHLSLRowMajorPacking, useStd140Packing) + " " +
Jamie Madill9cf6c072013-06-20 11:55:53 -04002927 decorateField(field.getFieldName(), structType) + arrayString(field) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04002928
2929 if (useStd140Packing)
2930 {
2931 structure += std140PostPaddingString(field);
2932 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04002933 }
2934
2935 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
2936 structure += (isNameless ? "} " : "};\n");
2937
2938 return structure;
2939}
2940
Jamie Madillc835df62013-06-21 09:15:32 -04002941TString OutputHLSL::structureTypeName(const TType &structType, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04002942{
2943 if (structType.getBasicType() != EbtStruct)
2944 {
2945 return typeString(structType);
2946 }
2947
2948 if (structType.getTypeName() == "")
2949 {
2950 return "";
2951 }
2952
2953 TString prefix = "";
2954
2955 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
2956 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04002957
2958 if (useStd140Packing)
2959 {
2960 prefix += "std";
2961 }
2962
Jamie Madill9cf6c072013-06-20 11:55:53 -04002963 if (useHLSLRowMajorPacking)
2964 {
Jamie Madillc835df62013-06-21 09:15:32 -04002965 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04002966 prefix += "rm";
2967 }
2968
2969 return prefix + typeString(structType);
2970}
2971
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002972void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00002973{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002974 if (name == "")
2975 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002976 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002977 }
2978
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00002979 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
2980 {
2981 return; // Already added
2982 }
2983
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002984 TType ctorType = type;
2985 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00002986 ctorType.setPrecision(EbpHigh);
2987 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00002988
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002989 TString ctorName = type.getStruct() ? decorate(name) : name;
2990
2991 typedef std::vector<TType> ParameterArray;
2992 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002993
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00002994 if (type.getStruct())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002995 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002996 mStructNames.insert(decorate(name));
2997
Jamie Madillc835df62013-06-21 09:15:32 -04002998 const TString &structure = structureString(type, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002999
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003000 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003001 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003002 // Add row-major packed struct for interface blocks
3003 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madillc835df62013-06-21 09:15:32 -04003004 structureString(type, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003005 "#pragma pack_matrix(column_major)\n";
3006
Jamie Madillc835df62013-06-21 09:15:32 -04003007 const TString &std140Prefix = "std";
3008 TString std140String = structureString(type, false, true);
3009
3010 const TString &std140RowMajorPrefix = "std_rm";
3011 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
3012 structureString(type, true, true) +
3013 "#pragma pack_matrix(column_major)\n";
3014
Jamie Madill9cf6c072013-06-20 11:55:53 -04003015 mStructDeclarations.push_back(structure);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003016 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003017 mStructDeclarations.push_back(std140String);
3018 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003019 }
3020
Jamie Madill9cf6c072013-06-20 11:55:53 -04003021 const TTypeList &fields = *type.getStruct();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003022 for (unsigned int i = 0; i < fields.size(); i++)
3023 {
3024 ctorParameters.push_back(*fields[i].type);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003025 }
3026 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003027 else if (parameters)
3028 {
3029 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3030 {
3031 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3032 }
3033 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003034 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003035
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003036 TString constructor;
3037
3038 if (ctorType.getStruct())
3039 {
3040 constructor += ctorName + " " + ctorName + "_ctor(";
3041 }
3042 else // Built-in type
3043 {
3044 constructor += typeString(ctorType) + " " + ctorName + "(";
3045 }
3046
3047 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3048 {
3049 const TType &type = ctorParameters[parameter];
3050
3051 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3052
3053 if (parameter < ctorParameters.size() - 1)
3054 {
3055 constructor += ", ";
3056 }
3057 }
3058
3059 constructor += ")\n"
3060 "{\n";
3061
3062 if (ctorType.getStruct())
3063 {
3064 constructor += " " + ctorName + " structure = {";
3065 }
3066 else
3067 {
3068 constructor += " return " + typeString(ctorType) + "(";
3069 }
3070
3071 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3072 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003073 int rows = ctorType.getRows();
3074 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003075 const TType &parameter = ctorParameters[0];
3076
3077 if (parameter.isScalar())
3078 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003079 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003080 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003081 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003082 {
3083 constructor += TString((row == col) ? "x0" : "0.0");
3084
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003085 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003086 {
3087 constructor += ", ";
3088 }
3089 }
3090 }
3091 }
3092 else if (parameter.isMatrix())
3093 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003094 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003095 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003096 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003097 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003098 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003099 {
3100 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3101 }
3102 else
3103 {
3104 constructor += TString((row == col) ? "1.0" : "0.0");
3105 }
3106
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003107 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003108 {
3109 constructor += ", ";
3110 }
3111 }
3112 }
3113 }
3114 else UNREACHABLE();
3115 }
3116 else
3117 {
3118 int remainingComponents = ctorType.getObjectSize();
3119 int parameterIndex = 0;
3120
3121 while (remainingComponents > 0)
3122 {
3123 const TType &parameter = ctorParameters[parameterIndex];
3124 bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
3125
3126 constructor += "x" + str(parameterIndex);
3127
3128 if (parameter.isScalar())
3129 {
3130 remainingComponents -= parameter.getObjectSize();
3131 }
3132 else if (parameter.isVector())
3133 {
3134 if (remainingComponents == parameter.getObjectSize() || moreParameters)
3135 {
3136 remainingComponents -= parameter.getObjectSize();
3137 }
Jamie Madilla9f52472013-06-06 11:56:43 -04003138 else if (remainingComponents < parameter.getNominalSize())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003139 {
3140 switch (remainingComponents)
3141 {
3142 case 1: constructor += ".x"; break;
3143 case 2: constructor += ".xy"; break;
3144 case 3: constructor += ".xyz"; break;
3145 case 4: constructor += ".xyzw"; break;
3146 default: UNREACHABLE();
3147 }
3148
3149 remainingComponents = 0;
3150 }
3151 else UNREACHABLE();
3152 }
3153 else if (parameter.isMatrix() || parameter.getStruct())
3154 {
3155 ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
3156
3157 remainingComponents -= parameter.getObjectSize();
3158 }
3159 else UNREACHABLE();
3160
3161 if (moreParameters)
3162 {
3163 parameterIndex++;
3164 }
3165
3166 if (remainingComponents)
3167 {
3168 constructor += ", ";
3169 }
3170 }
3171 }
3172
3173 if (ctorType.getStruct())
3174 {
3175 constructor += "};\n"
3176 " return structure;\n"
3177 "}\n";
3178 }
3179 else
3180 {
3181 constructor += ");\n"
3182 "}\n";
3183 }
3184
daniel@transgaming.com63691862010-04-29 03:32:42 +00003185 mConstructors.insert(constructor);
3186}
3187
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003188const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3189{
3190 TInfoSinkBase &out = mBody;
3191
3192 if (type.getBasicType() == EbtStruct)
3193 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003194 out << structLookup(type.getTypeName()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003195
3196 const TTypeList *structure = type.getStruct();
3197
3198 for (size_t i = 0; i < structure->size(); i++)
3199 {
3200 const TType *fieldType = (*structure)[i].type;
3201
3202 constUnion = writeConstantUnion(*fieldType, constUnion);
3203
3204 if (i != structure->size() - 1)
3205 {
3206 out << ", ";
3207 }
3208 }
3209
3210 out << ")";
3211 }
3212 else
3213 {
3214 int size = type.getObjectSize();
3215 bool writeType = size > 1;
3216
3217 if (writeType)
3218 {
3219 out << typeString(type) << "(";
3220 }
3221
3222 for (int i = 0; i < size; i++, constUnion++)
3223 {
3224 switch (constUnion->getType())
3225 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003226 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003227 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003228 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003229 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003230 default: UNREACHABLE();
3231 }
3232
3233 if (i != size - 1)
3234 {
3235 out << ", ";
3236 }
3237 }
3238
3239 if (writeType)
3240 {
3241 out << ")";
3242 }
3243 }
3244
3245 return constUnion;
3246}
3247
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003248TString OutputHLSL::scopeString(unsigned int depthLimit)
3249{
3250 TString string;
3251
3252 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3253 {
3254 string += "_" + str(i);
3255 }
3256
3257 return string;
3258}
3259
3260TString OutputHLSL::scopedStruct(const TString &typeName)
3261{
3262 if (typeName == "")
3263 {
3264 return typeName;
3265 }
3266
3267 return typeName + scopeString(mScopeDepth);
3268}
3269
3270TString OutputHLSL::structLookup(const TString &typeName)
3271{
3272 for (int depth = mScopeDepth; depth >= 0; depth--)
3273 {
3274 TString scopedName = decorate(typeName + scopeString(depth));
3275
3276 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3277 {
3278 if (*structName == scopedName)
3279 {
3280 return scopedName;
3281 }
3282 }
3283 }
3284
3285 UNREACHABLE(); // Should have found a matching constructor
3286
3287 return typeName;
3288}
3289
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003290TString OutputHLSL::decorate(const TString &string)
3291{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003292 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003293 {
3294 return "_" + string;
3295 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003296
3297 return string;
3298}
3299
apatrick@chromium.org65756022012-01-17 21:45:38 +00003300TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003301{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003302 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003303 {
3304 return "ex_" + string;
3305 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003306
3307 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003308}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003309
3310TString OutputHLSL::decorateField(const TString &string, const TType &structure)
3311{
3312 if (structure.getTypeName().compare(0, 3, "gl_") != 0)
3313 {
3314 return decorate(string);
3315 }
3316
3317 return string;
3318}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003319
3320TString OutputHLSL::registerString(TIntermSymbol *operand)
3321{
3322 ASSERT(operand->getQualifier() == EvqUniform);
3323
3324 if (IsSampler(operand->getBasicType()))
3325 {
3326 return "s" + str(samplerRegister(operand));
3327 }
3328
3329 return "c" + str(uniformRegister(operand));
3330}
3331
3332int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3333{
3334 const TType &type = sampler->getType();
3335 ASSERT(IsSampler(type.getBasicType()));
3336
3337 int index = mSamplerRegister;
3338 mSamplerRegister += sampler->totalRegisterCount();
3339
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003340 declareUniform(type, sampler->getSymbol(), index);
3341
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003342 return index;
3343}
3344
3345int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3346{
3347 const TType &type = uniform->getType();
3348 ASSERT(!IsSampler(type.getBasicType()));
3349
3350 int index = mUniformRegister;
3351 mUniformRegister += uniform->totalRegisterCount();
3352
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003353 declareUniform(type, uniform->getSymbol(), index);
3354
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003355 return index;
3356}
3357
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003358void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int index, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003359{
3360 const TTypeList *structure = type.getStruct();
3361
3362 if (!structure)
3363 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003364 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3365 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 +00003366 }
3367 else
3368 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003369 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 +00003370
3371 int fieldIndex = index;
3372
3373 for (size_t i = 0; i < structure->size(); i++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003374 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003375 TType fieldType = *(*structure)[i].type;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003376 const TString &fieldName = fieldType.getFieldName();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003377
Jamie Madill010fffa2013-06-20 11:55:53 -04003378 // make sure to copy matrix packing information
3379 fieldType.setLayoutQualifier(type.getLayoutQualifier());
3380
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003381 declareUniformToList(fieldType, fieldName, fieldIndex, structUniform.fields);
3382 fieldIndex += fieldType.totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003383 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003384
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003385 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003386 }
3387}
3388
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003389void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3390{
3391 declareUniformToList(type, name, index, mActiveUniforms);
3392}
3393
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003394GLenum OutputHLSL::glVariableType(const TType &type)
3395{
3396 if (type.getBasicType() == EbtFloat)
3397 {
3398 if (type.isScalar())
3399 {
3400 return GL_FLOAT;
3401 }
3402 else if (type.isVector())
3403 {
3404 switch(type.getNominalSize())
3405 {
3406 case 2: return GL_FLOAT_VEC2;
3407 case 3: return GL_FLOAT_VEC3;
3408 case 4: return GL_FLOAT_VEC4;
3409 default: UNREACHABLE();
3410 }
3411 }
3412 else if (type.isMatrix())
3413 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003414 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003415 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003416 case 2:
3417 switch(type.getRows())
3418 {
3419 case 2: return GL_FLOAT_MAT2;
3420 case 3: return GL_FLOAT_MAT2x3;
3421 case 4: return GL_FLOAT_MAT2x4;
3422 default: UNREACHABLE();
3423 }
3424
3425 case 3:
3426 switch(type.getRows())
3427 {
3428 case 2: return GL_FLOAT_MAT3x2;
3429 case 3: return GL_FLOAT_MAT3;
3430 case 4: return GL_FLOAT_MAT3x4;
3431 default: UNREACHABLE();
3432 }
3433
3434 case 4:
3435 switch(type.getRows())
3436 {
3437 case 2: return GL_FLOAT_MAT4x2;
3438 case 3: return GL_FLOAT_MAT4x3;
3439 case 4: return GL_FLOAT_MAT4;
3440 default: UNREACHABLE();
3441 }
3442
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003443 default: UNREACHABLE();
3444 }
3445 }
3446 else UNREACHABLE();
3447 }
3448 else if (type.getBasicType() == EbtInt)
3449 {
3450 if (type.isScalar())
3451 {
3452 return GL_INT;
3453 }
3454 else if (type.isVector())
3455 {
3456 switch(type.getNominalSize())
3457 {
3458 case 2: return GL_INT_VEC2;
3459 case 3: return GL_INT_VEC3;
3460 case 4: return GL_INT_VEC4;
3461 default: UNREACHABLE();
3462 }
3463 }
3464 else UNREACHABLE();
3465 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003466 else if (type.getBasicType() == EbtUInt)
3467 {
3468 if (type.isScalar())
3469 {
3470 return GL_UNSIGNED_INT;
3471 }
3472 else if (type.isVector())
3473 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003474 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003475 {
3476 case 2: return GL_UNSIGNED_INT_VEC2;
3477 case 3: return GL_UNSIGNED_INT_VEC3;
3478 case 4: return GL_UNSIGNED_INT_VEC4;
3479 default: UNREACHABLE();
3480 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003481 }
3482 else UNREACHABLE();
3483 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003484 else if (type.getBasicType() == EbtBool)
3485 {
3486 if (type.isScalar())
3487 {
3488 return GL_BOOL;
3489 }
3490 else if (type.isVector())
3491 {
3492 switch(type.getNominalSize())
3493 {
3494 case 2: return GL_BOOL_VEC2;
3495 case 3: return GL_BOOL_VEC3;
3496 case 4: return GL_BOOL_VEC4;
3497 default: UNREACHABLE();
3498 }
3499 }
3500 else UNREACHABLE();
3501 }
3502 else if (type.getBasicType() == EbtSampler2D)
3503 {
3504 return GL_SAMPLER_2D;
3505 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003506 else if (type.getBasicType() == EbtSampler3D)
3507 {
3508 return GL_SAMPLER_3D;
3509 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003510 else if (type.getBasicType() == EbtSamplerCube)
3511 {
3512 return GL_SAMPLER_CUBE;
3513 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003514 else if (type.getBasicType() == EbtSampler2DArray)
3515 {
3516 return GL_SAMPLER_2D_ARRAY;
3517 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003518 else if (type.getBasicType() == EbtISampler2D)
3519 {
3520 return GL_INT_SAMPLER_2D;
3521 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003522 else if (type.getBasicType() == EbtISampler3D)
3523 {
3524 return GL_INT_SAMPLER_3D;
3525 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003526 else if (type.getBasicType() == EbtISamplerCube)
3527 {
3528 return GL_INT_SAMPLER_CUBE;
3529 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003530 else if (type.getBasicType() == EbtISampler2DArray)
3531 {
3532 return GL_INT_SAMPLER_2D_ARRAY;
3533 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003534 else if (type.getBasicType() == EbtUSampler2D)
3535 {
3536 return GL_UNSIGNED_INT_SAMPLER_2D;
3537 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003538 else if (type.getBasicType() == EbtUSampler3D)
3539 {
3540 return GL_UNSIGNED_INT_SAMPLER_3D;
3541 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003542 else if (type.getBasicType() == EbtUSamplerCube)
3543 {
3544 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3545 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003546 else if (type.getBasicType() == EbtUSampler2DArray)
3547 {
3548 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3549 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003550 else UNREACHABLE();
3551
3552 return GL_NONE;
3553}
3554
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003555GLenum OutputHLSL::glVariablePrecision(const TType &type)
3556{
3557 if (type.getBasicType() == EbtFloat)
3558 {
3559 switch (type.getPrecision())
3560 {
3561 case EbpHigh: return GL_HIGH_FLOAT;
3562 case EbpMedium: return GL_MEDIUM_FLOAT;
3563 case EbpLow: return GL_LOW_FLOAT;
3564 case EbpUndefined:
3565 // Should be defined as the default precision by the parser
3566 default: UNREACHABLE();
3567 }
3568 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003569 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003570 {
3571 switch (type.getPrecision())
3572 {
3573 case EbpHigh: return GL_HIGH_INT;
3574 case EbpMedium: return GL_MEDIUM_INT;
3575 case EbpLow: return GL_LOW_INT;
3576 case EbpUndefined:
3577 // Should be defined as the default precision by the parser
3578 default: UNREACHABLE();
3579 }
3580 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003581
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003582 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003583 return GL_NONE;
3584}
3585
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003586bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3587{
3588 switch(qualifier)
3589 {
3590 case EvqVaryingOut:
3591 case EvqInvariantVaryingOut:
3592 case EvqSmoothOut:
3593 case EvqFlatOut:
3594 case EvqCentroidOut:
3595 return true;
3596 }
3597
3598 return false;
3599}
3600
3601bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3602{
3603 switch(qualifier)
3604 {
3605 case EvqVaryingIn:
3606 case EvqInvariantVaryingIn:
3607 case EvqSmoothIn:
3608 case EvqFlatIn:
3609 case EvqCentroidIn:
3610 return true;
3611 }
3612
3613 return false;
3614}
3615
3616bool OutputHLSL::isVarying(TQualifier qualifier)
3617{
3618 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3619}
3620
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003621}