blob: 22a4e4fcb29b5937a912870196294820bb66349f [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
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000256TString OutputHLSL::interfaceBlockMemberTypeString(const TType &memberType)
257{
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 Madill529077d2013-06-20 11:55:54 -0400270 return structureTypeName(memberType, matrixPacking == EmpColumnMajor);
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{
328 if (!type.isMatrix() && !type.isArray())
329 {
330 return "";
331 }
332
333 const GLenum glType = glVariableType(type);
334 int numComponents = 0;
335
336 if (type.isMatrix())
337 {
338 const bool isRowMajorMatrix = (type.getLayoutQualifier().matrixPacking == EmpRowMajor);
339 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
340 }
341 else
342 {
343 numComponents = gl::UniformComponentCount(glType);
344 }
345
346 TString padding;
347 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
348 {
349 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
350 }
351 return padding;
352}
353
354TString OutputHLSL::interfaceBlockMemberString(const TTypeList &typeList, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000355{
356 TString hlsl;
357
Jamie Madill574d9dd2013-06-20 11:55:56 -0400358 int elementIndex = 0;
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000359
360 for (unsigned int typeIndex = 0; typeIndex < typeList.size(); typeIndex++)
361 {
362 const TType &memberType = *typeList[typeIndex].type;
Jamie Madill574d9dd2013-06-20 11:55:56 -0400363
364 if (blockStorage == EbsStd140)
365 {
366 if (memberType.getBasicType() == EbtStruct)
367 {
368 UNIMPLEMENTED();
369 }
370 else
371 {
372 // 2 and 3 component vector types in some cases need pre-padding
373 hlsl += std140PrePaddingString(memberType, &elementIndex);
374 }
375 }
376
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000377 hlsl += " " + interfaceBlockMemberTypeString(memberType) +
378 " " + decorate(memberType.getFieldName()) + arrayString(memberType) + ";\n";
Jamie Madill574d9dd2013-06-20 11:55:56 -0400379
380 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
381 if (blockStorage == EbsStd140)
382 {
383 hlsl += std140PostPaddingString(memberType);
384 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000385 }
386
387 return hlsl;
388}
389
390TString OutputHLSL::interfaceBlockStructString(const TType &interfaceBlockType)
391{
392 const TTypeList &typeList = *interfaceBlockType.getStruct();
Jamie Madill574d9dd2013-06-20 11:55:56 -0400393 const TLayoutBlockStorage blockStorage = interfaceBlockType.getLayoutQualifier().blockStorage;
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000394
395 return "struct " + interfaceBlockStructName(interfaceBlockType) + "\n"
396 "{\n" +
Jamie Madill574d9dd2013-06-20 11:55:56 -0400397 interfaceBlockMemberString(typeList, blockStorage) +
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000398 "};\n\n";
399}
400
401TString OutputHLSL::interfaceBlockString(const TType &interfaceBlockType, unsigned int registerIndex, unsigned int arrayIndex)
402{
403 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
404 const TString &blockName = interfaceBlockType.getTypeName() + arrayIndexString;
405 TString hlsl;
406
407 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
408 "{\n";
409
410 if (interfaceBlockType.hasInstanceName())
411 {
412 hlsl += " " + interfaceBlockStructName(interfaceBlockType) + " " + interfaceBlockInstanceString(interfaceBlockType, arrayIndex) + ";\n";
413 }
414 else
415 {
416 const TTypeList &typeList = *interfaceBlockType.getStruct();
Jamie Madill574d9dd2013-06-20 11:55:56 -0400417 const TLayoutBlockStorage blockStorage = interfaceBlockType.getLayoutQualifier().blockStorage;
418 hlsl += interfaceBlockMemberString(typeList, blockStorage);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000419 }
420
421 hlsl += "};\n\n";
422
423 return hlsl;
424}
425
Jamie Madill440dc742013-06-20 11:55:55 -0400426// Use the same layout for packed and shared
427void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
428{
429 interfaceBlock->layout = newLayout;
430 interfaceBlock->blockInfo.clear();
431
432 switch (newLayout)
433 {
434 case BLOCKLAYOUT_SHARED:
435 case BLOCKLAYOUT_PACKED:
436 {
437 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
438 hlslEncoder.encodeFields(interfaceBlock->activeUniforms);
439 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
440 }
441 break;
442
443 case BLOCKLAYOUT_STANDARD:
444 {
445 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
446 stdEncoder.encodeFields(interfaceBlock->activeUniforms);
447 interfaceBlock->dataSize = stdEncoder.getBlockSize();
448 }
449 break;
450
451 default:
452 UNREACHABLE();
453 break;
454 }
455}
456
Jamie Madill574d9dd2013-06-20 11:55:56 -0400457BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
458{
459 switch (blockStorage)
460 {
461 case EbsPacked: return BLOCKLAYOUT_PACKED;
462 case EbsShared: return BLOCKLAYOUT_SHARED;
463 case EbsStd140: return BLOCKLAYOUT_STANDARD;
464 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
465 }
466}
467
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468void OutputHLSL::header()
469{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000470 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000472 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000473 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000474 TString varyings;
475 TString attributes;
476
477 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
478 {
479 const TType &type = uniform->second->getType();
480 const TString &name = uniform->second->getSymbol();
481
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000482 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
483 {
484 int index = samplerRegister(mReferencedUniforms[name]);
485
486 uniforms += "uniform SamplerState sampler_" + decorateUniform(name, type) + arrayString(type) +
487 " : register(s" + str(index) + ");\n";
488
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000489 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000490 " : register(t" + str(index) + ");\n";
491 }
492 else
493 {
494 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
495 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
496 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000497 }
498
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000499 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
500 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000501 const TType &nodeType = interfaceBlockIt->second->getType();
502 const TType &interfaceBlockType = nodeType.isInterfaceBlockMember() ? *nodeType.getInterfaceBlockType() : nodeType;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000503 const TString &blockName = interfaceBlockType.getTypeName();
504 const TTypeList &typeList = *interfaceBlockType.getStruct();
505
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000506 const unsigned int arraySize = interfaceBlockType.isArray() ? interfaceBlockType.getArraySize() : 0;
507 sh::InterfaceBlock interfaceBlock(blockName.c_str(), arraySize, mInterfaceBlockRegister);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000508 for (unsigned int typeIndex = 0; typeIndex < typeList.size(); typeIndex++)
509 {
510 const TType &memberType = *typeList[typeIndex].type;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000511 const TString &fullUniformName = interfaceBlockUniformName(interfaceBlockType, memberType);
512 declareUniformToList(memberType, fullUniformName, typeIndex, interfaceBlock.activeUniforms);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000513 }
514
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000515 mInterfaceBlockRegister += std::max(1u, interfaceBlock.arraySize);
516
Jamie Madill574d9dd2013-06-20 11:55:56 -0400517 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlockType.getLayoutQualifier().blockStorage);
518 setBlockLayout(&interfaceBlock, blockLayoutType);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000519 mActiveInterfaceBlocks.push_back(interfaceBlock);
520
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000521 if (interfaceBlockType.hasInstanceName())
522 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000523 interfaceBlocks += interfaceBlockStructString(interfaceBlockType);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000524 }
525
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000526 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000527 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000528 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
529 {
530 interfaceBlocks += interfaceBlockString(interfaceBlockType, interfaceBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000531 }
532 }
533 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000534 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000535 interfaceBlocks += interfaceBlockString(interfaceBlockType, interfaceBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000536 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000537 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000538
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000539 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
540 {
541 const TType &type = varying->second->getType();
542 const TString &name = varying->second->getSymbol();
543
544 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000545 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
546 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000547 }
548
549 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
550 {
551 const TType &type = attribute->second->getType();
552 const TString &name = attribute->second->getSymbol();
553
554 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400555
556 ShaderVariable shaderVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
557 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
558 mActiveAttributes.push_back(shaderVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000559 }
560
Jamie Madill529077d2013-06-20 11:55:54 -0400561 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
562 {
563 out << *structDeclaration;
564 }
565
566 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
567 {
568 out << *constructor;
569 }
570
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400571 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000573 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000574 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000575
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000576 out << "// Varyings\n";
577 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400578 out << "\n";
579
580 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000581 {
Jamie Madill46131a32013-06-20 11:55:50 -0400582 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000583 {
Jamie Madill46131a32013-06-20 11:55:50 -0400584 const TString &variableName = outputVariableIt->first;
585 const TType &variableType = outputVariableIt->second->getType();
586 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
587
588 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
589 " = " + initializer(variableType) + ";\n";
590
591 ShaderVariable outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
592 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
593 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000594 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000595 }
Jamie Madill46131a32013-06-20 11:55:50 -0400596 else
597 {
598 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
599
600 out << "static float4 gl_Color[" << numColorValues << "] =\n"
601 "{\n";
602 for (unsigned int i = 0; i < numColorValues; i++)
603 {
604 out << " float4(0, 0, 0, 0)";
605 if (i + 1 != numColorValues)
606 {
607 out << ",";
608 }
609 out << "\n";
610 }
611
612 out << "};\n";
613 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000614
615 if (mUsesFragCoord)
616 {
617 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
618 }
619
620 if (mUsesPointCoord)
621 {
622 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
623 }
624
625 if (mUsesFrontFacing)
626 {
627 out << "static bool gl_FrontFacing = false;\n";
628 }
629
630 out << "\n";
631
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000632 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000633 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000634 out << "struct gl_DepthRangeParameters\n"
635 "{\n"
636 " float near;\n"
637 " float far;\n"
638 " float diff;\n"
639 "};\n"
640 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000641 }
642
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000643 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000644 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000645 out << "cbuffer DriverConstants : register(b1)\n"
646 "{\n";
647
648 if (mUsesDepthRange)
649 {
650 out << " float3 dx_DepthRange : packoffset(c0);\n";
651 }
652
653 if (mUsesFragCoord)
654 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000655 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000656 }
657
658 if (mUsesFragCoord || mUsesFrontFacing)
659 {
660 out << " float3 dx_DepthFront : packoffset(c2);\n";
661 }
662
663 out << "};\n";
664 }
665 else
666 {
667 if (mUsesDepthRange)
668 {
669 out << "uniform float3 dx_DepthRange : register(c0);";
670 }
671
672 if (mUsesFragCoord)
673 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000674 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000675 }
676
677 if (mUsesFragCoord || mUsesFrontFacing)
678 {
679 out << "uniform float3 dx_DepthFront : register(c2);\n";
680 }
681 }
682
683 out << "\n";
684
685 if (mUsesDepthRange)
686 {
687 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
688 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000689 }
690
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000692 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000693
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000694 if (!interfaceBlocks.empty())
695 {
696 out << interfaceBlocks;
697 out << "\n";
698 }
699
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000700 if (usingMRTExtension && mNumRenderTargets > 1)
701 {
702 out << "#define GL_USES_MRT\n";
703 }
704
705 if (mUsesFragColor)
706 {
707 out << "#define GL_USES_FRAG_COLOR\n";
708 }
709
710 if (mUsesFragData)
711 {
712 out << "#define GL_USES_FRAG_DATA\n";
713 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000714 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000715 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000716 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000717 out << "// Attributes\n";
718 out << attributes;
719 out << "\n"
720 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
721
722 if (mUsesPointSize)
723 {
724 out << "static float gl_PointSize = float(1);\n";
725 }
726
727 out << "\n"
728 "// Varyings\n";
729 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000730 out << "\n";
731
732 if (mUsesDepthRange)
733 {
734 out << "struct gl_DepthRangeParameters\n"
735 "{\n"
736 " float near;\n"
737 " float far;\n"
738 " float diff;\n"
739 "};\n"
740 "\n";
741 }
742
743 if (mOutputType == SH_HLSL11_OUTPUT)
744 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000745 if (mUsesDepthRange)
746 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000747 out << "cbuffer DriverConstants : register(b1)\n"
748 "{\n"
749 " float3 dx_DepthRange : packoffset(c0);\n"
750 "};\n"
751 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000752 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000753 }
754 else
755 {
756 if (mUsesDepthRange)
757 {
758 out << "uniform float3 dx_DepthRange : register(c0);\n";
759 }
760
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000761 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000762 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000763 }
764
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000765 if (mUsesDepthRange)
766 {
767 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
768 "\n";
769 }
770
771 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000772 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000773
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000774 if (!interfaceBlocks.empty())
775 {
776 out << interfaceBlocks;
777 out << "\n";
778 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400779 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000780
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400781 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
782 {
783 // Return type
784 switch(textureFunction->sampler)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000785 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400786 case EbtSampler2D: out << "float4 "; break;
787 case EbtSampler3D: out << "float4 "; break;
788 case EbtSamplerCube: out << "float4 "; break;
789 case EbtSampler2DArray: out << "float4 "; break;
790 case EbtISampler2D: out << "int4 "; break;
791 case EbtISampler3D: out << "int4 "; break;
792 case EbtISamplerCube: out << "int4 "; break;
793 case EbtISampler2DArray: out << "int4 "; break;
794 case EbtUSampler2D: out << "uint4 "; break;
795 case EbtUSampler3D: out << "uint4 "; break;
796 case EbtUSamplerCube: out << "uint4 "; break;
797 case EbtUSampler2DArray: out << "uint4 "; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400798 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000799 }
800
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400801 // Function name
802 out << textureFunction->name();
803
804 // Argument list
805 int hlslCoords = 4;
806
807 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000808 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400809 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000810 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400811 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
812 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
813 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000814 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400815
816 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000817 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400818 case TextureFunction::IMPLICIT: break;
819 case TextureFunction::BIAS: hlslCoords = 4; break;
820 case TextureFunction::LOD: hlslCoords = 4; break;
821 case TextureFunction::LOD0: hlslCoords = 4; break;
822 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000823 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400824 }
825 else if (mOutputType == SH_HLSL11_OUTPUT)
826 {
827 switch(textureFunction->sampler)
828 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400829 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
830 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
831 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
832 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 2; break;
833 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
834 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
835 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
836 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 2; break;
837 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
838 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
839 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
840 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 2; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400841 default: UNREACHABLE();
842 }
843 }
844 else UNREACHABLE();
845
846 switch(textureFunction->coords)
847 {
848 case 2: out << ", float2 t"; break;
849 case 3: out << ", float3 t"; break;
850 case 4: out << ", float4 t"; break;
851 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000852 }
853
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400854 switch(textureFunction->mipmap)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000855 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400856 case TextureFunction::IMPLICIT: break;
857 case TextureFunction::BIAS: out << ", float bias"; break;
858 case TextureFunction::LOD: out << ", float lod"; break;
859 case TextureFunction::LOD0: break;
860 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000861 }
862
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400863 out << ")\n"
864 "{\n"
865 " return ";
866
867 // HLSL intrinsic
868 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000869 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400870 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000871 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400872 case EbtSampler2D: out << "tex2D"; break;
873 case EbtSamplerCube: out << "texCUBE"; break;
874 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000875 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400876 }
877 else if (mOutputType == SH_HLSL11_OUTPUT)
878 {
879 out << "x.Sample";
880 }
881 else UNREACHABLE();
882
883 if (mOutputType == SH_HLSL9_OUTPUT)
884 {
885 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000886 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400887 case TextureFunction::IMPLICIT: out << "(s, "; break;
888 case TextureFunction::BIAS: out << "bias(s, "; break;
889 case TextureFunction::LOD: out << "lod(s, "; break;
890 case TextureFunction::LOD0: out << "lod(s, "; break;
891 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000892 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400893 }
894 else if (mOutputType == SH_HLSL11_OUTPUT)
895 {
896 switch(textureFunction->mipmap)
897 {
898 case TextureFunction::IMPLICIT: out << "(s, "; break;
899 case TextureFunction::BIAS: out << "Bias(s, "; break;
900 case TextureFunction::LOD: out << "Level(s, "; break;
901 case TextureFunction::LOD0: out << "Level(s, "; break;
902 default: UNREACHABLE();
903 }
904 }
905 else UNREACHABLE();
906
907 switch(hlslCoords)
908 {
909 case 2: out << "float2("; break;
910 case 3: out << "float3("; break;
911 case 4: out << "float4("; break;
912 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000913 }
914
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400915 TString proj = "";
916
917 if (textureFunction->proj)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400918 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400919 switch(textureFunction->coords)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400920 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400921 case 3: proj = " / t.z"; break;
922 case 4: proj = " / t.w"; break;
923 default: UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400924 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400925 }
926
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400927 out << "t.x" + proj + ", t.y" + proj;
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400928
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400929 if (mOutputType == SH_HLSL9_OUTPUT)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400930 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400931 if (hlslCoords >= 3)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400932 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400933 if (textureFunction->coords < 3)
934 {
935 out << ", 0";
936 }
937 else
938 {
939 out << ", t.z" + proj;
940 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400941 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400942
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400943 if (hlslCoords == 4)
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400944 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400945 switch(textureFunction->mipmap)
946 {
947 case TextureFunction::BIAS: out << ", bias"; break;
948 case TextureFunction::LOD: out << ", lod"; break;
949 case TextureFunction::LOD0: out << ", 0"; break;
950 default: UNREACHABLE();
951 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400952 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -0400953
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400954 out << "));\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000955 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400956 else if (mOutputType == SH_HLSL11_OUTPUT)
957 {
958 if (hlslCoords >= 3)
959 {
960 out << ", t.z" + proj;
961 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000962
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400963 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000964 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400965 case TextureFunction::IMPLICIT: out << "));"; break;
966 case TextureFunction::BIAS: out << "), bias);"; break;
967 case TextureFunction::LOD: out << "), lod);"; break;
968 case TextureFunction::LOD0: out << "), 0);"; break;
969 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000970 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000971 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400972 else UNREACHABLE();
973
974 out << "}\n"
975 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976 }
977
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000978 if (mUsesFragCoord)
979 {
980 out << "#define GL_USES_FRAG_COORD\n";
981 }
982
983 if (mUsesPointCoord)
984 {
985 out << "#define GL_USES_POINT_COORD\n";
986 }
987
988 if (mUsesFrontFacing)
989 {
990 out << "#define GL_USES_FRONT_FACING\n";
991 }
992
993 if (mUsesPointSize)
994 {
995 out << "#define GL_USES_POINT_SIZE\n";
996 }
997
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000998 if (mUsesDepthRange)
999 {
1000 out << "#define GL_USES_DEPTH_RANGE\n";
1001 }
1002
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001003 if (mUsesXor)
1004 {
1005 out << "bool xor(bool p, bool q)\n"
1006 "{\n"
1007 " return (p || q) && !(p && q);\n"
1008 "}\n"
1009 "\n";
1010 }
1011
1012 if (mUsesMod1)
1013 {
1014 out << "float mod(float x, float y)\n"
1015 "{\n"
1016 " return x - y * floor(x / y);\n"
1017 "}\n"
1018 "\n";
1019 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001020
1021 if (mUsesMod2v)
1022 {
1023 out << "float2 mod(float2 x, float2 y)\n"
1024 "{\n"
1025 " return x - y * floor(x / y);\n"
1026 "}\n"
1027 "\n";
1028 }
1029
1030 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001031 {
1032 out << "float2 mod(float2 x, float y)\n"
1033 "{\n"
1034 " return x - y * floor(x / y);\n"
1035 "}\n"
1036 "\n";
1037 }
1038
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001039 if (mUsesMod3v)
1040 {
1041 out << "float3 mod(float3 x, float3 y)\n"
1042 "{\n"
1043 " return x - y * floor(x / y);\n"
1044 "}\n"
1045 "\n";
1046 }
1047
1048 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001049 {
1050 out << "float3 mod(float3 x, float y)\n"
1051 "{\n"
1052 " return x - y * floor(x / y);\n"
1053 "}\n"
1054 "\n";
1055 }
1056
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001057 if (mUsesMod4v)
1058 {
1059 out << "float4 mod(float4 x, float4 y)\n"
1060 "{\n"
1061 " return x - y * floor(x / y);\n"
1062 "}\n"
1063 "\n";
1064 }
1065
1066 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001067 {
1068 out << "float4 mod(float4 x, float y)\n"
1069 "{\n"
1070 " return x - y * floor(x / y);\n"
1071 "}\n"
1072 "\n";
1073 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001074
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001075 if (mUsesFaceforward1)
1076 {
1077 out << "float faceforward(float N, float I, float Nref)\n"
1078 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001079 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001080 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001081 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001082 " }\n"
1083 " else\n"
1084 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001085 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001086 " }\n"
1087 "}\n"
1088 "\n";
1089 }
1090
1091 if (mUsesFaceforward2)
1092 {
1093 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1094 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001095 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001096 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001097 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001098 " }\n"
1099 " else\n"
1100 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001101 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001102 " }\n"
1103 "}\n"
1104 "\n";
1105 }
1106
1107 if (mUsesFaceforward3)
1108 {
1109 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1110 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001111 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001112 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001113 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001114 " }\n"
1115 " else\n"
1116 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001117 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001118 " }\n"
1119 "}\n"
1120 "\n";
1121 }
1122
1123 if (mUsesFaceforward4)
1124 {
1125 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1126 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001127 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001128 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001129 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001130 " }\n"
1131 " else\n"
1132 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001133 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001134 " }\n"
1135 "}\n"
1136 "\n";
1137 }
1138
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001139 for (unsigned int cols = 2; cols <= 4; cols++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001140 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001141 for (unsigned int rows = 2; rows <= 4; rows++)
1142 {
1143 if (mUsesEqualMat[cols][rows])
1144 {
1145 TString matrixType = "float" + str(cols) + "x" + str(rows);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001146
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001147 out << "bool equal(" + matrixType + " m, " + matrixType + " n)\n"
1148 "{\n";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001149
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001150 for (unsigned int row = 0; row < rows; row++)
1151 {
1152 if (row == 0)
1153 {
1154 out << " return ";
1155 }
1156 else
1157 {
1158 out << " ";
1159 }
1160
1161 for (unsigned int col = 0; col < cols; col++)
1162 {
1163 TString index = "[" + str(col) + "][" + str(row) + "]";
1164 out << "m" + index + " == n" + index;
1165
1166 if (col == cols-1 && row == rows-1)
1167 {
1168 out << ";\n";
1169 }
1170 else if (col == cols-1)
1171 {
1172 out << " &&\n";
1173 }
1174 else
1175 {
1176 out << " && ";
1177 }
1178 }
1179 }
1180
1181 out << "}\n";
1182 }
1183 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001184 }
1185
1186 if (mUsesEqualVec2)
1187 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001188 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001189 "{\n"
1190 " return v.x == u.x && v.y == u.y;\n"
1191 "}\n";
1192 }
1193
1194 if (mUsesEqualVec3)
1195 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001196 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001197 "{\n"
1198 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1199 "}\n";
1200 }
1201
1202 if (mUsesEqualVec4)
1203 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001204 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001205 "{\n"
1206 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1207 "}\n";
1208 }
1209
1210 if (mUsesEqualIVec2)
1211 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001212 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001213 "{\n"
1214 " return v.x == u.x && v.y == u.y;\n"
1215 "}\n";
1216 }
1217
1218 if (mUsesEqualIVec3)
1219 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001220 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001221 "{\n"
1222 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1223 "}\n";
1224 }
1225
1226 if (mUsesEqualIVec4)
1227 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001228 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001229 "{\n"
1230 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1231 "}\n";
1232 }
1233
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001234 if (mUsesEqualUVec2)
1235 {
1236 out << "bool equal(uint2 v, uint2 u)\n"
1237 "{\n"
1238 " return v.x == u.x && v.y == u.y;\n"
1239 "}\n";
1240 }
1241
1242 if (mUsesEqualUVec3)
1243 {
1244 out << "bool equal(uint3 v, uint3 u)\n"
1245 "{\n"
1246 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1247 "}\n";
1248 }
1249
1250 if (mUsesEqualUVec4)
1251 {
1252 out << "bool equal(uint4 v, uint4 u)\n"
1253 "{\n"
1254 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1255 "}\n";
1256 }
1257
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001258 if (mUsesEqualBVec2)
1259 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001260 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001261 "{\n"
1262 " return v.x == u.x && v.y == u.y;\n"
1263 "}\n";
1264 }
1265
1266 if (mUsesEqualBVec3)
1267 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001268 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001269 "{\n"
1270 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1271 "}\n";
1272 }
1273
1274 if (mUsesEqualBVec4)
1275 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001276 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001277 "{\n"
1278 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1279 "}\n";
1280 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001281
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001282 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001283 {
1284 out << "float atanyx(float y, float x)\n"
1285 "{\n"
1286 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1287 " return atan2(y, x);\n"
1288 "}\n";
1289 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001290
1291 if (mUsesAtan2_2)
1292 {
1293 out << "float2 atanyx(float2 y, float2 x)\n"
1294 "{\n"
1295 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1296 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1297 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1298 "}\n";
1299 }
1300
1301 if (mUsesAtan2_3)
1302 {
1303 out << "float3 atanyx(float3 y, float3 x)\n"
1304 "{\n"
1305 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1306 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1307 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1308 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1309 "}\n";
1310 }
1311
1312 if (mUsesAtan2_4)
1313 {
1314 out << "float4 atanyx(float4 y, float4 x)\n"
1315 "{\n"
1316 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1317 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1318 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1319 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1320 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1321 "}\n";
1322 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001323}
1324
1325void OutputHLSL::visitSymbol(TIntermSymbol *node)
1326{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001327 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001328
1329 TString name = node->getSymbol();
1330
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001331 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001332 {
1333 mUsesDepthRange = true;
1334 out << name;
1335 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001336 else
1337 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001338 TQualifier qualifier = node->getQualifier();
1339
1340 if (qualifier == EvqUniform)
1341 {
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001342 if (node->getType().isInterfaceBlockMember())
1343 {
1344 const TString& interfaceBlockTypeName = node->getType().getInterfaceBlockType()->getTypeName();
1345 mReferencedInterfaceBlocks[interfaceBlockTypeName] = node;
1346 out << decorateUniform(name, node->getType());
1347 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001348 else if (node->getBasicType() == EbtInterfaceBlock)
1349 {
1350 const TString& interfaceBlockTypeName = node->getType().getTypeName();
1351 mReferencedInterfaceBlocks[interfaceBlockTypeName] = node;
1352 out << decorateUniform(name, node->getType());
1353 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001354 else
1355 {
1356 mReferencedUniforms[name] = node;
1357 out << decorateUniform(name, node->getType());
1358 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001359 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001360 else if (qualifier == EvqAttribute || qualifier == EvqVertexInput)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001361 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001362 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001363 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001364 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001365 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001366 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001367 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001368 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001369 }
Jamie Madill46131a32013-06-20 11:55:50 -04001370 else if (qualifier == EvqFragmentOutput)
1371 {
1372 mReferencedOutputVariables[name] = node;
1373 out << "out_" << name;
1374 }
1375 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001376 {
1377 out << "gl_Color[0]";
1378 mUsesFragColor = true;
1379 }
1380 else if (qualifier == EvqFragData)
1381 {
1382 out << "gl_Color";
1383 mUsesFragData = true;
1384 }
1385 else if (qualifier == EvqFragCoord)
1386 {
1387 mUsesFragCoord = true;
1388 out << name;
1389 }
1390 else if (qualifier == EvqPointCoord)
1391 {
1392 mUsesPointCoord = true;
1393 out << name;
1394 }
1395 else if (qualifier == EvqFrontFacing)
1396 {
1397 mUsesFrontFacing = true;
1398 out << name;
1399 }
1400 else if (qualifier == EvqPointSize)
1401 {
1402 mUsesPointSize = true;
1403 out << name;
1404 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001405 else
1406 {
1407 out << decorate(name);
1408 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001409 }
1410}
1411
1412bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1413{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001414 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001415
1416 switch (node->getOp())
1417 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001418 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001419 case EOpInitialize:
1420 if (visit == PreVisit)
1421 {
1422 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1423 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1424 // new variable is created before the assignment is evaluated), so we need to convert
1425 // this to "float t = x, x = t;".
1426
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001427 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1428 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001429
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001430 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1431 expression->traverse(&searchSymbol);
1432 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001433
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001434 if (sameSymbol)
1435 {
1436 // Type already printed
1437 out << "t" + str(mUniqueIndex) + " = ";
1438 expression->traverse(this);
1439 out << ", ";
1440 symbolNode->traverse(this);
1441 out << " = t" + str(mUniqueIndex);
1442
1443 mUniqueIndex++;
1444 return false;
1445 }
1446 }
1447 else if (visit == InVisit)
1448 {
1449 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001450 }
1451 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001452 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1453 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1454 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1455 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1456 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1457 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001458 if (visit == PreVisit)
1459 {
1460 out << "(";
1461 }
1462 else if (visit == InVisit)
1463 {
1464 out << " = mul(";
1465 node->getLeft()->traverse(this);
1466 out << ", transpose(";
1467 }
1468 else
1469 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001470 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001471 }
1472 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001473 case EOpMatrixTimesMatrixAssign:
1474 if (visit == PreVisit)
1475 {
1476 out << "(";
1477 }
1478 else if (visit == InVisit)
1479 {
1480 out << " = mul(";
1481 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001482 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001483 }
1484 else
1485 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001486 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001487 }
1488 break;
1489 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001490 case EOpIndexDirect:
1491 if (node->getLeft()->getBasicType() == EbtInterfaceBlock)
1492 {
1493 if (visit == PreVisit)
1494 {
1495 const TType &interfaceBlockType = node->getLeft()->getType();
1496 mReferencedInterfaceBlocks[interfaceBlockType.getInstanceName()] = node->getLeft()->getAsSymbolNode();
1497 out << interfaceBlockInstanceString(interfaceBlockType, node->getRight()->getAsConstantUnion()->getIConst(0));
1498 return false;
1499 }
1500 }
1501 else
1502 {
1503 outputTriplet(visit, "", "[", "]");
1504 }
1505 break;
1506 case EOpIndexIndirect:
1507 // We do not currently support indirect references to interface blocks
1508 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1509 outputTriplet(visit, "", "[", "]");
1510 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001511 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001512 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001513 if (visit == InVisit)
1514 {
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001515 out << "." + decorateField(node->getType().getFieldName(), node->getLeft()->getType());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001516
1517 return false;
1518 }
1519 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001520 case EOpVectorSwizzle:
1521 if (visit == InVisit)
1522 {
1523 out << ".";
1524
1525 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1526
1527 if (swizzle)
1528 {
1529 TIntermSequence &sequence = swizzle->getSequence();
1530
1531 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1532 {
1533 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1534
1535 if (element)
1536 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001537 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001538
1539 switch (i)
1540 {
1541 case 0: out << "x"; break;
1542 case 1: out << "y"; break;
1543 case 2: out << "z"; break;
1544 case 3: out << "w"; break;
1545 default: UNREACHABLE();
1546 }
1547 }
1548 else UNREACHABLE();
1549 }
1550 }
1551 else UNREACHABLE();
1552
1553 return false; // Fully processed
1554 }
1555 break;
1556 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1557 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1558 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1559 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001560 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001561 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001562 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001563 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001564 if (node->getOp() == EOpEqual)
1565 {
1566 outputTriplet(visit, "(", " == ", ")");
1567 }
1568 else
1569 {
1570 outputTriplet(visit, "(", " != ", ")");
1571 }
1572 }
1573 else if (node->getLeft()->getBasicType() == EbtStruct)
1574 {
1575 if (node->getOp() == EOpEqual)
1576 {
1577 out << "(";
1578 }
1579 else
1580 {
1581 out << "!(";
1582 }
1583
1584 const TTypeList *fields = node->getLeft()->getType().getStruct();
1585
1586 for (size_t i = 0; i < fields->size(); i++)
1587 {
1588 const TType *fieldType = (*fields)[i].type;
1589
1590 node->getLeft()->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 node->getRight()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001593 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001594
1595 if (i < fields->size() - 1)
1596 {
1597 out << " && ";
1598 }
1599 }
1600
1601 out << ")";
1602
1603 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001604 }
1605 else
1606 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001607 if (node->getLeft()->isMatrix())
1608 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001609 mUsesEqualMat[node->getLeft()->getCols()][node->getLeft()->getRows()] = true;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001610 }
1611 else if (node->getLeft()->isVector())
1612 {
1613 switch (node->getLeft()->getBasicType())
1614 {
1615 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001616 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001617 {
1618 case 2: mUsesEqualVec2 = true; break;
1619 case 3: mUsesEqualVec3 = true; break;
1620 case 4: mUsesEqualVec4 = true; break;
1621 default: UNREACHABLE();
1622 }
1623 break;
1624 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001625 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001626 {
1627 case 2: mUsesEqualIVec2 = true; break;
1628 case 3: mUsesEqualIVec3 = true; break;
1629 case 4: mUsesEqualIVec4 = true; break;
1630 default: UNREACHABLE();
1631 }
1632 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001633 case EbtUInt:
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001634 switch (node->getLeft()->getNominalSize())
1635 {
1636 case 2: mUsesEqualUVec2 = true; break;
1637 case 3: mUsesEqualUVec3 = true; break;
1638 case 4: mUsesEqualUVec4 = true; break;
1639 default: UNREACHABLE();
1640 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001641 break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001642 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001643 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001644 {
1645 case 2: mUsesEqualBVec2 = true; break;
1646 case 3: mUsesEqualBVec3 = true; break;
1647 case 4: mUsesEqualBVec4 = true; break;
1648 default: UNREACHABLE();
1649 }
1650 break;
1651 default: UNREACHABLE();
1652 }
1653 }
1654 else UNREACHABLE();
1655
1656 if (node->getOp() == EOpEqual)
1657 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001658 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001659 }
1660 else
1661 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001662 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001663 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001664 }
1665 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1667 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1668 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1669 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1670 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001671 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001672 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1673 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001674 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001675 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001676 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001677 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001678 case EOpLogicalXor:
1679 mUsesXor = true;
1680 outputTriplet(visit, "xor(", ", ", ")");
1681 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001682 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001683 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001684 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685 default: UNREACHABLE();
1686 }
1687
1688 return true;
1689}
1690
1691bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1692{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693 switch (node->getOp())
1694 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001695 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1696 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1697 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1698 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1699 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1700 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1701 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001702 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001703 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 case EOpConvFloatToBool:
1705 switch (node->getOperand()->getType().getNominalSize())
1706 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001707 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1708 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1709 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1710 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711 default: UNREACHABLE();
1712 }
1713 break;
1714 case EOpConvBoolToFloat:
1715 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001716 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717 switch (node->getOperand()->getType().getNominalSize())
1718 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001719 case 1: outputTriplet(visit, "float(", "", ")"); break;
1720 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1721 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1722 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723 default: UNREACHABLE();
1724 }
1725 break;
1726 case EOpConvFloatToInt:
1727 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001728 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729 switch (node->getOperand()->getType().getNominalSize())
1730 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001731 case 1: outputTriplet(visit, "int(", "", ")"); break;
1732 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1733 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1734 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 default: UNREACHABLE();
1736 }
1737 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001738 case EOpConvFloatToUInt:
1739 case EOpConvBoolToUInt:
1740 case EOpConvIntToUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001741 switch (node->getOperand()->getType().getCols())
1742 {
1743 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001744 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1745 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1746 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001747 default: UNREACHABLE();
1748 }
1749 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001750 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1751 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1752 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1753 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1754 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1755 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1756 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1757 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1758 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1759 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1760 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1761 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1762 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1763 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1764 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1765 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1766 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1767 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1768 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1769 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1770 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001771 case EOpDFdx:
1772 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1773 {
1774 outputTriplet(visit, "(", "", ", 0.0)");
1775 }
1776 else
1777 {
1778 outputTriplet(visit, "ddx(", "", ")");
1779 }
1780 break;
1781 case EOpDFdy:
1782 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1783 {
1784 outputTriplet(visit, "(", "", ", 0.0)");
1785 }
1786 else
1787 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001788 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001789 }
1790 break;
1791 case EOpFwidth:
1792 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1793 {
1794 outputTriplet(visit, "(", "", ", 0.0)");
1795 }
1796 else
1797 {
1798 outputTriplet(visit, "fwidth(", "", ")");
1799 }
1800 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001801 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1802 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803 default: UNREACHABLE();
1804 }
1805
1806 return true;
1807}
1808
1809bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1810{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001811 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813 switch (node->getOp())
1814 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001815 case EOpSequence:
1816 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001817 if (mInsideFunction)
1818 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001819 outputLineDirective(node->getLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001820 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001821
1822 mScopeDepth++;
1823
1824 if (mScopeBracket.size() < mScopeDepth)
1825 {
1826 mScopeBracket.push_back(0); // New scope level
1827 }
1828 else
1829 {
1830 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1831 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001832 }
1833
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001834 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1835 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001836 outputLineDirective((*sit)->getLine());
1837
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001838 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001839
1840 out << ";\n";
1841 }
1842
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001843 if (mInsideFunction)
1844 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001845 outputLineDirective(node->getEndLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001846 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001847
1848 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001849 }
1850
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001851 return false;
1852 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853 case EOpDeclaration:
1854 if (visit == PreVisit)
1855 {
1856 TIntermSequence &sequence = node->getSequence();
1857 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001858
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001859 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001861 if (variable->getType().getStruct())
1862 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001863 addConstructor(variable->getType(), scopedStruct(variable->getType().getTypeName()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001864 }
1865
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001866 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001867 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001868 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001869 {
1870 out << "static ";
1871 }
1872
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001873 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001875 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001876 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001877 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001879 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001881 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001882 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00001883 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001884 }
1885 else
1886 {
1887 (*sit)->traverse(this);
1888 }
1889
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001890 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001891 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001892 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893 }
1894 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001896 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1897 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001898 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001899 }
1900 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001901 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001902 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001903 {
1904 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1905 {
1906 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1907
1908 if (symbol)
1909 {
1910 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1911 mReferencedVaryings[symbol->getSymbol()] = symbol;
1912 }
1913 else
1914 {
1915 (*sit)->traverse(this);
1916 }
1917 }
1918 }
1919
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920 return false;
1921 }
1922 else if (visit == InVisit)
1923 {
1924 out << ", ";
1925 }
1926 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001927 case EOpPrototype:
1928 if (visit == PreVisit)
1929 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001930 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001931
1932 TIntermSequence &arguments = node->getSequence();
1933
1934 for (unsigned int i = 0; i < arguments.size(); i++)
1935 {
1936 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1937
1938 if (symbol)
1939 {
1940 out << argumentString(symbol);
1941
1942 if (i < arguments.size() - 1)
1943 {
1944 out << ", ";
1945 }
1946 }
1947 else UNREACHABLE();
1948 }
1949
1950 out << ");\n";
1951
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001952 // Also prototype the Lod0 variant if needed
1953 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1954 {
1955 mOutputLod0Function = true;
1956 node->traverse(this);
1957 mOutputLod0Function = false;
1958 }
1959
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001960 return false;
1961 }
1962 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001963 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001964 case EOpFunction:
1965 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001966 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001968 out << typeString(node->getType()) << " ";
1969
1970 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001972 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001974 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001975 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001976 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001977 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001978
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001979 TIntermSequence &sequence = node->getSequence();
1980 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1981
1982 for (unsigned int i = 0; i < arguments.size(); i++)
1983 {
1984 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1985
1986 if (symbol)
1987 {
1988 if (symbol->getType().getStruct())
1989 {
1990 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getTypeName()), NULL);
1991 }
1992
1993 out << argumentString(symbol);
1994
1995 if (i < arguments.size() - 1)
1996 {
1997 out << ", ";
1998 }
1999 }
2000 else UNREACHABLE();
2001 }
2002
2003 out << ")\n"
2004 "{\n";
2005
2006 if (sequence.size() > 1)
2007 {
2008 mInsideFunction = true;
2009 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002010 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002011 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002012
2013 out << "}\n";
2014
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002015 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2016 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002017 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002018 {
2019 mOutputLod0Function = true;
2020 node->traverse(this);
2021 mOutputLod0Function = false;
2022 }
2023 }
2024
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002025 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026 }
2027 break;
2028 case EOpFunctionCall:
2029 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002030 TString name = TFunction::unmangleName(node->getName());
2031 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002032 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002033
2034 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002036 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037 }
2038 else
2039 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002040 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2041
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002042 TextureFunction textureFunction;
2043 textureFunction.sampler = samplerType;
2044 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
2045 textureFunction.mipmap = TextureFunction::IMPLICIT;
2046
2047 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002048 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002049 textureFunction.mipmap = TextureFunction::IMPLICIT;
2050 textureFunction.proj = false;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002051 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002052 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002053 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002054 textureFunction.mipmap = TextureFunction::IMPLICIT;
2055 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002056 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002057 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002058 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002059 textureFunction.mipmap = TextureFunction::LOD;
2060 textureFunction.proj = false;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002061 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002062 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002063 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002064 textureFunction.mipmap = TextureFunction::LOD;
2065 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002066 }
2067 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002068
2069 if (textureFunction.mipmap != TextureFunction::LOD)
2070 {
2071 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2072 {
2073 textureFunction.mipmap = TextureFunction::LOD0;
2074 }
2075 else if (arguments.size() == 3)
2076 {
2077 textureFunction.mipmap = TextureFunction::BIAS;
2078 }
2079 }
2080
2081 mUsesTexture.insert(textureFunction);
2082
2083 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002085
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002086 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2087 {
2088 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2089 {
2090 out << "texture_";
2091 (*arg)->traverse(this);
2092 out << ", sampler_";
2093 }
2094
2095 (*arg)->traverse(this);
2096
2097 if (arg < arguments.end() - 1)
2098 {
2099 out << ", ";
2100 }
2101 }
2102
2103 out << ")";
2104
2105 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106 }
2107 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002108 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002109 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002110 addConstructor(node->getType(), "vec1", &node->getSequence());
2111 outputTriplet(visit, "vec1(", "", ")");
2112 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002113 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002114 addConstructor(node->getType(), "vec2", &node->getSequence());
2115 outputTriplet(visit, "vec2(", ", ", ")");
2116 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002117 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002118 addConstructor(node->getType(), "vec3", &node->getSequence());
2119 outputTriplet(visit, "vec3(", ", ", ")");
2120 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002121 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002122 addConstructor(node->getType(), "vec4", &node->getSequence());
2123 outputTriplet(visit, "vec4(", ", ", ")");
2124 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002125 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002126 addConstructor(node->getType(), "bvec1", &node->getSequence());
2127 outputTriplet(visit, "bvec1(", "", ")");
2128 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002129 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002130 addConstructor(node->getType(), "bvec2", &node->getSequence());
2131 outputTriplet(visit, "bvec2(", ", ", ")");
2132 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002133 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002134 addConstructor(node->getType(), "bvec3", &node->getSequence());
2135 outputTriplet(visit, "bvec3(", ", ", ")");
2136 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002137 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002138 addConstructor(node->getType(), "bvec4", &node->getSequence());
2139 outputTriplet(visit, "bvec4(", ", ", ")");
2140 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002141 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002142 addConstructor(node->getType(), "ivec1", &node->getSequence());
2143 outputTriplet(visit, "ivec1(", "", ")");
2144 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002145 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002146 addConstructor(node->getType(), "ivec2", &node->getSequence());
2147 outputTriplet(visit, "ivec2(", ", ", ")");
2148 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002149 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002150 addConstructor(node->getType(), "ivec3", &node->getSequence());
2151 outputTriplet(visit, "ivec3(", ", ", ")");
2152 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002153 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002154 addConstructor(node->getType(), "ivec4", &node->getSequence());
2155 outputTriplet(visit, "ivec4(", ", ", ")");
2156 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002157 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002158 addConstructor(node->getType(), "uvec1", &node->getSequence());
2159 outputTriplet(visit, "uvec1(", "", ")");
2160 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002161 case EOpConstructUVec2:
2162 addConstructor(node->getType(), "uvec2", &node->getSequence());
2163 outputTriplet(visit, "uvec2(", ", ", ")");
2164 break;
2165 case EOpConstructUVec3:
2166 addConstructor(node->getType(), "uvec3", &node->getSequence());
2167 outputTriplet(visit, "uvec3(", ", ", ")");
2168 break;
2169 case EOpConstructUVec4:
2170 addConstructor(node->getType(), "uvec4", &node->getSequence());
2171 outputTriplet(visit, "uvec4(", ", ", ")");
2172 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002173 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002174 addConstructor(node->getType(), "mat2", &node->getSequence());
2175 outputTriplet(visit, "mat2(", ", ", ")");
2176 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002177 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002178 addConstructor(node->getType(), "mat3", &node->getSequence());
2179 outputTriplet(visit, "mat3(", ", ", ")");
2180 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002181 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002182 addConstructor(node->getType(), "mat4", &node->getSequence());
2183 outputTriplet(visit, "mat4(", ", ", ")");
2184 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002185 case EOpConstructStruct:
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002186 addConstructor(node->getType(), scopedStruct(node->getType().getTypeName()), &node->getSequence());
2187 outputTriplet(visit, structLookup(node->getType().getTypeName()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002188 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002189 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2190 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2191 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2192 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2193 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2194 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002195 case EOpMod:
2196 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002197 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002198 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2199 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2200 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002201 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002202 case 11: mUsesMod1 = true; break;
2203 case 22: mUsesMod2v = true; break;
2204 case 21: mUsesMod2f = true; break;
2205 case 33: mUsesMod3v = true; break;
2206 case 31: mUsesMod3f = true; break;
2207 case 44: mUsesMod4v = true; break;
2208 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002209 default: UNREACHABLE();
2210 }
2211
2212 outputTriplet(visit, "mod(", ", ", ")");
2213 }
2214 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002215 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002216 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002217 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002218 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2219 {
2220 case 1: mUsesAtan2_1 = true; break;
2221 case 2: mUsesAtan2_2 = true; break;
2222 case 3: mUsesAtan2_3 = true; break;
2223 case 4: mUsesAtan2_4 = true; break;
2224 default: UNREACHABLE();
2225 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002226 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002227 break;
2228 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2229 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2230 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2231 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2232 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2233 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2234 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2235 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2236 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002237 case EOpFaceForward:
2238 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002239 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002240 {
2241 case 1: mUsesFaceforward1 = true; break;
2242 case 2: mUsesFaceforward2 = true; break;
2243 case 3: mUsesFaceforward3 = true; break;
2244 case 4: mUsesFaceforward4 = true; break;
2245 default: UNREACHABLE();
2246 }
2247
2248 outputTriplet(visit, "faceforward(", ", ", ")");
2249 }
2250 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2252 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2253 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254 default: UNREACHABLE();
2255 }
2256
2257 return true;
2258}
2259
2260bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2261{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002262 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002264 if (node->usesTernaryOperator())
2265 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002266 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002267 }
2268 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002270 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002271
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002272 out << "if(";
2273
2274 node->getCondition()->traverse(this);
2275
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002276 out << ")\n";
2277
2278 outputLineDirective(node->getLine());
2279 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280
daniel@transgaming.combb885322010-04-15 20:45:24 +00002281 if (node->getTrueBlock())
2282 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002283 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002284 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002286 outputLineDirective(node->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002287 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002288
2289 if (node->getFalseBlock())
2290 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002291 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002292
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002293 outputLineDirective(node->getFalseBlock()->getLine());
2294 out << "{\n";
2295
2296 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002297 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002298
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002299 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002300 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002301 }
2302 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303
2304 return false;
2305}
2306
2307void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2308{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002309 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310}
2311
2312bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2313{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002314 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2315
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002316 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002317 {
2318 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2319 }
2320
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002321 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002322 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002323 if (handleExcessiveLoop(node))
2324 {
2325 return false;
2326 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002327 }
2328
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002329 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330
alokp@chromium.org52813552010-11-16 18:36:09 +00002331 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002333 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002334
2335 outputLineDirective(node->getLine());
2336 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337 }
2338 else
2339 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002340 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341
2342 if (node->getInit())
2343 {
2344 node->getInit()->traverse(this);
2345 }
2346
2347 out << "; ";
2348
alokp@chromium.org52813552010-11-16 18:36:09 +00002349 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002351 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 }
2353
2354 out << "; ";
2355
alokp@chromium.org52813552010-11-16 18:36:09 +00002356 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002358 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 }
2360
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002361 out << ")\n";
2362
2363 outputLineDirective(node->getLine());
2364 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002365 }
2366
2367 if (node->getBody())
2368 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002369 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 }
2371
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002372 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002373 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374
alokp@chromium.org52813552010-11-16 18:36:09 +00002375 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002377 outputLineDirective(node->getCondition()->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378 out << "while(\n";
2379
alokp@chromium.org52813552010-11-16 18:36:09 +00002380 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381
daniel@transgaming.com73536982012-03-21 20:45:49 +00002382 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383 }
2384
daniel@transgaming.com73536982012-03-21 20:45:49 +00002385 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002387 mInsideDiscontinuousLoop = wasDiscontinuous;
2388
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 return false;
2390}
2391
2392bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2393{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002394 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395
2396 switch (node->getFlowOp())
2397 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002398 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002399 case EOpBreak:
2400 if (visit == PreVisit)
2401 {
2402 if (mExcessiveLoopIndex)
2403 {
2404 out << "{Break";
2405 mExcessiveLoopIndex->traverse(this);
2406 out << " = true; break;}\n";
2407 }
2408 else
2409 {
2410 out << "break;\n";
2411 }
2412 }
2413 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002414 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415 case EOpReturn:
2416 if (visit == PreVisit)
2417 {
2418 if (node->getExpression())
2419 {
2420 out << "return ";
2421 }
2422 else
2423 {
2424 out << "return;\n";
2425 }
2426 }
2427 else if (visit == PostVisit)
2428 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002429 if (node->getExpression())
2430 {
2431 out << ";\n";
2432 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 }
2434 break;
2435 default: UNREACHABLE();
2436 }
2437
2438 return true;
2439}
2440
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002441void OutputHLSL::traverseStatements(TIntermNode *node)
2442{
2443 if (isSingleStatement(node))
2444 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002445 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002446 }
2447
2448 node->traverse(this);
2449}
2450
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002451bool OutputHLSL::isSingleStatement(TIntermNode *node)
2452{
2453 TIntermAggregate *aggregate = node->getAsAggregate();
2454
2455 if (aggregate)
2456 {
2457 if (aggregate->getOp() == EOpSequence)
2458 {
2459 return false;
2460 }
2461 else
2462 {
2463 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2464 {
2465 if (!isSingleStatement(*sit))
2466 {
2467 return false;
2468 }
2469 }
2470
2471 return true;
2472 }
2473 }
2474
2475 return true;
2476}
2477
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002478// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2479// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002480bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2481{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002482 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002483 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002484
2485 // Parse loops of the form:
2486 // for(int index = initial; index [comparator] limit; index += increment)
2487 TIntermSymbol *index = NULL;
2488 TOperator comparator = EOpNull;
2489 int initial = 0;
2490 int limit = 0;
2491 int increment = 0;
2492
2493 // Parse index name and intial value
2494 if (node->getInit())
2495 {
2496 TIntermAggregate *init = node->getInit()->getAsAggregate();
2497
2498 if (init)
2499 {
2500 TIntermSequence &sequence = init->getSequence();
2501 TIntermTyped *variable = sequence[0]->getAsTyped();
2502
2503 if (variable && variable->getQualifier() == EvqTemporary)
2504 {
2505 TIntermBinary *assign = variable->getAsBinaryNode();
2506
2507 if (assign->getOp() == EOpInitialize)
2508 {
2509 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2510 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2511
2512 if (symbol && constant)
2513 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002514 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002515 {
2516 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002517 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002518 }
2519 }
2520 }
2521 }
2522 }
2523 }
2524
2525 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002526 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002527 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002528 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002529
2530 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2531 {
2532 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2533
2534 if (constant)
2535 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002536 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002537 {
2538 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002539 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002540 }
2541 }
2542 }
2543 }
2544
2545 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002546 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002547 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002548 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2549 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002550
2551 if (binaryTerminal)
2552 {
2553 TOperator op = binaryTerminal->getOp();
2554 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2555
2556 if (constant)
2557 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002558 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002559 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002560 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002561
2562 switch (op)
2563 {
2564 case EOpAddAssign: increment = value; break;
2565 case EOpSubAssign: increment = -value; break;
2566 default: UNIMPLEMENTED();
2567 }
2568 }
2569 }
2570 }
2571 else if (unaryTerminal)
2572 {
2573 TOperator op = unaryTerminal->getOp();
2574
2575 switch (op)
2576 {
2577 case EOpPostIncrement: increment = 1; break;
2578 case EOpPostDecrement: increment = -1; break;
2579 case EOpPreIncrement: increment = 1; break;
2580 case EOpPreDecrement: increment = -1; break;
2581 default: UNIMPLEMENTED();
2582 }
2583 }
2584 }
2585
2586 if (index != NULL && comparator != EOpNull && increment != 0)
2587 {
2588 if (comparator == EOpLessThanEqual)
2589 {
2590 comparator = EOpLessThan;
2591 limit += 1;
2592 }
2593
2594 if (comparator == EOpLessThan)
2595 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002596 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002597
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002598 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002599 {
2600 return false; // Not an excessive loop
2601 }
2602
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002603 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2604 mExcessiveLoopIndex = index;
2605
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002606 out << "{int ";
2607 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002608 out << ";\n"
2609 "bool Break";
2610 index->traverse(this);
2611 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002612
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002613 bool firstLoopFragment = true;
2614
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002615 while (iterations > 0)
2616 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002617 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002618
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002619 if (!firstLoopFragment)
2620 {
2621 out << "if(!Break";
2622 index->traverse(this);
2623 out << ") {\n";
2624 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002625
2626 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2627 {
2628 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2629 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002630
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002631 // for(int index = initial; index < clampedLimit; index += increment)
2632
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002633 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002634 index->traverse(this);
2635 out << " = ";
2636 out << initial;
2637
2638 out << "; ";
2639 index->traverse(this);
2640 out << " < ";
2641 out << clampedLimit;
2642
2643 out << "; ";
2644 index->traverse(this);
2645 out << " += ";
2646 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002647 out << ")\n";
2648
2649 outputLineDirective(node->getLine());
2650 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002651
2652 if (node->getBody())
2653 {
2654 node->getBody()->traverse(this);
2655 }
2656
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002657 outputLineDirective(node->getLine());
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002658 out << ";}\n";
2659
2660 if (!firstLoopFragment)
2661 {
2662 out << "}\n";
2663 }
2664
2665 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002666
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002667 initial += MAX_LOOP_ITERATIONS * increment;
2668 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002669 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002670
2671 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002672
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002673 mExcessiveLoopIndex = restoreIndex;
2674
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002675 return true;
2676 }
2677 else UNIMPLEMENTED();
2678 }
2679
2680 return false; // Not handled as an excessive loop
2681}
2682
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002683void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002684{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002685 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002687 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688 {
2689 out << preString;
2690 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002691 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692 {
2693 out << inString;
2694 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002695 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002696 {
2697 out << postString;
2698 }
2699}
2700
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002701void OutputHLSL::outputLineDirective(int line)
2702{
2703 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2704 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002705 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002706 mBody << "#line " << line;
2707
2708 if (mContext.sourcePath)
2709 {
2710 mBody << " \"" << mContext.sourcePath << "\"";
2711 }
2712
2713 mBody << "\n";
2714 }
2715}
2716
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002717TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2718{
2719 TQualifier qualifier = symbol->getQualifier();
2720 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002721 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002722
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002723 if (name.empty()) // HLSL demands named arguments, also for prototypes
2724 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002725 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002726 }
2727 else
2728 {
2729 name = decorate(name);
2730 }
2731
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002732 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2733 {
2734 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2735 qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type);
2736 }
2737
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002738 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002739}
2740
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002741TString OutputHLSL::interpolationString(TQualifier qualifier)
2742{
2743 switch(qualifier)
2744 {
2745 case EvqVaryingIn: return "";
2746 case EvqInvariantVaryingIn: return "";
2747 case EvqSmoothIn: return "linear";
2748 case EvqFlatIn: return "nointerpolation";
2749 case EvqCentroidIn: return "centroid";
2750 case EvqVaryingOut: return "";
2751 case EvqInvariantVaryingOut: return "";
2752 case EvqSmoothOut: return "linear";
2753 case EvqFlatOut: return "nointerpolation";
2754 case EvqCentroidOut: return "centroid";
2755 default: UNREACHABLE();
2756 }
2757
2758 return "";
2759}
2760
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002761TString OutputHLSL::qualifierString(TQualifier qualifier)
2762{
2763 switch(qualifier)
2764 {
2765 case EvqIn: return "in";
2766 case EvqOut: return "out";
2767 case EvqInOut: return "inout";
2768 case EvqConstReadOnly: return "const";
2769 default: UNREACHABLE();
2770 }
2771
2772 return "";
2773}
2774
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775TString OutputHLSL::typeString(const TType &type)
2776{
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002777 if (type.getBasicType() == EbtStruct)
2778 {
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002779 if (type.getTypeName() != "")
2780 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002781 return structLookup(type.getTypeName());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002782 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002783 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002784 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04002785 return structureString(type, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002786 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002787 }
2788 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00002790 int cols = type.getCols();
2791 int rows = type.getRows();
2792 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002793 }
2794 else
2795 {
2796 switch (type.getBasicType())
2797 {
2798 case EbtFloat:
2799 switch (type.getNominalSize())
2800 {
2801 case 1: return "float";
2802 case 2: return "float2";
2803 case 3: return "float3";
2804 case 4: return "float4";
2805 }
2806 case EbtInt:
2807 switch (type.getNominalSize())
2808 {
2809 case 1: return "int";
2810 case 2: return "int2";
2811 case 3: return "int3";
2812 case 4: return "int4";
2813 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002814 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04002815 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002816 {
2817 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002818 case 2: return "uint2";
2819 case 3: return "uint3";
2820 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002821 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002822 case EbtBool:
2823 switch (type.getNominalSize())
2824 {
2825 case 1: return "bool";
2826 case 2: return "bool2";
2827 case 3: return "bool3";
2828 case 4: return "bool4";
2829 }
2830 case EbtVoid:
2831 return "void";
2832 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002833 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04002834 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04002835 case EbtSampler2DArray:
2836 case EbtISampler2DArray:
2837 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002838 return "sampler2D";
2839 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002840 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04002841 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002842 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00002843 case EbtSamplerExternalOES:
2844 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002845 default:
2846 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002847 }
2848 }
2849
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002850 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002851 return "<unknown type>";
2852}
2853
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002854TString OutputHLSL::textureString(const TType &type)
2855{
2856 switch (type.getBasicType())
2857 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -04002858 case EbtSampler2D: return "Texture2D";
2859 case EbtSamplerCube: return "TextureCube";
2860 case EbtSamplerExternalOES: return "Texture2D";
2861 case EbtSampler2DArray: return "Texture2DArray";
2862 case EbtISampler2D: return "Texture2D<int4>";
2863 case EbtISamplerCube: return "TextureCube<int4>";
2864 case EbtISampler2DArray: return "Texture2DArray<int4>";
2865 case EbtUSampler2D: return "Texture2D<uint4>";
2866 case EbtUSamplerCube: return "TextureCube<uint4>";
2867 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002868 default:
2869 break;
2870 }
2871
2872 UNREACHABLE();
2873 return "<unknown texture type>";
2874}
2875
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002876TString OutputHLSL::arrayString(const TType &type)
2877{
2878 if (!type.isArray())
2879 {
2880 return "";
2881 }
2882
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002883 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002884}
2885
2886TString OutputHLSL::initializer(const TType &type)
2887{
2888 TString string;
2889
daniel@transgaming.comead23042010-04-29 03:35:36 +00002890 for (int component = 0; component < type.getObjectSize(); component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002891 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002892 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002893
daniel@transgaming.comead23042010-04-29 03:35:36 +00002894 if (component < type.getObjectSize() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002895 {
2896 string += ", ";
2897 }
2898 }
2899
daniel@transgaming.comead23042010-04-29 03:35:36 +00002900 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002901}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002902
Jamie Madill9cf6c072013-06-20 11:55:53 -04002903TString OutputHLSL::structureString(const TType &structType, bool useHLSLRowMajorPacking)
2904{
2905 ASSERT(structType.getStruct());
2906
2907 const TTypeList &fields = *structType.getStruct();
2908 const bool isNameless = (structType.getTypeName() == "");
2909 const TString &structName = structureTypeName(structType, useHLSLRowMajorPacking);
2910
2911 const TString declareString = (isNameless ? "struct" : "struct " + structName);
2912
2913 TString structure;
2914 structure += declareString + "\n"
2915 "{\n";
2916
2917 for (unsigned int i = 0; i < fields.size(); i++)
2918 {
2919 const TType &field = *fields[i].type;
2920
2921 structure += " " + structureTypeName(field, useHLSLRowMajorPacking) + " " +
2922 decorateField(field.getFieldName(), structType) + arrayString(field) + ";\n";
2923 }
2924
2925 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
2926 structure += (isNameless ? "} " : "};\n");
2927
2928 return structure;
2929}
2930
2931TString OutputHLSL::structureTypeName(const TType &structType, bool useHLSLRowMajorPacking)
2932{
2933 if (structType.getBasicType() != EbtStruct)
2934 {
2935 return typeString(structType);
2936 }
2937
2938 if (structType.getTypeName() == "")
2939 {
2940 return "";
2941 }
2942
2943 TString prefix = "";
2944
2945 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
2946 // GLSL column-major maps to HLSL row-major, and the converse is true
2947 if (useHLSLRowMajorPacking)
2948 {
2949 prefix += "rm";
2950 }
2951
2952 return prefix + typeString(structType);
2953}
2954
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002955void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00002956{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002957 if (name == "")
2958 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002959 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002960 }
2961
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00002962 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
2963 {
2964 return; // Already added
2965 }
2966
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002967 TType ctorType = type;
2968 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00002969 ctorType.setPrecision(EbpHigh);
2970 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00002971
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002972 TString ctorName = type.getStruct() ? decorate(name) : name;
2973
2974 typedef std::vector<TType> ParameterArray;
2975 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002976
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00002977 if (type.getStruct())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002978 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002979 mStructNames.insert(decorate(name));
2980
Jamie Madill9cf6c072013-06-20 11:55:53 -04002981 const TString &structure = structureString(type, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002982
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002983 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002984 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04002985 // Add row-major packed struct for interface blocks
2986 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
2987 structureString(type, true) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00002988 "#pragma pack_matrix(column_major)\n";
2989
Jamie Madill9cf6c072013-06-20 11:55:53 -04002990 mStructDeclarations.push_back(structure);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00002991 mStructDeclarations.push_back(rowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002992 }
2993
Jamie Madill9cf6c072013-06-20 11:55:53 -04002994 const TTypeList &fields = *type.getStruct();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002995 for (unsigned int i = 0; i < fields.size(); i++)
2996 {
2997 ctorParameters.push_back(*fields[i].type);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002998 }
2999 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003000 else if (parameters)
3001 {
3002 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3003 {
3004 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3005 }
3006 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003007 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003008
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003009 TString constructor;
3010
3011 if (ctorType.getStruct())
3012 {
3013 constructor += ctorName + " " + ctorName + "_ctor(";
3014 }
3015 else // Built-in type
3016 {
3017 constructor += typeString(ctorType) + " " + ctorName + "(";
3018 }
3019
3020 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3021 {
3022 const TType &type = ctorParameters[parameter];
3023
3024 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3025
3026 if (parameter < ctorParameters.size() - 1)
3027 {
3028 constructor += ", ";
3029 }
3030 }
3031
3032 constructor += ")\n"
3033 "{\n";
3034
3035 if (ctorType.getStruct())
3036 {
3037 constructor += " " + ctorName + " structure = {";
3038 }
3039 else
3040 {
3041 constructor += " return " + typeString(ctorType) + "(";
3042 }
3043
3044 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3045 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003046 int rows = ctorType.getRows();
3047 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003048 const TType &parameter = ctorParameters[0];
3049
3050 if (parameter.isScalar())
3051 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003052 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003053 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003054 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003055 {
3056 constructor += TString((row == col) ? "x0" : "0.0");
3057
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003058 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003059 {
3060 constructor += ", ";
3061 }
3062 }
3063 }
3064 }
3065 else if (parameter.isMatrix())
3066 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003067 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003068 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003069 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003070 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003071 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003072 {
3073 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3074 }
3075 else
3076 {
3077 constructor += TString((row == col) ? "1.0" : "0.0");
3078 }
3079
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003080 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003081 {
3082 constructor += ", ";
3083 }
3084 }
3085 }
3086 }
3087 else UNREACHABLE();
3088 }
3089 else
3090 {
3091 int remainingComponents = ctorType.getObjectSize();
3092 int parameterIndex = 0;
3093
3094 while (remainingComponents > 0)
3095 {
3096 const TType &parameter = ctorParameters[parameterIndex];
3097 bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
3098
3099 constructor += "x" + str(parameterIndex);
3100
3101 if (parameter.isScalar())
3102 {
3103 remainingComponents -= parameter.getObjectSize();
3104 }
3105 else if (parameter.isVector())
3106 {
3107 if (remainingComponents == parameter.getObjectSize() || moreParameters)
3108 {
3109 remainingComponents -= parameter.getObjectSize();
3110 }
Jamie Madilla9f52472013-06-06 11:56:43 -04003111 else if (remainingComponents < parameter.getNominalSize())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003112 {
3113 switch (remainingComponents)
3114 {
3115 case 1: constructor += ".x"; break;
3116 case 2: constructor += ".xy"; break;
3117 case 3: constructor += ".xyz"; break;
3118 case 4: constructor += ".xyzw"; break;
3119 default: UNREACHABLE();
3120 }
3121
3122 remainingComponents = 0;
3123 }
3124 else UNREACHABLE();
3125 }
3126 else if (parameter.isMatrix() || parameter.getStruct())
3127 {
3128 ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
3129
3130 remainingComponents -= parameter.getObjectSize();
3131 }
3132 else UNREACHABLE();
3133
3134 if (moreParameters)
3135 {
3136 parameterIndex++;
3137 }
3138
3139 if (remainingComponents)
3140 {
3141 constructor += ", ";
3142 }
3143 }
3144 }
3145
3146 if (ctorType.getStruct())
3147 {
3148 constructor += "};\n"
3149 " return structure;\n"
3150 "}\n";
3151 }
3152 else
3153 {
3154 constructor += ");\n"
3155 "}\n";
3156 }
3157
daniel@transgaming.com63691862010-04-29 03:32:42 +00003158 mConstructors.insert(constructor);
3159}
3160
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003161const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3162{
3163 TInfoSinkBase &out = mBody;
3164
3165 if (type.getBasicType() == EbtStruct)
3166 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003167 out << structLookup(type.getTypeName()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003168
3169 const TTypeList *structure = type.getStruct();
3170
3171 for (size_t i = 0; i < structure->size(); i++)
3172 {
3173 const TType *fieldType = (*structure)[i].type;
3174
3175 constUnion = writeConstantUnion(*fieldType, constUnion);
3176
3177 if (i != structure->size() - 1)
3178 {
3179 out << ", ";
3180 }
3181 }
3182
3183 out << ")";
3184 }
3185 else
3186 {
3187 int size = type.getObjectSize();
3188 bool writeType = size > 1;
3189
3190 if (writeType)
3191 {
3192 out << typeString(type) << "(";
3193 }
3194
3195 for (int i = 0; i < size; i++, constUnion++)
3196 {
3197 switch (constUnion->getType())
3198 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003199 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003200 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003201 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003202 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003203 default: UNREACHABLE();
3204 }
3205
3206 if (i != size - 1)
3207 {
3208 out << ", ";
3209 }
3210 }
3211
3212 if (writeType)
3213 {
3214 out << ")";
3215 }
3216 }
3217
3218 return constUnion;
3219}
3220
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003221TString OutputHLSL::scopeString(unsigned int depthLimit)
3222{
3223 TString string;
3224
3225 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3226 {
3227 string += "_" + str(i);
3228 }
3229
3230 return string;
3231}
3232
3233TString OutputHLSL::scopedStruct(const TString &typeName)
3234{
3235 if (typeName == "")
3236 {
3237 return typeName;
3238 }
3239
3240 return typeName + scopeString(mScopeDepth);
3241}
3242
3243TString OutputHLSL::structLookup(const TString &typeName)
3244{
3245 for (int depth = mScopeDepth; depth >= 0; depth--)
3246 {
3247 TString scopedName = decorate(typeName + scopeString(depth));
3248
3249 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3250 {
3251 if (*structName == scopedName)
3252 {
3253 return scopedName;
3254 }
3255 }
3256 }
3257
3258 UNREACHABLE(); // Should have found a matching constructor
3259
3260 return typeName;
3261}
3262
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003263TString OutputHLSL::decorate(const TString &string)
3264{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003265 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003266 {
3267 return "_" + string;
3268 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003269
3270 return string;
3271}
3272
apatrick@chromium.org65756022012-01-17 21:45:38 +00003273TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003274{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003275 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003276 {
3277 return "ex_" + string;
3278 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003279
3280 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003281}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003282
3283TString OutputHLSL::decorateField(const TString &string, const TType &structure)
3284{
3285 if (structure.getTypeName().compare(0, 3, "gl_") != 0)
3286 {
3287 return decorate(string);
3288 }
3289
3290 return string;
3291}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003292
3293TString OutputHLSL::registerString(TIntermSymbol *operand)
3294{
3295 ASSERT(operand->getQualifier() == EvqUniform);
3296
3297 if (IsSampler(operand->getBasicType()))
3298 {
3299 return "s" + str(samplerRegister(operand));
3300 }
3301
3302 return "c" + str(uniformRegister(operand));
3303}
3304
3305int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3306{
3307 const TType &type = sampler->getType();
3308 ASSERT(IsSampler(type.getBasicType()));
3309
3310 int index = mSamplerRegister;
3311 mSamplerRegister += sampler->totalRegisterCount();
3312
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003313 declareUniform(type, sampler->getSymbol(), index);
3314
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003315 return index;
3316}
3317
3318int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3319{
3320 const TType &type = uniform->getType();
3321 ASSERT(!IsSampler(type.getBasicType()));
3322
3323 int index = mUniformRegister;
3324 mUniformRegister += uniform->totalRegisterCount();
3325
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003326 declareUniform(type, uniform->getSymbol(), index);
3327
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003328 return index;
3329}
3330
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003331void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int index, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003332{
3333 const TTypeList *structure = type.getStruct();
3334
3335 if (!structure)
3336 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003337 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3338 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 +00003339 }
3340 else
3341 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003342 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 +00003343
3344 int fieldIndex = index;
3345
3346 for (size_t i = 0; i < structure->size(); i++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003347 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003348 TType fieldType = *(*structure)[i].type;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003349 const TString &fieldName = fieldType.getFieldName();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003350
Jamie Madill010fffa2013-06-20 11:55:53 -04003351 // make sure to copy matrix packing information
3352 fieldType.setLayoutQualifier(type.getLayoutQualifier());
3353
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003354 declareUniformToList(fieldType, fieldName, fieldIndex, structUniform.fields);
3355 fieldIndex += fieldType.totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003356 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003357
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003358 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003359 }
3360}
3361
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003362void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3363{
3364 declareUniformToList(type, name, index, mActiveUniforms);
3365}
3366
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003367GLenum OutputHLSL::glVariableType(const TType &type)
3368{
3369 if (type.getBasicType() == EbtFloat)
3370 {
3371 if (type.isScalar())
3372 {
3373 return GL_FLOAT;
3374 }
3375 else if (type.isVector())
3376 {
3377 switch(type.getNominalSize())
3378 {
3379 case 2: return GL_FLOAT_VEC2;
3380 case 3: return GL_FLOAT_VEC3;
3381 case 4: return GL_FLOAT_VEC4;
3382 default: UNREACHABLE();
3383 }
3384 }
3385 else if (type.isMatrix())
3386 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003387 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003388 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003389 case 2:
3390 switch(type.getRows())
3391 {
3392 case 2: return GL_FLOAT_MAT2;
3393 case 3: return GL_FLOAT_MAT2x3;
3394 case 4: return GL_FLOAT_MAT2x4;
3395 default: UNREACHABLE();
3396 }
3397
3398 case 3:
3399 switch(type.getRows())
3400 {
3401 case 2: return GL_FLOAT_MAT3x2;
3402 case 3: return GL_FLOAT_MAT3;
3403 case 4: return GL_FLOAT_MAT3x4;
3404 default: UNREACHABLE();
3405 }
3406
3407 case 4:
3408 switch(type.getRows())
3409 {
3410 case 2: return GL_FLOAT_MAT4x2;
3411 case 3: return GL_FLOAT_MAT4x3;
3412 case 4: return GL_FLOAT_MAT4;
3413 default: UNREACHABLE();
3414 }
3415
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003416 default: UNREACHABLE();
3417 }
3418 }
3419 else UNREACHABLE();
3420 }
3421 else if (type.getBasicType() == EbtInt)
3422 {
3423 if (type.isScalar())
3424 {
3425 return GL_INT;
3426 }
3427 else if (type.isVector())
3428 {
3429 switch(type.getNominalSize())
3430 {
3431 case 2: return GL_INT_VEC2;
3432 case 3: return GL_INT_VEC3;
3433 case 4: return GL_INT_VEC4;
3434 default: UNREACHABLE();
3435 }
3436 }
3437 else UNREACHABLE();
3438 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003439 else if (type.getBasicType() == EbtUInt)
3440 {
3441 if (type.isScalar())
3442 {
3443 return GL_UNSIGNED_INT;
3444 }
3445 else if (type.isVector())
3446 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003447 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003448 {
3449 case 2: return GL_UNSIGNED_INT_VEC2;
3450 case 3: return GL_UNSIGNED_INT_VEC3;
3451 case 4: return GL_UNSIGNED_INT_VEC4;
3452 default: UNREACHABLE();
3453 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003454 }
3455 else UNREACHABLE();
3456 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003457 else if (type.getBasicType() == EbtBool)
3458 {
3459 if (type.isScalar())
3460 {
3461 return GL_BOOL;
3462 }
3463 else if (type.isVector())
3464 {
3465 switch(type.getNominalSize())
3466 {
3467 case 2: return GL_BOOL_VEC2;
3468 case 3: return GL_BOOL_VEC3;
3469 case 4: return GL_BOOL_VEC4;
3470 default: UNREACHABLE();
3471 }
3472 }
3473 else UNREACHABLE();
3474 }
3475 else if (type.getBasicType() == EbtSampler2D)
3476 {
3477 return GL_SAMPLER_2D;
3478 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003479 else if (type.getBasicType() == EbtSampler3D)
3480 {
3481 return GL_SAMPLER_3D;
3482 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003483 else if (type.getBasicType() == EbtSamplerCube)
3484 {
3485 return GL_SAMPLER_CUBE;
3486 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003487 else if (type.getBasicType() == EbtSampler2DArray)
3488 {
3489 return GL_SAMPLER_2D_ARRAY;
3490 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003491 else if (type.getBasicType() == EbtISampler2D)
3492 {
3493 return GL_INT_SAMPLER_2D;
3494 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003495 else if (type.getBasicType() == EbtISampler3D)
3496 {
3497 return GL_INT_SAMPLER_3D;
3498 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003499 else if (type.getBasicType() == EbtISamplerCube)
3500 {
3501 return GL_INT_SAMPLER_CUBE;
3502 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003503 else if (type.getBasicType() == EbtISampler2DArray)
3504 {
3505 return GL_INT_SAMPLER_2D_ARRAY;
3506 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003507 else if (type.getBasicType() == EbtUSampler2D)
3508 {
3509 return GL_UNSIGNED_INT_SAMPLER_2D;
3510 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003511 else if (type.getBasicType() == EbtUSampler3D)
3512 {
3513 return GL_UNSIGNED_INT_SAMPLER_3D;
3514 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003515 else if (type.getBasicType() == EbtUSamplerCube)
3516 {
3517 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3518 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003519 else if (type.getBasicType() == EbtUSampler2DArray)
3520 {
3521 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3522 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003523 else UNREACHABLE();
3524
3525 return GL_NONE;
3526}
3527
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003528GLenum OutputHLSL::glVariablePrecision(const TType &type)
3529{
3530 if (type.getBasicType() == EbtFloat)
3531 {
3532 switch (type.getPrecision())
3533 {
3534 case EbpHigh: return GL_HIGH_FLOAT;
3535 case EbpMedium: return GL_MEDIUM_FLOAT;
3536 case EbpLow: return GL_LOW_FLOAT;
3537 case EbpUndefined:
3538 // Should be defined as the default precision by the parser
3539 default: UNREACHABLE();
3540 }
3541 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003542 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003543 {
3544 switch (type.getPrecision())
3545 {
3546 case EbpHigh: return GL_HIGH_INT;
3547 case EbpMedium: return GL_MEDIUM_INT;
3548 case EbpLow: return GL_LOW_INT;
3549 case EbpUndefined:
3550 // Should be defined as the default precision by the parser
3551 default: UNREACHABLE();
3552 }
3553 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003554
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003555 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003556 return GL_NONE;
3557}
3558
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003559bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3560{
3561 switch(qualifier)
3562 {
3563 case EvqVaryingOut:
3564 case EvqInvariantVaryingOut:
3565 case EvqSmoothOut:
3566 case EvqFlatOut:
3567 case EvqCentroidOut:
3568 return true;
3569 }
3570
3571 return false;
3572}
3573
3574bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3575{
3576 switch(qualifier)
3577 {
3578 case EvqVaryingIn:
3579 case EvqInvariantVaryingIn:
3580 case EvqSmoothIn:
3581 case EvqFlatIn:
3582 case EvqCentroidIn:
3583 return true;
3584 }
3585
3586 return false;
3587}
3588
3589bool OutputHLSL::isVarying(TQualifier qualifier)
3590{
3591 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3592}
3593
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003594}